forked from mroonga/mroonga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmrn_sys.cpp
More file actions
88 lines (80 loc) · 2.56 KB
/
Copy pathmrn_sys.cpp
File metadata and controls
88 lines (80 loc) · 2.56 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
/* -*- 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mrn_sys.hpp"
bool mrn_hash_put(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj *value)
{
int added;
bool succeed;
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);
succeed = false;
} else {
// store address of value
memcpy(buf, &value, sizeof(grn_obj *));
GRN_LOG(ctx, GRN_LOG_DEBUG, "hash put (key=%s)", key);
succeed = true;
}
return succeed;
}
bool mrn_hash_get(grn_ctx *ctx, grn_hash *hash, const char *key, grn_obj **value)
{
bool found;
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);
found = false;
} else {
// restore address of value
memcpy(value, buf, sizeof(grn_obj *));
found = true;
}
return found;
}
bool mrn_hash_remove(grn_ctx *ctx, grn_hash *hash, const char *key)
{
bool succeed;
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);
succeed = false;
} 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);
succeed = false;
} else {
GRN_LOG(ctx, GRN_LOG_DEBUG, "hash remove (key=%s)", key);
succeed = true;
}
}
return succeed;
}