sophia-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.gitmodules +3 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +29 -0
  7. data/Rakefile +22 -0
  8. data/ext/extconf.rb +13 -0
  9. data/ext/sophia.c +220 -0
  10. data/lib/sophia-ruby.rb +1 -0
  11. data/lib/sophia/version.rb +3 -0
  12. data/sophia-ruby.gemspec +47 -0
  13. data/test/test_sophia.rb +33 -0
  14. data/vendor/sophia/.gitignore +18 -0
  15. data/vendor/sophia/COPYRIGHT +29 -0
  16. data/vendor/sophia/README +5 -0
  17. data/vendor/sophia/db/a.h +58 -0
  18. data/vendor/sophia/db/cat.c +195 -0
  19. data/vendor/sophia/db/cat.h +32 -0
  20. data/vendor/sophia/db/core.h +129 -0
  21. data/vendor/sophia/db/crc.c +343 -0
  22. data/vendor/sophia/db/crc.h +14 -0
  23. data/vendor/sophia/db/cursor.c +551 -0
  24. data/vendor/sophia/db/cursor.h +47 -0
  25. data/vendor/sophia/db/e.c +49 -0
  26. data/vendor/sophia/db/e.h +49 -0
  27. data/vendor/sophia/db/file.c +355 -0
  28. data/vendor/sophia/db/file.h +106 -0
  29. data/vendor/sophia/db/gc.c +71 -0
  30. data/vendor/sophia/db/gc.h +14 -0
  31. data/vendor/sophia/db/i.c +368 -0
  32. data/vendor/sophia/db/i.h +155 -0
  33. data/vendor/sophia/db/list.h +91 -0
  34. data/vendor/sophia/db/lock.h +77 -0
  35. data/vendor/sophia/db/macro.h +20 -0
  36. data/vendor/sophia/db/makefile +44 -0
  37. data/vendor/sophia/db/merge.c +662 -0
  38. data/vendor/sophia/db/merge.h +14 -0
  39. data/vendor/sophia/db/meta.h +87 -0
  40. data/vendor/sophia/db/recover.c +433 -0
  41. data/vendor/sophia/db/recover.h +14 -0
  42. data/vendor/sophia/db/ref.h +111 -0
  43. data/vendor/sophia/db/rep.c +128 -0
  44. data/vendor/sophia/db/rep.h +120 -0
  45. data/vendor/sophia/db/sophia.h +84 -0
  46. data/vendor/sophia/db/sp.c +626 -0
  47. data/vendor/sophia/db/sp.h +50 -0
  48. data/vendor/sophia/db/task.h +70 -0
  49. data/vendor/sophia/db/track.h +99 -0
  50. data/vendor/sophia/db/util.c +105 -0
  51. data/vendor/sophia/db/util.h +25 -0
  52. data/vendor/sophia/makefile +7 -0
  53. data/vendor/sophia/sophia.gyp +30 -0
  54. data/vendor/sophia/test/common.c +870 -0
  55. data/vendor/sophia/test/crash.c +492 -0
  56. data/vendor/sophia/test/i.c +403 -0
  57. data/vendor/sophia/test/limit.c +65 -0
  58. data/vendor/sophia/test/makefile +30 -0
  59. data/vendor/sophia/test/merge.c +890 -0
  60. data/vendor/sophia/test/recover.c +1550 -0
  61. data/vendor/sophia/test/test.h +66 -0
  62. metadata +134 -0
