forked from mroonga/mroonga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrn_sys.c
More file actions
176 lines (163 loc) · 4.69 KB
/
mrn_sys.c
File metadata and controls
176 lines (163 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2010 Tetsuro IKEDA
Copyright(C) 2011-2012 Kentoku SHIBA
Copyright(C) 2011 Kouhei Sutou <kou@clear-code.com>
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.h"
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;
}
/**
* "./${db}/${table}" ==> "${db}.mrn"
* "./${db}/" ==> "${db}.mrn"
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==>
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0.mrn"
*/
char *mrn_db_path_gen(const char *db_path_in_mysql, char *dest)
{
if (strncmp(db_path_in_mysql, "./", 2) == 0) {
int i = 2, j = 0, len;
len = strlen(db_path_in_mysql);
while (db_path_in_mysql[i] != '/' && i < len) {
dest[j++] = db_path_in_mysql[i++];
}
dest[j] = '\0';
} else {
strcpy(dest, db_path_in_mysql);
}
strcat(dest, MRN_DB_FILE_SUFFIX);
return dest;
}
/**
* "./${db}/${table}" ==> "${db}"
* "./${db}/" ==> "${db}"
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0" ==>
* "/tmp/mysql-test/var/tmp/mysqld.1/#sql27c5_1_0"
*/
char *mrn_db_name_gen(const char *db_path_in_mysql, char *dest)
{
if (strncmp(db_path_in_mysql, "./", 2) == 0) {
int i = 2, j = 0, len;
len = strlen(db_path_in_mysql);
while (db_path_in_mysql[i] != '/' && i < len) {
dest[j++] = db_path_in_mysql[i++];
}
dest[j] = '\0';
} else {
strcpy(dest, db_path_in_mysql);
}
return dest;
}
/**
* "./${db}/${table}" ==> "${table}" (with encoding first '_')
*/
char *mrn_table_name_gen(const char *arg, char *dest)
{
int len = strlen(arg);
int i=len, j=0;
for (; arg[--i] != '/' ;) {}
if (arg[i + 1] == '_') {
dest[j++] = '@';
dest[j++] = '0';
dest[j++] = '0';
dest[j++] = '5';
dest[j++] = 'f';
i++;
}
for (; i < len ;) {
dest[j++] = arg[++i];
}
dest[j] = '\0';
return dest;
}
/**
* "./${db}/${table}" ==> "${table}" (without encoding first '_')
*/
char *mrn_table_name_gen_for_mysql(const char *arg, char *dest)
{
int len = strlen(arg);
int i=len, j=0;
for (; arg[--i] != '/' ;) {}
for (; i < len ;) {
dest[j++] = arg[++i];
}
dest[j] = '\0';
return dest;
}
/**
* "${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;
}