ruby-tokyotyrant 0.4 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +23 -3
- data/Rakefile +3 -5
- data/benchmarks/bulk_db.rb +23 -0
- data/benchmarks/bulk_table.rb +51 -3
- data/benchmarks/db.rb +61 -22
- data/benchmarks/table.rb +62 -20
- data/ext/tokyo_tyrant.c +75 -3
- data/ext/tokyo_tyrant.h +18 -3
- data/{lib → ext}/tokyo_tyrant/balancer.rb +3 -6
- data/ext/tokyo_tyrant/rails/tokyo_tyrant_store.rb +106 -0
- data/ext/tokyo_tyrant_bdb.c +138 -0
- data/ext/tokyo_tyrant_bdb.h +8 -0
- data/ext/tokyo_tyrant_consistent_hash.c +34 -0
- data/ext/tokyo_tyrant_consistent_hash.h +8 -0
- data/ext/tokyo_tyrant_db.c +3 -3
- data/ext/tokyo_tyrant_module.c +75 -24
- data/ext/tokyo_tyrant_module.h +1 -1
- data/ext/tokyo_tyrant_table.c +2 -2
- data/spec/spec_base.rb +1 -1
- data/spec/start_tyrants.sh +7 -0
- data/spec/stop_tyrants.sh +2 -1
- data/spec/tokyo_tyrant_bdb_spec.rb +34 -0
- data/spec/tokyo_tyrant_spec.rb +10 -0
- metadata +20 -19
- data/ext/tokyo_utils.c +0 -362
- data/ext/tokyo_utils.h +0 -8
data/ext/tokyo_tyrant_module.h
CHANGED
data/ext/tokyo_tyrant_table.c
CHANGED
@@ -25,7 +25,7 @@ static VALUE cTable_put_method(VALUE vself, VALUE vkey, VALUE vcols, int method)
|
|
25
25
|
break;
|
26
26
|
}
|
27
27
|
|
28
|
-
if(!res) mTokyoTyrant_exception(vself);
|
28
|
+
if(!res) mTokyoTyrant_exception(vself, NULL);
|
29
29
|
tcmapdel(cols);
|
30
30
|
return Qtrue;
|
31
31
|
}
|
@@ -94,7 +94,7 @@ static VALUE cTable_get(VALUE vself, VALUE vkey){
|
|
94
94
|
vkey = StringValueEx(vkey);
|
95
95
|
if(!(cols = tcrdbtblget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)))){
|
96
96
|
if ((ecode = tcrdbecode(db))) {
|
97
|
-
if (ecode != TTENOREC) mTokyoTyrant_exception(vself);
|
97
|
+
if (ecode != TTENOREC) mTokyoTyrant_exception(vself, NULL);
|
98
98
|
}
|
99
99
|
return Qnil;
|
100
100
|
} else {
|
data/spec/spec_base.rb
CHANGED
data/spec/start_tyrants.sh
CHANGED
data/spec/stop_tyrants.sh
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
# stopping the spec ttservers
|
4
4
|
|
5
|
-
ruby -e "%w{ tmp/t_spec.pid tmp/tt1_spec.pid tmp/tt2_spec.pid }.each { |pf| File.exist?(pf) && Process.kill(9, File.read(pf).strip.to_i) }"
|
5
|
+
ruby -e "%w{ tmp/t_spec.pid tmp/tt1_spec.pid tmp/tt2_spec.pid tmp/ttb_spec.pid }.each { |pf| File.exist?(pf) && Process.kill(9, File.read(pf).strip.to_i) }"
|
6
6
|
|
7
7
|
rm tmp/t_spec.pid
|
8
8
|
rm tmp/tt1_spec.pid
|
9
9
|
rm tmp/tt2_spec.pid
|
10
|
+
rm tmp/ttb_spec.pid
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname.join('spec_base') unless $root
|
3
|
+
|
4
|
+
describe TokyoTyrant::BDB, "with an open database" do
|
5
|
+
before do
|
6
|
+
@db = TokyoTyrant::BDB.new('127.0.0.1', 45003)
|
7
|
+
@db.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should put duplicate keys and values" do
|
11
|
+
key = 'desert'
|
12
|
+
values = ['baklava', 'chocolate mousse', 'tiramisu', 'tiramisu']
|
13
|
+
h = { key => values }
|
14
|
+
@db.outlist(key)
|
15
|
+
values.each do |value|
|
16
|
+
@db.putdup(key, value).should.be.true
|
17
|
+
end
|
18
|
+
@db.getlist(h.keys).should == h
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should put a list" do
|
22
|
+
h = { 'mushrooms' => ['portobello', 'button'] }
|
23
|
+
@db.outlist(h.keys)
|
24
|
+
@db.putlist(h).should == []
|
25
|
+
@db.getlist(h.keys).should == h
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should getlist" do
|
29
|
+
h = { 'mushrooms' => ['portobello', 'button'] }
|
30
|
+
@db.outlist(h.keys)
|
31
|
+
@db.putlist(h)
|
32
|
+
@db.getlist(h.keys).should == h
|
33
|
+
end
|
34
|
+
end
|
data/spec/tokyo_tyrant_spec.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
require Pathname(__FILE__).dirname.join('spec_base') unless $root
|
3
3
|
|
4
|
+
describe TokyoTyrant::DB, "with an erroneous socket path" do
|
5
|
+
it "should throw an error" do
|
6
|
+
begin
|
7
|
+
TokyoTyrant::DB.new('/tmp/no_such_file_for_ruby_tokyo_tyrant', 0)
|
8
|
+
rescue => e
|
9
|
+
e.message.should == "Can't open unix socket: /tmp/no_such_file_for_ruby_tokyo_tyrant"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
4
14
|
describe TokyoTyrant::DB, "with an open database" do
|
5
15
|
|
6
16
|
before do
|
metadata
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-tokyotyrant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 5
|
8
|
+
version: "0.5"
|
5
9
|
platform: ruby
|
6
10
|
authors:
|
7
11
|
- Flinn
|
@@ -9,19 +13,10 @@ autorequire:
|
|
9
13
|
bindir: bin
|
10
14
|
cert_chain: []
|
11
15
|
|
12
|
-
date:
|
16
|
+
date: 2010-03-31 00:00:00 -04:00
|
13
17
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: fast_hash_ring
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.1.1
|
24
|
-
version:
|
18
|
+
dependencies: []
|
19
|
+
|
25
20
|
description:
|
26
21
|
email: flinn@actsasflinn.com
|
27
22
|
executables: []
|
@@ -36,6 +31,10 @@ files:
|
|
36
31
|
- README.rdoc
|
37
32
|
- ext/tokyo_tyrant.c
|
38
33
|
- ext/tokyo_tyrant.h
|
34
|
+
- ext/tokyo_tyrant_bdb.c
|
35
|
+
- ext/tokyo_tyrant_bdb.h
|
36
|
+
- ext/tokyo_tyrant_consistent_hash.c
|
37
|
+
- ext/tokyo_tyrant_consistent_hash.h
|
39
38
|
- ext/tokyo_tyrant_db.c
|
40
39
|
- ext/tokyo_tyrant_db.h
|
41
40
|
- ext/tokyo_tyrant_module.c
|
@@ -44,9 +43,8 @@ files:
|
|
44
43
|
- ext/tokyo_tyrant_query.h
|
45
44
|
- ext/tokyo_tyrant_table.c
|
46
45
|
- ext/tokyo_tyrant_table.h
|
47
|
-
- ext/
|
48
|
-
- ext/
|
49
|
-
- lib/tokyo_tyrant/balancer.rb
|
46
|
+
- ext/tokyo_tyrant/balancer.rb
|
47
|
+
- ext/tokyo_tyrant/rails/tokyo_tyrant_store.rb
|
50
48
|
- spec/ext.lua
|
51
49
|
- spec/plu_db.rb
|
52
50
|
- spec/spec.rb
|
@@ -55,6 +53,7 @@ files:
|
|
55
53
|
- spec/stop_tyrants.sh
|
56
54
|
- spec/tokyo_tyrant_balancer_db_spec.rb
|
57
55
|
- spec/tokyo_tyrant_balancer_table_spec.rb
|
56
|
+
- spec/tokyo_tyrant_bdb_spec.rb
|
58
57
|
- spec/tokyo_tyrant_query_spec.rb
|
59
58
|
- spec/tokyo_tyrant_spec.rb
|
60
59
|
- spec/tokyo_tyrant_table_spec.rb
|
@@ -76,18 +75,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
75
|
requirements:
|
77
76
|
- - ">="
|
78
77
|
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
79
80
|
version: "0"
|
80
|
-
version:
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
requirements:
|
83
83
|
- - ">="
|
84
84
|
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
85
87
|
version: "0"
|
86
|
-
version:
|
87
88
|
requirements: []
|
88
89
|
|
89
90
|
rubyforge_project:
|
90
|
-
rubygems_version: 1.3.
|
91
|
+
rubygems_version: 1.3.6
|
91
92
|
signing_key:
|
92
93
|
specification_version: 3
|
93
94
|
summary: A C based TokyoTyrant Ruby binding
|
data/ext/tokyo_utils.c
DELETED
@@ -1,362 +0,0 @@
|
|
1
|
-
#include <tokyo_utils.h>
|
2
|
-
|
3
|
-
extern TCMDB *cMDB_getdb(VALUE vself){
|
4
|
-
TCMDB *db;
|
5
|
-
Data_Get_Struct(rb_iv_get(vself, MDBVNDATA), TCMDB, db);
|
6
|
-
return db;
|
7
|
-
}
|
8
|
-
|
9
|
-
static void cMDB_free(TCMDB *db){
|
10
|
-
tcmdbdel(db);
|
11
|
-
}
|
12
|
-
|
13
|
-
static VALUE cMDB_bnum(VALUE vself){
|
14
|
-
return rb_iv_get(vself, "@bnum");
|
15
|
-
}
|
16
|
-
|
17
|
-
static VALUE cMDB_initialize(int argc, VALUE *argv, VALUE vself){
|
18
|
-
VALUE bnum;
|
19
|
-
TCMDB *db;
|
20
|
-
|
21
|
-
rb_scan_args(argc, argv, "0", &bnum);
|
22
|
-
if(NIL_P(bnum)){
|
23
|
-
db = tcmdbnew();
|
24
|
-
} else {
|
25
|
-
db = tcmdbnew2(bnum);
|
26
|
-
}
|
27
|
-
|
28
|
-
rb_iv_set(vself, "@bnum", bnum);
|
29
|
-
|
30
|
-
rb_iv_set(vself, MDBVNDATA, Data_Wrap_Struct(rb_cObject, 0, cMDB_free, db));
|
31
|
-
|
32
|
-
return Qtrue;
|
33
|
-
}
|
34
|
-
|
35
|
-
static VALUE cMDB_put_method(VALUE vself, VALUE vkey, VALUE vstr, int method){
|
36
|
-
bool res;
|
37
|
-
TCMDB *db = cMDB_getdb(vself);
|
38
|
-
|
39
|
-
vkey = StringValueEx(vkey);
|
40
|
-
vstr = StringValueEx(vstr);
|
41
|
-
|
42
|
-
res = true;
|
43
|
-
switch(method){
|
44
|
-
case TTPUT:
|
45
|
-
tcmdbput(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vstr), RSTRING_LEN(vstr));
|
46
|
-
break;
|
47
|
-
case TTPUTKEEP:
|
48
|
-
res = tcmdbputkeep(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vstr), RSTRING_LEN(vstr));
|
49
|
-
break;
|
50
|
-
case TTPUTCAT:
|
51
|
-
tcmdbputcat(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), RSTRING_PTR(vstr), RSTRING_LEN(vstr));
|
52
|
-
break;
|
53
|
-
default:
|
54
|
-
res = false;
|
55
|
-
break;
|
56
|
-
}
|
57
|
-
|
58
|
-
return res ? Qtrue : Qfalse;
|
59
|
-
}
|
60
|
-
|
61
|
-
static VALUE cMDB_put(VALUE vself, VALUE vkey, VALUE vstr){
|
62
|
-
return cMDB_put_method(vself, vkey, vstr, TTPUT);
|
63
|
-
}
|
64
|
-
|
65
|
-
static VALUE cMDB_putkeep(VALUE vself, VALUE vkey, VALUE vstr){
|
66
|
-
return cMDB_put_method(vself, vkey, vstr, TTPUTKEEP);
|
67
|
-
}
|
68
|
-
|
69
|
-
static VALUE cMDB_putcat(VALUE vself, VALUE vkey, VALUE vstr){
|
70
|
-
return cMDB_put_method(vself, vkey, vstr, TTPUTCAT);
|
71
|
-
}
|
72
|
-
|
73
|
-
static VALUE cMDB_get(VALUE vself, VALUE vkey){
|
74
|
-
VALUE vval;
|
75
|
-
char *buf;
|
76
|
-
int bsiz;
|
77
|
-
TCMDB *db = cMDB_getdb(vself);
|
78
|
-
|
79
|
-
// this is ugly
|
80
|
-
vkey = StringValueEx(vkey);
|
81
|
-
if(!(buf = tcmdbget(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), &bsiz))){
|
82
|
-
return Qnil;
|
83
|
-
} else {
|
84
|
-
vval = StringRaw(buf, bsiz);
|
85
|
-
}
|
86
|
-
|
87
|
-
tcfree(buf);
|
88
|
-
return vval;
|
89
|
-
}
|
90
|
-
|
91
|
-
static VALUE cMDB_vsiz(VALUE vself, VALUE vkey){
|
92
|
-
TCMDB *db = cMDB_getdb(vself);
|
93
|
-
|
94
|
-
vkey = StringValueEx(vkey);
|
95
|
-
return INT2NUM(tcmdbvsiz(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)));
|
96
|
-
}
|
97
|
-
|
98
|
-
static VALUE cMDB_fetch(int argc, VALUE *argv, VALUE vself){
|
99
|
-
VALUE vkey, vrv, vforce;
|
100
|
-
rb_scan_args(argc, argv, "11", &vkey, &vforce);
|
101
|
-
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
102
|
-
if(vforce == Qnil) vforce = Qfalse;
|
103
|
-
|
104
|
-
if(vforce != Qfalse || (vrv = cMDB_get(vself, vkey)) == Qnil){
|
105
|
-
vrv = rb_yield(vkey);
|
106
|
-
cMDB_put(vself, vkey, vrv);
|
107
|
-
}
|
108
|
-
return vrv;
|
109
|
-
}
|
110
|
-
|
111
|
-
static VALUE cMDB_each(VALUE vself){
|
112
|
-
VALUE vrv;
|
113
|
-
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
114
|
-
TCMDB *db = cMDB_getdb(vself);
|
115
|
-
vrv = Qnil;
|
116
|
-
tcmdbiterinit(db);
|
117
|
-
int ksiz;
|
118
|
-
char *kbuf;
|
119
|
-
while((kbuf = tcmdbiternext(db, &ksiz)) != NULL){
|
120
|
-
int vsiz;
|
121
|
-
char *vbuf = tcmdbget(db, kbuf, ksiz, &vsiz);
|
122
|
-
vrv = rb_yield_values(2, rb_str_new2(kbuf), StringRaw(vbuf, vsiz));
|
123
|
-
tcfree(vbuf);
|
124
|
-
tcfree(kbuf);
|
125
|
-
}
|
126
|
-
return vrv;
|
127
|
-
}
|
128
|
-
|
129
|
-
static VALUE cMDB_each_value(VALUE vself){
|
130
|
-
VALUE vrv;
|
131
|
-
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
132
|
-
TCMDB *db = cMDB_getdb(vself);
|
133
|
-
vrv = Qnil;
|
134
|
-
tcmdbiterinit(db);
|
135
|
-
int ksiz;
|
136
|
-
char *kbuf;
|
137
|
-
while((kbuf = tcmdbiternext(db, &ksiz)) != NULL){
|
138
|
-
int vsiz;
|
139
|
-
char *vbuf = tcmdbget(db, kbuf, ksiz, &vsiz);
|
140
|
-
vrv = rb_yield_values(1, StringRaw(vbuf, vsiz));
|
141
|
-
tcfree(vbuf);
|
142
|
-
tcfree(kbuf);
|
143
|
-
}
|
144
|
-
return vrv;
|
145
|
-
}
|
146
|
-
|
147
|
-
static VALUE cMDB_values(VALUE vself){
|
148
|
-
VALUE vary;
|
149
|
-
TCMDB *db = cMDB_getdb(vself);
|
150
|
-
vary = rb_ary_new2(tcmdbrnum(db));
|
151
|
-
tcmdbiterinit(db);
|
152
|
-
int ksiz;
|
153
|
-
char *kbuf;
|
154
|
-
while((kbuf = tcmdbiternext(db, &ksiz)) != NULL){
|
155
|
-
int vsiz;
|
156
|
-
char *vbuf = tcmdbget(db, kbuf, ksiz, &vsiz);
|
157
|
-
rb_ary_push(vary, StringRaw(vbuf, vsiz));
|
158
|
-
tcfree(vbuf);
|
159
|
-
tcfree(kbuf);
|
160
|
-
}
|
161
|
-
return vary;
|
162
|
-
}
|
163
|
-
|
164
|
-
static VALUE cMDB_out(VALUE vself, VALUE vkey){
|
165
|
-
TCMDB *db = cMDB_getdb(vself);
|
166
|
-
|
167
|
-
vkey = StringValueEx(vkey);
|
168
|
-
return tcmdbout(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) ? Qtrue : Qfalse;
|
169
|
-
}
|
170
|
-
|
171
|
-
static VALUE cMDB_check(VALUE vself, VALUE vkey){
|
172
|
-
TCMDB *db = cMDB_getdb(vself);
|
173
|
-
|
174
|
-
vkey = StringValueEx(vkey);
|
175
|
-
return tcmdbvsiz(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey)) >= 0 ? Qtrue : Qfalse;
|
176
|
-
}
|
177
|
-
|
178
|
-
static VALUE cMDB_iterinit(VALUE vself){
|
179
|
-
TCMDB *db = cMDB_getdb(vself);
|
180
|
-
|
181
|
-
tcmdbiterinit(db);
|
182
|
-
return Qtrue;
|
183
|
-
}
|
184
|
-
|
185
|
-
static VALUE cMDB_iternext(VALUE vself){
|
186
|
-
VALUE vval;
|
187
|
-
char *vbuf;
|
188
|
-
TCMDB *db = cMDB_getdb(vself);
|
189
|
-
|
190
|
-
if(!(vbuf = tcmdbiternext2(db))) return Qnil;
|
191
|
-
vval = rb_str_new2(vbuf);
|
192
|
-
tcfree(vbuf);
|
193
|
-
|
194
|
-
return vval;
|
195
|
-
}
|
196
|
-
|
197
|
-
static VALUE cMDB_fwmkeys(int argc, VALUE *argv, VALUE vself){
|
198
|
-
VALUE vprefix, vmax, vary;
|
199
|
-
TCLIST *keys;
|
200
|
-
int max;
|
201
|
-
TCMDB *db = cMDB_getdb(vself);
|
202
|
-
rb_scan_args(argc, argv, "11", &vprefix, &vmax);
|
203
|
-
|
204
|
-
vprefix = StringValueEx(vprefix);
|
205
|
-
max = (vmax == Qnil) ? -1 : NUM2INT(vmax);
|
206
|
-
keys = tcmdbfwmkeys(db, RSTRING_PTR(vprefix), RSTRING_LEN(vprefix), max);
|
207
|
-
vary = listtovary(keys);
|
208
|
-
tclistdel(keys);
|
209
|
-
return vary;
|
210
|
-
}
|
211
|
-
|
212
|
-
static VALUE cMDB_keys(VALUE vself){
|
213
|
-
/*
|
214
|
-
VALUE vary;
|
215
|
-
char *kxstr;
|
216
|
-
TCMDB *db = cMDB_getdb(vself);
|
217
|
-
vary = rb_ary_new2(tcmdbrnum(db));
|
218
|
-
tcmdbiterinit(db);
|
219
|
-
while((kxstr = tcmdbiternext2(db)) != NULL){
|
220
|
-
rb_ary_push(vary, rb_str_new2(kxstr));
|
221
|
-
}
|
222
|
-
return vary;
|
223
|
-
*/
|
224
|
-
|
225
|
-
// Using forward matching keys with an empty string is 100x faster than iternext+get
|
226
|
-
VALUE vary;
|
227
|
-
TCLIST *keys;
|
228
|
-
char *prefix;
|
229
|
-
TCMDB *db = cMDB_getdb(vself);
|
230
|
-
prefix = "";
|
231
|
-
keys = tcmdbfwmkeys2(db, prefix, -1);
|
232
|
-
vary = listtovary(keys);
|
233
|
-
tclistdel(keys);
|
234
|
-
return vary;
|
235
|
-
}
|
236
|
-
|
237
|
-
static VALUE cMDB_addint(VALUE vself, VALUE vkey, int inum){
|
238
|
-
TCMDB *db = cMDB_getdb(vself);
|
239
|
-
vkey = StringValueEx(vkey);
|
240
|
-
|
241
|
-
inum = tcmdbaddint(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), inum);
|
242
|
-
return inum == INT_MIN ? Qnil : INT2NUM(inum);
|
243
|
-
}
|
244
|
-
|
245
|
-
static VALUE cMDB_add_int(int argc, VALUE *argv, VALUE vself){
|
246
|
-
VALUE vkey, vnum;
|
247
|
-
int inum = 1;
|
248
|
-
|
249
|
-
rb_scan_args(argc, argv, "11", &vkey, &vnum);
|
250
|
-
vkey = StringValueEx(vkey);
|
251
|
-
if(NIL_P(vnum)) vnum = INT2NUM(inum);
|
252
|
-
|
253
|
-
return cMDB_addint(vself, vkey, NUM2INT(vnum));
|
254
|
-
}
|
255
|
-
|
256
|
-
static VALUE cMDB_get_int(VALUE vself, VALUE vkey){
|
257
|
-
return cMDB_addint(vself, vkey, 0);
|
258
|
-
}
|
259
|
-
|
260
|
-
static VALUE cMDB_adddouble(VALUE vself, VALUE vkey, double dnum){
|
261
|
-
TCMDB *db = cMDB_getdb(vself);
|
262
|
-
|
263
|
-
vkey = StringValueEx(vkey);
|
264
|
-
dnum = tcmdbadddouble(db, RSTRING_PTR(vkey), RSTRING_LEN(vkey), dnum);
|
265
|
-
return isnan(dnum) ? Qnil : rb_float_new(dnum);
|
266
|
-
}
|
267
|
-
|
268
|
-
static VALUE cMDB_add_double(int argc, VALUE *argv, VALUE vself){
|
269
|
-
VALUE vkey, vnum;
|
270
|
-
double dnum = 1.0;
|
271
|
-
|
272
|
-
rb_scan_args(argc, argv, "11", &vkey, &vnum);
|
273
|
-
vkey = StringValueEx(vkey);
|
274
|
-
if(NIL_P(vnum)) vnum = rb_float_new(dnum);
|
275
|
-
|
276
|
-
return cMDB_adddouble(vself, vkey, NUM2DBL(vnum));
|
277
|
-
}
|
278
|
-
|
279
|
-
static VALUE cMDB_get_double(VALUE vself, VALUE vkey){
|
280
|
-
return cMDB_adddouble(vself, vkey, 0.0);
|
281
|
-
}
|
282
|
-
|
283
|
-
static VALUE cMDB_vanish(VALUE vself){
|
284
|
-
TCMDB *db = cMDB_getdb(vself);
|
285
|
-
|
286
|
-
tcmdbvanish(db);
|
287
|
-
return Qtrue;
|
288
|
-
}
|
289
|
-
|
290
|
-
static VALUE cMDB_rnum(VALUE vself){
|
291
|
-
TCMDB *db = cMDB_getdb(vself);
|
292
|
-
|
293
|
-
return LL2NUM(tcmdbrnum(db));
|
294
|
-
}
|
295
|
-
|
296
|
-
static VALUE cMDB_empty(VALUE vself){
|
297
|
-
TCMDB *db = cMDB_getdb(vself);
|
298
|
-
|
299
|
-
return tcmdbrnum(db) < 1 ? Qtrue : Qfalse;
|
300
|
-
}
|
301
|
-
|
302
|
-
static VALUE cMDB_size(VALUE vself){
|
303
|
-
TCMDB *db = cMDB_getdb(vself);
|
304
|
-
|
305
|
-
return LL2NUM(tcmdbmsiz(db));
|
306
|
-
}
|
307
|
-
|
308
|
-
static VALUE cMDB_each_key(VALUE vself){
|
309
|
-
VALUE vrv;
|
310
|
-
char *kxstr;
|
311
|
-
if(rb_block_given_p() != Qtrue) rb_raise(rb_eArgError, "no block given");
|
312
|
-
TCMDB *db = cMDB_getdb(vself);
|
313
|
-
vrv = Qnil;
|
314
|
-
tcmdbiterinit(db);
|
315
|
-
while((kxstr = tcmdbiternext2(db)) != NULL){
|
316
|
-
vrv = rb_yield_values(1, rb_str_new2(kxstr));
|
317
|
-
}
|
318
|
-
return vrv;
|
319
|
-
}
|
320
|
-
|
321
|
-
void init_utils(){
|
322
|
-
rb_define_private_method(cMDB, "initialize", cMDB_initialize, -1);
|
323
|
-
rb_define_method(cMDB, "bnum", cMDB_bnum, 0);
|
324
|
-
rb_define_method(cMDB, "put", cMDB_put, 2);
|
325
|
-
rb_define_alias(cMDB, "[]=", "put");
|
326
|
-
rb_define_method(cMDB, "putkeep", cMDB_putkeep, 2);
|
327
|
-
rb_define_method(cMDB, "putcat", cMDB_putcat, 2);
|
328
|
-
rb_define_method(cMDB, "get", cMDB_get, 1);
|
329
|
-
rb_define_alias(cMDB, "[]", "get");
|
330
|
-
rb_define_method(cMDB, "vsiz", cMDB_vsiz, 1);
|
331
|
-
rb_define_method(cMDB, "out", cMDB_out, 1);
|
332
|
-
rb_define_method(cMDB, "check", cMDB_check, 1);
|
333
|
-
rb_define_alias(cMDB, "has_key?", "check");
|
334
|
-
rb_define_alias(cMDB, "key?", "check");
|
335
|
-
rb_define_alias(cMDB, "include?", "check");
|
336
|
-
rb_define_alias(cMDB, "member?", "check");
|
337
|
-
rb_define_method(cMDB, "iterinit", cMDB_iterinit, 0);
|
338
|
-
rb_define_method(cMDB, "iternext", cMDB_iternext, 0);
|
339
|
-
rb_define_method(cMDB, "fwmkeys", cMDB_fwmkeys, -1);
|
340
|
-
rb_define_method(cMDB, "keys", cMDB_keys, 0);
|
341
|
-
rb_define_method(cMDB, "add_int", cMDB_add_int, -1);
|
342
|
-
rb_define_alias(cMDB, "addint", "add_int");
|
343
|
-
rb_define_alias(cMDB, "increment", "add_int");
|
344
|
-
rb_define_method(cMDB, "get_int", cMDB_get_int, 1);
|
345
|
-
rb_define_method(cMDB, "add_double", cMDB_add_double, -1);
|
346
|
-
rb_define_alias(cMDB, "adddouble", "add_double");
|
347
|
-
rb_define_method(cMDB, "get_double", cMDB_get_double, 1);
|
348
|
-
rb_define_method(cMDB, "vanish", cMDB_vanish, 0);
|
349
|
-
rb_define_alias(cMDB, "clear", "vanish");
|
350
|
-
rb_define_method(cMDB, "rnum", cMDB_rnum, 0);
|
351
|
-
rb_define_alias(cMDB, "count", "rnum");
|
352
|
-
rb_define_alias(cMDB, "length", "rnum");
|
353
|
-
rb_define_method(cMDB, "size", cMDB_size, 0);
|
354
|
-
rb_define_method(cMDB, "empty?", cMDB_empty, 0);
|
355
|
-
rb_define_method(cMDB, "each_key", cMDB_each_key, 0);
|
356
|
-
|
357
|
-
rb_define_method(cMDB, "fetch", cMDB_fetch, -1);
|
358
|
-
rb_define_method(cMDB, "each", cMDB_each, 0);
|
359
|
-
rb_define_alias(cMDB, "each_pair", "each");
|
360
|
-
rb_define_method(cMDB, "each_value", cMDB_each_value, 0);
|
361
|
-
rb_define_method(cMDB, "values", cMDB_values, 0);
|
362
|
-
}
|