[Groonga-commit] groonga/groonga at cfeb1eb [master] Add grn_conf_cursor

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Jan 14 19:19:40 JST 2016


Kouhei Sutou	2016-01-14 19:19:40 +0900 (Thu, 14 Jan 2016)

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

  Message:
    Add grn_conf_cursor

  Copied files:
    lib/grn_conf.h
      (from include/groonga/conf.h)
  Modified files:
    include/groonga/conf.h
    include/groonga/groonga.h
    lib/conf.c
    lib/db.c
    lib/sources.am

  Modified: include/groonga/conf.h (+9 -0)
===================================================================
--- include/groonga/conf.h    2016-01-14 19:18:36 +0900 (62511dc)
+++ include/groonga/conf.h    2016-01-14 19:19:40 +0900 (fa57935)
@@ -36,6 +36,15 @@ GRN_API grn_rc grn_conf_get(grn_ctx *ctx,
 GRN_API grn_rc grn_conf_delete(grn_ctx *ctx,
                                const char *key, int32_t key_size);
 
+GRN_API grn_obj *grn_conf_cursor_open(grn_ctx *ctx);
+GRN_API grn_bool grn_conf_cursor_next(grn_ctx *ctx, grn_obj *cursor);
+GRN_API uint32_t grn_conf_cursor_get_key(grn_ctx *ctx,
+                                         grn_obj *cursor,
+                                         const char **key);
+GRN_API uint32_t grn_conf_cursor_get_value(grn_ctx *ctx,
+                                           grn_obj *cursor,
+                                           const char **value);
+
 #ifdef __cplusplus
 }
 #endif

  Modified: include/groonga/groonga.h (+1 -0)
===================================================================
--- include/groonga/groonga.h    2016-01-14 19:18:36 +0900 (e50b473)
+++ include/groonga/groonga.h    2016-01-14 19:19:40 +0900 (27cd62e)
@@ -361,6 +361,7 @@ typedef unsigned short int grn_obj_flags;
 #define GRN_CURSOR_TABLE_NO_KEY        (0x13)
 #define GRN_CURSOR_COLUMN_INDEX        (0x18)
 #define GRN_CURSOR_COLUMN_GEO_INDEX    (0x1a)
+#define GRN_CURSOR_CONF                (0x1f)
 #define GRN_TYPE                       (0x20)
 #define GRN_PROC                       (0x21)
 #define GRN_EXPR                       (0x22)

  Modified: lib/conf.c (+100 -2)
===================================================================
--- lib/conf.c    2016-01-14 19:18:36 +0900 (4b0b6bd)
+++ lib/conf.c    2016-01-14 19:19:40 +0900 (4e183f4)
@@ -17,8 +17,7 @@
 */
 
 #include "grn_ctx_impl.h"
-#include "grn_hash.h"
-#include "grn_db.h"
+#include "grn_conf.h"
 
 #include <string.h>
 
@@ -171,3 +170,102 @@ grn_conf_delete(grn_ctx *ctx,
 
   GRN_API_RETURN(ctx->rc);
 }
+
+grn_obj *
+grn_conf_cursor_open(grn_ctx *ctx)
+{
+  grn_obj *db;
+  grn_hash *conf;
+  grn_conf_cursor *cursor;
+  grn_id id;
+
+  GRN_API_ENTER;
+
+  if (!ctx || !ctx->impl || !(db = ctx->impl->db)) {
+    ERR(GRN_INVALID_ARGUMENT, "[conf][cursor][open] DB isn't initialized");
+    GRN_API_RETURN(NULL);
+  }
+  conf = ((grn_db *)db)->conf;
+
+  cursor = GRN_MALLOCN(grn_conf_cursor, 1);
+  if (!cursor) {
+    ERR(GRN_NO_MEMORY_AVAILABLE,
+        "[conf][cursor][open] failed to allocate memory for conf cursor");
+    GRN_API_RETURN(NULL);
+  }
+
+  GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_CONF);
+  cursor->hash_cursor = grn_hash_cursor_open(ctx, conf,
+                                             NULL, -1,
+                                             NULL, -1,
+                                             0, -1, 0);
+  if (!cursor->hash_cursor) {
+    GRN_FREE(cursor);
+    ERR(GRN_NO_MEMORY_AVAILABLE,
+        "[conf][cursor][open] failed to allocate memory for hash cursor");
+    GRN_API_RETURN(NULL);
+  }
+
+  id = grn_obj_register(ctx, ctx->impl->db, NULL, 0);
+  DB_OBJ(cursor)->header.domain = GRN_ID_NIL;
+  DB_OBJ(cursor)->range = GRN_ID_NIL;
+  grn_db_obj_init(ctx, ctx->impl->db, id, DB_OBJ(cursor));
+
+  GRN_API_RETURN((grn_obj *)cursor);
+}
+
+grn_rc
+grn_conf_cursor_close(grn_ctx *ctx, grn_conf_cursor *cursor)
+{
+  grn_hash_cursor_close(ctx, cursor->hash_cursor);
+  GRN_FREE(cursor);
+
+  return GRN_SUCCESS;
+}
+
+grn_bool
+grn_conf_cursor_next(grn_ctx *ctx, grn_obj *cursor)
+{
+  grn_bool have_next;
+  grn_conf_cursor *conf_cursor = (grn_conf_cursor *)cursor;
+
+  GRN_API_ENTER;
+
+  have_next = grn_hash_cursor_next(ctx, conf_cursor->hash_cursor) != GRN_ID_NIL;
+
+  GRN_API_RETURN(have_next);
+}
+
+uint32_t
+grn_conf_cursor_get_key(grn_ctx *ctx, grn_obj *cursor, const char **key)
+{
+  void *key_raw;
+  uint32_t key_size;
+  grn_conf_cursor *conf_cursor = (grn_conf_cursor *)cursor;
+
+  GRN_API_ENTER;
+
+  key_size = grn_hash_cursor_get_key(ctx, conf_cursor->hash_cursor, &key_raw);
+  *key = key_raw;
+
+  GRN_API_RETURN(key_size);
+}
+
+uint32_t
+grn_conf_cursor_get_value(grn_ctx *ctx, grn_obj *cursor, const char **value)
+{
+  void *value_raw;
+  uint32_t value_size;
+  uint32_t value_size_raw;
+  grn_conf_cursor *conf_cursor = (grn_conf_cursor *)cursor;
+
+  GRN_API_ENTER;
+
+  value_size_raw = grn_hash_cursor_get_value(ctx,
+                                             conf_cursor->hash_cursor,
+                                             &value_raw);
+  *value = (char *)value_raw + sizeof(uint32_t);
+  value_size = *((uint32_t *)value_raw);
+
+  GRN_API_RETURN(value_size);
+}

  Modified: lib/db.c (+6 -1)
===================================================================
--- lib/db.c    2016-01-14 19:18:36 +0900 (629ced1)
+++ lib/db.c    2016-01-14 19:19:40 +0900 (c04b171)
@@ -1,5 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2009-2015 Brazil
+/*
+  Copyright(C) 2009-2016 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -15,6 +16,7 @@
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
 #include "grn.h"
+#include "grn_conf.h"
 #include "grn_db.h"
 #include "grn_hash.h"
 #include "grn_pat.h"
@@ -9688,6 +9690,9 @@ grn_obj_close(grn_ctx *ctx, grn_obj *obj)
     case GRN_CURSOR_COLUMN_GEO_INDEX :
       grn_geo_cursor_close(ctx, obj);
       break;
+    case GRN_CURSOR_CONF :
+      grn_conf_cursor_close(ctx, (grn_conf_cursor *)obj);
+      break;
     case GRN_TYPE :
       GRN_FREE(obj);
       rc = GRN_SUCCESS;

  Copied: lib/grn_conf.h (+10 -14) 51%
===================================================================
--- include/groonga/conf.h    2016-01-14 19:18:36 +0900 (62511dc)
+++ lib/grn_conf.h    2016-01-14 19:19:40 +0900 (410c028)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2015-2016 Brazil
+  Copyright(C) 2016 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -18,23 +18,19 @@
 
 #pragma once
 
+#include "grn_db.h"
+#include "grn_hash.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#define GRN_CONF_MAX_KEY_SIZE GRN_TABLE_MAX_KEY_SIZE
-#define GRN_CONF_MAX_VALUE_SIZE                                         \
-  (GRN_CONF_VALUE_SPACE_SIZE - sizeof(uint32_t) - 1) /* 1 is for '\0' */
-#define GRN_CONF_VALUE_SPACE_SIZE (4 * 1024)
-
-GRN_API grn_rc grn_conf_set(grn_ctx *ctx,
-                            const char *key, int32_t key_size,
-                            const char *value, int32_t value_size);
-GRN_API grn_rc grn_conf_get(grn_ctx *ctx,
-                            const char *key, int32_t key_size,
-                            const char **value, uint32_t *value_size);
-GRN_API grn_rc grn_conf_delete(grn_ctx *ctx,
-                               const char *key, int32_t key_size);
+typedef struct {
+  grn_db_obj obj;
+  grn_hash_cursor *hash_cursor;
+} grn_conf_cursor;
+
+grn_rc grn_conf_cursor_close(grn_ctx *ctx, grn_conf_cursor *cursor);
 
 #ifdef __cplusplus
 }

  Modified: lib/sources.am (+1 -0)
===================================================================
--- lib/sources.am    2016-01-14 19:18:36 +0900 (8fa3b58)
+++ lib/sources.am    2016-01-14 19:19:40 +0900 (015673e)
@@ -3,6 +3,7 @@ libgroonga_la_SOURCES =				\
 	grn_com.h				\
 	command.c				\
 	conf.c					\
+	grn_conf.h				\
 	ctx.c					\
 	grn_ctx.h				\
 	grn_ctx_impl.h				\
-------------- next part --------------
HTML����������������������������...
Download 



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