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) ⇒ 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.
370 371 372 373 374 |
# File 'lib/slatedb/database.rb', line 370 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.
399 400 401 402 |
# File 'lib/slatedb/database.rb', line 399 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.
484 485 486 487 488 489 |
# File 'lib/slatedb/database.rb', line 484 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.
342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/slatedb/database.rb', line 342 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.
494 495 496 |
# File 'lib/slatedb/database.rb', line 494 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) ⇒ Iterator
Scan all keys with a given prefix.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/slatedb/database.rb', line 256 def scan_prefix(prefix, 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_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.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/slatedb/database.rb', line 453 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.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/slatedb/database.rb', line 421 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.
315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/slatedb/database.rb', line 315 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 |