class Scalaroid::Transaction

Write or read operations on Scalaris inside a transaction.

Public Class Methods

new(conn = JSONConnection.new()) click to toggle source

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

abort() click to toggle source

Aborts all previously created operations inside the transaction.

# File lib/scalaroid/transaction.rb, line 102
def abort
  @tlog = nil
end
add_del_on_list(key, to_add, to_remove) click to toggle source

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
add_on_nr(key, to_add) click to toggle source

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
commit() click to toggle source

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
new_req_list(other = nil) click to toggle source

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
process_result_add_del_on_list(result) click to toggle source

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
process_result_add_on_nr(result) click to toggle source

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
process_result_read(result) click to toggle source

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
process_result_test_and_set(result) click to toggle source

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
process_result_write(result) click to toggle source

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
read(key) click to toggle source

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
req_list(reqlist) click to toggle source

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
test_and_set(key, old_value, new_value) click to toggle source

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
write(key, value, binary = false) click to toggle source

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

_process_result_commit(result) click to toggle source

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