Class: StrokeDB::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/strokedb/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Transaction

Returns a new instance of Transaction.



7
8
9
10
11
# File 'lib/strokedb/transaction.rb', line 7

def initialize(options = {})
  @options = options.stringify_keys
  @uuid = Util.random_uuid
  storage.add_chained_storage!(store.storage)
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid.



5
6
7
# File 'lib/strokedb/transaction.rb', line 5

def uuid
  @uuid
end

Instance Method Details

#commit!Object



58
59
60
61
# File 'lib/strokedb/transaction.rb', line 58

def commit!
  storage.sync_chained_storage!(store.storage)
  true
end

#executeObject

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/strokedb/transaction.rb', line 38

def execute
  raise ArgumentError, "no block provided" unless block_given?

  Thread.current[:strokedb_transactions] ||= []
  Thread.current[:strokedb_transactions].push self
  
  @timestamp = LTS.new(store.timestamp.counter,uuid)

  begin
    result = yield(self)
  rescue 
    throw $!
  ensure
    Thread.current[:strokedb_transactions].pop
  end
  
  
  result
end

#find(uuid, version = nil, opts = {}) ⇒ Object



25
26
27
# File 'lib/strokedb/transaction.rb', line 25

def find(uuid, version=nil, opts = {})
  storage.find(uuid,version,opts.merge(:store => self))
end

#head_version(uuid) ⇒ Object



29
30
31
# File 'lib/strokedb/transaction.rb', line 29

def head_version(uuid)
  storage.head_version(uuid,{ :store => self })
end

#inspectObject



21
22
23
# File 'lib/strokedb/transaction.rb', line 21

def inspect
  "#<Transaction #{@uuid}>"
end

#rollback!Object



63
64
65
66
# File 'lib/strokedb/transaction.rb', line 63

def rollback!
  @options['storage'] = setup_storage
  true
end

#save!(doc) ⇒ Object



33
34
35
36
# File 'lib/strokedb/transaction.rb', line 33

def save!(doc)
  @timestamp = @timestamp.next
  storage.save!(doc,@timestamp)
end

#storageObject



13
14
15
# File 'lib/strokedb/transaction.rb', line 13

def storage
  @options['storage'] ||= setup_storage
end

#storeObject



17
18
19
# File 'lib/strokedb/transaction.rb', line 17

def store
  @options['store']
end