[Groonga-commit] groonga/groonga at edf4bfa [master] Extract common code

Back to archive index
Kouhei Sutou null+****@clear*****
Mon Nov 19 16:10:45 JST 2018


Kouhei Sutou	2018-11-19 16:10:45 +0900 (Mon, 19 Nov 2018)

  Revision: edf4bfa120ebc49d86127a5840a85cf0ce5c913a
  https://github.com/groonga/groonga/commit/edf4bfa120ebc49d86127a5840a85cf0ce5c913a

  Message:
    Extract common code

  Modified files:
    lib/expr.c

  Modified: lib/expr.c (+32 -51)
===================================================================
--- lib/expr.c    2018-11-19 16:00:30 +0900 (6136f5a88)
+++ lib/expr.c    2018-11-19 16:10:45 +0900 (ab78db362)
@@ -3631,6 +3631,33 @@ grn_table_select_index_use_sequential_search(grn_ctx *ctx,
   return GRN_TRUE;
 }
 
+static grn_inline grn_id
+grn_table_select_index_resolve_key(grn_ctx *ctx,
+                                   grn_obj *domain,
+                                   grn_obj *key)
+{
+  if (GRN_OBJ_GET_DOMAIN(key) == DB_OBJ(domain)->id) {
+    return GRN_RECORD_VALUE(key);
+  } else if (GRN_OBJ_GET_DOMAIN(key) == domain->header.domain) {
+    return grn_table_get(ctx,
+                         domain,
+                         GRN_BULK_HEAD(key),
+                         GRN_BULK_VSIZE(key));
+  } else {
+    grn_id id = GRN_ID_NIL;
+    grn_obj casted_key;
+    GRN_OBJ_INIT(&casted_key, GRN_BULK, 0, domain->header.domain);
+    if (grn_obj_cast(ctx, key, &casted_key, GRN_FALSE) == GRN_SUCCESS) {
+      id = grn_table_get(ctx,
+                         domain,
+                         GRN_BULK_HEAD(&casted_key),
+                         GRN_BULK_VSIZE(&casted_key));
+    }
+    GRN_OBJ_FIN(ctx, &casted_key);
+    return id;
+  }
+}
+
 static grn_inline grn_bool
 grn_table_select_index_equal(grn_ctx *ctx,
                              grn_obj *table,
@@ -3719,34 +3746,7 @@ grn_table_select_index_equal(grn_ctx *ctx,
 
       grn_table_select_index_report(ctx, tag, index);
 
-      if (GRN_OBJ_GET_DOMAIN(si->query) == DB_OBJ(domain)->id) {
-        tid = GRN_RECORD_VALUE(si->query);
-      } else {
-        grn_id key_type;
-        grn_obj *key;
-        grn_obj key_buffer;
-
-        key_type = domain->header.domain;
-        if (key_type == si->query->header.domain) {
-          key = si->query;
-        } else {
-          grn_rc rc;
-          GRN_OBJ_INIT(&key_buffer, GRN_BULK, 0, key_type);
-          rc = grn_obj_cast(ctx, si->query, &key_buffer, GRN_FALSE);
-          if (rc == GRN_SUCCESS) {
-            key = &key_buffer;
-          } else {
-            GRN_OBJ_FIN(ctx, &key_buffer);
-            key = si->query;
-          }
-        }
-        tid = grn_table_get(ctx, domain,
-                            GRN_BULK_HEAD(key),
-                            GRN_BULK_VSIZE(key));
-        if (key == &key_buffer) {
-          GRN_OBJ_FIN(ctx, &key_buffer);
-        }
-      }
+      tid = grn_table_select_index_resolve_key(ctx, domain, si->query);
       if (tid != GRN_ID_NIL) {
         uint32_t sid;
         int32_t weight;
@@ -3855,27 +3855,10 @@ grn_table_select_index_not_equal(grn_ctx *ctx,
     grn_obj *domain = grn_ctx_at(ctx, index->header.domain);
     if (domain) {
       grn_id tid;
-      if (GRN_OBJ_GET_DOMAIN(si->query) == DB_OBJ(domain)->id) {
-        tid = GRN_RECORD_VALUE(si->query);
-      } else {
-        if (GRN_OBJ_GET_DOMAIN(si->query) == domain->header.domain) {
-          tid = grn_table_get(ctx, domain,
-                              GRN_BULK_HEAD(si->query),
-                              GRN_BULK_VSIZE(si->query));
-        } else {
-          grn_obj casted_query;
-          GRN_OBJ_INIT(&casted_query, GRN_BULK, 0, domain->header.domain);
-          if (grn_obj_cast(ctx, si->query, &casted_query, GRN_FALSE) ==
-              GRN_SUCCESS) {
-            tid = grn_table_get(ctx, domain,
-                                GRN_BULK_HEAD(&casted_query),
-                                GRN_BULK_VSIZE(&casted_query));
-          } else {
-            tid = GRN_ID_NIL;
-          }
-          GRN_OBJ_FIN(ctx, &casted_query);
-        }
-      }
+
+      grn_table_select_index_report(ctx, "[not-equal]", index);
+
+      tid = grn_table_select_index_resolve_key(ctx, domain, si->query);
       if (tid == GRN_ID_NIL) {
         processed = GRN_TRUE;
       } else {
@@ -3884,8 +3867,6 @@ grn_table_select_index_not_equal(grn_ctx *ctx,
         grn_ii *ii = (grn_ii *)index;
         grn_ii_cursor *ii_cursor;
 
-        grn_table_select_index_report(ctx, "[not-equal]", index);
-
         sid = GRN_UINT32_VALUE_AT(&(si->wv), 0);
         weight = GRN_INT32_VALUE_AT(&(si->wv), 1);
         ii_cursor = grn_ii_cursor_open(ctx, ii, tid,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20181119/c2d3e0fc/attachment-0001.html>


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