Author: rooneg
Date: Tue Mar  1 19:37:53 2005
New Revision: 155864

URL: http://svn.apache.org/viewcvs?view=rev&rev=155864
Log:
Add support for reading documents from segments other than the first
one.

As part of this, revamp the max_docs code, so that it caches the max
docs for the index, this lets us remove pool args from those functions.

While this particular part of the code seems fine, it isn't actually
working yet, due to other problems I'm currently trying to figure out,
once those are resolved tests that exercise this functionality will be
forthcoming.

* src/index/segments.c
  (lcn_segments_t): store max docs and start points for each segment.
  (lcn_segments_read): init starts and max_docs.
  (lcn_segments_max_docs): return cached value, remove pool arg.
  (segment_index): helper function.
  (lcn_segments_get_document): return docs from segments past the
   first one.

* include/lcn_index.h
  (lcn_segments_max_docs): remove pool arg.

* include/lcn_segments.h
  (lcn_segments_max_docs): remove pool arg.

* src/index/index.c
  (lcn_index_max_docs): remove pool arg.

* test/index/index_test.c
  (test_index_max_docs): update calls to lcn_index_max_docs.

Modified:
    incubator/lucene4c/trunk/include/lcn_index.h
    incubator/lucene4c/trunk/include/lcn_segments.h
    incubator/lucene4c/trunk/src/index/index.c
    incubator/lucene4c/trunk/src/index/segments.c
    incubator/lucene4c/trunk/test/index/index_test.c

Modified: incubator/lucene4c/trunk/include/lcn_index.h
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/include/lcn_index.h?view=diff&r1=155863&r2=155864
==============================================================================
--- incubator/lucene4c/trunk/include/lcn_index.h (original)
+++ incubator/lucene4c/trunk/include/lcn_index.h Tue Mar  1 19:37:53 2005
@@ -43,11 +43,9 @@
                 const char *d,
                 apr_pool_t *pool);
 
-/** Return the maximum document number contained in index @a idx, using
- * @a pool for temporary allocations.
- */
+/** Return the maximum document number contained in index @a idx. */
 apr_uint32_t
-lcn_index_max_docs (lcn_index_t *idx, apr_pool_t *pool);
+lcn_index_max_docs (lcn_index_t *idx);
 
 /** Opaque iterator for iterating over a list of documents. */
 typedef struct lcn_doc_iter_t lcn_doc_iter_t;

Modified: incubator/lucene4c/trunk/include/lcn_segments.h
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/include/lcn_segments.h?view=diff&r1=155863&r2=155864
==============================================================================
--- incubator/lucene4c/trunk/include/lcn_segments.h (original)
+++ incubator/lucene4c/trunk/include/lcn_segments.h Tue Mar  1 19:37:53 2005
@@ -65,11 +65,9 @@
 apr_array_header_t *
 lcn_segments_names (const lcn_segments_t *segments, apr_pool_t *pool);
 
-/** Return the max docs contained within @a segments, using @a pool for
- * temporary allocation.
- */
+/** Return the max docs contained within @a segments. */
 apr_uint32_t
-lcn_segments_max_docs (const lcn_segments_t *segments, apr_pool_t *pool);
+lcn_segments_max_docs (const lcn_segments_t *segments);
 
 /** Return the document freqency @a doc_freq, document numbers @a docs, and
  * frequencies @a freqs of the documents containing @a term in segment number

Modified: incubator/lucene4c/trunk/src/index/index.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/index/index.c?view=diff&r1=155863&r2=155864
==============================================================================
--- incubator/lucene4c/trunk/src/index/index.c (original)
+++ incubator/lucene4c/trunk/src/index/index.c Tue Mar  1 19:37:53 2005
@@ -43,9 +43,9 @@
 }
 
 apr_uint32_t
-lcn_index_max_docs (lcn_index_t *idx, apr_pool_t *pool)
+lcn_index_max_docs (lcn_index_t *idx)
 {
-  return lcn_segments_max_docs (idx->segments, pool);
+  return lcn_segments_max_docs (idx->segments);
 }
 
 struct lcn_doc_iter_t {

Modified: incubator/lucene4c/trunk/src/index/segments.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/src/index/segments.c?view=diff&r1=155863&r2=155864
==============================================================================
--- incubator/lucene4c/trunk/src/index/segments.c (original)
+++ incubator/lucene4c/trunk/src/index/segments.c Tue Mar  1 19:37:53 2005
@@ -28,6 +28,9 @@
   apr_uint32_t format;
   apr_uint64_t version;
 
+  apr_uint32_t *starts;
+  apr_uint32_t max_docs;
+
   apr_hash_t *segments; /* name (lcn_char_t *) -> segment (lcn_segment_t *) */
 
   apr_array_header_t *segments_bynum;
