Class: Scalaroid::TransactionSingleOp

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

Overview

Single write or read operations on Scalaris.

Instance Method Summary collapse

Methods included from InternalScalarisNopClose

#close_connection, #nop

Constructor Details

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

Create a new object using the given connection



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

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

Instance Method Details

#add_del_on_list(key, to_add, to_remove) ⇒ Object

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.



96
97
98
99
100
# File 'lib/scalaroid/transaction_single_op.rb', line 96

def add_del_on_list(key, to_add, to_remove)
  result = @conn.call(:add_del_on_list, [key, to_add, to_remove])
  @conn.class.check_fail_abort(result)
  @conn.class.process_result_add_del_on_list(result)
end

#add_on_nr(key, to_add) ⇒ Object

Changes the number stored at the given key, i.e. adds some value. Assumes 0 if no value exists at key.



104
105
106
107
108
# File 'lib/scalaroid/transaction_single_op.rb', line 104

def add_on_nr(key, to_add)
  result = @conn.call(:add_on_nr, [key, to_add])
  @conn.class.check_fail_abort(result)
  @conn.class.process_result_add_on_nr(result)
end

#new_req_list(other = nil) ⇒ Object

Returns a new ReqList object allowing multiple parallel requests.



10
11
12
# File 'lib/scalaroid/transaction_single_op.rb', line 10

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.



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

def process_result_add_del_on_list(result)
  @conn.class.check_fail_abort(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.



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

def process_result_add_on_nr(result)
  @conn.class.check_fail_abort(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.



35
36
37
# File 'lib/scalaroid/transaction_single_op.rb', line 35

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.



71
72
73
74
# File 'lib/scalaroid/transaction_single_op.rb', line 71

def process_result_test_and_set(result)
  @conn.class.check_fail_abort(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.



43
44
45
46
47
# File 'lib/scalaroid/transaction_single_op.rb', line 43

def process_result_write(result)
  # note: we need to process a commit result as the write has been committed
  @conn.class.check_fail_abort(result)
  @conn.class.process_result_commit(result)
end

#read(key) ⇒ Object

Read the value at key. Beware: lists of (small) integers may be (falsely) returned as a string - use str_to_list() to convert such strings.



79
80
81
82
# File 'lib/scalaroid/transaction_single_op.rb', line 79

def read(key)
  result = @conn.call(:read, [key])
  @conn.class.process_result_read(result)
end

#req_list(reqlist) ⇒ Object

Issues multiple parallel requests to scalaris; each will be committed. NOTE: The execution order of multiple requests on the same key is undefined! 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’]. Elements of this list can be processed with process_result_read() and process_result_write().



23
24
25
26
# File 'lib/scalaroid/transaction_single_op.rb', line 23

def req_list(reqlist)
  result = @conn.call(:req_list_commit_each, [reqlist.get_requests()])
  @conn.class.process_result_req_list_tso(result)
end

#test_and_set(key, oldvalue, newvalue) ⇒ Object

Atomic test and set, i.e. if the old value at key is oldvalue, then write newvalue.



112
113
114
115
116
# File 'lib/scalaroid/transaction_single_op.rb', line 112

def test_and_set(key, old_value, new_value)
  result = @conn.call(:test_and_set, [key, old_value, new_value])
  @conn.class.check_fail_abort(result)
  @conn.class.process_result_test_and_set(result)
end

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

Write the value to key.



85
86
87
88
89
90
# File 'lib/scalaroid/transaction_single_op.rb', line 85

def write(key, value, binary = false)
  value = @conn.class.encode_value(value, binary)
  result = @conn.call(:write, [key, value])
  @conn.class.check_fail_abort(result)
  @conn.class.process_result_commit(result)
end