Class: SlateDb::Database
- Inherits:
-
Object
- Object
- SlateDb::Database
- Defined in:
- lib/slatedb/database.rb
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary collapse
-
.open(path, url: nil, merge_operator: nil) {|db| ... } ⇒ Database
Open a database at the given path.
Instance Method Summary collapse
-
#batch(await_durable: nil, seqnum: nil) {|batch| ... } ⇒ void
Create and write a batch using a block.
-
#begin_transaction(isolation: nil) {|txn| ... } ⇒ Transaction, Object
Begin a new transaction.
-
#create_checkpoint(lifetime: nil, name: nil) ⇒ Hash
Create a checkpoint of the database.
-
#delete(key, await_durable: nil, seqnum: nil) ⇒ void
Delete a key.
-
#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?
Get a value by key.
-
#get_key_value(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ Hash?
(also: #get_entry)
Get a key-value pair with SlateDB metadata.
-
#merge(key, value, ttl: nil, await_durable: nil, seqnum: nil) ⇒ void
Merge a value into the database.
-
#metrics ⇒ Metrics
Get database metrics registry.
-
#put(key, value, ttl: nil, await_durable: nil, seqnum: nil) ⇒ void
Store a key-value pair.
-
#scan(start_key, end_key = nil, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, order: nil) ⇒ Iterator
Scan a range of keys.
-
#scan_prefix(prefix, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, order: nil, from: nil, to: nil) ⇒ Iterator
Scan all keys with a given prefix.
-
#snapshot {|snapshot| ... } ⇒ Snapshot, Object
Create a snapshot for consistent reads.
-
#transaction(isolation: nil) {|txn| ... } ⇒ Object
Execute a block within a transaction.
-
#write(batch, await_durable: nil, seqnum: nil) ⇒ void
Write a batch of operations atomically.
Class Method Details
.open(path, url: nil, merge_operator: nil) {|db| ... } ⇒ Database
Open a database at the given path.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/slatedb/database.rb', line 41 def open(path, url: nil, merge_operator: nil) opts = {} case merge_operator when Symbol, String opts[:merge_operator] = merge_operator.to_s when Proc # Store the proc to prevent GC and pass to Rust @_merge_operator_proc = merge_operator opts[:merge_operator_proc] = merge_operator end db = _open(path, url, opts) if block_given? begin yield db ensure begin db.close rescue StandardError nil end end else db end end |
Instance Method Details
#batch(await_durable: nil, seqnum: nil) {|batch| ... } ⇒ void
This method returns an undefined value.
Create and write a batch using a block.
382 383 384 385 386 |
# File 'lib/slatedb/database.rb', line 382 def batch(await_durable: nil, seqnum: nil) b = WriteBatch.new yield b write(b, await_durable: await_durable, seqnum: seqnum) end |
#begin_transaction(isolation: nil) {|txn| ... } ⇒ Transaction, Object
Begin a new transaction.
411 412 413 414 |
# File 'lib/slatedb/database.rb', line 411 def begin_transaction(isolation: nil) isolation_str = isolation&.to_s _begin_transaction(isolation_str) end |
#create_checkpoint(lifetime: nil, name: nil) ⇒ Hash
Create a checkpoint of the database.
496 497 498 499 500 501 |
# File 'lib/slatedb/database.rb', line 496 def create_checkpoint(lifetime: nil, name: nil) opts = {} opts[:lifetime] = lifetime if lifetime opts[:name] = name if name _create_checkpoint(opts) end |
#delete(key, await_durable: nil, seqnum: nil) ⇒ void
This method returns an undefined value.
Delete a key.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/slatedb/database.rb', line 177 def delete(key, await_durable: nil, seqnum: nil) opts = {} opts[:await_durable] = await_durable unless await_durable.nil? opts[:seqnum] = seqnum if seqnum if opts.empty? _delete(key) else (key, opts) end end |
#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?
Get a value by key.
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/slatedb/database.rb', line 85 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 (key, opts) end end |
#get_key_value(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ Hash? Also known as: get_entry
Get a key-value pair with SlateDB metadata.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/slatedb/database.rb', line 111 def get_key_value(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_value(key) else (key, opts) end end |
#merge(key, value, ttl: nil, await_durable: nil, seqnum: nil) ⇒ void
This method returns an undefined value.
Merge a value into the database.
354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/slatedb/database.rb', line 354 def merge(key, value, ttl: nil, await_durable: nil, seqnum: nil) opts = {} opts[:ttl] = ttl if ttl opts[:await_durable] = await_durable unless await_durable.nil? opts[:seqnum] = seqnum if seqnum if opts.empty? _merge(key, value) else (key, value, opts) end end |
#metrics ⇒ Metrics
Get database metrics registry.
506 507 508 |
# File 'lib/slatedb/database.rb', line 506 def metrics _metrics end |
#put(key, value, ttl: nil, await_durable: nil, seqnum: nil) ⇒ void
This method returns an undefined value.
Store a key-value pair.
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/slatedb/database.rb', line 150 def put(key, value, ttl: nil, await_durable: nil, seqnum: nil) opts = {} opts[:ttl] = ttl if ttl opts[:await_durable] = await_durable unless await_durable.nil? opts[:seqnum] = seqnum if seqnum if opts.empty? _put(key, value) else (key, value, opts) end end |
#scan(start_key, end_key = nil, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, order: nil) ⇒ Iterator
Scan a range of keys.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/slatedb/database.rb', line 216 def scan(start_key, end_key = nil, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, order: nil, &) opts = ( durability_filter: durability_filter, dirty: dirty, read_ahead_bytes: read_ahead_bytes, cache_blocks: cache_blocks, max_fetch_tasks: max_fetch_tasks, order: order ) iter = if opts.empty? _scan(start_key, end_key) else (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, order: nil, from: nil, to: nil) ⇒ Iterator
Scan all keys with a given prefix.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/slatedb/database.rb', line 265 def scan_prefix(prefix, durability_filter: nil, dirty: nil, read_ahead_bytes: nil, cache_blocks: nil, max_fetch_tasks: nil, order: nil, from: nil, to: nil, &) opts = ( durability_filter: durability_filter, dirty: dirty, read_ahead_bytes: read_ahead_bytes, cache_blocks: cache_blocks, max_fetch_tasks: max_fetch_tasks, order: order ) opts[:subrange_from] = from if from opts[:subrange_to] = to if to iter = if opts.empty? _scan_prefix(prefix) else (prefix, opts) end if block_given? iter.each(&) else iter end end |
#snapshot {|snapshot| ... } ⇒ Snapshot, Object
Create a snapshot for consistent reads.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/slatedb/database.rb', line 465 def snapshot snap = _snapshot if block_given? begin yield snap ensure begin snap.close rescue StandardError nil end end else snap end end |
#transaction(isolation: nil) {|txn| ... } ⇒ Object
Execute a block within a transaction.
The transaction is automatically committed if the block succeeds, or rolled back if an exception is raised.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/slatedb/database.rb', line 433 def transaction(isolation: nil) txn = begin_transaction(isolation: isolation) begin result = yield txn txn.commit result rescue StandardError begin txn.rollback rescue StandardError nil end raise end end |
#write(batch, await_durable: nil, seqnum: nil) ⇒ void
This method returns an undefined value.
Write a batch of operations atomically.
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/slatedb/database.rb', line 327 def write(batch, await_durable: nil, seqnum: nil) opts = {} opts[:await_durable] = await_durable unless await_durable.nil? opts[:seqnum] = seqnum if seqnum if opts.empty? _write(batch) else (batch, opts) end end |