@@ -87,6 +90,18 @@
       APR_ARRAY_PUSH ((*segments)->segments_bynum, lcn_segment_t *) = segment;
     }
 
+  (*segments)->starts = apr_pcalloc (pool, sizeof (apr_uint32_t) * segcount);
+
+  for (i = 0; i < segcount; ++i)
+    {
+      (*segments)->starts[i] = (*segments)->max_docs;
+
+      (*segments)->max_docs
+        += lcn_segment_size (APR_ARRAY_IDX ((*segments)->segments_bynum,
+                                            i,
+                                            lcn_segment_t *));
+    }
+
   lcn_pool_destroy (subpool);
 
   return LCN_NO_ERROR;
@@ -141,23 +156,9 @@
 }
 
 apr_uint32_t
-lcn_segments_max_docs (const lcn_segments_t *segments, apr_pool_t *pool)
+lcn_segments_max_docs (const lcn_segments_t *segments)
 {
-  apr_uint32_t rv = 0;
-  apr_hash_index_t *hi;
-
-  for (hi = apr_hash_first (pool, segments->segments);
-       hi;
-       hi = apr_hash_next (hi))
-    {
-      void *val;
-
-      apr_hash_this (hi, NULL, NULL, &val);
-
-      rv += lcn_segment_size (val);
-    }
-
-  return rv;
+  return segments->max_docs;
 }
 
 lcn_error_t *
@@ -181,24 +182,42 @@
   return LCN_NO_ERROR;
 }
 
+static int
+segment_index (lcn_segments_t *segments, apr_uint32_t n)
+{
+  int i;
+
+  for (i = 0; i < segments->segments_bynum->nelts; ++i)
+    {
+      if (n < segments->starts[i])
+        {
+          return i - 1;
+        }
+    }
+
+  return segments->segments_bynum->nelts - 1;
+}
+
 lcn_error_t *
 lcn_segments_get_document (lcn_document_t **doc,
                            lcn_segments_t *segments,
                            apr_uint32_t docnum,
                            apr_pool_t *pool)
 {
-  lcn_segment_t *seg;
-
-  /* XXX find appropriate segment for docnum */
-  if (docnum > lcn_segment_size (APR_ARRAY_IDX (segments->segments_bynum,
-                                                0,
-                                                lcn_segment_t *)))
-    return lcn_error_create
-            (APR_ENOTIMPL,
-             NULL,
-             "return documents in segment > 0 not yet implemented");
+  if (docnum > segments->max_docs)
+    {
+      return lcn_error_create (APR_EINVAL,
+                               NULL,
+                               "requested nonexistant document");
+    }
   else
-    seg = APR_ARRAY_IDX (segments->segments_bynum, 0, lcn_segment_t *);
+    {
+      int idx = segment_index (segments, docnum);
 
-  return lcn_segment_get_document (doc, seg, docnum, pool);
+      lcn_segment_t *seg = APR_ARRAY_IDX (segments->segments_bynum,
+                                          idx,
+                                          lcn_segment_t *);
+
+      return lcn_segment_get_document (doc, seg, docnum, pool);
+    }
 }

Modified: incubator/lucene4c/trunk/test/index/index_test.c
URL: 
http://svn.apache.org/viewcvs/incubator/lucene4c/trunk/test/index/index_test.c?view=diff&r1=155863&r2=155864
==============================================================================
--- incubator/lucene4c/trunk/test/index/index_test.c (original)
+++ incubator/lucene4c/trunk/test/index/index_test.c Tue Mar  1 19:37:53 2005
@@ -38,13 +38,13 @@
 
   CHK_ERR (lcn_index_open (&idx, "test/data/index", p));
 
-  md = lcn_index_max_docs (idx, p);
+  md = lcn_index_max_docs (idx);
 
   ABTS_INT_EQUAL (tc, 395, md);
 
   CHK_ERR (lcn_index_open (&idx, "test/data/index-cfs", p));
 
-  md = lcn_index_max_docs (idx, p);
+  md = lcn_index_max_docs (idx);
 
   ABTS_INT_EQUAL (tc, 1969, md);
 


Reply via email to