[Groonga-commit] groonga/groonga [master] Add functions to calculate entry_size.

Back to archive index

null+****@clear***** null+****@clear*****
2012年 3月 30日 (金) 10:46:16 JST


Susumu Yata	2012-03-30 10:46:16 +0900 (Fri, 30 Mar 2012)

  New Revision: 4e9b7ae200506fee8942245e41b7adef2deb8d60

  Log:
    Add functions to calculate entry_size.

  Modified files:
    lib/hash.c

  Modified: lib/hash.c (+39 -22)
===================================================================
--- lib/hash.c    2012-03-30 10:32:13 +0900 (211343e)
+++ lib/hash.c    2012-03-30 10:46:16 +0900 (7719526)
@@ -1019,15 +1019,11 @@ match_key(grn_ctx *ctx, grn_hash *hash, entry_str *ee, uint32_t h,
 
 #define STEP(x) (((x) >> 2) | 0x1010101)
 
-inline static grn_rc
-io_hash_init(grn_ctx *ctx, grn_hash *ih, const char *path, uint32_t key_size,
-             uint32_t value_size, uint32_t flags, grn_encoding encoding,
-             uint32_t init_size)
+static uint32_t
+grn_io_hash_calculate_entry_size(uint32_t key_size, uint32_t value_size,
+                                 uint32_t flags)
 {
-  grn_io *io;
-  struct grn_hash_header *header;
-  uint32_t entry_size, w_of_element, m;
-  for (m = IDX_MASK_IN_A_SEGMENT + 1; m < init_size * 2; m *= 2);
+  uint32_t entry_size;
   if (flags & GRN_OBJ_KEY_VAR_SIZE) {
     entry_size = (intptr_t)(&((entry_str *)0)->dummy) + value_size;
   } else {
@@ -1037,6 +1033,19 @@ io_hash_init(grn_ctx *ctx, grn_hash *ih, const char *path, uint32_t key_size,
       entry_size = (intptr_t)(&((entry *)0)->dummy) + key_size + value_size;
     }
   }
+  return entry_size;
+}
+
+inline static grn_rc
+io_hash_init(grn_ctx *ctx, grn_hash *ih, const char *path, uint32_t key_size,
+             uint32_t value_size, uint32_t flags, grn_encoding encoding,
+             uint32_t init_size)
+{
+  grn_io *io;
+  struct grn_hash_header *header;
+  uint32_t entry_size, w_of_element, m;
+  for (m = IDX_MASK_IN_A_SEGMENT + 1; m < init_size * 2; m *= 2);
+  entry_size = grn_io_hash_calculate_entry_size(key_size, value_size, flags);
   w_of_element = 0;
   while ((1U << w_of_element) < entry_size) {
     w_of_element++;
@@ -1089,23 +1098,13 @@ io_hash_init(grn_ctx *ctx, grn_hash *ih, const char *path, uint32_t key_size,
 
 #define INITIAL_INDEX_SIZE 256U
 
-static grn_rc
-grn_tiny_hash_init(grn_ctx *ctx, grn_hash *hash, const char *path,
-                   uint32_t key_size, uint32_t value_size, uint32_t flags,
-                   grn_encoding encoding)
+static uint32_t
+grn_tiny_hash_calculate_entry_size(uint32_t key_size, uint32_t value_size,
+                                   uint32_t flags)
 {
   uint32_t entry_size;
-
-  if (path) {
-    return GRN_INVALID_ARGUMENT;
-  }
-  hash->index = GRN_CTX_ALLOC(ctx, INITIAL_INDEX_SIZE * sizeof(grn_id));
-  if (!hash->index) {
-    return GRN_NO_MEMORY_AVAILABLE;
-  }
-
   if (flags & GRN_OBJ_KEY_VAR_SIZE) {
-    entry_size = (uintptr_t)(&((entry_astr *)0)->dummy) + value_size;
+    entry_size = (uintptr_t)&((entry_astr *)0)->dummy + value_size;
   } else {
     if (key_size == sizeof(uint32_t)) {
       entry_size = (uintptr_t)(&((entry *)0)->dummy) + value_size;
@@ -1117,7 +1116,25 @@ grn_tiny_hash_init(grn_ctx *ctx, grn_hash *hash, const char *path,
     entry_size += sizeof(uintptr_t) - 1;
     entry_size &= ~(sizeof(uintptr_t) - 1);
   }
+  return entry_size;
+}
+
+static grn_rc
+grn_tiny_hash_init(grn_ctx *ctx, grn_hash *hash, const char *path,
+                   uint32_t key_size, uint32_t value_size, uint32_t flags,
+                   grn_encoding encoding)
+{
+  uint32_t entry_size;
+
+  if (path) {
+    return GRN_INVALID_ARGUMENT;
+  }
+  hash->index = GRN_CTX_ALLOC(ctx, INITIAL_INDEX_SIZE * sizeof(grn_id));
+  if (!hash->index) {
+    return GRN_NO_MEMORY_AVAILABLE;
+  }
 
+  entry_size = grn_tiny_hash_calculate_entry_size(key_size, value_size, flags);
   hash->obj.header.flags = flags;
   hash->ctx = ctx;
   hash->key_size = key_size;




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