[Groonga-commit] groonga/groonga [master] Use new entry types in put_key().

Back to archive index

null+****@clear***** null+****@clear*****
2012年 4月 2日 (月) 16:12:20 JST


Susumu Yata	2012-04-02 16:12:20 +0900 (Mon, 02 Apr 2012)

  New Revision: 83d858875f3e49148af472bb1a374aefefb24761

  Log:
    Use new entry types in put_key().

  Modified files:
    lib/hash.c

  Modified: lib/hash.c (+73 -75)
===================================================================
--- lib/hash.c    2012-04-02 12:56:29 +0900 (25efd2f)
+++ lib/hash.c    2012-04-02 16:12:20 +0900 (2d6b489)
@@ -985,107 +985,105 @@ grn_hash_entry_get_value(grn_hash *hash, grn_hash_entry *entry)
   }
 }
 
-inline static char *
-get_key(grn_ctx *ctx, grn_hash *hash, entry_str *n)
-{
-  return grn_hash_entry_get_key(ctx, hash, (grn_hash_entry *)n);
-/*  if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) {*/
-/*    if (n->flag & HASH_IMMEDIATE) {*/
-/*      return (char *)&n->str;*/
-/*    } else {*/
-/*      if (IO_HASHP(hash)) {*/
-/*        return (char *)grn_io_hash_key_at(ctx, hash, n->str);*/
-/*      } else {*/
-/*        return ((entry_astr *)n)->str;*/
-/*      }*/
-/*    }*/
-/*  } else {*/
-/*    if (hash->key_size == sizeof(uint32_t)) {*/
-/*      return ((grn_hash_plain_entry *)n)->key;*/
-/*    } else {*/
-/*      return ((grn_hash_rich_entry *)n)->key_and_value;*/
-/*    }*/
-/*  }*/
-}
-
-inline static void *
-get_value(grn_hash *hash, entry_str *n)
-{
-  return grn_hash_entry_get_value(hash, (grn_hash_entry *)n);
-/*  if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) {*/
-/*    if (IO_HASHP(hash)) {*/
-/*      return ((entry_str *)n)->dummy;*/
-/*    } else {*/
-/*      return ((entry_astr *)n)->dummy;*/
-/*    }*/
-/*  } else {*/
-/*    if (hash->key_size == sizeof(uint32_t)) {*/
-/*      return ((entry *)n)->dummy;*/
-/*    } else {*/
-/*      return &((entry *)n)->dummy[hash->key_size];*/
-/*    }*/
-/*  }*/
-}
-
-inline static void
-put_key_(grn_ctx *ctx, grn_hash *hash, entry_str *n, const char *key, int len)
+inline static grn_rc
+grn_io_hash_entry_put_key(grn_ctx *ctx, grn_hash *hash,
+                          grn_io_hash_entry *entry,
+                          const void *key, unsigned int key_size)
 {
-  uint32_t res, ts;
-  if (n->size) {
-    res = n->str;
+  uint32_t key_offset;
+  if (entry->key_size) {
+    key_offset = entry->key.offset;
   } else {
-    if (len >= GRN_HASH_SEGMENT_SIZE) { return; /* error */ }
-    res = hash->header->curr_key;
-    ts = (res + len) >> W_OF_KEY_IN_A_SEGMENT;
-    if (res >> W_OF_KEY_IN_A_SEGMENT != ts) {
-      res = hash->header->curr_key = ts << W_OF_KEY_IN_A_SEGMENT;
+    uint32_t segment_id;
+    if (key_size >= GRN_HASH_SEGMENT_SIZE) {
+      return GRN_INVALID_ARGUMENT;
+    }
+    key_offset = hash->header->curr_key;
+    segment_id = (key_offset + key_size) >> W_OF_KEY_IN_A_SEGMENT;
+    if ((key_offset >> W_OF_KEY_IN_A_SEGMENT) != segment_id) {
+      key_offset = hash->header->curr_key = segment_id << W_OF_KEY_IN_A_SEGMENT;
     }
-    hash->header->curr_key += len;
-    n->str = res;
+    hash->header->curr_key += key_size;
+    entry->key.offset = key_offset;
   }
+
   {
-    void * const dest = grn_io_hash_key_at(ctx, hash, res);
-    if (!dest) { return; }
-    memcpy(dest, key, len);
+    void * const key_ptr = grn_io_hash_key_at(ctx, hash, key_offset);
+    if (!key_ptr) {
+      return GRN_NO_MEMORY_AVAILABLE;
+    }
+    memcpy(key_ptr, key, key_size);
   }
+  return GRN_SUCCESS;
 }
 
 inline static grn_rc
-put_key(grn_ctx *ctx, grn_hash *hash, entry_str *n, uint32_t h,
-        const char *key, unsigned int len)
+grn_hash_entry_put_key(grn_ctx *ctx, grn_hash *hash,
+                       grn_hash_entry *entry, uint32_t hash_value,
+                       const void *key, unsigned int key_size)
 {
-  n->key = h;
   if (hash->obj.header.flags & GRN_OBJ_KEY_VAR_SIZE) {
     if (IO_HASHP(hash)) {
-      if (len <= sizeof(uint32_t)) {
-        n->flag = HASH_IMMEDIATE;
-        memcpy(&n->str, key, len);
+      if (key_size <= sizeof(entry->io_entry.key.buf)) {
+        memcpy(entry->io_entry.key.buf, key, key_size);
+        entry->io_entry.flag = HASH_IMMEDIATE;
       } else {
-        n->flag = 0;
-        put_key_(ctx, hash, n, key, len);
+        const grn_rc rc =
+            grn_io_hash_entry_put_key(ctx, hash, (grn_io_hash_entry *)entry,
+                                      key, key_size);
+        if (rc) {
+          return rc;
+        }
+        entry->io_entry.flag = 0;
       }
+      entry->io_entry.hash_value = hash_value;
+      entry->io_entry.key_size = key_size;
     } else {
-      if (len <= sizeof(char *)) {
-        n->flag = HASH_IMMEDIATE;
-        memcpy(&((entry_astr *)n)->str, key, len);
+      if (key_size <= sizeof(entry->tiny_entry.key.buf)) {
+        memcpy(entry->tiny_entry.key.buf, key, key_size);
+        entry->tiny_entry.flag = HASH_IMMEDIATE;
       } else {
-        grn_ctx *ctx = hash->ctx;
-        if (!(((entry_astr *)n)->str = GRN_CTX_ALLOC(ctx, len))) {
+        grn_ctx * const ctx = hash->ctx;
+        entry->tiny_entry.key.ptr = GRN_CTX_ALLOC(ctx, key_size);
+        if (!entry->tiny_entry.key.ptr) {
           return GRN_NO_MEMORY_AVAILABLE;
         }
-        memcpy(((entry_astr *)n)->str, key, len);
-        n->flag = 0;
+        memcpy(entry->tiny_entry.key.ptr, key, key_size);
+        entry->tiny_entry.flag = 0;
       }
+      entry->io_entry.hash_value = hash_value;
+      entry->tiny_entry.key_size = key_size;
     }
-    n->size = len;
   } else {
-    if (hash->key_size != sizeof(uint32_t)) {
-      memcpy(((entry *)n)->dummy, key, len);
+    if (hash->key_size == sizeof(uint32_t)) {
+      *(uint32_t *)entry->plain_entry.key = hash_value;
+    } else {
+      entry->rich_entry.hash_value = hash_value;
+      memcpy(entry->rich_entry.key_and_value, key, key_size);
     }
   }
   return GRN_SUCCESS;
 }
 
+inline static char *
+get_key(grn_ctx *ctx, grn_hash *hash, entry_str *n)
+{
+  return grn_hash_entry_get_key(ctx, hash, (grn_hash_entry *)n);
+}
+
+inline static void *
+get_value(grn_hash *hash, entry_str *n)
+{
+  return grn_hash_entry_get_value(hash, (grn_hash_entry *)n);
+}
+
+inline static grn_rc
+put_key(grn_ctx *ctx, grn_hash *hash, entry_str *n, uint32_t h,
+        const char *key, unsigned int len)
+{
+  return grn_hash_entry_put_key(ctx, hash, (grn_hash_entry *)n, h, key, len);
+}
+
 inline static int
 match_key(grn_ctx *ctx, grn_hash *hash, entry_str *ee, uint32_t h,
           const char *key, unsigned int len)




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