rocksdb-ruby-kmang 0.1.4

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b12189f75e53a2526f96e1f1b0705fe681c76d5
4
+ data.tar.gz: c49fd886da3a018efa48a707c1fea53b6f62359b
5
+ SHA512:
6
+ metadata.gz: 477fefed800b984480f1fbef565f81ae7181e1f10ff4daf65b1e414bd6af1a69692830a78d7d905a0f61c23ec41ba27a6d1cbd48f24337b15a3a20d0e82e3649
7
+ data.tar.gz: 2967915cb17e8ae7d447c5e14459b0c1a3db481665d8116334fdbee0d174ca47752b2d7f14ecfa457eb9b9629354e91a6980cb8c7c4b9132524b50c4c16b2ccd
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rocksdb-ruby.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013-2016 Isamu Arimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # RocksDB
2
+
3
+ The rocksdb is a persistent in-process key-value store.
4
+
5
+ Read more about it here: http://rocksdb.org/
6
+
7
+ This gem contains Ruby bindings so that you can use it from your Ruby process.
8
+
9
+ ## Installation
10
+
11
+ First install rocksdb.
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'rocksdb-ruby'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rocksdb-ruby
24
+
25
+ ## Usage
26
+
27
+ require "rocksdb"
28
+
29
+ # Reads And Writes
30
+ key = "test"
31
+ value = "1"
32
+ rocksdb = RocksDB::DB.new "/tmp/file"
33
+ rocksdb.put(key, value)
34
+ new_value = rocksdb.get(key)
35
+ rocksdb.delete(key)
36
+
37
+ #Atomic Updates
38
+ batch = RocksDB::Batch.new
39
+ batch.delete("test:batch1")
40
+ batch.put("test:batch2", "b")
41
+ rocksdb.write(batch)
42
+
43
+ #Iteration
44
+ iterator = rocksdb.new_iterator
45
+
46
+ iterator.seek_to_first
47
+ while(iterator.valid)
48
+ iterator.value
49
+ iterator.key
50
+ iterator.next
51
+ end
52
+ iterator.close
53
+
54
+ #Block
55
+ rocksdb.each do |data|
56
+ puts data
57
+ end
58
+
59
+ #Hash access
60
+ rocksdb['key'] = data
61
+ puts rocksdb['key']
62
+
63
+
64
+ rocksdb.close
65
+
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+ task :build do
7
+ Dir.chdir('ext/rocksdb/') do
8
+ output = `ruby extconf.rb`
9
+ raise output unless $? == 0
10
+ output = `make`
11
+ raise output unless $? == 0
12
+ end
13
+ end
14
+
15
+ task :spec => :build
@@ -0,0 +1,9 @@
1
+ require "mkmf"
2
+
3
+ dir_config('rocksdb')
4
+ RbConfig::CONFIG["CPP"] = "g++ -E -std=gnu++11"
5
+
6
+ have_header('rocksdb/db.h')
7
+ have_library('rocksdb')
8
+ $CPPFLAGS << " -std=gnu++11"
9
+ create_makefile("RocksDB/RocksDB")
@@ -0,0 +1,38 @@
1
+ #include "rocksdb_batch_rb.h"
2
+
3
+ extern "C" {
4
+ #include <ruby.h>
5
+
6
+ VALUE batch_alloc(VALUE klass){
7
+ rocksdb::WriteBatch *batch = ALLOC(rocksdb::WriteBatch);
8
+ batch = new rocksdb::WriteBatch;
9
+ return Data_Wrap_Struct(klass, 0, -1, batch);
10
+ }
11
+
12
+ VALUE rocksdb_write_batch_init(){
13
+ return Qtrue;
14
+ }
15
+ VALUE rocksdb_write_batch_put(VALUE self, VALUE v_key, VALUE v_value){
16
+ Check_Type(v_key, T_STRING);
17
+ Check_Type(v_value, T_STRING);
18
+
19
+ rocksdb::WriteBatch *batch;
20
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
21
+ std::string value = std::string((char*)RSTRING_PTR(v_value), RSTRING_LEN(v_value));
22
+
23
+ Data_Get_Struct(self, rocksdb::WriteBatch, batch);
24
+ batch->Put(key, value);
25
+ return Qnil;
26
+ }
27
+ VALUE rocksdb_write_batch_delete(VALUE self, VALUE v_key){
28
+ Check_Type(v_key, T_STRING);
29
+
30
+ rocksdb::WriteBatch *batch;
31
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
32
+
33
+ Data_Get_Struct(self, rocksdb::WriteBatch, batch);
34
+ batch->Delete(key);
35
+
36
+ return Qnil;
37
+ }
38
+ }
@@ -0,0 +1,14 @@
1
+ #include "rocksdb/db.h"
2
+ #include "rocksdb/write_batch.h"
3
+
4
+ extern "C" {
5
+
6
+ #include <ruby.h>
7
+
8
+ typedef VALUE (*METHOD)(...);
9
+
10
+ VALUE rocksdb_write_batch_init();
11
+ VALUE batch_alloc(VALUE klass);
12
+ VALUE rocksdb_write_batch_put(VALUE self, VALUE v_key, VALUE v_value);
13
+ VALUE rocksdb_write_batch_delete(VALUE self, VALUE v_key);
14
+ }
@@ -0,0 +1,271 @@
1
+ #include "rocksdb_rb.h"
2
+ #include "rocksdb_db_rb.h"
3
+ #include "ruby/encoding.h"
4
+ #include <iostream>
5
+
6
+ extern "C" {
7
+ #include <ruby.h>
8
+
9
+ VALUE rocksdb_db_init(int argc, VALUE* argv, VALUE self) {
10
+ VALUE v_db_file_name;
11
+ VALUE v_options;
12
+ rocksdb_pointer* db_pointer;
13
+ rocksdb::DB* db;
14
+ rocksdb::Options options;
15
+ rocksdb::Status status;
16
+ std::string db_file_name;
17
+ bool readonly;
18
+
19
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
20
+ rb_scan_args(argc, argv, "11", &v_db_file_name, &v_options);
21
+
22
+ Check_Type(v_db_file_name, T_STRING);
23
+ db_file_name = std::string((char*)RSTRING_PTR(v_db_file_name));
24
+
25
+ readonly = false;
26
+ if (TYPE(v_options) == T_HASH) {
27
+ VALUE v = rb_hash_aref(v_options, ID2SYM(rb_intern("readonly")));
28
+ if(v == Qtrue){
29
+ readonly = true;
30
+ }
31
+ set_opt(&options, &v_options);
32
+ }
33
+ //std::cout << options.max_bytes_for_level_base << "\n";
34
+ //std::cout << options.max_grandparent_overlap_factor << "\n";
35
+ //std::cout << options.delete_obsolete_files_period_micros << "\n";
36
+
37
+ options.create_if_missing = true;
38
+ if(readonly){
39
+ status = rocksdb::DB::OpenForReadOnly(options, db_file_name, &db);
40
+ }else{
41
+ status = rocksdb::DB::Open(options, db_file_name, &db);
42
+ }
43
+
44
+ db_pointer->db = db;
45
+
46
+ return status.ok() ? Qtrue : Qfalse;
47
+ }
48
+
49
+ void set_opt_unit_val(uint64_t* opt, char* name, VALUE *v_options){
50
+
51
+ VALUE v2 = rb_hash_aref(*v_options, ID2SYM(rb_intern(name)));
52
+ if(RB_TYPE_P(v2, T_FIXNUM)){
53
+ *opt = NUM2INT(v2);
54
+ }
55
+ }
56
+ void set_opt_int_val(int* opt, char* name, VALUE *v_options){
57
+ VALUE v2 = rb_hash_aref(*v_options, ID2SYM(rb_intern(name)));
58
+ if(RB_TYPE_P(v2, T_FIXNUM)){
59
+ *opt = NUM2INT(v2);
60
+ }
61
+ }
62
+
63
+ void set_opt(rocksdb::Options* options, VALUE *v_options){
64
+ set_opt_unit_val(&options->max_bytes_for_level_base, (char *) "max_bytes_for_level_base", v_options);
65
+ set_opt_int_val(&options->max_grandparent_overlap_factor, (char *) "max_grandparent_overlap_factor", v_options);
66
+ set_opt_unit_val(&options->delete_obsolete_files_period_micros, (char *) "delete_obsolete_files_period_micros", v_options);
67
+ }
68
+
69
+ VALUE db_alloc(VALUE klass){
70
+ rocksdb_pointer* db_pointer = ALLOC(rocksdb_pointer);
71
+ return Data_Wrap_Struct(klass, 0, db_free, db_pointer);
72
+ }
73
+
74
+ VALUE rocksdb_db_put(VALUE self, VALUE v_key, VALUE v_value) {
75
+ Check_Type(v_key, T_STRING);
76
+ Check_Type(v_value, T_STRING);
77
+
78
+ rocksdb_pointer* db_pointer;
79
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
80
+
81
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
82
+ std::string value = std::string((char*)RSTRING_PTR(v_value), RSTRING_LEN(v_value));
83
+
84
+ // std::cout << key << "\n";
85
+ rocksdb::Status status = db_pointer->db->Put(rocksdb::WriteOptions(), key, value);
86
+
87
+ return status.ok() ? Qtrue : Qfalse;
88
+ }
89
+
90
+ VALUE rocksdb_db_write(VALUE self, VALUE v_write){
91
+ rocksdb_pointer* db_pointer;
92
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
93
+
94
+ rocksdb::WriteBatch *batch;
95
+ Data_Get_Struct(v_write, rocksdb::WriteBatch, batch);
96
+
97
+ rocksdb::Status status = db_pointer->db->Write(rocksdb::WriteOptions(), batch);
98
+ return status.ok() ? Qtrue : Qfalse;
99
+ }
100
+
101
+ VALUE rocksdb_db_get(VALUE self, VALUE v_key){
102
+ Check_Type(v_key, T_STRING);
103
+
104
+ rocksdb_pointer* db_pointer;
105
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
106
+
107
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
108
+ std::string value;
109
+ rocksdb::Status status = db_pointer->db->Get(rocksdb::ReadOptions(), key, &value);
110
+
111
+ return (status.IsNotFound()) ? Qnil : rb_enc_str_new(value.data(), value.size(), rb_utf8_encoding());
112
+
113
+ }
114
+
115
+
116
+ VALUE rocksdb_db_multi_get(VALUE self, VALUE v_array){
117
+ Check_Type(v_array, T_ARRAY);
118
+
119
+ rocksdb_pointer* db_pointer;
120
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
121
+
122
+ long i;
123
+ long length = RARRAY_LEN(v_array);
124
+ std::vector<std::string> values(length);
125
+ std::vector<rocksdb::Slice> keys(length);
126
+ std::vector<rocksdb::Status> status;
127
+
128
+ for(i=0; i < length; i++){
129
+ VALUE op = rb_ary_entry(v_array, i);
130
+ keys[i] = rocksdb::Slice((char*)RSTRING_PTR(op), RSTRING_LEN(op));
131
+ }
132
+
133
+ status = db_pointer->db->MultiGet(rocksdb::ReadOptions(),keys,&values);
134
+ for(i=0; i < length; i++){
135
+ rb_ary_store(v_array, i, rb_enc_str_new(values[i].data(), values[i].size(), rb_utf8_encoding()));
136
+ }
137
+ return v_array;
138
+ }
139
+
140
+ VALUE rocksdb_db_delete(VALUE self, VALUE v_key){
141
+ Check_Type(v_key, T_STRING);
142
+
143
+ rocksdb_pointer* db_pointer;
144
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
145
+
146
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
147
+ rocksdb::Status status = db_pointer->db->Delete(rocksdb::WriteOptions(), key);
148
+
149
+ return status.ok() ? Qtrue : Qfalse;
150
+ }
151
+
152
+ VALUE rocksdb_db_exists(VALUE self, VALUE v_key){
153
+ Check_Type(v_key, T_STRING);
154
+
155
+ rocksdb_pointer* db_pointer;
156
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
157
+
158
+ std::string key = std::string((char*)RSTRING_PTR(v_key), RSTRING_LEN(v_key));
159
+ std::string value = std::string();
160
+
161
+ return db_pointer->db->KeyMayExist(rocksdb::ReadOptions(), key, &value) ? Qtrue : Qfalse;
162
+ }
163
+
164
+ VALUE rocksdb_db_close(VALUE self){
165
+ rocksdb_pointer* db_pointer;
166
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
167
+
168
+ if(db_pointer->db != NULL){
169
+ delete db_pointer->db;
170
+ db_pointer->db = NULL;
171
+ }
172
+ return Qnil;
173
+ }
174
+
175
+ void db_free(rocksdb_pointer* db_pointer){
176
+ if(db_pointer->db != NULL){
177
+ delete db_pointer->db;
178
+ db_pointer->db = NULL;
179
+ }
180
+ delete db_pointer;
181
+ }
182
+
183
+ VALUE rocksdb_db_new_iterator(VALUE self){
184
+ rocksdb_pointer* db_pointer;
185
+ rocksdb_iterator_pointer* rocksdb_it;
186
+
187
+ VALUE klass;
188
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
189
+
190
+ rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
191
+
192
+ klass = rb_class_new_instance(0, NULL, cRocksdb_iterator);
193
+
194
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
195
+ rocksdb_it->it = it;
196
+ return klass;
197
+ }
198
+
199
+
200
+ VALUE rocksdb_db_each(VALUE self){
201
+ if(!rb_block_given_p()){
202
+ return rocksdb_db_new_iterator(self);
203
+ }
204
+
205
+ rocksdb_pointer* db_pointer;
206
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
207
+ rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
208
+
209
+ for (it->SeekToFirst(); it->Valid(); it->Next()) {
210
+ rb_yield(rb_enc_str_new(it->value().data(), it->value().size(), rb_utf8_encoding()));
211
+ }
212
+
213
+ delete it;
214
+ return self;
215
+ }
216
+
217
+ VALUE rocksdb_db_each_index(VALUE self){
218
+ if(!rb_block_given_p()){
219
+ return rocksdb_db_new_iterator(self);
220
+ }
221
+
222
+ rocksdb_pointer* db_pointer;
223
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
224
+ rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
225
+
226
+ for (it->SeekToFirst(); it->Valid(); it->Next()) {
227
+ rb_yield(rb_enc_str_new(it->key().data(), it->key().size(), rb_utf8_encoding()));
228
+ }
229
+
230
+ delete it;
231
+ return self;
232
+ }
233
+
234
+ VALUE rocksdb_db_each_with_index(VALUE self){
235
+ if(!rb_block_given_p()){
236
+ return rocksdb_db_new_iterator(self);
237
+ }
238
+
239
+ rocksdb_pointer* db_pointer;
240
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
241
+ rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
242
+
243
+ for (it->SeekToFirst(); it->Valid(); it->Next()) {
244
+ VALUE a = rb_enc_str_new(it->key().data(), it->key().size(), rb_utf8_encoding());
245
+ VALUE b = rb_enc_str_new(it->value().data(), it->value().size(), rb_utf8_encoding());
246
+ rb_yield_values(2, a, b);
247
+ }
248
+
249
+ delete it;
250
+ return self;
251
+ }
252
+
253
+
254
+
255
+ VALUE rocksdb_db_reverse_each(VALUE self){
256
+ rocksdb_pointer* db_pointer;
257
+ Data_Get_Struct(self, rocksdb_pointer, db_pointer);
258
+ rocksdb::Iterator* it = db_pointer->db->NewIterator(rocksdb::ReadOptions());
259
+
260
+ for (it->SeekToLast(); it->Valid(); it->Prev()) {
261
+ rb_yield(rb_enc_str_new(it->value().data(), it->value().size(), rb_utf8_encoding()));
262
+ }
263
+
264
+ delete it;
265
+ return self;
266
+ }
267
+
268
+ VALUE rocksdb_db_debug(VALUE self){
269
+ return Qnil;
270
+ }
271
+ }
@@ -0,0 +1,25 @@
1
+ #include "rocksdb/db.h"
2
+ #include "rocksdb/write_batch.h"
3
+
4
+ extern "C" {
5
+ #include <ruby.h>
6
+ VALUE rocksdb_db_init(int argc, VALUE* argv, VALUE self);
7
+ VALUE db_alloc(VALUE klass);
8
+ VALUE rocksdb_db_put(VALUE self, VALUE v_key, VALUE v_value);
9
+ VALUE rocksdb_db_write(VALUE self, VALUE v_write);
10
+ VALUE rocksdb_db_get(VALUE self, VALUE v_key);
11
+ VALUE rocksdb_db_multi_get(VALUE self, VALUE v_array);
12
+ VALUE rocksdb_db_delete(VALUE self, VALUE v_key);
13
+ VALUE rocksdb_db_exists(VALUE self, VALUE v_key);
14
+ VALUE rocksdb_db_close(VALUE self);
15
+ VALUE rocksdb_db_debug(VALUE self);
16
+ VALUE rocksdb_db_new_iterator(VALUE self);
17
+ VALUE rocksdb_db_each(VALUE self);
18
+ VALUE rocksdb_db_each_index(VALUE self);
19
+ VALUE rocksdb_db_each_with_index(VALUE self);
20
+ VALUE rocksdb_db_reverse_each(VALUE self);
21
+ void db_free(rocksdb_pointer* db_pointer);
22
+ void set_opt(rocksdb::Options* opt, VALUE *v_options);
23
+ void set_opt_unit_val(uint64_t* opt, char* name, VALUE *v_options);
24
+ void set_opt_int_val(int* opt, char* name, VALUE *v_options);
25
+ }
@@ -0,0 +1,83 @@
1
+ #include "rocksdb_rb.h"
2
+ #include "rocksdb_batch_rb.h"
3
+ #include "ruby/encoding.h"
4
+ #include <iostream>
5
+
6
+ extern "C" {
7
+ #include <ruby.h>
8
+
9
+ VALUE rocksdb_iterator_seek_to_first(VALUE klass){
10
+ rocksdb_iterator_pointer* rocksdb_it;
11
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
12
+ rocksdb_it->it->SeekToFirst();
13
+
14
+ return Qnil;
15
+ }
16
+
17
+ VALUE rocksdb_iterator_seek_to_last(VALUE klass){
18
+ rocksdb_iterator_pointer* rocksdb_it;
19
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
20
+ rocksdb_it->it->SeekToLast();
21
+
22
+ return Qnil;
23
+ }
24
+
25
+ VALUE rocksdb_iterator_seek(VALUE klass, VALUE v_target){
26
+ Check_Type(v_target, T_STRING);
27
+ rocksdb::Slice target = rocksdb::Slice((char*)RSTRING_PTR(v_target), RSTRING_LEN(v_target));
28
+
29
+ rocksdb_iterator_pointer* rocksdb_it;
30
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
31
+ rocksdb_it->it->Seek(target);
32
+
33
+ return Qnil;
34
+ }
35
+
36
+ VALUE rocksdb_iterator_alloc(VALUE klass){
37
+ rocksdb_iterator_pointer* it = ALLOC(rocksdb_iterator_pointer);
38
+ return Data_Wrap_Struct(klass, 0, -1, it);
39
+ }
40
+
41
+ VALUE rocksdb_iterator_valid(VALUE klass){
42
+ rocksdb_iterator_pointer* rocksdb_it;
43
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
44
+ return rocksdb_it->it->Valid() ? Qtrue : Qfalse;
45
+ }
46
+
47
+ VALUE rocksdb_iterator_key(VALUE klass){
48
+ rocksdb_iterator_pointer* rocksdb_it;
49
+ std::string value;
50
+
51
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
52
+ value = rocksdb_it->it->key().ToString();
53
+
54
+ return rb_enc_str_new(value.data(), value.size(), rb_utf8_encoding());
55
+ }
56
+
57
+ VALUE rocksdb_iterator_value(VALUE klass){
58
+ rocksdb_iterator_pointer* rocksdb_it;
59
+ std::string value;
60
+
61
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
62
+ value = rocksdb_it->it->value().ToString();
63
+
64
+ return rb_enc_str_new(value.data(), value.size(), rb_utf8_encoding());
65
+
66
+ }
67
+
68
+ VALUE rocksdb_iterator_next(VALUE klass){
69
+ rocksdb_iterator_pointer* rocksdb_it;
70
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
71
+ rocksdb_it->it->Next();
72
+
73
+ return Qnil;
74
+ }
75
+
76
+ VALUE rocksdb_iterator_close(VALUE klass){
77
+ rocksdb_iterator_pointer* rocksdb_it;
78
+ Data_Get_Struct(klass, rocksdb_iterator_pointer , rocksdb_it);
79
+ delete rocksdb_it->it;
80
+
81
+ return Qnil;
82
+ }
83
+ }
@@ -0,0 +1,21 @@
1
+ #include "rocksdb/db.h"
2
+ #include "rocksdb/write_batch.h"
3
+
4
+ extern "C" {
5
+
6
+ #include <ruby.h>
7
+
8
+ typedef VALUE (*METHOD)(...);
9
+
10
+ VALUE rocksdb_iterator_seek_to_first(VALUE klass);
11
+ VALUE rocksdb_iterator_seek_to_last(VALUE klass);
12
+ VALUE rocksdb_iterator_seek(VALUE klass, VALUE v_target);
13
+ VALUE rocksdb_iterator_alloc(VALUE klass);
14
+ VALUE rocksdb_iterator_valid(VALUE klass);
15
+ VALUE rocksdb_iterator_key(VALUE klass);
16
+ VALUE rocksdb_iterator_value(VALUE klass);
17
+ VALUE rocksdb_iterator_next(VALUE klass);
18
+ VALUE rocksdb_iterator_close(VALUE klass);
19
+
20
+ }
21
+
@@ -0,0 +1,61 @@
1
+ #include "rocksdb_rb.h"
2
+ #include "rocksdb_db_rb.h"
3
+ #include "rocksdb_batch_rb.h"
4
+ #include "rocksdb_status_rb.h"
5
+ #include "rocksdb_iterator_rb.h"
6
+
7
+ extern "C" {
8
+ VALUE cRocksdb_iterator;
9
+
10
+ void Init_RocksDB(){
11
+
12
+ VALUE cRocksdb;
13
+ VALUE cRocksdb_db;
14
+ VALUE cRocksdb_write_batch;
15
+ VALUE cRocksdb_read_options;
16
+ VALUE cRocksdb_write_options;
17
+ VALUE cRocksdb_status;
18
+
19
+ cRocksdb = rb_define_module("RocksDB");
20
+ cRocksdb_db = rb_define_class_under(cRocksdb, "DB", rb_cObject);
21
+ rb_define_alloc_func(cRocksdb_db, db_alloc);
22
+
23
+ rb_define_private_method(cRocksdb_db, "initialize", (METHOD)rocksdb_db_init, -1);
24
+ rb_define_method(cRocksdb_db, "put", (METHOD)rocksdb_db_put, 2);
25
+ rb_define_method(cRocksdb_db, "write", (METHOD)rocksdb_db_write, 1);
26
+ rb_define_method(cRocksdb_db, "get", (METHOD)rocksdb_db_get, 1);
27
+ rb_define_method(cRocksdb_db, "multi_get", (METHOD)rocksdb_db_multi_get, 1);
28
+ rb_define_method(cRocksdb_db, "delete", (METHOD)rocksdb_db_delete, 1);
29
+ rb_define_method(cRocksdb_db, "exists?", (METHOD)rocksdb_db_exists, 1);
30
+ rb_define_method(cRocksdb_db, "close", (METHOD)rocksdb_db_close, 0);
31
+ rb_define_method(cRocksdb_db, "debug", (METHOD)rocksdb_db_debug, 0);
32
+ rb_define_method(cRocksdb_db, "new_iterator", (METHOD)rocksdb_db_new_iterator, 0);
33
+
34
+ rb_define_method(cRocksdb_db, "iterator", (METHOD)rocksdb_db_each, 0);
35
+ rb_define_method(cRocksdb_db, "each_index", (METHOD)rocksdb_db_each_index, 0);
36
+ rb_define_method(cRocksdb_db, "each_with_index", (METHOD)rocksdb_db_each_with_index, 0);
37
+ rb_define_method(cRocksdb_db, "reverse_each", (METHOD)rocksdb_db_reverse_each, 0);
38
+
39
+ cRocksdb_write_batch = rb_define_class_under(cRocksdb, "Batch", rb_cObject);
40
+ rb_define_alloc_func(cRocksdb_write_batch, batch_alloc);
41
+ rb_define_private_method(cRocksdb_write_batch, "initialize", (METHOD)rocksdb_write_batch_init, 0);
42
+ rb_define_method(cRocksdb_write_batch, "put", (METHOD)rocksdb_write_batch_put, 2);
43
+ rb_define_method(cRocksdb_write_batch, "delete", (METHOD)rocksdb_write_batch_delete, 1);
44
+
45
+ cRocksdb_iterator = rb_define_class_under(cRocksdb, "Iterator", rb_cObject);
46
+ rb_define_alloc_func(cRocksdb_iterator, rocksdb_iterator_alloc);
47
+ rb_define_method(cRocksdb_iterator, "seek_to_first", (METHOD)rocksdb_iterator_seek_to_first, 0);
48
+ rb_define_method(cRocksdb_iterator, "seek_to_last", (METHOD)rocksdb_iterator_seek_to_last, 0);
49
+ rb_define_method(cRocksdb_iterator, "seek", (METHOD)rocksdb_iterator_seek, 1);
50
+ rb_define_method(cRocksdb_iterator, "valid", (METHOD)rocksdb_iterator_valid, 0);
51
+ rb_define_method(cRocksdb_iterator, "key", (METHOD)rocksdb_iterator_key, 0);
52
+ rb_define_method(cRocksdb_iterator, "value", (METHOD)rocksdb_iterator_value, 0);
53
+ rb_define_method(cRocksdb_iterator, "next", (METHOD)rocksdb_iterator_next, 0);
54
+ rb_define_method(cRocksdb_iterator, "close", (METHOD)rocksdb_iterator_close, 0);
55
+
56
+ cRocksdb_status = rb_define_class_under(cRocksdb, "Status", rb_cObject);
57
+ cRocksdb_read_options = rb_define_class_under(cRocksdb, "ReadOptions", rb_cObject);
58
+ cRocksdb_write_options = rb_define_class_under(cRocksdb, "WriteOptions", rb_cObject);
59
+
60
+ }
61
+ }
@@ -0,0 +1,24 @@
1
+ #include "rocksdb/db.h"
2
+ #include "rocksdb/write_batch.h"
3
+
4
+ #ifndef RUBY_ROCKSDB_H
5
+ #define RUBY_ROCKSDB_H 1
6
+
7
+
8
+ extern "C" {
9
+
10
+ #include <ruby.h>
11
+ extern VALUE cRocksdb_iterator;
12
+
13
+ typedef VALUE (*METHOD)(...);
14
+
15
+ struct rocksdb_pointer{
16
+ rocksdb::DB* db;
17
+ };
18
+
19
+ struct rocksdb_iterator_pointer{
20
+ rocksdb::Iterator* it;
21
+ };
22
+
23
+ }
24
+ #endif
@@ -0,0 +1,3 @@
1
+ extern "C" {
2
+ #include <ruby.h>
3
+ }
@@ -0,0 +1,3 @@
1
+ extern "C" {
2
+ #include <ruby.h>
3
+ }
@@ -0,0 +1,28 @@
1
+ require "rocksdb/RocksDB" # the c extension
2
+ require "rocksdb/ruby/version"
3
+
4
+ module RocksDB
5
+ class DB
6
+ include Enumerable
7
+
8
+ class << self
9
+ end
10
+
11
+ alias :includes? :exists?
12
+ alias :contains? :exists?
13
+ alias :member? :exists?
14
+ alias :[] :get
15
+ alias :[]= :put
16
+ alias :close! :close
17
+
18
+ def each(&block)
19
+ if block_given?
20
+ self.each_with_index do |key, value|
21
+ block.call(value)
22
+ end
23
+ else
24
+ self.iterator
25
+ end
26
+ end
27
+ end
28
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ module Rocksdb
2
+ module Ruby
3
+ VERSION = "0.1.4"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rocksdb/ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.extensions = ["ext/rocksdb/extconf.rb"]
8
+
9
+ spec.name = "rocksdb-ruby-kmang"
10
+ spec.version = Rocksdb::Ruby::VERSION
11
+ spec.authors = ["Isamu Arimoto"]
12
+ spec.email = ["isamu.a@gmail.com"]
13
+ spec.summary = %q{A simple RocksDB library for Ruby}
14
+ spec.homepage = "https://github.com/klemens-mang/rocksdb-ruby"
15
+ spec.license = "mit"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["ext", "lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "rocksdb"
4
+
5
+ describe RocksDB do
6
+ before do
7
+ @rocksdb = RocksDB::DB.new "/tmp/file"
8
+ end
9
+
10
+ it 'should get null contained data' do
11
+ @aaa = "aa\0aa"
12
+
13
+ @rocksdb.put("test:null", @aaa)
14
+ expect(@rocksdb.get("test:null")).to eq "aa\0aa"
15
+ end
16
+
17
+ it 'should get from null contained key' do
18
+ @key = "test:aa\0aa"
19
+ @rocksdb.put(@key, "aaa")
20
+ expect(@rocksdb.get(@key)).to eq "aaa"
21
+
22
+ @key = "test:aa"
23
+ expect(@rocksdb.get(@key)).to eq nil
24
+
25
+ end
26
+
27
+ it 'should get multi data' do
28
+ @rocksdb.put("test:nullmulti1\0a", "a\01")
29
+ @rocksdb.put("test:nullmulti2\0a", "b\02")
30
+ @rocksdb.put("test:nullmulti3\0a", "c\03")
31
+
32
+ expect(@rocksdb.multi_get(["test:nullmulti1\0a", "test:nullmulti2\0a", "test:nullmulti3\0a"])).to eq ["a\01", "b\02", "c\03"]
33
+ expect(@rocksdb.multi_get(["test:nullmulti1", "test:nullmulti2", "test:nullmulti3"])).to eq ["", "", ""]
34
+ end
35
+
36
+ after do
37
+ @rocksdb.close
38
+ end
39
+
40
+ end
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "rocksdb"
4
+
5
+ describe RocksDB do
6
+ before do
7
+ @rocksdb = RocksDB::DB.new "/tmp/file3", {:max_bytes_for_level_base => 10485760, :max_grandparent_overlap_factor => 20}
8
+ end
9
+
10
+ it 'should get data' do
11
+ @rocksdb.put("test:multi_db", "10")
12
+ expect(@rocksdb.get("test:multi_db")).to eq "10"
13
+ end
14
+
15
+ after do
16
+ @rocksdb.close
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "rocksdb"
4
+
5
+ describe RocksDB do
6
+ before do
7
+ @rocksdb = RocksDB::DB.new "/tmp/file2"
8
+ @rocksdb.put("test:multi_db", "1")
9
+ @rocksdb.close
10
+
11
+ @rocksdb2 = RocksDB::DB.new "/tmp/file2", {:readonly => true}
12
+ end
13
+
14
+ it 'should get data' do
15
+ @rocksdb2.put("test:multi_db", "10")
16
+ expect(@rocksdb2.get("test:multi_db")).to eq "1"
17
+ end
18
+
19
+ after do
20
+ @rocksdb2.close
21
+ end
22
+ end
@@ -0,0 +1,123 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "rocksdb"
4
+
5
+ describe RocksDB do
6
+ before do
7
+ @rocksdb = RocksDB::DB.new "/tmp/file"
8
+ end
9
+
10
+ it 'should get data' do
11
+ @rocksdb.put("test:read", "1")
12
+ expect(@rocksdb.get("test:read")).to eq "1"
13
+ end
14
+
15
+ it 'should put data' do
16
+ expect(@rocksdb.put("test:put", "2")).to be true
17
+ expect(@rocksdb.get("test:put")).to eq "2"
18
+ end
19
+
20
+ it 'should delete data' do
21
+ @rocksdb.put("test:delete", "3")
22
+ expect(@rocksdb.get("test:delete")).to eq "3"
23
+
24
+ expect(@rocksdb.delete("test:delete")).to be true
25
+ expect(@rocksdb.get("test:delete")).to be_nil
26
+ end
27
+
28
+ it 'should get multi data' do
29
+ @rocksdb.put("test:multi1", "a")
30
+ @rocksdb.put("test:multi2", "b")
31
+ @rocksdb.put("test:multi3", "c")
32
+
33
+ expect(@rocksdb.multi_get(["test:multi1", "test:multi2", "test:multi3"])).to eq ["a", "b", "c"]
34
+ end
35
+
36
+ it 'should put data atomic update' do
37
+ @rocksdb.put("test:batch1", "a")
38
+ @rocksdb.delete("test:batch2")
39
+
40
+ expect(@rocksdb.get("test:batch1")).to eq "a"
41
+ expect(@rocksdb.get("test:batch")).to be_nil
42
+
43
+ batch = RocksDB::Batch.new
44
+ batch.delete("test:batch1")
45
+ batch.put("test:batch2", "b")
46
+ @rocksdb.write(batch)
47
+
48
+ expect(@rocksdb.get("test:batch1")).to be_nil
49
+ expect(@rocksdb.get("test:batch2")).to eq "b"
50
+ end
51
+
52
+ it 'should use multiple db' do
53
+ @rocksdb2 = RocksDB::DB.new "/tmp/file2"
54
+
55
+ @rocksdb.put("test:multi_db", "1")
56
+ @rocksdb2.put("test:multi_db", "2")
57
+
58
+ expect(@rocksdb.get("test:multi_db")).to eq "1"
59
+ expect(@rocksdb2.get("test:multi_db")).to eq "2"
60
+ @rocksdb2.close
61
+ end
62
+
63
+ it 'should use japanese charactor' do
64
+ @rocksdb.put("test:japanese", "あいうえお")
65
+
66
+ expect(@rocksdb.get("test:japanese")).to eq "あいうえお"
67
+ expect(@rocksdb.multi_get(["test:japanese"])).to eq ["あいうえお"]
68
+ end
69
+
70
+ it 'should use each' do
71
+ iter = @rocksdb.each
72
+
73
+ array = []
74
+ @rocksdb.each do |value|
75
+ expect(value).not_to be_empty
76
+ array << value
77
+ end
78
+
79
+ rev_array = []
80
+ @rocksdb.reverse_each do |value|
81
+ expect(value).not_to be_empty
82
+ rev_array << value
83
+ end
84
+
85
+
86
+ expect(array).to eq rev_array.reverse
87
+
88
+ @rocksdb.each_index do |key|
89
+ expect(key).not_to be_empty
90
+ end
91
+
92
+ @rocksdb.each_with_index do |key, value|
93
+ expect(key).not_to be_empty
94
+ expect(value).not_to be_empty
95
+ end
96
+
97
+ iter.close
98
+ end
99
+
100
+ it 'should exists?' do
101
+ @rocksdb.put("test:exists?", "a")
102
+ @rocksdb.delete("test:noexists?")
103
+ expect(@rocksdb.exists?("test:exists?")).to be true
104
+ expect(@rocksdb.exists?("test:noexists?")).to be false
105
+
106
+ expect(@rocksdb.includes?("test:exists?")).to be true
107
+ expect(@rocksdb.includes?("test:noexists?")).to be false
108
+
109
+ end
110
+
111
+ it 'hash' do
112
+ @rocksdb.delete("test:hash")
113
+ expect(@rocksdb["test:hash"]).to be_nil
114
+ @rocksdb["test:hash"] = "a"
115
+ expect(@rocksdb["test:hash"]).to eq "a"
116
+
117
+ end
118
+
119
+ after do
120
+ @rocksdb.close
121
+ end
122
+ end
123
+
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+ require "RocksDB"
4
+
5
+ describe RocksDB do
6
+ before do
7
+ @rocksdb = RocksDB::DB.new "/tmp/file"
8
+ end
9
+
10
+ it 'should use iterator' do
11
+ iterator = @rocksdb.new_iterator
12
+
13
+ iterator.seek_to_first
14
+
15
+ expect(iterator.valid).to be true
16
+ while(iterator.valid)
17
+ expect(iterator.value).not_to be_empty
18
+ expect(iterator.key).not_to be_empty
19
+ iterator.next
20
+ end
21
+ iterator.close
22
+ end
23
+
24
+ it 'should seek iterator' do
25
+ iterator = @rocksdb.new_iterator
26
+
27
+ iterator.seek("test:put")
28
+
29
+ iterator.valid
30
+ expect(iterator.value).to eq "2"
31
+ expect(iterator.key).to eq "test:put"
32
+
33
+ iterator.close
34
+ end
35
+
36
+ after do
37
+ @rocksdb.close
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ $: << File.dirname(__FILE__) + '/../ext/rocksdb'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocksdb-ruby-kmang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Isamu Arimoto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - isamu.a@gmail.com
58
+ executables: []
59
+ extensions:
60
+ - ext/rocksdb/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - ext/rocksdb/extconf.rb
69
+ - ext/rocksdb/rocksdb_batch_rb.cc
70
+ - ext/rocksdb/rocksdb_batch_rb.h
71
+ - ext/rocksdb/rocksdb_db_rb.cc
72
+ - ext/rocksdb/rocksdb_db_rb.h
73
+ - ext/rocksdb/rocksdb_iterator_rb.cc
74
+ - ext/rocksdb/rocksdb_iterator_rb.h
75
+ - ext/rocksdb/rocksdb_rb.cc
76
+ - ext/rocksdb/rocksdb_rb.h
77
+ - ext/rocksdb/rocksdb_status_rb.cc
78
+ - ext/rocksdb/rocksdb_status_rb.h
79
+ - lib/rocksdb.rb
80
+ - lib/rocksdb/ruby.rb
81
+ - lib/rocksdb/ruby/version.rb
82
+ - rocksdb-ruby.gemspec
83
+ - spec/db_null_spec.rb
84
+ - spec/db_options_spec.rb
85
+ - spec/db_readonly_spec.rb
86
+ - spec/db_spec.rb
87
+ - spec/iterator_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: https://github.com/klemens-mang/rocksdb-ruby
90
+ licenses:
91
+ - mit
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - ext
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A simple RocksDB library for Ruby
114
+ test_files:
115
+ - spec/db_null_spec.rb
116
+ - spec/db_options_spec.rb
117
+ - spec/db_readonly_spec.rb
118
+ - spec/db_spec.rb
119
+ - spec/iterator_spec.rb
120
+ - spec/spec_helper.rb