[Groonga-commit] groonga/groonga at b1b8716 [master] Split code for grn_obj_search_column_index()

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Feb 24 11:08:04 JST 2014


Kouhei Sutou	2014-02-24 11:08:04 +0900 (Mon, 24 Feb 2014)

  New Revision: b1b8716d9a0bf9e691ce83f22bd05230b10a1854
  https://github.com/groonga/groonga/commit/b1b8716d9a0bf9e691ce83f22bd05230b10a1854

  Message:
    Split code for grn_obj_search_column_index()

  Modified files:
    lib/db.c

  Modified: lib/db.c (+68 -47)
===================================================================
--- lib/db.c    2014-02-24 10:23:57 +0900 (e95c21f)
+++ lib/db.c    2014-02-24 11:08:04 +0900 (8b38317)
@@ -2822,6 +2822,71 @@ exit :
 }
 
 static grn_rc
+grn_obj_search_column_index_by_id(grn_ctx *ctx, grn_obj *obj, grn_id tid,
+                                  grn_obj *res, grn_operator op,
+                                  grn_search_optarg *optarg)
+{
+  grn_ii_cursor *c = grn_ii_cursor_open(ctx, (grn_ii *)obj, tid,
+                                        GRN_ID_NIL, GRN_ID_MAX, 1, 0);
+  if (c) {
+    grn_ii_posting *pos;
+    grn_hash *s = (grn_hash *)res;
+    while ((pos = grn_ii_cursor_next(ctx, c))) {
+      /* todo: support orgarg(op)
+         res_add(ctx, s, (grn_rset_posinfo *) pos,
+         get_weight(ctx, s, pos->rid, pos->sid, wvm, optarg), op);
+      */
+      grn_hash_add(ctx, s, pos, s->key_size, NULL, NULL);
+    }
+    grn_ii_cursor_close(ctx, c);
+  }
+
+  return GRN_SUCCESS;
+}
+
+static grn_rc
+grn_obj_search_column_index_by_key(grn_ctx *ctx, grn_obj *obj, grn_obj *query,
+                                   grn_obj *res, grn_operator op,
+                                   grn_search_optarg *optarg)
+{
+  grn_rc rc;
+  unsigned int key_type = GRN_ID_NIL;
+  const char *key;
+  unsigned int key_len;
+  grn_obj *table;
+  grn_obj casted_query;
+  grn_bool need_cast = GRN_FALSE;
+
+  table = grn_ctx_at(ctx, obj->header.domain);
+  if (table) {
+    key_type = table->header.domain;
+    need_cast = (query->header.domain != key_type);
+    grn_obj_unlink(ctx, table);
+  }
+  if (need_cast) {
+    GRN_OBJ_INIT(&casted_query, GRN_BULK, 0, key_type);
+    rc = grn_obj_cast(ctx, query, &casted_query, GRN_FALSE);
+    if (rc == GRN_SUCCESS) {
+      key = GRN_BULK_HEAD(&casted_query);
+      key_len = GRN_BULK_VSIZE(&casted_query);
+    }
+  } else {
+    rc = GRN_SUCCESS;
+    key = GRN_BULK_HEAD(query);
+    key_len = GRN_BULK_VSIZE(query);
+  }
+  if (rc == GRN_SUCCESS) {
+    rc = grn_ii_sel(ctx, (grn_ii *)obj, key, key_len,
+                    (grn_hash *)res, op, optarg);
+  }
+  if (need_cast) {
+    GRN_OBJ_FIN(ctx, &casted_query);
+  }
+
+  return rc;
+}
+
+static grn_rc
 grn_obj_search_column_index(grn_ctx *ctx, grn_obj *obj, grn_obj *query,
                             grn_obj *res, grn_operator op,
                             grn_search_optarg *optarg)
@@ -2834,54 +2899,10 @@ grn_obj_search_column_index(grn_ctx *ctx, grn_obj *obj, grn_obj *query,
       if (query->header.domain == obj->header.domain &&
           GRN_BULK_VSIZE(query) == sizeof(grn_id)) {
         grn_id tid = *((grn_id *)GRN_BULK_HEAD(query));
-        grn_ii_cursor *c = grn_ii_cursor_open(ctx, (grn_ii *)obj, tid,
-                                              GRN_ID_NIL, GRN_ID_MAX, 1, 0);
-        if (c) {
-          grn_ii_posting *pos;
-          grn_hash *s = (grn_hash *)res;
-          while ((pos = grn_ii_cursor_next(ctx, c))) {
-            /* todo: support orgarg(op)
-               res_add(ctx, s, (grn_rset_posinfo *) pos,
-               get_weight(ctx, s, pos->rid, pos->sid, wvm, optarg), op);
-            */
-            grn_hash_add(ctx, s, pos, s->key_size, NULL, NULL);
-          }
-          grn_ii_cursor_close(ctx, c);
-        }
-        return GRN_SUCCESS;
+        rc = grn_obj_search_column_index_by_id(ctx, obj, tid, res, op, optarg);
       } else {
-        unsigned int key_type = GRN_ID_NIL;
-        const char *key;
-        unsigned int key_len;
-        grn_obj *table;
-        grn_obj casted_query;
-        grn_bool need_cast = GRN_FALSE;
-
-        table = grn_ctx_at(ctx, obj->header.domain);
-        if (table) {
-          key_type = table->header.domain;
-          need_cast = (query->header.domain != key_type);
-          grn_obj_unlink(ctx, table);
-        }
-        if (need_cast) {
-          GRN_OBJ_INIT(&casted_query, GRN_BULK, 0, key_type);
-          rc = grn_obj_cast(ctx, query, &casted_query, GRN_FALSE);
-          if (rc == GRN_SUCCESS) {
-            key = GRN_BULK_HEAD(&casted_query);
-            key_len = GRN_BULK_VSIZE(&casted_query);
-          }
-        } else {
-          rc = GRN_SUCCESS;
-          key = GRN_BULK_HEAD(query);
-          key_len = GRN_BULK_VSIZE(query);
-        }
-        if (rc == GRN_SUCCESS) {
-          rc = grn_ii_sel(ctx, (grn_ii *)obj, key, key_len,
-                          (grn_hash *)res, op, optarg);
-        }
-        if (need_cast) {
-          GRN_OBJ_FIN(ctx, &casted_query);
-        }
+        rc = grn_obj_search_column_index_by_key(ctx, obj, query,
+                                                res, op, optarg);
       }
       break;
     case GRN_QUERY :
-------------- next part --------------
HTML����������������������������...
下載 



More information about the Groonga-commit mailing list
Back to archive index