synapse-mongo 0.1.1 → 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.
- data/lib/synapse/event_store/mongo/cursor_event_stream.rb +2 -2
- data/lib/synapse/event_store/mongo/event_store.rb +1 -1
- data/lib/synapse/event_store/mongo/per_commit_strategy.rb +1 -1
- data/lib/synapse/event_store/mongo/per_event_strategy.rb +1 -1
- data/lib/synapse/event_store/mongo/storage_strategy.rb +1 -1
- data/lib/synapse/event_store/mongo/template.rb +2 -2
- data/lib/synapse/mongo/version.rb +1 -1
- data/lib/synapse/process_manager/mongo/process_document.rb +61 -0
- data/lib/synapse/process_manager/mongo/process_repository.rb +90 -0
- data/lib/synapse/process_manager/mongo/template.rb +23 -0
- data/lib/synapse/process_manager/mongo.rb +3 -0
- data/lib/synapse-mongo.rb +6 -1
- data/test/event_store/mongo/event_store_test.rb +5 -2
- data/test/process_manager/mongo/repository_test.rb +71 -0
- metadata +9 -4
@@ -0,0 +1,61 @@
|
|
1
|
+
module Synapse
|
2
|
+
module ProcessManager
|
3
|
+
module Mongo
|
4
|
+
# Mongo document that represents a process instance
|
5
|
+
class ProcessDocument
|
6
|
+
# @param [Process] process
|
7
|
+
# @param [Serializer] serializer
|
8
|
+
# @return [ProcessDocument]
|
9
|
+
def from_process(process, serializer)
|
10
|
+
serialized_process = serializer.serialize process, String
|
11
|
+
|
12
|
+
@id = process.id
|
13
|
+
@serialized = serialized_process.content
|
14
|
+
@type = serialized_process.type.name
|
15
|
+
@correlations = Array.new
|
16
|
+
|
17
|
+
process.correlations.each do |correlation|
|
18
|
+
correlation_hash = {
|
19
|
+
key: correlation.key,
|
20
|
+
value: correlation.value
|
21
|
+
}
|
22
|
+
|
23
|
+
@correlations.push correlation_hash
|
24
|
+
end
|
25
|
+
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [Hash] hash
|
30
|
+
# @return [ProcessDocument]
|
31
|
+
def from_hash(hash)
|
32
|
+
hash.symbolize_keys!
|
33
|
+
|
34
|
+
@id = hash.fetch :_id
|
35
|
+
@serialized = hash.fetch :serialized
|
36
|
+
@type = hash.fetch :type
|
37
|
+
@correlations = hash.fetch :correlations
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
# @return [Hash]
|
43
|
+
def to_hash
|
44
|
+
{ _id: @id,
|
45
|
+
serialized: @serialized,
|
46
|
+
type: @type,
|
47
|
+
correlations: @correlations }
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param [Serializer] serializer
|
51
|
+
# @return [Process]
|
52
|
+
def to_process(serializer)
|
53
|
+
serialized_type = Serialization::SerializedType.new @type, nil
|
54
|
+
serialized_object = Serialization::SerializedObject.new @serialized, String, serialized_type
|
55
|
+
|
56
|
+
serializer.deserialize serialized_object
|
57
|
+
end
|
58
|
+
end # ProcessDocument
|
59
|
+
end # Mongo
|
60
|
+
end # ProcessManager
|
61
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Synapse
|
2
|
+
module ProcessManager
|
3
|
+
module Mongo
|
4
|
+
# Implementation of a process repository that serializes process instances to an
|
5
|
+
# underlying MongoDB collection
|
6
|
+
class MongoProcessRepository < ProcessRepository
|
7
|
+
# @return [ResourceInjector]
|
8
|
+
attr_accessor :resource_injector
|
9
|
+
|
10
|
+
# @param [Serializer] serializer
|
11
|
+
# @param [Template] template
|
12
|
+
# @return [undefined]
|
13
|
+
def initialize(serializer, template)
|
14
|
+
@resource_injector = ResourceInjector.new
|
15
|
+
@serializer = serializer
|
16
|
+
@template = template
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [Class] type
|
20
|
+
# @param [Correlation] correlation
|
21
|
+
# @return [Set]
|
22
|
+
def find(type, correlation)
|
23
|
+
process_type = @serializer.type_for(type).name
|
24
|
+
|
25
|
+
query = {
|
26
|
+
type: process_type,
|
27
|
+
correlations: {
|
28
|
+
key: correlation.key,
|
29
|
+
value: correlation.value
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
identifiers = Set.new
|
34
|
+
identifiers.tap do
|
35
|
+
cursor = @template.process_collection.find query
|
36
|
+
cursor.each do |process|
|
37
|
+
identifiers.add process.fetch '_id'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param [String] id
|
43
|
+
# @return [Process] Returns nil if process could not be found
|
44
|
+
def load(id)
|
45
|
+
hash = @template.process_collection.find_one _id: id
|
46
|
+
|
47
|
+
if hash
|
48
|
+
document = ProcessDocument.new
|
49
|
+
document.from_hash(hash).to_process(@serializer).tap do |loaded_process|
|
50
|
+
@resource_injector.inject_resources(loaded_process)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param [Process] process
|
56
|
+
# @return [undefined]
|
57
|
+
def commit(process)
|
58
|
+
if process.active?
|
59
|
+
@template.process_collection.save to_hash process
|
60
|
+
else
|
61
|
+
@template.process_collection.remove _id: process.id
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# @param [Process] process
|
66
|
+
# @return [undefined]
|
67
|
+
def add(process)
|
68
|
+
if process.active?
|
69
|
+
@template.process_collection.insert to_hash process
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
# Marks a process's correlations as committed and converts it to a hash suitable for
|
76
|
+
# insertion or update in a Mongo collection
|
77
|
+
#
|
78
|
+
# @param [Process] process
|
79
|
+
# @return [Hash]
|
80
|
+
def to_hash(process)
|
81
|
+
process.correlations.commit
|
82
|
+
|
83
|
+
document = ProcessDocument.new
|
84
|
+
document.from_process process, @serializer
|
85
|
+
document.to_hash
|
86
|
+
end
|
87
|
+
end # MongoProcessRepository
|
88
|
+
end # Mongo
|
89
|
+
end # ProcessManager
|
90
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Synapse
|
2
|
+
module ProcessManager
|
3
|
+
module Mongo
|
4
|
+
# Template for accessing collections needed by the process repository
|
5
|
+
class Template < Common::Mongo::BaseTemplate
|
6
|
+
# @return [String] Name of the collection containing processes
|
7
|
+
attr_accessor :process_repository_name
|
8
|
+
|
9
|
+
# @param [Mongo::MongoClient] client
|
10
|
+
# @return [undefined]
|
11
|
+
def initialize(client)
|
12
|
+
super
|
13
|
+
@process_repository_name = 'processes'
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Mongo::Collection]
|
17
|
+
def process_collection
|
18
|
+
database.collection @process_repository_name
|
19
|
+
end
|
20
|
+
end # Template
|
21
|
+
end # Mongo
|
22
|
+
end # ProcessManager
|
23
|
+
end
|
data/lib/synapse-mongo.rb
CHANGED
@@ -9,12 +9,17 @@ module Synapse
|
|
9
9
|
# Utility classes used by Mongo components
|
10
10
|
module Mongo
|
11
11
|
extend ActiveSupport::Autoload
|
12
|
-
|
13
12
|
autoload :BaseTemplate
|
14
13
|
end
|
15
14
|
end
|
16
15
|
|
17
16
|
module EventStore
|
17
|
+
extend ActiveSupport::Autoload
|
18
|
+
autoload :Mongo
|
19
|
+
end
|
20
|
+
|
21
|
+
module ProcessManager
|
22
|
+
extend ActiveSupport::Autoload
|
18
23
|
autoload :Mongo
|
19
24
|
end
|
20
25
|
|
@@ -8,9 +8,12 @@ module Synapse
|
|
8
8
|
def test_integration
|
9
9
|
client = ::Mongo::MongoClient.new
|
10
10
|
template = Template.new client
|
11
|
+
template.event_collection.drop
|
12
|
+
template.snapshot_collection.drop
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
converter_factory = Serialization::ConverterFactory.new
|
15
|
+
serializer = Serialization::MarshalSerializer.new converter_factory
|
16
|
+
upcaster_chain = Upcasting::UpcasterChain.new converter_factory
|
14
17
|
|
15
18
|
[DocumentPerCommitStrategy, DocumentPerEventStrategy].each do |type|
|
16
19
|
strategy = type.new template, serializer, upcaster_chain
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module ProcessManager
|
5
|
+
module Mongo
|
6
|
+
class MongoProcessRepositoryTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
client = ::Mongo::MongoClient.new
|
10
|
+
template = Template.new client
|
11
|
+
template.process_collection.drop
|
12
|
+
|
13
|
+
converter_factory = Serialization::ConverterFactory.new
|
14
|
+
serializer = Serialization::MarshalSerializer.new converter_factory
|
15
|
+
|
16
|
+
@repository = MongoProcessRepository.new serializer, template
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_add
|
20
|
+
process = Process.new
|
21
|
+
|
22
|
+
@repository.add process
|
23
|
+
loaded = @repository.load process.id
|
24
|
+
|
25
|
+
assert_equal process.id, loaded.id
|
26
|
+
assert_equal 0, process.correlations.additions.count
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_commit
|
30
|
+
correlation = Correlation.new :order_id, '123'
|
31
|
+
|
32
|
+
process = Process.new
|
33
|
+
@repository.add process
|
34
|
+
|
35
|
+
process.correlations.add correlation
|
36
|
+
@repository.commit process
|
37
|
+
|
38
|
+
loaded = @repository.load process.id
|
39
|
+
|
40
|
+
assert loaded.correlations.include? correlation
|
41
|
+
|
42
|
+
loaded.send :finish
|
43
|
+
@repository.commit loaded
|
44
|
+
|
45
|
+
assert_nil @repository.load process.id
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_find
|
49
|
+
correlation_a = Correlation.new :order_id, '123'
|
50
|
+
correlation_b = Correlation.new :order_id, '456'
|
51
|
+
|
52
|
+
process_a = Process.new
|
53
|
+
process_a.correlations.add correlation_a
|
54
|
+
@repository.add process_a
|
55
|
+
process_b = Process.new
|
56
|
+
process_b.correlations.add correlation_a
|
57
|
+
@repository.add process_b
|
58
|
+
process_c = Process.new
|
59
|
+
process_c.correlations.add correlation_b
|
60
|
+
@repository.add process_c
|
61
|
+
|
62
|
+
identifiers = @repository.find Process, correlation_a
|
63
|
+
assert_equal Set[process_a.id, process_b.id], identifiers
|
64
|
+
identifiers = @repository.find Process, correlation_b
|
65
|
+
assert_equal Set[process_c.id], identifiers
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: synapse-mongo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: synapse-core
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: 0.4.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: 0.4.0
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: mongo
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -51,6 +51,10 @@ extra_rdoc_files: []
|
|
51
51
|
files:
|
52
52
|
- lib/synapse-mongo.rb
|
53
53
|
- lib/synapse/mongo/version.rb
|
54
|
+
- lib/synapse/process_manager/mongo/process_repository.rb
|
55
|
+
- lib/synapse/process_manager/mongo/process_document.rb
|
56
|
+
- lib/synapse/process_manager/mongo/template.rb
|
57
|
+
- lib/synapse/process_manager/mongo.rb
|
54
58
|
- lib/synapse/event_store/mongo/cursor_event_stream.rb
|
55
59
|
- lib/synapse/event_store/mongo/event_store.rb
|
56
60
|
- lib/synapse/event_store/mongo/storage_strategy.rb
|
@@ -61,6 +65,7 @@ files:
|
|
61
65
|
- lib/synapse/common/mongo/base_template.rb
|
62
66
|
- lib/synapse/serialization/converter/bson.rb
|
63
67
|
- test/test_helper.rb
|
68
|
+
- test/process_manager/mongo/repository_test.rb
|
64
69
|
- test/event_store/mongo/event_store_test.rb
|
65
70
|
- test/common/mongo/base_template_test.rb
|
66
71
|
- test/serialization/converter/bson_test.rb
|