mongoid_identity_map 0.2.0 → 0.3.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.
- data/lib/mongoid_identity_map/identity_map.rb +19 -5
- data/lib/mongoid_identity_map/version.rb +1 -1
- data/lib/mongoid_identity_map.rb +0 -1
- data/spec/mongoid_identity_map/contexts/identity_mappable_spec.rb +16 -20
- data/spec/mongoid_identity_map/identity_map_spec.rb +65 -29
- metadata +3 -5
- data/lib/mongoid_identity_map/thread_local_hash.rb +0 -27
- data/spec/mongoid_identity_map/thread_local_hash_spec.rb +0 -44
@@ -2,16 +2,30 @@ module MongoidIdentityMap
|
|
2
2
|
class IdentityMap
|
3
3
|
|
4
4
|
class << self
|
5
|
-
def
|
6
|
-
|
5
|
+
def set(key, model)
|
6
|
+
thread_local_hash[key] = thread_local_hash.values.detect {|value| value == model} || model
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def get(key)
|
10
|
+
thread_local_hash[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch(key)
|
14
|
+
get(key) || set(key, yield)
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove(model)
|
18
|
+
thread_local_hash.delete_if {|key, value| value == model}
|
11
19
|
end
|
12
20
|
|
13
21
|
def clear
|
14
|
-
|
22
|
+
Thread.current[:mongoid_identity_map_current_thread_hash] = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def thread_local_hash
|
28
|
+
Thread.current[:mongoid_identity_map_current_thread_hash] ||= Hash.new
|
15
29
|
end
|
16
30
|
end
|
17
31
|
|
data/lib/mongoid_identity_map.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "mongoid"
|
2
2
|
|
3
3
|
require "mongoid_identity_map/clear_middleware"
|
4
|
-
require "mongoid_identity_map/thread_local_hash"
|
5
4
|
require "mongoid_identity_map/identity_map"
|
6
5
|
require "mongoid_identity_map/railtie" if defined?(Rails)
|
7
6
|
require "mongoid_identity_map/contexts/identity_mappable"
|
@@ -8,42 +8,38 @@ describe MongoidIdentityMap::Contexts::IdentityMappable do
|
|
8
8
|
let(:model_attributes) do
|
9
9
|
model.attributes
|
10
10
|
end
|
11
|
+
|
12
|
+
let(:selector) do
|
13
|
+
{:selector => :value}
|
14
|
+
end
|
11
15
|
|
12
16
|
describe "#one" do
|
13
|
-
let(:selector) do
|
14
|
-
{:selector => :value}
|
15
|
-
end
|
16
|
-
|
17
17
|
before do
|
18
18
|
Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
|
19
19
|
end
|
20
|
-
|
21
|
-
it "should
|
22
|
-
Model.where(selector).one.should == model
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should fetch model from identity map" do
|
20
|
+
|
21
|
+
it "should fetch model from identity map under key comprised of find selector merged with model class" do
|
26
22
|
MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
|
27
23
|
Model.where(selector).one
|
28
24
|
end
|
25
|
+
|
26
|
+
it "should return model" do
|
27
|
+
Model.where(selector).one.should == model
|
28
|
+
end
|
29
29
|
end
|
30
30
|
|
31
31
|
describe "#first" do
|
32
|
-
let(:selector) do
|
33
|
-
{:selector => :value}
|
34
|
-
end
|
35
|
-
|
36
32
|
before do
|
37
33
|
Model.collection.stub!(:find_one).with(selector, {}).and_return(model_attributes)
|
38
34
|
end
|
39
|
-
|
40
|
-
it "should
|
41
|
-
Model.where(selector).first.should == model
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should fetch model from identity map" do
|
35
|
+
|
36
|
+
it "should fetch model from identity map under key comprised of find selector merged with model class" do
|
45
37
|
MongoidIdentityMap::IdentityMap.should_receive(:fetch).with(selector.merge(:_klass => Model))
|
46
38
|
Model.where(selector).first
|
47
39
|
end
|
40
|
+
|
41
|
+
it "should return model" do
|
42
|
+
Model.where(selector).first.should == model
|
43
|
+
end
|
48
44
|
end
|
49
45
|
end
|
@@ -1,62 +1,98 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe MongoidIdentityMap::IdentityMap do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
describe "
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
let(:model) {Model.new}
|
5
|
+
let(:other_model) {Model.new}
|
6
|
+
let(:key) {:key}
|
7
|
+
let(:other_key) {:key2}
|
8
|
+
|
9
|
+
describe "storage" do
|
10
|
+
describe "in multiple threads" do
|
11
|
+
let(:other_model) {Model.new}
|
12
|
+
|
13
|
+
it "should keep each thread's storage separate" do
|
14
|
+
MongoidIdentityMap::IdentityMap.set(:key, model)
|
15
|
+
|
16
|
+
Thread.new do
|
17
|
+
MongoidIdentityMap::IdentityMap.set(:key, other_model)
|
18
|
+
MongoidIdentityMap::IdentityMap.get(:key).should == other_model
|
19
|
+
end.join
|
20
|
+
|
21
|
+
MongoidIdentityMap::IdentityMap.get(:key).should == model
|
13
22
|
end
|
14
23
|
end
|
15
24
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
25
|
+
describe "when the same model is stored under different keys" do
|
26
|
+
let(:same_model_different_instance) {Model.new(model.attributes)}
|
27
|
+
|
28
|
+
it "should store the same instance of the model under the different keys" do
|
29
|
+
MongoidIdentityMap::IdentityMap.set(:key, model)
|
30
|
+
MongoidIdentityMap::IdentityMap.set(:key2, same_model_different_instance)
|
31
|
+
MongoidIdentityMap::IdentityMap.get(:key).should be(model)
|
32
|
+
MongoidIdentityMap::IdentityMap.get(:key2).should be(model)
|
20
33
|
end
|
34
|
+
end
|
35
|
+
end
|
21
36
|
|
22
|
-
|
23
|
-
|
24
|
-
|
37
|
+
describe ".fetch" do
|
38
|
+
let(:fetch) do
|
39
|
+
MongoidIdentityMap::IdentityMap.fetch(key) do
|
40
|
+
Model.first
|
25
41
|
end
|
42
|
+
end
|
43
|
+
|
44
|
+
before do
|
45
|
+
Model.stub!(:first).and_return(model)
|
46
|
+
end
|
26
47
|
|
27
|
-
|
28
|
-
|
48
|
+
context "when model doesn't exist in identity map" do
|
49
|
+
it "should store model in identity map" do
|
29
50
|
fetch
|
51
|
+
MongoidIdentityMap::IdentityMap.get(key).should be(model)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return model" do
|
55
|
+
fetch.should be(model)
|
30
56
|
end
|
31
57
|
end
|
32
58
|
|
33
|
-
context "when
|
59
|
+
context "when model exists in identity map" do
|
34
60
|
before do
|
35
|
-
MongoidIdentityMap::
|
61
|
+
MongoidIdentityMap::IdentityMap.set(key, model)
|
36
62
|
end
|
37
63
|
|
38
|
-
it "should
|
39
|
-
|
64
|
+
it "should not yield block" do
|
65
|
+
Model.should_not_receive(:first)
|
66
|
+
fetch
|
40
67
|
end
|
41
68
|
|
42
|
-
it "should
|
43
|
-
|
44
|
-
fetch
|
69
|
+
it "should return model" do
|
70
|
+
fetch.should be(model)
|
45
71
|
end
|
46
72
|
end
|
47
73
|
end
|
48
74
|
|
49
75
|
describe ".remove" do
|
50
|
-
|
51
|
-
MongoidIdentityMap::
|
52
|
-
|
76
|
+
before do
|
77
|
+
MongoidIdentityMap::IdentityMap.set(key, model)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should remove model from identity map" do
|
81
|
+
MongoidIdentityMap::IdentityMap.remove(model)
|
82
|
+
MongoidIdentityMap::IdentityMap.get(key).should be_nil
|
53
83
|
end
|
54
84
|
end
|
55
85
|
|
56
86
|
describe ".clear" do
|
87
|
+
before do
|
88
|
+
MongoidIdentityMap::IdentityMap.set(key, model)
|
89
|
+
MongoidIdentityMap::IdentityMap.set(other_key, other_model)
|
90
|
+
end
|
91
|
+
|
57
92
|
it "should clear identity map" do
|
58
|
-
MongoidIdentityMap::ThreadLocalHash.should_receive(:clear)
|
59
93
|
MongoidIdentityMap::IdentityMap.clear
|
94
|
+
MongoidIdentityMap::IdentityMap.get(key).should be_nil
|
95
|
+
MongoidIdentityMap::IdentityMap.get(other_key).should be_nil
|
60
96
|
end
|
61
97
|
end
|
62
98
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Guilherme Cirne
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-05-
|
17
|
+
date: 2011-05-08 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -112,14 +112,12 @@ files:
|
|
112
112
|
- lib/mongoid_identity_map/identity_map.rb
|
113
113
|
- lib/mongoid_identity_map/persistence/identity_mappable.rb
|
114
114
|
- lib/mongoid_identity_map/railtie.rb
|
115
|
-
- lib/mongoid_identity_map/thread_local_hash.rb
|
116
115
|
- lib/mongoid_identity_map/version.rb
|
117
116
|
- mongoid_identity_map.gemspec
|
118
117
|
- spec/mongoid_identity_map/clear_middleware_spec.rb
|
119
118
|
- spec/mongoid_identity_map/contexts/identity_mappable_spec.rb
|
120
119
|
- spec/mongoid_identity_map/identity_map_spec.rb
|
121
120
|
- spec/mongoid_identity_map/persistence/identity_mappable_spec.rb
|
122
|
-
- spec/mongoid_identity_map/thread_local_hash_spec.rb
|
123
121
|
- spec/spec_helper.rb
|
124
122
|
- spec/support/model.rb
|
125
123
|
has_rdoc: true
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module MongoidIdentityMap
|
2
|
-
class ThreadLocalHash
|
3
|
-
class << self
|
4
|
-
def get(key)
|
5
|
-
thread_local_hash[key]
|
6
|
-
end
|
7
|
-
|
8
|
-
def set(key, value)
|
9
|
-
thread_local_hash[key] = value
|
10
|
-
end
|
11
|
-
|
12
|
-
def remove(value)
|
13
|
-
thread_local_hash.delete_if {|k, v| v == value}
|
14
|
-
end
|
15
|
-
|
16
|
-
def clear
|
17
|
-
Thread.current[:mongoid_identity_map_current_thread_hash] = nil
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def thread_local_hash
|
23
|
-
Thread.current[:mongoid_identity_map_current_thread_hash] ||= Hash.new
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe MongoidIdentityMap::ThreadLocalHash do
|
4
|
-
describe "storing key value pairs" do
|
5
|
-
it "should store key value pairs" do
|
6
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
|
7
|
-
|
8
|
-
Thread.new do
|
9
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value2")
|
10
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value2"
|
11
|
-
end.join
|
12
|
-
|
13
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe ".remove" do
|
18
|
-
it "should remove value" do
|
19
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
|
20
|
-
|
21
|
-
Thread.new do
|
22
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
|
23
|
-
MongoidIdentityMap::ThreadLocalHash.remove("value")
|
24
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should be_nil
|
25
|
-
end.join
|
26
|
-
|
27
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe ".clear" do
|
32
|
-
it "should clear" do
|
33
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
|
34
|
-
|
35
|
-
Thread.new do
|
36
|
-
MongoidIdentityMap::ThreadLocalHash.set(:key, "value")
|
37
|
-
MongoidIdentityMap::ThreadLocalHash.clear
|
38
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should be_nil
|
39
|
-
end.join
|
40
|
-
|
41
|
-
MongoidIdentityMap::ThreadLocalHash.get(:key).should == "value"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|