Skip to content

Commit

Permalink
initial global snapshot solution with store in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
hallgren committed Jun 8, 2018
1 parent 292cd63 commit babf221
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 42 deletions.
22 changes: 16 additions & 6 deletions lib/sandthorn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "sandthorn/errors"
require "sandthorn/aggregate_root"
require "sandthorn/event_stores"
require "sandthorn/snapshot_store"
require 'yaml'
require 'securerandom'

Expand All @@ -10,7 +11,8 @@ class << self
extend Forwardable

def_delegators :configuration, :event_stores
def_delegators :configuration, :snapshot
def_delegators :configuration, :snapshot?
def_delegators :configuration, :snapshot_store

def default_event_store
event_stores.default_store
Expand Down Expand Up @@ -40,12 +42,16 @@ def all aggregate_type
event_store_for(aggregate_type).all(aggregate_type)
end

def find aggregate_id, aggregate_type
event_store_for(aggregate_type).find(aggregate_id, aggregate_type)
def find aggregate_id, aggregate_type, after_aggregate_version = 0
event_store_for(aggregate_type).find(aggregate_id, aggregate_type, after_aggregate_version)
end

def save_snapshot(aggregate)
raise "Not Implemented"
def save_snapshot aggregate
snapshot_store.save aggregate.aggregate_id, aggregate
end

def find_snapshot aggregate_id
return snapshot_store.find aggregate_id
end

def find_event_store(name)
Expand Down Expand Up @@ -85,14 +91,18 @@ def map_types= data
@event_stores.map_types data
end

def snapshot
def snapshot?
@snapshot ||= false
end

def snapshot=(value)
@snapshot = value
end

def snapshot_store
@snapshot_store ||= SnapshotStore.new
end

alias_method :event_stores=, :event_store=
end
end
Expand Down
26 changes: 19 additions & 7 deletions lib/sandthorn/aggregate_root_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def save
@aggregate_originating_version = @aggregate_current_event_version
end

Sandthorn.save_snapshot self if Sandthorn.snapshot?

self
end

Expand Down Expand Up @@ -70,9 +72,17 @@ def event_store(event_store = nil)
end
end

def snapshot(value = nil)
if value
@snapshot = value
else
@snapshot
end
end

def all
Sandthorn.all(self).map { |events|
aggregate_build events
aggregate_build events, nil
}
end

Expand All @@ -83,13 +93,15 @@ def find id

def aggregate_find aggregate_id
begin
events = Sandthorn.find(aggregate_id, self)
unless events && !events.empty?
aggregate_from_snapshot = Sandthorn.find_snapshot(aggregate_id) if Sandthorn.snapshot?
current_aggregate_version = aggregate_from_snapshot.nil? ? 0 : aggregate_from_snapshot.aggregate_current_event_version
events = Sandthorn.find(aggregate_id, self, current_aggregate_version)
if aggregate_from_snapshot.nil? && events.empty?
raise Errors::AggregateNotFound
end

return aggregate_build events
rescue Exception
return aggregate_build events, aggregate_from_snapshot
rescue Exception => error
raise Errors::AggregateNotFound
end

Expand All @@ -111,8 +123,8 @@ def new *args, &block

end

def aggregate_build events
aggregate = create_new_empty_aggregate
def aggregate_build events, aggregate_from_snapshot = nil
aggregate = aggregate_from_snapshot || create_new_empty_aggregate

if events.any?
current_aggregate_version = events.last[:aggregate_version]
Expand Down
22 changes: 0 additions & 22 deletions lib/sandthorn/aggregate_root_snapshot.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/sandthorn/snapshot_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Sandthorn
class SnapshotStore
def initialize
@store = Hash.new
end

def save key, value
@store[key] = value
end

def find key
@store[key]
end
end
end
2 changes: 1 addition & 1 deletion sandthorn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "autotest-standalone"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "sandthorn_driver_sequel", ">= 4.0"
# spec.add_development_dependency "sandthorn_driver_sequel", ">= 4.0"
end
11 changes: 11 additions & 0 deletions spec/aggregate_root_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def no_state_change_only_empty_event
end
end

describe "::snapshot" do
let(:klass) { Class.new { include Sandthorn::AggregateRoot } }
it "is available as a class method" do
expect(klass).to respond_to(:snapshot)
end
it "sets the snapshot to true and returns it" do
klass.snapshot(true)
expect(klass.snapshot).to eq(true)
end
end

describe "when get all aggregates from DirtyClass" do

before(:each) do
Expand Down
8 changes: 2 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@ def class_including(mod)
def url
"sqlite://spec/db/sequel_driver.sqlite3"
end

def sqlite_store_setup

SandthornDriverSequel.migrate_db url: url

driver = SandthornDriverSequel.driver_from_url(url: url) do |conf|
conf.event_serializer = Proc.new { |data| YAML::dump(data) }
conf.event_deserializer = Proc.new { |data| YAML::load(data) }
end

Sandthorn.configure do |c|
c.event_store = driver
c.event_store = SandthornDriverSequel.driver_from_url(url: url)
c.snapshot = true
end

Expand Down

0 comments on commit babf221

Please sign in to comment.