Class: Scalaroid::Transaction

Inherits:
Object
  • Object
show all
Includes:
InternalScalarisNopClose
Defined in:
lib/scalaroid/transaction.rb

Overview

Write or read operations on Scalaris inside a transaction.

Instance Method Summary collapse

Methods included from InternalScalarisNopClose

#close_connection, #nop

Constructor Details

#initialize(conn = JSONConnection.new()) ⇒ Transaction

Create a new object using the given connection



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

def initialize(conn = JSONConnection.new())
  @conn = conn
  @tlog = nil
end

Instance Method Details

#abortObject

Aborts all previously created operations inside the transaction.



102
103
104
# File 'lib/scalaroid/transaction.rb', line 102

def abort
  @tlog = nil
end

#add_del_on_list(key, to_add, to_remove) ⇒ Object

Issues a add_del_on_list operation to scalaris and adds it to the current transaction. Changes the list stored at the given key, i.e. first adds all items in to_add then removes all items in to_remove. Both, to_add and to_remove, must be lists. Assumes en empty list if no value exists at key.



128
129
130
131
# File 'lib/scalaroid/transaction.rb', line 128

def add_del_on_list(key, to_add, to_remove)
  result = req_list(new_req_list().add_add_del_on_list(key, to_add, to_remove))[0]
  process_result_add_del_on_list(result)
end

#add_on_nr(key, to_add) ⇒ Object

Issues a add_on_nr operation to scalaris and adds it to the current transaction. Changes the number stored at the given key, i.e. adds some value. Assumes 0 if no value exists at key.



137
138
139
140
# File 'lib/scalaroid/transaction.rb', line 137

def add_on_nr(key, to_add)
  result = req_list(new_req_list().add_add_on_nr(key, to_add))[0]
  process_result_add_on_nr(result)
end

#commitObject

Issues a commit operation to Scalaris validating the previously created operations inside the transaction.



94
95
96
97
98
99
# File 'lib/scalaroid/transaction.rb', line 94

def commit
  result = req_list(new_req_list().add_commit())[0]
  _process_result_commit(result)
  # reset tlog (minor optimization which is not done in req_list):
  @tlog = nil
end

#new_req_list(other = nil) ⇒ Object

Returns a new ReqList object allowing multiple parallel requests.



11
12
13
# File 'lib/scalaroid/transaction.rb', line 11

def new_req_list(other = nil)
  @conn.class.new_req_list_t(other)
end

#process_result_add_del_on_list(result) ⇒ Object

Processes a result element from the list returned by req_list() which originated from a add_del_on_list operation. Raises the appropriate exceptions if a failure occurred during the operation.



62
63
64
# File 'lib/scalaroid/transaction.rb', line 62

def process_result_add_del_on_list(result)
  @conn.class.process_result_add_del_on_list(result)
end

#process_result_add_on_nr(result) ⇒ Object

Processes a result element from the list returned by req_list() which originated from a add_on_nr operation. Raises the appropriate exceptions if a failure occurred during the operation.



70
71
72
# File 'lib/scalaroid/transaction.rb', line 70

def process_result_add_on_nr(result)
  @conn.class.process_result_add_on_nr(result)
end

#process_result_read(result) ⇒ Object

Processes a result element from the list returned by req_list() which originated from a read operation. Returns the read value on success. Raises the appropriate exceptions if a failure occurred during the operation. Beware: lists of (small) integers may be (falsely) returned as a string - use str_to_list() to convert such strings.



46
47
48
# File 'lib/scalaroid/transaction.rb', line 46

def process_result_read(result)
  @conn.class.process_result_read(result)
end

#process_result_test_and_set(result) ⇒ Object

Processes a result element from the list returned by req_list() which originated from a test_and_set operation. Raises the appropriate exceptions if a failure occurred during the operation.



78
79
80
# File 'lib/scalaroid/transaction.rb', line 78

def process_result_test_and_set(result)
  @conn.class.process_result_test_and_set(result)
end

#process_result_write(result) ⇒ Object

Processes a result element from the list returned by req_list() which originated from a write operation. Raises the appropriate exceptions if a failure occurred during the operation.



54
55
56
# File 'lib/scalaroid/transaction.rb', line 54

def process_result_write(result)
  @conn.class.process_result_write(result)
end

#read(key) ⇒ Object

Issues a read operation to Scalaris, adds it to the current transaction and returns the result. Beware: lists of (small) integers may be (falsely) returned as a string - use str_to_list() to convert such strings.



110
111
112
113
# File 'lib/scalaroid/transaction.rb', line 110

def read(key)
  result = req_list(new_req_list().add_read(key))[0]
  return process_result_read(result)
end

#req_list(reqlist) ⇒ Object

Issues multiple parallel requests to Scalaris. Request lists can be created using new_req_list(). The returned list has the following form: [‘ok’ or ‘ok’, ‘value’: xxx or ‘fail’, ‘reason’: ‘timeout’ or ‘abort’ or ‘not_found’]. The elements of this list can be processed with process_result_read(), process_result_write() and process_result_commit().



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scalaroid/transaction.rb', line 22

def req_list(reqlist)
  if @tlog == nil
    result = @conn.call(:req_list, [reqlist.get_requests()])
  else
    result = @conn.call(:req_list, [@tlog, reqlist.get_requests()])
  end
  result = @conn.class.process_result_req_list_t(result)
  @tlog = result[:tlog]
  result = result[:result]
  if reqlist.is_commit()
    _process_result_commit(result[-1])
    # transaction was successful: reset transaction log
    @tlog = nil
  end
  result
end

#test_and_set(key, old_value, new_value) ⇒ Object

Issues a test_and_set operation to scalaris and adds it to the current transaction. Atomic test and set, i.e. if the old value at key is old_value, then write new_value.



146
147
148
149
# File 'lib/scalaroid/transaction.rb', line 146

def test_and_set(key, old_value, new_value)
  result = req_list(new_req_list().add_test_and_set(key, old_value, new_value))[0]
  process_result_test_and_set(result)
end

#write(key, value, binary = false) ⇒ Object

Issues a write operation to Scalaris and adds it to the current transaction.



117
118
119
120
# File 'lib/scalaroid/transaction.rb', line 117

def write(key, value, binary = false)
  result = req_list(new_req_list().add_write(key, value, binary))[0]
  _process_result_commit(result)
end