@@ -0,0 +1,403 @@
1
+
2
+ /*
3
+ * sophia database
4
+ * sphia.org
5
+ *
6
+ * Copyright (c) Dmitry Simonenko
7
+ * BSD License
8
+ */
9
+
10
+ #include <time.h>
11
+ #include <sophia.h>
12
+ #include <sp.h>
13
+ #include "test.h"
14
+
15
+ static spa a;
16
+
17
+ static inline spv *newv(uint32_t k) {
18
+ spv *v = sp_malloc(&a, sizeof(spv) + sizeof(uint32_t));
19
+ if (spunlikely(v == NULL))
20
+ return NULL;
21
+ v->epoch = 0;
22
+ v->crc = 0;
23
+ v->size = sizeof(k);
24
+ v->flags = 0;
25
+ memcpy(v->key, &k, sizeof(k));
26
+ return v;
27
+ }
28
+
29
+ static inline void freekey(spv *v) {
30
+ sp_free(&a, v);
31
+ }
32
+
33
+ static inline int
34
+ cmp(char *a, size_t asz, char *b, size_t bsz, void *arg) {
35
+ register uint32_t av = *(uint32_t*)a;
36
+ register uint32_t bv = *(uint32_t*)b;
37
+ if (av == bv)
38
+ return 0;
39
+ return (av > bv) ? 1 : -1;
40
+ }
41
+
42
+ static void
43
+ init(void) {
44
+ spi i;
45
+ t( sp_iinit(&i, &a, 256, cmp, NULL) == 0);
46
+ sp_ifree(&i);
47
+ }
48
+
49
+ static void
50
+ set(void) {
51
+ spi index;
52
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
53
+ int k = 0;
54
+ while (k < 8) {
55
+ spv *v = newv(k);
56
+ spv *old = NULL;
57
+ t( v != NULL );
58
+ t( sp_iset(&index, v, &old) == 0);
59
+ t( old == NULL );
60
+ k++;
61
+ }
62
+ sp_ifree(&index);
63
+ }
64
+
65
+ static void
66
+ set_split(void) {
67
+ spi index;
68
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
69
+ int k = 0;
70
+ while (k < 32) {
71
+ spv *v = newv(k);
72
+ spv *old = NULL;
73
+ t( v != NULL );
74
+ t( sp_iset(&index, v, &old) == 0);
75
+ t( old == NULL );
76
+ k++;
77
+ }
78
+ sp_ifree(&index);
79
+ }
80
+
81
+ static void
82
+ set_get(void) {
83
+ spi index;
84
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
85
+ int k = 0;
86
+ while (k < 8) {
87
+ spv *v = newv(k);
88
+ spv *old = NULL;
89
+ t( v != NULL );
90
+ t( sp_iset(&index, v, &old) == 0);
91
+ t( old == NULL );
92
+ k++;
93
+ }
94
+ k = 0;
95
+ while (k < 8) {
96
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
97
+ t( v != NULL );
98
+ t( *(uint32_t*)v->key == k );
99
+ k++;
100
+ }
101
+ sp_ifree(&index);
102
+ }
103
+
104
+ static void
105
+ set_get_split(void) {
106
+ spi index;
107
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
108
+ int k = 0;
109
+ while (k < 32) {
110
+ spv *v = newv(k);
111
+ spv *old = NULL;
112
+ t( v != NULL );
113
+ t( sp_iset(&index, v, &old) == 0);
114
+ t( old == NULL );
115
+ k++;
116
+ }
117
+ k = 0;
118
+ while (k < 32) {
119
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
120
+ t( v != NULL );
121
+ t( *(uint32_t*)v->key == k );
122
+ k++;
123
+ }
124
+ sp_ifree(&index);
125
+ }
126
+
127
+ static void
128
+ set_fetchfwd(void) {
129
+ spi index;
130
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
131
+ int k = 0;
132
+ while (k < 8) {
133
+ spv *v = newv(k);
134
+ spv *old = NULL;
135
+ t( v != NULL );
136
+ t( sp_iset(&index, v, &old) == 0);
137
+ t( old == NULL );
138
+ k++;
139
+ }
140
+ spv *max = sp_imax(&index);
141
+ t( max != NULL );
142
+ t( *(uint32_t*)max->key == 7 );
143
+ k = 0;
144
+ spii it;
145
+ sp_iopen(&it, &index);
146
+ do {
147
+ spv *v = sp_ival(&it);
148
+ t( v != NULL );
149
+ t( *(uint32_t*)v->key == k );
150
+ k++;
151
+ } while (sp_inext(&it));
152
+ sp_ifree(&index);
153
+ }
154
+
155
+ static void
156
+ set_fetchbkw(void) {
157
+ spi index;
158
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
159
+ int k = 0;
160
+ while (k < 8) {
161
+ spv *v = newv(k);
162
+ spv *old = NULL;
163
+ t( v != NULL );
164
+ t( sp_iset(&index, v, &old) == 0);
165
+ t( old == NULL );
166
+ k++;
167
+ }
168
+ k = 7;
169
+ spii it;
170
+ sp_iopen(&it, &index);
171
+ sp_ilast(&it);
172
+ do {
173
+ spv *v = sp_ival(&it);
174
+ t( v != NULL );
175
+ t( *(uint32_t*)v->key == k );
176
+ k--;
177
+ } while (sp_iprev(&it));
178
+ sp_ifree(&index);
179
+ }
180
+
181
+ static void
182
+ set_fetchfwd_split(void) {
183
+ spi index;
184
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
185
+ int k = 0;
186
+ while (k < 73) {
187
+ spv *v = newv(k);
188
+ spv *old = NULL;
189
+ t( v != NULL );
190
+ t( sp_iset(&index, v, &old) == 0);
191
+ t( old == NULL );
192
+ k++;
193
+ }
194
+ k = 0;
195
+ spii it;
196
+ sp_iopen(&it, &index);
197
+ do {
198
+ spv *v = sp_ival(&it);
199
+ t( v != NULL );
200
+ t( *(uint32_t*)v->key == k );
201
+ k++;
202
+ } while (sp_inext(&it));
203
+ sp_ifree(&index);
204
+ }
205
+
206
+ static void
207
+ set_fetchbkw_split(void) {
208
+ spi index;
209
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
210
+ int k = 0;
211
+ while (k < 89) {
212
+ spv *v = newv(k);
213
+ spv *old = NULL;
214
+ t( v != NULL );
215
+ t( sp_iset(&index, v, &old) == 0);
216
+ t( old == NULL );
217
+ k++;
218
+ }
219
+ spv *max = sp_imax(&index);
220
+ t( max != NULL );
221
+ t( *(uint32_t*)max->key == 88 );
222
+ k = 88;
223
+ spii it;
224
+ sp_iopen(&it, &index);
225
+ sp_ilast(&it);
226
+ do {
227
+ spv *v = sp_ival(&it);
228
+ t( v != NULL );
229
+ t( *(uint32_t*)v->key == k );
230
+ k--;
231
+ } while (sp_iprev(&it));
232
+ sp_ifree(&index);
233
+ }
234
+
235
+ static void
236
+ set_del(void) {
237
+ spi index;
238
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
239
+ int k = 0;
240
+ while (k < 8) {
241
+ spv *v = newv(k);
242
+ spv *old = NULL;
243
+ t( v != NULL );
244
+ t( sp_iset(&index, v, &old) == 0);
245
+ t( old == NULL );
246
+ k++;
247
+ }
248
+ k = 0;
249
+ while (k < 8) {
250
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
251
+ t( v != NULL );
252
+ t( *(uint32_t*)v->key == k );
253
+ spv *old = NULL;
254
+ t( sp_idelraw(&index, (char*)&k, sizeof(k), &old) == 1);
255
+ t( old != NULL );
256
+ t( *(uint32_t*)old->key == k );
257
+ freekey(old);
258
+ k++;
259
+ }
260
+ t ( index.count == 0 );
261
+ t ( index.icount == 1 );
262
+ spv *max = sp_imax(&index);
263
+ t( max == NULL );
264
+ spii it;
265
+ sp_iopen(&it, &index);
266
+ t( sp_ival(&it) == NULL );
267
+ t( sp_inext(&it) == 0 );
268
+ sp_ifree(&index);
269
+ }
270
+
271
+ static void
272
+ set_del_split(void) {
273
+ spi index;
274
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
275
+ int k = 0;
276
+ while (k < 37) {
277
+ spv *v = newv(k);
278
+ spv *old = NULL;
279
+ t( v != NULL );
280
+ t( sp_iset(&index, v, &old) == 0);
281
+ t( old == NULL );
282
+ k++;
283
+ }
284
+ k = 0;
285
+ while (k < 37) {
286
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
287
+ t( v != NULL );
288
+ t( *(uint32_t*)v->key == k );
289
+ spv *old = NULL;
290
+ t( sp_idelraw(&index, (char*)&k, sizeof(k), &old) == 1);
291
+ t( old != NULL );
292
+ t( *(uint32_t*)old->key == k );
293
+ freekey(old);
294
+ k++;
295
+ }
296
+ t ( index.count == 0 );
297
+ t ( index.icount == 1 );
298
+ spv *max = sp_imax(&index);
299
+ t( max == NULL );
300
+ spii it;
301
+ sp_iopen(&it, &index);
302
+ t( sp_ival(&it) == NULL );
303
+ t( sp_inext(&it) == 0 );
304
+ sp_ifree(&index);
305
+ }
306
+
307
+ static void
308
+ set_delbkw_split(void) {
309
+ spi index;
310
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
311
+ int k = 0;
312
+ while (k < 37) {
313
+ spv *v = newv(k);
314
+ spv *old = NULL;
315
+ t( v != NULL );
316
+ t( sp_iset(&index, v, &old) == 0);
317
+ t( old == NULL );
318
+ k++;
319
+ }
320
+ k = 36;
321
+ while (k >= 0) {
322
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
323
+ t( v != NULL );
324
+ t( *(uint32_t*)v->key == k );
325
+ spv *old = NULL;
326
+ t( sp_idelraw(&index, (char*)&k, sizeof(k), &old) == 1);
327
+ t( old != NULL );
328
+ t( *(uint32_t*)old->key == k );
329
+ freekey(old);
330
+ k--;
331
+ }
332
+ t ( index.count == 0 );
333
+ t ( index.icount == 1 );
334
+ spv *max = sp_imax(&index);
335
+ t( max == NULL );
336
+ spii it;
337
+ sp_iopen(&it, &index);
338
+ t( sp_ival(&it) == NULL );
339
+ t( sp_inext(&it) == 0 );
340
+ sp_ifree(&index);
341
+ }
342
+
343
+ static void
344
+ set_delrnd_split(void) {
345
+ spi index;
346
+ t( sp_iinit(&index, &a, 16, cmp, NULL) == 0);
347
+ int count = 397;
348
+ int k = 0;
349
+ while (k < count) {
350
+ spv *v = newv(k);
351
+ spv *old = NULL;
352
+ t( v != NULL );
353
+ t( sp_iset(&index, v, &old) == 0);
354
+ t( old == NULL );
355
+ k++;
356
+ }
357
+ srand(time(NULL));
358
+ int total = count;
359
+ while (total != 0) {
360
+ k = rand() % count;
361
+ spv *v = sp_igetraw(&index, (char*)&k, sizeof(k));
362
+ spv *old = NULL;
363
+ int rc = sp_idelraw(&index, (char*)&k, sizeof(k), &old);
364
+ if (rc == 1) {
365
+ t( v == old );
366
+ total--;
367
+ t( old != NULL );
368
+ freekey(old);
369
+ } else {
370
+ t( v == NULL );
371
+ }
372
+ }
373
+ t ( index.count == 0 );
374
+ t ( index.icount == 1 );
375
+ spv *max = sp_imax(&index);
376
+ t( max == NULL );
377
+ spii it;
378
+ sp_iopen(&it, &index);
379
+ t( sp_ival(&it) == NULL );
380
+ t( sp_inext(&it) == 0 );
381
+ sp_ifree(&index);
382
+ }
383
+
384
+ int
385
+ main(int argc, char *argv[])
386
+ {
387
+ sp_allocinit(&a, &sp_allocstd, NULL);
388
+
389
+ test(init);
390
+ test(set);
391
+ test(set_split);
392
+ test(set_get);
393
+ test(set_get_split);
394
+ test(set_fetchfwd);
395
+ test(set_fetchbkw);
396
+ test(set_fetchfwd_split);
397
+ test(set_fetchbkw_split);
398
+ test(set_del);
399
+ test(set_del_split);
400
+ test(set_delbkw_split);
401
+ test(set_delrnd_split);
402
+ return 0;
403
+ }
@@ -0,0 +1,65 @@
1
+
2
+ /*
3
+ * sophia database
4
+ * sphia.org
5
+ *
6
+ * Copyright (c) Dmitry Simonenko
7
+ * BSD License
8
+ */
9
+
10
+ #include <sophia.h>
11
+ #include "test.h"
12
+
13
+ static char *dbrep = "./rep";
14
+
15
+ static inline int
16
+ cmp(char *a, size_t asz, char *b, size_t bsz, void *arg) {
17
+ register uint32_t av = *(uint32_t*)a;
18
+ register uint32_t bv = *(uint32_t*)b;
19
+ if (av == bv)
20
+ return 0;
21
+ return (av > bv) ? 1 : -1;
22
+ }
23
+
24
+ static void
25
+ limit_key(void) {
26
+ void *env = sp_env();
27
+ t( env != NULL );
28
+ t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
29
+ t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
30
+ void *db = sp_open(env);
31
+ t( db != NULL );
32
+ char buf[1];
33
+ t( sp_set(db, buf, UINT16_MAX + 1, buf, sizeof(buf)) == -1 );
34
+ t( sp_error(env) != NULL );
35
+ t( sp_destroy(db) == 0 );
36
+ t( sp_destroy(env) == 0 );
37
+ rmrf(dbrep);
38
+ }
39
+
40
+ static void
41
+ limit_value(void) {
42
+ void *env = sp_env();
43
+ t( env != NULL );
44
+ t( sp_ctl(env, SPDIR, SPO_CREAT|SPO_RDWR, dbrep) == 0 );
45
+ t( sp_ctl(env, SPCMP, cmp, NULL) == 0 );
46
+ void *db = sp_open(env);
47
+ t( db != NULL );
48
+ char buf[1];
49
+ t( sp_set(db, buf, sizeof(buf), buf, UINT32_MAX + 1ULL) == -1 );
50
+ t( sp_error(env) != NULL );
51
+ t( sp_destroy(db) == 0 );
52
+ t( sp_destroy(env) == 0 );
53
+ rmrf(dbrep);
54
+ }
55
+
56
+ int
57
+ main(int argc, char *argv[])
58
+ {
59
+ rmrf(dbrep);
60
+
61
+ test(limit_key);
62
+ if (sizeof(size_t) > 4)
63
+ test(limit_value);
64
+ return 0;
65
+ }