-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultimap-memcachedb.patch
More file actions
220 lines (210 loc) · 6.49 KB
/
multimap-memcachedb.patch
File metadata and controls
220 lines (210 loc) · 6.49 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Index: memcachedb.c
===================================================================
--- memcachedb.c (revision 98)
+++ memcachedb.c (working copy)
@@ -910,6 +910,69 @@
out_string(c, "ERROR");
}
+static int txn_begin_and_cursor(DB_TXN **txn, DBC **cursorp, u_int32_t flags)
+{
+ int ret;
+
+ /* transcation and cursor */
+ ret = env->txn_begin(env, NULL, txn, 0);
+ if (ret != 0)
+ return ret;
+
+ /* Get a cursor, we use 2 degree isolation */
+ ret = dbp->cursor(dbp, *txn, cursorp, flags);
+ if (ret != 0)
+ return ret;
+
+ return 0;
+}
+
+static int conn_grow(conn *c, int i)
+{
+ if (i < c->isize)
+ return 0;
+
+ item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2);
+
+ if (!new_list)
+ return -1;
+
+ c->isize *= 2;
+ c->ilist = new_list;
+
+ return 0;
+}
+
+static int conn_push_value(conn *c, item *it, int i)
+{
+ int ret;
+
+ ret = conn_grow(c, i);
+ if (ret)
+ return ret;
+
+ /*
+ * Construct the response. Each hit adds three elements to the
+ * outgoing data list:
+ * "VALUE "
+ * key
+ * " " + flags + " " + data length + "\r\n" + data (with \r\n)
+ */
+
+ if (add_iov(c, "VALUE ", 6) != 0 ||
+ add_iov(c, ITEM_key(it), it->nkey) != 0 ||
+ add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0) {
+ return -1;
+ }
+
+ if (settings.verbose > 1)
+ fprintf(stderr, ">%d sending key %s\n", c->sfd, ITEM_key(it));
+
+ *(c->ilist + i) = it;
+
+ return 0;
+}
+
/* ntokens is overwritten here... shrug.. */
static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens) {
char *key;
@@ -924,6 +987,9 @@
do {
while(key_token->length != 0) {
+ int ret;
+ DB_TXN *txn;
+ DBC *cursorp;
key = key_token->value;
nkey = key_token->length;
@@ -939,48 +1005,38 @@
}
stats_get_cmds++;
-
- it = item_get(key, nkey);
- if (it) {
- if (i >= c->isize) {
- item **new_list = realloc(c->ilist, sizeof(item *) * c->isize * 2);
- if (new_list) {
- c->isize *= 2;
- c->ilist = new_list;
- } else {
- item_free(it);
- it = NULL;
- break;
- }
- }
+ ret = txn_begin_and_cursor(&txn, &cursorp, DB_READ_COMMITTED);
+ if (ret != 0) {
+ fprintf(stderr, "envp->txn_begin or dbp->cursor: %s\n",
+ db_strerror(ret));
+ out_string(c, "SERVER_ERROR envp->txn_begin or dbp->cursor");
+ return;
+ }
- /*
- * Construct the response. Each hit adds three elements to the
- * outgoing data list:
- * "VALUE "
- * key
- * " " + flags + " " + data length + "\r\n" + data (with \r\n)
- */
+ it = item_cget(cursorp, key, nkey, DB_SET);
+ if (it)
+ stats_get_hits++;
+ else
+ stats_get_misses++;
- if (add_iov(c, "VALUE ", 6) != 0 ||
- add_iov(c, ITEM_key(it), it->nkey) != 0 ||
- add_iov(c, ITEM_suffix(it), it->nsuffix + it->nbytes) != 0)
- {
- item_free(it);
- it = NULL;
- break;
- }
-
- if (settings.verbose > 1)
- fprintf(stderr, ">%d sending key %s\n", c->sfd, ITEM_key(it));
-
- stats_get_hits++;
- *(c->ilist + i) = it;
+ while (it) {
+ ret = conn_push_value(c, it, i);
+ if (ret) {
+ item_free(it);
+ it = NULL;
+ break;
+ }
i++;
+ it = item_cget(cursorp, NULL, 0, DB_NEXT_DUP);
+ }
+ cursorp->close(cursorp);
- } else {
- stats_get_misses++;
+ /* txn commit */
+ ret = txn->commit(txn, 0);
+ if (ret != 0) {
+ fprintf(stderr, "txn->commit: %s\n", db_strerror(ret));
+ txn->abort(txn);
}
key_token++;
Index: doc/multimap.txt
===================================================================
--- doc/multimap.txt (revision 0)
+++ doc/multimap.txt (revision 0)
@@ -0,0 +1,39 @@
+Multimap MemcacheDB
+===================
+
+Multimap MemcacheDB permits storing duplicate data items for the same key.
+Retrieval request may response multiple items for just one key.
+
+All commands supported by MemcacheDB are also supported by Multimap MemcacheDB.
+But some commands ("append", "prepend", "replace", "incr", "decr") do not
+behave as expected and undefined.
+
+Examples
+========
+
+libmemcached client example
+
+ // store duplicate data items for the same key
+ memcached_set(&memc, "key", 3, "value1", 7, 0, 0);
+ memcached_set(&memc, "key", 3, "value2", 7, 0, 0);
+ memcached_set(&memc, "key", 3, "value3", 7, 0, 0);
+
+ // retrieve multiple items for a key
+ const char *keys[] = { "key", };
+ const size_t key_lens[] = { 3, };
+ memcached_mget(&memc, keys, key_lens, 1);
+
+ value1 = memcached_fetch(&memc, NULL, NULL, &value1_length, &flags, &ret);
+ value2 = memcached_fetch(&memc, NULL, NULL, &value2_length, &flags, &ret);
+ value3 = memcached_fetch(&memc, NULL, NULL, &value3_length, &flags, &ret);
+
+ // MEMCACHED_END is returned when all object that have been found are
+ // returned
+ value4 = memcached_fetch(&memc, NULL, NULL, &value4_length, &flags, &ret);
+ assert(value4 == NULL && ret == MEMCACHED_END);
+
+ // values returned by memcached_fetch() must be freed
+ free(value1);
+ free(value2);
+ free(value3);
+
Index: bdb.c
===================================================================
--- bdb.c (revision 98)
+++ bdb.c (working copy)
@@ -306,6 +306,11 @@
exit(EXIT_FAILURE);
}
+ if ((ret = dbp->set_flags(dbp, DB_DUP)) != 0) {
+ fprintf(stderr, "dbp->set_flags: %s\n", db_strerror(ret));
+ exit(EXIT_FAILURE);
+ }
+
/* try to open db*/
ret = dbp->open(dbp, NULL, bdb_settings.db_file, NULL, bdb_settings.db_type, bdb_settings.db_flags, 0664);
switch (ret){