class Scalaroid::Transaction
Write or read operations on Scalaris inside a transaction.
Public Class Methods
Create a new object using the given connection
# File lib/scalaroid/transaction.rb, line 5 def initialize(conn = JSONConnection.new()) @conn = conn @tlog = nil end
Public Instance Methods
Aborts all previously created operations inside the transaction.
# File lib/scalaroid/transaction.rb, line 102 def abort @tlog = nil end
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.
# 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
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.
# 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
Issues a commit operation to Scalaris validating the previously created operations inside the transaction.
# 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
Returns a new ReqList object allowing multiple parallel requests.
# File lib/scalaroid/transaction.rb, line 11 def new_req_list(other = nil) @conn.class.new_req_list_t(other) end
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.
# 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
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.
# File lib/scalaroid/transaction.rb, line 70 def process_result_add_on_nr(result) @conn.class.process_result_add_on_nr(result) end
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.
# File lib/scalaroid/transaction.rb, line 46 def process_result_read(result) @conn.class.process_result_read(result) end
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.
# File lib/scalaroid/transaction.rb, line 78 def process_result_test_and_set(result) @conn.class.process_result_test_and_set(result) end
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.
# File lib/scalaroid/transaction.rb, line 54 def process_result_write(result) @conn.class.process_result_write(result) end
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.
# 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
Issues multiple parallel requests to Scalaris. Request lists can be created using new_req_list
(). The returned list has the following form: [{‘status’: ‘ok’} or {‘status’: ‘ok’, ‘value’: xxx} or {‘status’: ‘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().
# 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
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.
# 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
Issues a write operation to Scalaris and adds it to the current transaction.
# 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
Private Instance Methods
Processes a result element from the list returned by req_list
() which originated from a commit operation. Raises the appropriate exceptions if a failure occurred during the operation.
# File lib/scalaroid/transaction.rb, line 86 def _process_result_commit(result) @conn.class.process_result_commit(result) end