Class: Imperium::Transaction

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

Overview

A Transaction support for KV API

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Initaialize a new transaction containing an array of operations



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

def initialize
  @operations = []
end

Instance Method Details

#add_operation(verb, key, value: nil, flags: nil, index: nil, session_id: nil) ⇒ Object

Returns list of operations to perform inside the atomic transaction.

Parameters:

  • verb (string)

    Specifies the type of operation to perform

  • key (string)

    Specifies the full path of the entry

  • value (string) (defaults to: nil)

    Specifies a base64-encoded blob of data. Values cannot be larger than 512kB.

  • flags (int) (defaults to: nil)

    Specifies an opaque unsigned integer that can be attached to each entry. Clients can choose to use this however makes sense for their application.

  • index (int) (defaults to: nil)

    Specifies an index.

  • session (string)

    Specifies a session.

Returns:

  • list of operations to perform inside the atomic transaction



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/imperium/transaction.rb', line 36

def add_operation(verb, key, value: nil, flags: nil, index: nil, session_id: nil)
  kv = {
    'Verb' => verb,
    'Key' => key
  }
  kv['Value'] = Base64.encode64(value) if value
  kv['Flags'] = flags if flags
  kv['Index'] = index if index
  kv['Session'] = session_id if session_id
  @operations << { 'KV' => kv }
end

#bodyObject

Get JSON object for operations

Returns:

  • JSON KV object



19
20
21
# File 'lib/imperium/transaction.rb', line 19

def body
  @operations.to_json
end

#set(key, value, flags: nil) ⇒ Object

Set or Put a key value pair

See Also:



12
13
14
# File 'lib/imperium/transaction.rb', line 12

def set(key, value, flags: nil)
  add_operation('set', key, value: value, flags: flags)
end