memstore 1.2.1 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.rdoc_options +19 -0
- data/README.md +148 -324
- data/lib/memstore.rb +2 -28
- data/lib/memstore/core.rb +129 -0
- data/lib/memstore/queries.rb +157 -117
- data/lib/memstore/refinements.rb +9 -0
- data/lib/memstore/version.rb +2 -4
- data/memstore.gemspec +13 -14
- data/spec/core_methods_spec.rb +67 -0
- data/spec/core_setup_spec.rb +66 -0
- data/spec/queries_spec.rb +66 -38
- metadata +22 -32
- data/lib/memstore/hashstore.rb +0 -99
- data/lib/memstore/json.rb +0 -31
- data/lib/memstore/msgpack.rb +0 -31
- data/lib/memstore/objectstore.rb +0 -259
- data/lib/memstore/yaml.rb +0 -35
- data/spec/hashstore_spec.rb +0 -67
- data/spec/json_spec.rb +0 -50
- data/spec/msgpack_spec.rb +0 -50
- data/spec/objectstore_spec.rb +0 -117
- data/spec/yaml_spec.rb +0 -50
data/spec/hashstore_spec.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "tempfile"
|
5
|
-
require "memstore"
|
6
|
-
|
7
|
-
describe MemStore::HashStore do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@store = MemStore::HashStore.new(:id)
|
11
|
-
10.times { |i| @store << { id: i } }
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can be instantiated with items" do
|
15
|
-
h = { a: 1, b: 2, c: 3 }
|
16
|
-
store = MemStore::HashStore.new(nil, h)
|
17
|
-
store.items.must_equal h
|
18
|
-
end
|
19
|
-
|
20
|
-
it "indexes items by Object#hash by default" do
|
21
|
-
h = {}
|
22
|
-
store = MemStore::HashStore.new.insert(h)
|
23
|
-
store.items[h.hash].must_equal h
|
24
|
-
end
|
25
|
-
|
26
|
-
it "indexes items using a custom key" do
|
27
|
-
@store.items[0].must_equal @store.all.first
|
28
|
-
end
|
29
|
-
|
30
|
-
it "can be converted to and from binary" do
|
31
|
-
restored = MemStore::HashStore.from_binary(@store.to_binary)
|
32
|
-
restored.must_be_instance_of MemStore::HashStore
|
33
|
-
restored.items.must_equal @store.items
|
34
|
-
restored.instance_variable_get(:@key).must_equal @store.instance_variable_get(:@key)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "returns nil when conversion from binary fails" do
|
38
|
-
MemStore::HashStore.from_binary(nil).must_equal nil
|
39
|
-
MemStore::HashStore.from_binary(Marshal.dump(Object.new)).must_equal nil
|
40
|
-
end
|
41
|
-
|
42
|
-
it "returns nil when marshalled object isn’t instance of HashStore" do
|
43
|
-
MemStore::HashStore.from_binary(MemStore::ObjectStore.new.to_binary).must_equal nil
|
44
|
-
end
|
45
|
-
|
46
|
-
it "can be serialized to and deserialized from a binary file" do
|
47
|
-
tmp = Tempfile.new("memstore")
|
48
|
-
@store.to_file(tmp)
|
49
|
-
restored = MemStore::HashStore.from_file(tmp)
|
50
|
-
restored.must_be_instance_of MemStore::HashStore
|
51
|
-
restored.items.must_equal @store.items
|
52
|
-
restored.instance_variable_get(:@key).must_equal @store.instance_variable_get(:@key)
|
53
|
-
tmp.unlink
|
54
|
-
end
|
55
|
-
|
56
|
-
it "can be converted to and from a hash" do
|
57
|
-
restored = MemStore::HashStore.from_hash(@store.to_hash)
|
58
|
-
restored.items.must_equal @store.items
|
59
|
-
restored.instance_variable_get(:@key).must_equal @store.instance_variable_get(:@key)
|
60
|
-
end
|
61
|
-
|
62
|
-
it "returns nil when conversion from hash fails" do
|
63
|
-
MemStore::HashStore.from_hash(nil).must_equal nil
|
64
|
-
MemStore::HashStore.from_hash({}).must_equal nil
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
data/spec/json_spec.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "tempfile"
|
5
|
-
require "memstore"
|
6
|
-
require "memstore/json"
|
7
|
-
|
8
|
-
describe MemStore::HashStore do
|
9
|
-
|
10
|
-
before do
|
11
|
-
@key = "id"
|
12
|
-
@store = MemStore::HashStore.new(@key)
|
13
|
-
10.times { |i| @store << { "id" => i.to_s } }
|
14
|
-
end
|
15
|
-
|
16
|
-
it "can be converted to and from JSON" do
|
17
|
-
restored = MemStore::HashStore.from_json(@store.to_json)
|
18
|
-
restored.items.must_equal @store.items
|
19
|
-
restored.instance_variable_get(:@key).must_equal @key
|
20
|
-
end
|
21
|
-
|
22
|
-
it "can be serialized to and deserialized from a JSON file" do
|
23
|
-
tmp = Tempfile.new("memstore")
|
24
|
-
@store.to_json_file(tmp)
|
25
|
-
restored = MemStore::HashStore.from_json_file(tmp)
|
26
|
-
restored.items.must_equal @store.items
|
27
|
-
restored.instance_variable_get(:@key).must_equal @key
|
28
|
-
tmp.unlink
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns nil when conversion from JSON fails" do
|
32
|
-
MemStore::HashStore.from_json(nil).must_equal nil
|
33
|
-
MemStore::HashStore.from_json(Marshal.dump(Object.new)).must_equal nil
|
34
|
-
end
|
35
|
-
|
36
|
-
it "supports concurrent access to a single JSON file" do
|
37
|
-
tmp = Tempfile.new("memstore")
|
38
|
-
fork do
|
39
|
-
MemStore::HashStore.with_json_file(tmp, "id") { |store| store.insert({"id"=>"1"}, {"id"=>"3"}) }
|
40
|
-
end
|
41
|
-
fork do
|
42
|
-
MemStore::HashStore.with_json_file(tmp, "id") { |store| store.insert({"id"=>"2"}, {"id"=>"4"}) }
|
43
|
-
end
|
44
|
-
sleep 0.1
|
45
|
-
restored = MemStore::HashStore.from_json_file(tmp)
|
46
|
-
restored.items.must_equal({"1"=>{"id"=>"1"}, "2"=>{"id"=>"2"}, "3"=>{"id"=>"3"}, "4"=>{"id"=>"4"}})
|
47
|
-
tmp.unlink
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
data/spec/msgpack_spec.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "tempfile"
|
5
|
-
require "memstore"
|
6
|
-
require "memstore/msgpack"
|
7
|
-
|
8
|
-
describe MemStore::HashStore do
|
9
|
-
|
10
|
-
before do
|
11
|
-
@key = "id"
|
12
|
-
@store = MemStore::HashStore.new(@key)
|
13
|
-
10.times { |i| @store << { "id" => i } }
|
14
|
-
end
|
15
|
-
|
16
|
-
it "can be converted to and from MessagePack" do
|
17
|
-
restored = MemStore::HashStore.from_msgpack(@store.to_msgpack)
|
18
|
-
restored.items.must_equal @store.items
|
19
|
-
restored.instance_variable_get(:@key).must_equal @key
|
20
|
-
end
|
21
|
-
|
22
|
-
it "can be serialized to and deserialized from a MessagePack file" do
|
23
|
-
tmp = Tempfile.new("memstore")
|
24
|
-
@store.to_msgpack_file(tmp)
|
25
|
-
restored = MemStore::HashStore.from_msgpack_file(tmp)
|
26
|
-
restored.items.must_equal @store.items
|
27
|
-
restored.instance_variable_get(:@key).must_equal @key
|
28
|
-
tmp.unlink
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns nil when conversion from MessagePack fails" do
|
32
|
-
MemStore::HashStore.from_msgpack(nil).must_equal nil
|
33
|
-
MemStore::HashStore.from_msgpack(Marshal.dump(Object.new)).must_equal nil
|
34
|
-
end
|
35
|
-
|
36
|
-
it "supports concurrent access to a single MessagePack file" do
|
37
|
-
tmp = Tempfile.new("memstore")
|
38
|
-
fork do
|
39
|
-
MemStore::HashStore.with_msgpack_file(tmp, "id") { |store| store.insert({"id"=>1}, {"id"=>3}) }
|
40
|
-
end
|
41
|
-
fork do
|
42
|
-
MemStore::HashStore.with_msgpack_file(tmp, "id") { |store| store.insert({"id"=>2}, {"id"=>4}) }
|
43
|
-
end
|
44
|
-
sleep 0.1
|
45
|
-
restored = MemStore::HashStore.from_msgpack_file(tmp)
|
46
|
-
restored.items.must_equal({1=>{"id"=>1}, 2=>{"id"=>2}, 3=>{"id"=>3}, 4=>{"id"=>4}})
|
47
|
-
tmp.unlink
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
data/spec/objectstore_spec.rb
DELETED
@@ -1,117 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "tempfile"
|
5
|
-
require "memstore"
|
6
|
-
|
7
|
-
describe MemStore::ObjectStore do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@store = MemStore::ObjectStore.new(:to_i)
|
11
|
-
10.times { |i| @store << i.to_f }
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can be instantiated with items" do
|
15
|
-
h = { a: 1, b: 2, c: 3 }
|
16
|
-
store = MemStore::ObjectStore.new(nil, h)
|
17
|
-
store.items.must_equal h
|
18
|
-
end
|
19
|
-
|
20
|
-
it "is the default when instantiating MemStore" do
|
21
|
-
MemStore.new.must_be_instance_of MemStore::ObjectStore
|
22
|
-
end
|
23
|
-
|
24
|
-
it "indexes items by Object#hash by default" do
|
25
|
-
o = Object.new
|
26
|
-
store = MemStore::ObjectStore.new.insert(o)
|
27
|
-
store.items[o.hash].must_equal o
|
28
|
-
end
|
29
|
-
|
30
|
-
it "indexes items using a custom key" do
|
31
|
-
@store.items[0].must_equal 0.0
|
32
|
-
end
|
33
|
-
|
34
|
-
it "returns the overall number of items" do
|
35
|
-
@store.length.must_equal 10
|
36
|
-
end
|
37
|
-
|
38
|
-
it "returns a single item by itself" do
|
39
|
-
@store[3].must_equal 3.0
|
40
|
-
end
|
41
|
-
|
42
|
-
it "returns multiple items as an array" do
|
43
|
-
@store[3, 4, 5, 6].must_equal [3.0, 4.0, 5.0, 6.0]
|
44
|
-
end
|
45
|
-
|
46
|
-
it "returns multiple items using a Range as an array" do
|
47
|
-
@store[0..9].must_equal @store.all
|
48
|
-
end
|
49
|
-
|
50
|
-
it "deletes a single item by reference and returns it by itself" do
|
51
|
-
@store.delete_item(3.0).must_equal 3.0
|
52
|
-
end
|
53
|
-
|
54
|
-
it "deletes multiple items by reference and returns them" do
|
55
|
-
@store.delete_items(3.0, 4.0, 5.0, 6.0).must_equal [3.0, 4.0, 5.0, 6.0]
|
56
|
-
@store.all.must_equal [0.0, 1.0, 2.0, 7.0, 8.0, 9.0]
|
57
|
-
end
|
58
|
-
|
59
|
-
it "deletes a single item by key and returns it by itself" do
|
60
|
-
@store.delete_key(3).must_equal 3.0
|
61
|
-
end
|
62
|
-
|
63
|
-
it "deletes multiple items by key and returns them as an array" do
|
64
|
-
@store.delete_keys(3, 4, 5, 6).must_equal [3.0, 4.0, 5.0, 6.0]
|
65
|
-
@store.all.must_equal [0.0, 1.0, 2.0, 7.0, 8.0, 9.0]
|
66
|
-
end
|
67
|
-
|
68
|
-
it "deletes multiple items by key using a Range and returns them as an array" do
|
69
|
-
@store.delete_keys(3..6).must_equal [3.0, 4.0, 5.0, 6.0]
|
70
|
-
@store.all.must_equal [0.0, 1.0, 2.0, 7.0, 8.0, 9.0]
|
71
|
-
end
|
72
|
-
|
73
|
-
it "can be converted to and from binary" do
|
74
|
-
restored = MemStore::ObjectStore.from_binary(@store.to_binary)
|
75
|
-
restored.must_be_instance_of MemStore::ObjectStore
|
76
|
-
restored.items.must_equal @store.items
|
77
|
-
restored.instance_variable_get(:@key).must_equal @store.instance_variable_get(:@key)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "returns nil when conversion from binary fails" do
|
81
|
-
MemStore::ObjectStore.from_binary(nil).must_equal nil
|
82
|
-
MemStore::ObjectStore.from_binary(Marshal.dump(Object.new)).must_equal nil
|
83
|
-
end
|
84
|
-
|
85
|
-
it "returns nil when marshalled object isn’t instance of ObjectStore" do
|
86
|
-
MemStore::ObjectStore.from_binary(MemStore::HashStore.new.to_binary).must_equal nil
|
87
|
-
end
|
88
|
-
|
89
|
-
it "can be serialized to and deserialized from a binary file" do
|
90
|
-
tmp = Tempfile.new("memstore")
|
91
|
-
@store.to_file(tmp)
|
92
|
-
restored = MemStore::ObjectStore.from_file(tmp)
|
93
|
-
restored.must_be_instance_of MemStore::ObjectStore
|
94
|
-
restored.items.must_equal @store.items
|
95
|
-
restored.instance_variable_get(:@key).must_equal @store.instance_variable_get(:@key)
|
96
|
-
tmp.unlink
|
97
|
-
end
|
98
|
-
|
99
|
-
it "returns nil when deserialization from binary file fails" do
|
100
|
-
MemStore::ObjectStore.from_file("does_not_exist").must_equal nil
|
101
|
-
end
|
102
|
-
|
103
|
-
it "supports concurrent access to a single binary file" do
|
104
|
-
tmp = Tempfile.new("memstore")
|
105
|
-
fork do
|
106
|
-
MemStore.with_file(tmp, :to_i) { |store| store.insert(1.0, 3.0) }
|
107
|
-
end
|
108
|
-
fork do
|
109
|
-
MemStore.with_file(tmp, :to_i) { |store| store.insert(2.0, 4.0) }
|
110
|
-
end
|
111
|
-
sleep 0.1
|
112
|
-
restored = MemStore.from_file(tmp)
|
113
|
-
restored.items.must_equal({1=>1.0, 2=>2.0, 3=>3.0, 4=>4.0})
|
114
|
-
tmp.unlink
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
data/spec/yaml_spec.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "tempfile"
|
5
|
-
require "memstore"
|
6
|
-
require "memstore/yaml"
|
7
|
-
|
8
|
-
describe MemStore::HashStore do
|
9
|
-
|
10
|
-
before do
|
11
|
-
@key = :id
|
12
|
-
@store = MemStore::HashStore.new(@key)
|
13
|
-
10.times { |i| @store << { id: i } }
|
14
|
-
end
|
15
|
-
|
16
|
-
it "can be converted to and from YAML" do
|
17
|
-
restored = MemStore::HashStore.from_yaml(@store.to_yaml)
|
18
|
-
restored.items.must_equal @store.items
|
19
|
-
restored.instance_variable_get(:@key).must_equal @key
|
20
|
-
end
|
21
|
-
|
22
|
-
it "can be serialized to and deserialized from a YAML file" do
|
23
|
-
tmp = Tempfile.new("memstore")
|
24
|
-
@store.to_yaml_file(tmp)
|
25
|
-
restored = MemStore::HashStore.from_yaml_file(tmp)
|
26
|
-
restored.items.must_equal @store.items
|
27
|
-
restored.instance_variable_get(:@key).must_equal @key
|
28
|
-
tmp.unlink
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns nil when conversion from YAML fails" do
|
32
|
-
MemStore::HashStore.from_yaml(nil).must_equal nil
|
33
|
-
MemStore::HashStore.from_yaml(Marshal.dump(Object.new)).must_equal nil
|
34
|
-
end
|
35
|
-
|
36
|
-
it "supports concurrent access to a single YAML file" do
|
37
|
-
tmp = Tempfile.new("memstore")
|
38
|
-
fork do
|
39
|
-
MemStore::HashStore.with_yaml_file(tmp, :id) { |store| store.insert({id: 1}, {id: 3}) }
|
40
|
-
end
|
41
|
-
fork do
|
42
|
-
MemStore::HashStore.with_yaml_file(tmp, :id) { |store| store.insert({id: 2}, {id: 4}) }
|
43
|
-
end
|
44
|
-
sleep 0.1
|
45
|
-
restored = MemStore::HashStore.from_yaml_file(tmp)
|
46
|
-
restored.items.must_equal({1=>{id: 1}, 2=>{id: 2}, 3=>{id: 3}, 4=>{id: 4}})
|
47
|
-
tmp.unlink
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|