null+****@clear*****
null+****@clear*****
2012年 4月 9日 (月) 17:08:37 JST
Kouhei Sutou 2012-04-09 17:08:37 +0900 (Mon, 09 Apr 2012)
New Revision: c0c5527f6e4d2d31ad9eb282fdcef3b0fe305ab4
Log:
Add missing mrn_sys.cpp and mrn_sys.hpp
Sorry...
Added files:
mrn_sys.cpp
mrn_sys.hpp
Added: mrn_sys.cpp (+95 -0) 100644
===================================================================
--- /dev/null
+++ mrn_sys.cpp 2012-04-09 17:08:37 +0900 (2bffc67)
@@ -0,0 +1,95 @@
+/* -*- c-basic-offset: 2 -*- */
+/*
+ Copyright(C) 2010 Tetsuro IKEDA
+ Copyright(C) 2011-2012 Kentoku SHIBA
+ Copyright(C) 2011 Kouhei Sutou <kou****@clear*****>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "mrn_sys.hpp"
+
+int mrn_hash_put(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj *value)
+{
+ int added, res=0;
+ void *buf;
+ grn_hash_add(ctx, hash, (const char *)key, strlen(key), &buf, &added);
+ // duplicate check
+ if (added == 0) {
+ GRN_LOG(ctx, GRN_LOG_WARNING, "hash put duplicated (key=%s)", key);
+ res = -1;
+ } else {
+ // store address of value
+ memcpy(buf, &value, sizeof(grn_obj *));
+ GRN_LOG(ctx, GRN_LOG_DEBUG, "hash put (key=%s)", key);
+ }
+ return res;
+}
+
+int mrn_hash_get(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj **value)
+{
+ int res = 0;
+ grn_id id;
+ void *buf;
+ id = grn_hash_get(ctx, hash, (const char *)key, strlen(key), &buf);
+ // key not found
+ if (id == GRN_ID_NIL) {
+ GRN_LOG(ctx, GRN_LOG_DEBUG, "hash get not found (key=%s)", key);
+ res = -1;
+ } else {
+ // restore address of value
+ memcpy(value, buf, sizeof(grn_obj *));
+ }
+ return res;
+}
+
+int mrn_hash_remove(grn_ctx *ctx, grn_hash *hash, const char *key)
+{
+ int res = 0;
+ grn_rc rc;
+ grn_id id;
+ id = grn_hash_get(ctx, hash, (const char*) key, strlen(key), NULL);
+ if (id == GRN_ID_NIL) {
+ GRN_LOG(ctx, GRN_LOG_WARNING, "hash remove not found (key=%s)", key);
+ res = -1;
+ } else {
+ rc = grn_hash_delete_by_id(ctx, hash, id, NULL);
+ if (rc != GRN_SUCCESS) {
+ GRN_LOG(ctx, GRN_LOG_ERROR, "hash remove error (key=%s)", key);
+ res = -1;
+ } else {
+ GRN_LOG(ctx, GRN_LOG_DEBUG, "hash remove (key=%s)", key);
+ }
+ }
+ return res;
+}
+
+/**
+ * "${table}" ==> "${table}-${index_name}"
+ */
+char *mrn_index_table_name_gen(const char *table_name,
+ const char *index_name,
+ char *dest)
+{
+ snprintf(dest, MRN_MAX_PATH_SIZE, "%s-%s", table_name, index_name);
+ return dest;
+}
Added: mrn_sys.hpp (+59 -0) 100644
===================================================================
--- /dev/null
+++ mrn_sys.hpp 2012-04-09 17:08:37 +0900 (08b8cab)
@@ -0,0 +1,59 @@
+/*
+ Copyright(C) 2010 Tetsuro IKEDA
+ Copyright(C) 2011 Kentoku SHIBA
+ Copyright(C) 2011-2012 Kouhei Sutou <kou****@clear*****>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+#ifndef _mrn_sys_h
+#define _mrn_sys_h
+
+#include <groonga.h>
+#include "mrn_macro.h"
+
+MRN_BEGIN_DECLS
+
+/* constants */
+#define MRN_BUFFER_SIZE 1024
+#define MRN_MAX_KEY_SIZE GRN_TABLE_MAX_KEY_SIZE
+#if defined(MAX_PATH)
+# define MRN_MAX_PATH_SIZE (MAX_PATH + 1)
+#elif defined(PATH_MAX)
+# define MRN_MAX_PATH_SIZE (PATH_MAX)
+#elif defined(MAXPATHLEN)
+# define MRN_MAX_PATH_SIZE (MAXPATHLEN)
+#else
+# define MRN_MAX_PATH_SIZE (256)
+#endif
+#define MRN_DB_FILE_SUFFIX ".mrn"
+#define MRN_LOG_FILE_PATH "groonga.log"
+#define MRN_COLUMN_NAME_ID "_id"
+#define MRN_COLUMN_NAME_KEY "_key"
+#define MRN_COLUMN_NAME_SCORE "_score"
+#ifndef MRN_PARSER_DEFAULT
+# define MRN_PARSER_DEFAULT "TokenBigram"
+#endif
+
+/* functions */
+int mrn_hash_put(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj *value);
+int mrn_hash_get(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj **value);
+int mrn_hash_remove(grn_ctx *ctx, grn_hash *hash, const char *key);
+
+char *mrn_index_table_name_gen(const char *arg, const char *idx_name, char *dest);
+
+MRN_END_DECLS
+
+#endif /* _mrn_sys_h */