gnista 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18a1c35381152407bbbdd6694e04424893936c13
4
- data.tar.gz: 94f3a0363f878f04a6d2b4572a1a10b56ec93f2f
3
+ metadata.gz: 36552dd994d8f7ed18d1f26290fca5eaab21608f
4
+ data.tar.gz: 778bd89aef04ff8f9ef1e2e1e7e2b3d24c0c4cd5
5
5
  SHA512:
6
- metadata.gz: b5d83b90fbe736a4d9f7ae3f22c268ccd5ab1d930276fc84f78ee2f93cf51ef9c53403a503e539320f6d1fd5d9f6498ee0601e064f3c1d091b2dc67d4c520ca8
7
- data.tar.gz: 3c5e593188384bd0f7d2e61652c35d4f04ba32e1033ee2141e4e8ac549fea19129752f28db81c87bda863ec8788df53bd3e36509cba11d905fa9e7d6e72cd01b
6
+ metadata.gz: 8ebbd55366f654164ea6b182b90526a64691423331580572253b316c1beb2b5a07b396db8c65e301de84c077e6884cc2759e5f10208ab163af3d02fdaed39024
7
+ data.tar.gz: bf3ce23592e12a42f12f11ff678a483efdd9ceb2128330ad2446eb645fbd858ea5a225b7e7ffb6f0a11081c1497133d7058858fd7a2fb52cb2fa4a1f53b4bc88
data/HISTORY.md CHANGED
@@ -1,3 +1,33 @@
1
+ # Version 0.0.4
2
+
3
+ Release date 2013-09-16
4
+
5
+ ### Added
6
+
7
+ * History for minor changes.
8
+ * Lesson learned regarding "gem yank".
9
+
10
+ # Version 0.0.3
11
+
12
+ Release date 2013-09-16
13
+
14
+ ### Added
15
+
16
+ * ```logpath``` for ```Logwriter```, ```Logreader``` and ```Hash```.
17
+ * ```hashpath``` for ```Hash```.
18
+ * Shorthand syntax for puts and gets. Similar to Ruby's native hash: []= and [].
19
+ * ```empty?``` method for ```Hash```.
20
+ * ```keys``` and ```values``` methods for ```Hash```.
21
+ * ```has_key?``` method for ```Hash```.
22
+ * ```maxkeylen``` and ```maxvaluelen``` for ```Logreader```.
23
+ * ```Logreader``` and ```Hash``` mixin [Enumerable](http://ruby-doc.org/core-2.0.0/Enumerable.html).
24
+ * ```write_batch``` implemented for ```Logwriter```.
25
+ * Useful ```inspect``` for all classes.
26
+
27
+ ### Fixed
28
+
29
+ * String-check for all input to ```Logwriter``` and ```Hash```.
30
+
1
31
  # Version 0.0.2
2
32
 
3
33
  Release date 2013-09-12
data/README.md CHANGED
@@ -29,34 +29,60 @@ logwriter = Gnista::Logwriter.new "mylog.log" # no compression
29
29
  logwriter = Gnista::Logwriter.new "mylog.log", 4000 # 4k compression block size
30
30
  logwriter = Gnista::Logwriter.new "mylog.log", :append # append to existing log
31
31
 
32
+ logwriter.logpath # => "mylog.log"
33
+
32
34
  logwriter.put "key", "value" # put entry
35
+ logwriter["key"] = "value"
33
36
  logwriter.del "key" # delete entry
34
-
35
37
  logwriter.flush
36
38
 
39
+ logwriter.write_batch do
40
+ logwriter["key1"] = "value1"
41
+ logwriter["key2"] = "value2"
42
+ end
43
+
44
+
37
45
  logreader = Gnista::Logreader.new "mylog.log"
46
+ logreader.logpath # => "mylog.log"
47
+
38
48
  logreader.each {|key,value,type| puts key, value, type }
39
49
  # => "key", "value"/nil, :put/:delete
40
50
 
51
+ logreader.maxkeylen # largest key length
52
+ logreader.maxkeylen # largest value length
53
+
54
+
41
55
  Gnista::Hash.write "mylog.hash", "mylog.log" # no preferred hash size
42
56
  Gnista::Hash.write "mylog.hash", "mylog.log", 4 # 32 bit murmurhash3_x86_32
43
57
  Gnista::Hash.write "mylog.hash", "mylog.log", 8 # lower 64-bit part of murmurhash3_x64_128
44
58
 
45
59
  hash = Gnista::Hash.new "mylog.hash", "mylog.log"
60
+ logwriter.hashpath # => "mylog.hash"
61
+ logwriter.logpath # => "mylog.log"
62
+
46
63
  hash.each {|key,value| puts key, value }
47
64
  hash.get "key" # => "value" or nil
65
+ hash["key"]
48
66
 
49
- hash.maxkeylen # largest key length
50
- hash.maxkeylen # largest value length
67
+ hash.maxkeylen
68
+ hash.maxkeylen
51
69
  hash.length
52
70
  hash.collisions
71
+ hash.empty?
72
+ hash.include? "key"
73
+ hash.keys # => ["key1", "key2"]
74
+ hash.values # => ["value1", "value2"]
75
+
53
76
 
54
77
  # Don't forget to close!
55
78
  logwriter.close
56
79
  logreader.close
57
80
  hash.close
81
+
82
+ logwriter.open? || logreader.open? || hash.open? # => false
58
83
  ```
59
84
 
85
+ Both ```Logreader```and ```Hash``` mixin [Enumerable](http://ruby-doc.org/core-2.0.0/Enumerable.html).
60
86
 
61
87
  ## Contributing
62
88
 
data/ext/gnista/gnista.c CHANGED
@@ -94,6 +94,7 @@ static VALUE method_logwriter_initialize(VALUE self, VALUE args) {
94
94
  i_logwriter->open = false;
95
95
  } else {
96
96
  i_logwriter->open = true;
97
+ rb_iv_set(self, "@logpath", rb_ary_entry(args, 0));
97
98
  }
98
99
 
99
100
  return self;
@@ -119,6 +120,8 @@ static VALUE method_logwriter_put(VALUE self, VALUE key, VALUE val) {
119
120
  sparkey_returncode returncode;
120
121
  instance_logwriter *i_logwriter = get_logwriter(self);
121
122
  check_open(i_logwriter->open);
123
+ Check_Type(key, T_STRING);
124
+ Check_Type(val, T_STRING);
122
125
 
123
126
  returncode = sparkey_logwriter_put(i_logwriter->logwriter, RSTRING_LEN(key), (uint8_t*)RSTRING_PTR(key), RSTRING_LEN(val), (uint8_t*)RSTRING_PTR(val));
124
127
 
@@ -133,6 +136,7 @@ static VALUE method_logwriter_delete(VALUE self, VALUE key) {
133
136
  sparkey_returncode returncode;
134
137
  instance_logwriter *i_logwriter = get_logwriter(self);
135
138
  check_open(i_logwriter->open);
139
+ Check_Type(key, T_STRING);
136
140
 
137
141
  returncode = sparkey_logwriter_delete(i_logwriter->logwriter, RSTRING_LEN(key), (uint8_t*)RSTRING_PTR(key));
138
142
 
@@ -203,6 +207,7 @@ static VALUE method_logreader_initialize(VALUE self, VALUE filename) {
203
207
  i_logreader->open = false;
204
208
  } else {
205
209
  i_logreader->open = true;
210
+ rb_iv_set(self, "@logpath", filename);
206
211
  }
207
212
 
208
213
  return self;
@@ -292,6 +297,26 @@ static VALUE method_logreader_open(VALUE self) {
292
297
  }
293
298
  }
294
299
 
300
+ static VALUE method_logreader_maxkeylen(VALUE self) {
301
+ instance_logreader *i_logreader = get_logreader(self);
302
+ uint64_t maxkeylen;
303
+ check_open(i_logreader->open);
304
+
305
+ maxkeylen = sparkey_logreader_maxkeylen(i_logreader->logreader);
306
+
307
+ return INT2NUM(maxkeylen);
308
+ }
309
+
310
+ static VALUE method_logreader_maxvaluelen(VALUE self) {
311
+ instance_logreader *i_logreader = get_logreader(self);
312
+ uint64_t maxvaluelen;
313
+ check_open(i_logreader->open);
314
+
315
+ maxvaluelen = sparkey_logreader_maxvaluelen(i_logreader->logreader);
316
+
317
+ return INT2NUM(maxvaluelen);
318
+ }
319
+
295
320
  /********************************************************************************/
296
321
  /**************** HASH **********************************************************/
297
322
 
@@ -356,6 +381,8 @@ static VALUE method_hash_initialize(VALUE self, VALUE hash_filename, VALUE log_f
356
381
  i_hashreader->open = false;
357
382
  } else {
358
383
  i_hashreader->open = true;
384
+ rb_iv_set(self, "@hashpath", hash_filename);
385
+ rb_iv_set(self, "@logpath", log_filename);
359
386
  }
360
387
 
361
388
  return Qnil;
@@ -436,6 +463,7 @@ static VALUE method_hash_get(VALUE self, VALUE key) {
436
463
  instance_hashreader *i_hashreader = get_hashreader(self);
437
464
  sparkey_logiter *logiter;
438
465
  check_open(i_hashreader->open);
466
+ Check_Type(key, T_STRING);
439
467
 
440
468
  returncode = sparkey_logiter_create(&logiter, sparkey_hash_getreader(i_hashreader->hashreader));
441
469
 
@@ -543,6 +571,8 @@ void Init_gnista() {
543
571
  rb_define_method(Logreader, "close", method_logreader_close, 0);
544
572
  rb_define_method(Logreader, "each", method_logreader_each, 0);
545
573
  rb_define_method(Logreader, "open?", method_logreader_open, 0);
574
+ rb_define_method(Logreader, "maxkeylen", method_logreader_maxkeylen, 0);
575
+ rb_define_method(Logreader, "maxvaluelen", method_logreader_maxvaluelen, 0);
546
576
 
547
577
  rb_define_alloc_func(Hash, alloc_hashreader);
548
578
  rb_define_singleton_method(Hash, "write", method_hash_write, -2);
data/lib/gnista.rb CHANGED
@@ -3,10 +3,65 @@ require "gnista/version"
3
3
 
4
4
  module Gnista
5
5
  class Logwriter
6
+ attr_reader :logpath
7
+
8
+ def write_batch
9
+ yield if block_given?
10
+ flush
11
+ end
12
+
13
+ def inspect
14
+ %(#<#{self.class} #{@logpath.inspect}>)
15
+ end
16
+
6
17
  alias_method :del, :delete
18
+ alias_method :[]=, :put
19
+ alias_method :close!, :close
20
+ end
21
+
22
+ class Logreader
23
+ include Enumerable
24
+
25
+ attr_reader :logpath
26
+
27
+ def inspect
28
+ %(#<#{self.class} #{@logpath.inspect}>)
29
+ end
30
+
31
+ alias_method :close!, :close
7
32
  end
8
33
 
9
34
  class Hash
35
+ include Enumerable
36
+
37
+ attr_reader :hashpath, :logpath
38
+
39
+ def keys
40
+ map {|key,value| key }
41
+ end
42
+
43
+ def values
44
+ map {|key,value| value }
45
+ end
46
+
47
+ def has_key?(key)
48
+ return self[key] != nil
49
+ end
50
+
51
+ def empty?
52
+ return length == 0
53
+ end
54
+
55
+ def inspect
56
+ %(#<#{self.class} #{@hashpath.inspect} #{@logpath.inspect}>)
57
+ end
58
+
10
59
  alias_method :size, :length
60
+ alias_method :[], :get
61
+ alias_method :include?, :has_key?
62
+ alias_method :member?, :has_key?
63
+ alias_method :contains?, :has_key?
64
+ alias_method :exists?, :has_key?
65
+ alias_method :close!, :close
11
66
  end
12
67
  end
@@ -1,3 +1,3 @@
1
1
  module Gnista
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -99,6 +99,7 @@ describe Gnista do
99
99
  it "can get from hash" do
100
100
  @logwriter.put "key1", "value1"
101
101
  @logwriter.put "key2", "value2"
102
+ @logwriter.put "key3", "value3"
102
103
  @logwriter.delete "key2"
103
104
  @logwriter.flush
104
105
  Gnista::Hash.write hash_path, log_path
@@ -109,12 +110,15 @@ describe Gnista do
109
110
 
110
111
  ret = hash.get "key2"
111
112
  ret.must_be_nil
113
+
114
+ ret = hash["key3"]
115
+ ret.must_equal "value3"
112
116
  FileUtils.rm hash_path
113
117
  end
114
118
 
115
119
  it "can get the hash length" do
116
120
  @logwriter.put "key1", "value1"
117
- @logwriter.put "key2", "value2"
121
+ @logwriter["key2"] = "value2"
118
122
  @logwriter.put "key3", "value3"
119
123
  @logwriter.flush
120
124
  Gnista::Hash.write hash_path, log_path
@@ -144,4 +148,100 @@ describe Gnista do
144
148
  FileUtils.rm hash_path
145
149
  end
146
150
 
151
+ it "can inspect logwriter, logreader and hash" do
152
+ logreader = Gnista::Logreader.new log_path
153
+ Gnista::Hash.write hash_path, log_path
154
+ hash = Gnista::Hash.new hash_path, log_path
155
+
156
+ @logwriter.inspect.must_equal %(#<#{@logwriter.class} #{log_path.inspect}>)
157
+ logreader.inspect.must_equal %(#<#{logreader.class} #{log_path.inspect}>)
158
+ hash.inspect.must_equal %(#<#{hash.class} #{hash_path.inspect} #{log_path.inspect}>)
159
+
160
+ hash.close
161
+ logreader.close
162
+ FileUtils.rm hash_path
163
+ end
164
+
165
+ it "can get the logpath and the hashpath from logwriter, logreader and hash" do
166
+ logreader = Gnista::Logreader.new log_path
167
+ Gnista::Hash.write hash_path, log_path
168
+ hash = Gnista::Hash.new hash_path, log_path
169
+
170
+ @logwriter.logpath.must_equal log_path
171
+ logreader.logpath.must_equal log_path
172
+ hash.hashpath.must_equal hash_path
173
+ hash.logpath.must_equal log_path
174
+
175
+ hash.close
176
+ logreader.close
177
+ FileUtils.rm hash_path
178
+ end
179
+
180
+ it "can use empty? on hash" do
181
+ Gnista::Hash.write hash_path, log_path
182
+ hash = Gnista::Hash.new hash_path, log_path
183
+ hash.empty?.must_equal true
184
+ hash.close
185
+
186
+ @logwriter["key"] = "value"
187
+ @logwriter.flush
188
+ Gnista::Hash.write hash_path, log_path
189
+ hash = Gnista::Hash.new hash_path, log_path
190
+ hash.empty?.must_equal false
191
+ hash.close
192
+ FileUtils.rm hash_path
193
+ end
194
+
195
+ it "can read logreader max key/value length" do
196
+ @logwriter.put "abc", "a"
197
+ @logwriter.put "a", "abcd"
198
+ @logwriter.flush
199
+
200
+ logreader = Gnista::Logreader.new log_path
201
+
202
+ logreader.maxkeylen.must_equal 3
203
+ logreader.maxvaluelen.must_equal 4
204
+ end
205
+
206
+ it "can decide if hash has_key?" do
207
+ @logwriter["key"] = "value"
208
+ @logwriter.flush
209
+
210
+ Gnista::Hash.write hash_path, log_path
211
+ hash = Gnista::Hash.new hash_path, log_path
212
+
213
+ hash.has_key?("key").must_equal true
214
+ hash.include?("key1").must_equal false
215
+
216
+ hash.close
217
+ FileUtils.rm hash_path
218
+ end
219
+
220
+ it "extract keys and values from hash" do
221
+ @logwriter["key1"] = "value1"
222
+ @logwriter["key2"] = "value2"
223
+ @logwriter.flush
224
+
225
+ Gnista::Hash.write hash_path, log_path
226
+ hash = Gnista::Hash.new hash_path, log_path
227
+
228
+ hash.keys.must_equal ["key1", "key2"]
229
+ hash.values.must_equal ["value1", "value2"]
230
+
231
+ hash.close
232
+ FileUtils.rm hash_path
233
+ end
234
+
235
+ it "can write_batch with logwriter" do
236
+ @logwriter.write_batch do
237
+ @logwriter["key1"] = "value1"
238
+ end
239
+
240
+ logreader = Gnista::Logreader.new log_path
241
+ _,value,_ = logreader.first
242
+ value.must_equal "value1"
243
+
244
+ logreader.close!
245
+ end
246
+
147
247
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gnista
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emanuel Andersson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-12 00:00:00.000000000 Z
11
+ date: 2013-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler