Class: SlateDb::Transaction

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

Instance Method Summary collapse

Instance Method Details

#commit(await_durable: nil, seqnum: nil) ⇒ void

This method returns an undefined value.

Commit the transaction.

Examples:

Commit a transaction

txn = db.begin_transaction
txn.put("key", "value")
txn.commit

Commit with an explicit sequence number

txn.commit(seqnum: 99)

Parameters:

  • await_durable (Boolean, nil) (defaults to: nil)

    Whether to wait for durability (default: true)

  • seqnum (Integer, nil) (defaults to: nil)

    User-supplied sequence number for the commit. When provided (and non-zero), it is used instead of the internally generated sequence number and must be strictly greater than the current maximum sequence number. (Requires SlateDB >= 0.13.0)



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/slatedb/transaction.rb', line 162

def commit(await_durable: nil, seqnum: nil)
  opts = {}
  opts[:await_durable] = await_durable unless await_durable.nil?
  opts[:seqnum] = seqnum if seqnum

  if opts.empty?
    _commit
  else
    _commit_with_options(opts)
  end
end

#delete(key) ⇒ void

This method returns an undefined value.

Delete a key within the transaction.

Parameters:

  • key (String)

    The key to delete



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

def delete(key)
  _delete(key)
end

#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?

Get a value by key within the transaction.

Parameters:

  • key (String)

    The key to look up

  • durability_filter (String, nil) (defaults to: nil)

    Filter by durability level

  • dirty (Boolean, nil) (defaults to: nil)

    Whether to include uncommitted data

  • cache_blocks (Boolean, nil) (defaults to: nil)

    Whether to cache blocks

Returns:

  • (String, nil)

    The value, or nil if not found



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/slatedb/transaction.rb', line 13

def get(key, durability_filter: nil, dirty: nil, cache_blocks: nil)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?

  if opts.empty?
    _get(key)
  else
    _get_with_options(key, opts)
  end
end

#mark_read(keys) ⇒ void

This method returns an undefined value.

Mark keys as read for conflict detection.

This explicitly tracks reads for conflict checking in serializable isolation, allowing selective read-write conflict detection even when keys weren’t actually read via get().

Examples:

Mark keys for conflict detection

db.transaction(isolation: :serializable) do |txn|
  txn.mark_read(["key1", "key2"])
  # These keys will now be checked for conflicts on commit
  txn.put("key3", "value")
end

Parameters:

  • keys (Array<String>)

    The keys to mark as read



141
142
143
# File 'lib/slatedb/transaction.rb', line 141

def mark_read(keys)
  _mark_read(Array(keys))
end

#merge(key, value, ttl: nil) ⇒ void

This method returns an undefined value.

Merge a value within the transaction.

Parameters:

  • key (String)

    The key to merge into

  • value (String)

    The merge operand to apply

  • ttl (Integer, nil) (defaults to: nil)

    Time-to-live in milliseconds



57
58
59
60
61
62
63
# File 'lib/slatedb/transaction.rb', line 57

def merge(key, value, ttl: nil)
  if ttl
    _merge_with_options(key, value, { ttl: ttl })
  else
    _merge(key, value)
  end
end

#put(key, value, ttl: nil) ⇒ void

This method returns an undefined value.

Store a key-value pair within the transaction.

Parameters:

  • key (String)

    The key to store

  • value (String)

    The value to store

  • ttl (Integer, nil) (defaults to: nil)

    Time-to-live in milliseconds



33
34
35
36
37
38
39
# File 'lib/slatedb/transaction.rb', line 33

def put(key, value, ttl: nil)
  if ttl
    _put_with_options(key, value, { ttl: ttl })
  else
    _put(key, value)
  end
end

#scan(start_key, end_key = nil, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil) ⇒ Iterator

Scan a range of keys within the transaction.

Parameters:

  • start_key (String)

    The start key (inclusive)

  • end_key (String, nil) (defaults to: nil)

    The end key (exclusive)

Returns:

  • (Iterator)

    An iterator over key-value pairs



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/slatedb/transaction.rb', line 71

def scan(start_key, end_key = nil, durability_filter: nil, dirty: nil,
         read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, &)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:read_ahead_bytes] = read_ahead_bytes if read_ahead_bytes
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?
  opts[:max_fetch_tasks] = max_fetch_tasks if max_fetch_tasks

  iter = if opts.empty?
           _scan(start_key, end_key)
         else
           _scan_with_options(start_key, end_key, opts)
         end

  if block_given?
    iter.each(&)
  else
    iter
  end
end

#scan_prefix(prefix, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil) ⇒ Iterator

Scan all keys with a given prefix within the transaction.

Parameters:

  • prefix (String)

    The key prefix to scan

  • durability_filter (String, nil) (defaults to: nil)

    Filter by durability level

  • dirty (Boolean, nil) (defaults to: nil)

    Whether to include uncommitted data

  • read_ahead_bytes (Integer, nil) (defaults to: nil)

    Number of bytes to read ahead

  • cache_blocks (Boolean, nil) (defaults to: nil)

    Whether to cache blocks

  • max_fetch_tasks (Integer, nil) (defaults to: nil)

    Maximum number of fetch tasks

Returns:

  • (Iterator)

    An iterator over key-value pairs



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/slatedb/transaction.rb', line 103

def scan_prefix(prefix, durability_filter: nil, dirty: nil,
                read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, &)
  opts = {}
  opts[:durability_filter] = durability_filter.to_s if durability_filter
  opts[:dirty] = dirty unless dirty.nil?
  opts[:read_ahead_bytes] = read_ahead_bytes if read_ahead_bytes
  opts[:cache_blocks] = cache_blocks unless cache_blocks.nil?
  opts[:max_fetch_tasks] = max_fetch_tasks if max_fetch_tasks

  iter = if opts.empty?
           _scan_prefix(prefix)
         else
           _scan_prefix_with_options(prefix, opts)
         end

  if block_given?
    iter.each(&)
  else
    iter
  end
end