rocksdb-ruby 0.1.5 → 0.2.0
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.
- checksums.yaml +4 -4
- data/ext/rocksdb/rocksdb_db_rb.cc +57 -4
- data/ext/rocksdb/rocksdb_db_rb.h +1 -0
- data/ext/rocksdb/rocksdb_rb.cc +3 -2
- data/ext/rocksdb/rocksdb_rb.h +1 -0
- data/lib/rocksdb.rb +35 -0
- data/lib/rocksdb/ruby/version.rb +1 -1
- data/spec/db_readonly_spec.rb +7 -1
- data/spec/db_spec.rb +35 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02a4d4a14d208c82b4350ad74137d5dc61787981
|
4
|
+
data.tar.gz: 11cb366cd0278dc76275ff163f450e8e18564a6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67921bce1b5f49bee1861089067ee59a9380913356e20455b8135dfe498c2799c5c9b88802221709aa09522442504c1a4e2c2a13d25e806af18c98184f552a0c
|
7
|
+
data.tar.gz: 0cdcbd2d5aa177d1c85db02ac03fe2f83562a9510357228ee36ce699fe527d1e6e4243aaa5d311a09b63c3775bcab19e6c71094c4cd1aacbfe5a36fca70c0ffc
|
@@ -26,7 +26,7 @@ extern "C" {
|
|
26
26
|
if (TYPE(v_options) == T_HASH) {
|
27
27
|
VALUE v = rb_hash_aref(v_options, ID2SYM(rb_intern("readonly")));
|
28
28
|
if(v == Qtrue){
|
29
|
-
|
29
|
+
readonly = true;
|
30
30
|
}
|
31
31
|
set_opt(&options, &v_options);
|
32
32
|
}
|
@@ -42,9 +42,19 @@ extern "C" {
|
|
42
42
|
}
|
43
43
|
|
44
44
|
db_pointer->db = db;
|
45
|
-
|
45
|
+
db_pointer->readonly = readonly;
|
46
|
+
|
46
47
|
return status.ok() ? Qtrue : Qfalse;
|
47
48
|
}
|
49
|
+
VALUE rocksdb_db_init2(int argc, VALUE* argv, VALUE self) {
|
50
|
+
rocksdb_pointer* db_pointer;
|
51
|
+
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
52
|
+
db_pointer->db = NULL;
|
53
|
+
|
54
|
+
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
55
|
+
|
56
|
+
return Qtrue;
|
57
|
+
}
|
48
58
|
|
49
59
|
void set_opt_unit_val(uint64_t* opt, char* name, VALUE *v_options){
|
50
60
|
|
@@ -80,7 +90,12 @@ extern "C" {
|
|
80
90
|
std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
|
81
91
|
std::string value = std::string((char*)RSTRING_PTR(v_value), RSTRING_LEN(v_value));
|
82
92
|
|
83
|
-
|
93
|
+
if (db_pointer->db == NULL) {
|
94
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
95
|
+
}
|
96
|
+
if (db_pointer->readonly) {
|
97
|
+
rb_raise(rb_eRuntimeError, "readonly");
|
98
|
+
}
|
84
99
|
rocksdb::Status status = db_pointer->db->Put(rocksdb::WriteOptions(), key, value);
|
85
100
|
|
86
101
|
return status.ok() ? Qtrue : Qfalse;
|
@@ -93,6 +108,12 @@ extern "C" {
|
|
93
108
|
rocksdb::WriteBatch *batch;
|
94
109
|
Data_Get_Struct(v_write, rocksdb::WriteBatch, batch);
|
95
110
|
|
111
|
+
if (db_pointer->db == NULL) {
|
112
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
113
|
+
}
|
114
|
+
if (db_pointer->readonly) {
|
115
|
+
rb_raise(rb_eRuntimeError, "readonly");
|
116
|
+
}
|
96
117
|
rocksdb::Status status = db_pointer->db->Write(rocksdb::WriteOptions(), batch);
|
97
118
|
return status.ok() ? Qtrue : Qfalse;
|
98
119
|
}
|
@@ -102,9 +123,11 @@ extern "C" {
|
|
102
123
|
|
103
124
|
rocksdb_pointer* db_pointer;
|
104
125
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
105
|
-
|
106
126
|
std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
|
107
127
|
std::string value;
|
128
|
+
if (db_pointer->db == NULL) {
|
129
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
130
|
+
}
|
108
131
|
rocksdb::Status status = db_pointer->db->Get(rocksdb::ReadOptions(), key, &value);
|
109
132
|
|
110
133
|
return (status.IsNotFound()) ? Qnil : rb_enc_str_new(value.data(), value.size(), rb_utf8_encoding());
|
@@ -129,6 +152,9 @@ extern "C" {
|
|
129
152
|
keys[i] = rocksdb::Slice((char*)RSTRING_PTR(op), RSTRING_LEN(op));
|
130
153
|
}
|
131
154
|
|
155
|
+
if (db_pointer->db == NULL) {
|
156
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
157
|
+
}
|
132
158
|
status = db_pointer->db->MultiGet(rocksdb::ReadOptions(),keys,&values);
|
133
159
|
for(i=0; i < length; i++){
|
134
160
|
rb_ary_store(v_array, i, rb_enc_str_new(values[i].data(), values[i].size(), rb_utf8_encoding()));
|
@@ -143,6 +169,12 @@ extern "C" {
|
|
143
169
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
144
170
|
|
145
171
|
std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
|
172
|
+
if (db_pointer->db == NULL) {
|
173
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
174
|
+
}
|
175
|
+
if (db_pointer->readonly) {
|
176
|
+
rb_raise(rb_eRuntimeError, "readonly");
|
177
|
+
}
|
146
178
|
rocksdb::Status status = db_pointer->db->Delete(rocksdb::WriteOptions(), key);
|
147
179
|
|
148
180
|
return status.ok() ? Qtrue : Qfalse;
|
@@ -157,6 +189,9 @@ extern "C" {
|
|
157
189
|
std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
|
158
190
|
std::string value = std::string();
|
159
191
|
|
192
|
+
if (db_pointer->db == NULL) {
|
193
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
194
|
+
}
|
160
195
|
return db_pointer->db->KeyMayExist(rocksdb::ReadOptions(), key, &value) ? Qtrue : Qfalse;
|
161
196
|
}
|
162
197
|
|
@@ -186,6 +221,9 @@ extern "C" {
|
|
186
221
|
VALUE klass;
|
187
222
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
188
223
|
|
224
|
+
if (db_pointer->db == NULL) {
|
225
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
226
|
+
}
|
189
227
|
rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
|
190
228
|
|
191
229
|
klass = rb_class_new_instance(0, NULL, cRocksdb_iterator);
|
@@ -203,6 +241,9 @@ extern "C" {
|
|
203
241
|
|
204
242
|
rocksdb_pointer* db_pointer;
|
205
243
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
244
|
+
if (db_pointer->db == NULL) {
|
245
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
246
|
+
}
|
206
247
|
rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
|
207
248
|
|
208
249
|
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
@@ -220,6 +261,9 @@ extern "C" {
|
|
220
261
|
|
221
262
|
rocksdb_pointer* db_pointer;
|
222
263
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
264
|
+
if (db_pointer->db == NULL) {
|
265
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
266
|
+
}
|
223
267
|
rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
|
224
268
|
|
225
269
|
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
@@ -237,6 +281,9 @@ extern "C" {
|
|
237
281
|
|
238
282
|
rocksdb_pointer* db_pointer;
|
239
283
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
284
|
+
if (db_pointer->db == NULL) {
|
285
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
286
|
+
}
|
240
287
|
rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
|
241
288
|
|
242
289
|
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
@@ -254,6 +301,9 @@ extern "C" {
|
|
254
301
|
VALUE rocksdb_db_reverse_each(VALUE self){
|
255
302
|
rocksdb_pointer* db_pointer;
|
256
303
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
304
|
+
if (db_pointer->db == NULL) {
|
305
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
306
|
+
}
|
257
307
|
rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
|
258
308
|
|
259
309
|
for (it->SeekToLast(); it->Valid(); it->Prev()) {
|
@@ -286,6 +336,9 @@ extern "C" {
|
|
286
336
|
|
287
337
|
rocksdb_pointer* db_pointer;
|
288
338
|
Data_Get_Struct(self, rocksdb_pointer, db_pointer);
|
339
|
+
if (db_pointer->db == NULL) {
|
340
|
+
rb_raise(rb_eRuntimeError, "db not open");
|
341
|
+
}
|
289
342
|
rocksdb::Status status = db_pointer->db->CompactRange(rocksdb::CompactRangeOptions(), &from, &to);
|
290
343
|
return status.ok() ? Qtrue : Qfalse;
|
291
344
|
}
|
data/ext/rocksdb/rocksdb_db_rb.h
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
extern "C" {
|
5
5
|
#include <ruby.h>
|
6
6
|
VALUE rocksdb_db_init(int argc, VALUE* argv, VALUE self);
|
7
|
+
VALUE rocksdb_db_init2(int argc, VALUE* argv, VALUE self);
|
7
8
|
VALUE db_alloc(VALUE klass);
|
8
9
|
VALUE rocksdb_db_put(VALUE self, VALUE v_key, VALUE v_value);
|
9
10
|
VALUE rocksdb_db_write(VALUE self, VALUE v_write);
|
data/ext/rocksdb/rocksdb_rb.cc
CHANGED
@@ -20,14 +20,15 @@ extern "C" {
|
|
20
20
|
cRocksdb_db = rb_define_class_under(cRocksdb, "DB", rb_cObject);
|
21
21
|
rb_define_alloc_func(cRocksdb_db, db_alloc);
|
22
22
|
|
23
|
-
rb_define_private_method(cRocksdb_db, "
|
23
|
+
rb_define_private_method(cRocksdb_db, "__initialize", (METHOD)rocksdb_db_init, -1);
|
24
|
+
rb_define_private_method(cRocksdb_db, "__initialize2", (METHOD)rocksdb_db_init2, -1);
|
24
25
|
rb_define_method(cRocksdb_db, "put", (METHOD)rocksdb_db_put, 2);
|
25
26
|
rb_define_method(cRocksdb_db, "write", (METHOD)rocksdb_db_write, 1);
|
26
27
|
rb_define_method(cRocksdb_db, "get", (METHOD)rocksdb_db_get, 1);
|
27
28
|
rb_define_method(cRocksdb_db, "multi_get", (METHOD)rocksdb_db_multi_get, 1);
|
28
29
|
rb_define_method(cRocksdb_db, "delete", (METHOD)rocksdb_db_delete, 1);
|
29
30
|
rb_define_method(cRocksdb_db, "exists?", (METHOD)rocksdb_db_exists, 1);
|
30
|
-
|
31
|
+
rb_define_private_method(cRocksdb_db, "__close", (METHOD)rocksdb_db_close, 0);
|
31
32
|
rb_define_method(cRocksdb_db, "debug", (METHOD)rocksdb_db_debug, 0);
|
32
33
|
rb_define_method(cRocksdb_db, "new_iterator", (METHOD)rocksdb_db_new_iterator, 0);
|
33
34
|
rb_define_method(cRocksdb_db, "compact", (METHOD)rocksdb_db_compact, -1);
|
data/ext/rocksdb/rocksdb_rb.h
CHANGED
data/lib/rocksdb.rb
CHANGED
@@ -2,10 +2,45 @@ require "rocksdb/RocksDB" # the c extension
|
|
2
2
|
require "rocksdb/ruby/version"
|
3
3
|
|
4
4
|
module RocksDB
|
5
|
+
class DBError < StandardError; end
|
5
6
|
class DB
|
6
7
|
include Enumerable
|
8
|
+
|
9
|
+
@@cache = {}
|
7
10
|
|
8
11
|
class << self
|
12
|
+
def get_instance *args
|
13
|
+
readonly = !!(args[1] && args[1][:readonly])
|
14
|
+
key = args[0]
|
15
|
+
|
16
|
+
if readonly
|
17
|
+
return new(*args)
|
18
|
+
end
|
19
|
+
unless @@cache[key]
|
20
|
+
@@cache[key] = new(*args)
|
21
|
+
end
|
22
|
+
@@cache[key]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize *args
|
27
|
+
readonly = !!(args[1] && args[1][:readonly])
|
28
|
+
@key = args[0]
|
29
|
+
|
30
|
+
if !readonly and @@cache[@key]
|
31
|
+
__initialize2(*args)
|
32
|
+
raise DBError.new("error #{@key.to_s} alread open")
|
33
|
+
end
|
34
|
+
|
35
|
+
__initialize(*args)
|
36
|
+
unless readonly
|
37
|
+
@@cache[@key] = self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def close
|
42
|
+
@@cache.delete(@key)
|
43
|
+
__close
|
9
44
|
end
|
10
45
|
|
11
46
|
alias :includes? :exists?
|
data/lib/rocksdb/ruby/version.rb
CHANGED
data/spec/db_readonly_spec.rb
CHANGED
@@ -12,8 +12,14 @@ describe RocksDB do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'should get data' do
|
15
|
-
@rocksdb2.put("test:multi_db", "10")
|
15
|
+
expect{@rocksdb2.put("test:multi_db", "10")}.to raise_error(RuntimeError)
|
16
|
+
expect{@rocksdb2.delete("test:multi_db")}.to raise_error(RuntimeError)
|
16
17
|
expect(@rocksdb2.get("test:multi_db")).to eq "1"
|
18
|
+
|
19
|
+
batch = RocksDB::Batch.new
|
20
|
+
batch.delete("test:batch1")
|
21
|
+
batch.put("test:batch2", "b")
|
22
|
+
expect{@rocksdb2.write(batch)}.to raise_error(RuntimeError)
|
17
23
|
end
|
18
24
|
|
19
25
|
after do
|
data/spec/db_spec.rb
CHANGED
@@ -116,6 +116,40 @@ describe RocksDB do
|
|
116
116
|
|
117
117
|
end
|
118
118
|
|
119
|
+
it 'race condition test' do
|
120
|
+
key = "test"
|
121
|
+
value = "1"
|
122
|
+
|
123
|
+
expect{RocksDB::DB.new "/tmp/file"}.to raise_error(RocksDB::DBError)
|
124
|
+
|
125
|
+
expect(@rocksdb.put("test:put", "1")).to be true
|
126
|
+
|
127
|
+
@rocksdb2 = RocksDB::DB.new "/tmp/file", {:readonly => true}
|
128
|
+
expect(@rocksdb2.get("test:put")).to eq "1"
|
129
|
+
|
130
|
+
@rocksdb.close
|
131
|
+
@rocksdb = RocksDB::DB.new "/tmp/file"
|
132
|
+
expect(@rocksdb.put("test:put", "2")).to be true
|
133
|
+
|
134
|
+
@rocksdb3 = RocksDB::DB.new "/tmp/file", {:readonly => true}
|
135
|
+
expect(@rocksdb3.get("test:put")).to eq "2"
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'singleton' do
|
140
|
+
@rocksdb2 = RocksDB::DB.get_instance("/tmp/file")
|
141
|
+
@rocksdb3 = RocksDB::DB.get_instance("/tmp/file")
|
142
|
+
expect(@rocksdb).to eq (@rocksdb3)
|
143
|
+
expect(@rocksdb2).to eq (@rocksdb3)
|
144
|
+
|
145
|
+
@rocksdb4 = RocksDB::DB.get_instance("/tmp/file", {:readonly => true})
|
146
|
+
expect(@rocksdb2).not_to eq (@rocksdb4)
|
147
|
+
|
148
|
+
@rocksdb2.close
|
149
|
+
expect{@rocksdb2.get("test:put")}.to raise_error(RuntimeError)
|
150
|
+
expect{@rocksdb3.get("test:put")}.to raise_error(RuntimeError)
|
151
|
+
end
|
152
|
+
|
119
153
|
context 'compact' do
|
120
154
|
it 'works with no parameters' do
|
121
155
|
expect(@rocksdb.compact).to eq(true)
|
@@ -133,7 +167,7 @@ describe RocksDB do
|
|
133
167
|
expect(@rocksdb.compact(nil, 'x')).to eq(true)
|
134
168
|
end
|
135
169
|
end
|
136
|
-
|
170
|
+
|
137
171
|
after do
|
138
172
|
@rocksdb.close
|
139
173
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocksdb-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Isamu Arimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|