[Groonga-commit] groonga/groonga [master] Added grn_index_cursor_open() and grn_index_cursor_next().

Back to archive index

null+****@clear***** null+****@clear*****
2010年 9月 19日 (日) 21:11:36 JST


Daijiro MORI	2010-09-19 12:11:36 +0000 (Sun, 19 Sep 2010)

  New Revision: abd877f09e54c3c679c04334f23f2b79fe64ce4e

  Log:
    Added grn_index_cursor_open() and grn_index_cursor_next().

  Modified files:
    groonga.h
    lib/db.c

  Modified: groonga.h (+36 -0)
===================================================================
--- groonga.h    2010-09-17 10:19:11 +0000 (4989aed)
+++ groonga.h    2010-09-19 12:11:36 +0000 (27aa012)
@@ -855,6 +855,42 @@ GRN_API grn_rc grn_table_cursor_delete(grn_ctx *ctx, grn_table_cursor *tc);
  **/
 GRN_API grn_obj *grn_table_cursor_table(grn_ctx *ctx, grn_table_cursor *tc);
 
+typedef struct {
+  grn_id rid;
+  grn_id sid;
+  unsigned int pos;
+  unsigned int tf;
+  unsigned int weight;
+  unsigned int rest;
+} grn_posting;
+
+/**
+ * grn_index_cursor_open
+ * @tc: 対象cursor
+ * @index: 対象cursor
+ * @rid_min: 出力するレコードidの下限
+ * @rid_max: 出力するレコードidの上限
+ *
+ * grn_table_cursorから取得できるそれぞれのレコードについて、
+ * GRN_OBJ_COLUMN_INDEX型のカラムの値を順番に取り出すためのカーソルを生成して返す。
+ * rid_min,rid_maxを指定して取得するレコードidの値を制限することができる。
+ * 戻り値であるgrn_index_cursorはgrn_obj_closeを使って解放する。
+ **/
+GRN_API grn_obj *grn_index_cursor_open(grn_ctx *ctx, grn_table_cursor *tc, grn_obj *index,
+                                       grn_id rid_min, grn_id rid_max, int flags);
+
+/**
+ * grn_index_cursor_next
+ * @ic: 対象cursor
+ * @tid: テーブルレコードID
+ *
+ * cursorの範囲内のインデックスの値を順番に取り出す。
+ * tidにNULL以外を指定した場合は、
+ * index_cursorを作成するときに指定したtable_cursorの現在の対象レコードのidを返す。
+ * 戻り値であるgrn_posting構造体は解放する必要はない。
+ **/
+GRN_API grn_posting *grn_index_cursor_next(grn_ctx *ctx, grn_obj *ic, grn_id *tid);
+
 #define GRN_TABLE_EACH(ctx,table,head,tail,id,key,key_size,value,block) do {\
   (ctx)->errlvl = GRN_LOG_NOTICE;\
   (ctx)->rc = GRN_SUCCESS;\

  Modified: lib/db.c (+58 -0)
===================================================================
--- lib/db.c    2010-09-17 10:19:11 +0000 (900cc83)
+++ lib/db.c    2010-09-19 12:11:36 +0000 (5aa6518)
@@ -2098,6 +2098,57 @@ grn_table_cursor_table(grn_ctx *ctx, grn_table_cursor *tc)
   GRN_API_RETURN(obj);
 }
 
+typedef struct {
+  grn_db_obj obj;
+  grn_obj *index;
+  grn_table_cursor *tc;
+  grn_ii_cursor *iic;
+  grn_id tid;
+  grn_id rid_min;
+  grn_id rid_max;
+  int flags;
+} grn_index_cursor;
+
+grn_obj *
+grn_index_cursor_open(grn_ctx *ctx, grn_table_cursor *tc,
+                      grn_obj *index, grn_id rid_min, grn_id rid_max, int flags)
+{
+  grn_index_cursor *ic = NULL;
+  GRN_API_ENTER;
+  if (tc && (ic = GRN_MALLOCN(grn_index_cursor, 1))) {
+    ic->tc = tc;
+    ic->index = index;
+    ic->iic = NULL;
+    ic->tid = GRN_ID_NIL;
+    ic->rid_min = rid_min;
+    ic->rid_max = rid_max;
+    ic->flags = flags;
+    GRN_DB_OBJ_SET_TYPE(ic, GRN_CURSOR_COLUMN_INDEX);
+  }
+  GRN_API_RETURN((grn_obj *)ic);
+}
+
+grn_posting *
+grn_index_cursor_next(grn_ctx *ctx, grn_obj *c, grn_id *tid)
+{
+  grn_ii_posting *ip = NULL;
+  grn_index_cursor *ic = (grn_index_cursor *)c;
+  GRN_API_ENTER;
+  if (ic->iic) { ip = grn_ii_cursor_next(ctx, ic->iic); }
+  if (!ip) {
+    if ((ic->tid = grn_table_cursor_next(ctx, ic->tc))) {
+      grn_ii *ii = (grn_ii *)ic->index;
+      if (ic->iic) { grn_ii_cursor_close(ctx, ic->iic); }
+      if ((ic->iic = grn_ii_cursor_open(ctx, ii, ic->tid,
+                                        ic->rid_min, ic->rid_max,
+                                        ii->n_elements, ic->flags))) {
+        ip = grn_ii_cursor_next(ctx, ic->iic);
+      }
+    }
+  }
+  GRN_API_RETURN((grn_posting *)ip);
+}
+
 grn_rc
 grn_table_search(grn_ctx *ctx, grn_obj *table, const void *key, uint32_t key_size,
                  grn_operator mode, grn_obj *res, grn_operator op)
@@ -5847,6 +5898,13 @@ grn_obj_close(grn_ctx *ctx, grn_obj *obj)
     case GRN_CURSOR_TABLE_VIEW :
       grn_view_cursor_close(ctx, (grn_view_cursor *)obj);
       break;
+    case GRN_CURSOR_COLUMN_INDEX :
+      {
+        grn_index_cursor *ic = (grn_index_cursor *)obj;
+        if (ic->iic) { grn_ii_cursor_close(ctx, ic->iic); }
+        GRN_FREE(ic);
+      }
+      break;
     case GRN_TYPE :
       GRN_FREE(obj);
       rc = GRN_SUCCESS;




Groonga-commit メーリングリストの案内
Back to archive index