Class: SlateDb::Database
- Inherits:
-
Object
- Object
- SlateDb::Database
- Defined in:
- lib/slatedb/database.rb
Class Method Summary collapse
-
.open(path, url: nil) {|db| ... } ⇒ Database
Open a database at the given path.
Instance Method Summary collapse
-
#batch(await_durable: nil) {|batch| ... } ⇒ void
Create and write a batch using a block.
-
#begin_transaction(isolation: nil) {|txn| ... } ⇒ Transaction, Object
Begin a new transaction.
-
#delete(key, await_durable: nil) ⇒ void
Delete a key.
-
#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?
Get a value by key.
-
#put(key, value, ttl: nil, await_durable: 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) ⇒ Iterator
Scan a range of keys.
-
#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) ⇒ void
Write a batch of operations atomically.
Class Method Details
.open(path, url: nil) {|db| ... } ⇒ Database
Open a database at the given path.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/slatedb/database.rb', line 28 def open(path, url: nil) db = _open(path, url) 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) {|batch| ... } ⇒ void
This method returns an undefined value.
Create and write a batch using a block.
211 212 213 214 215 |
# File 'lib/slatedb/database.rb', line 211 def batch(await_durable: nil) b = WriteBatch.new yield b write(b, await_durable: await_durable) end |
#begin_transaction(isolation: nil) {|txn| ... } ⇒ Transaction, Object
Begin a new transaction.
240 241 242 243 |
# File 'lib/slatedb/database.rb', line 240 def begin_transaction(isolation: nil) isolation_str = isolation&.to_s _begin_transaction(isolation_str) end |
#delete(key, await_durable: nil) ⇒ void
This method returns an undefined value.
Delete a key.
115 116 117 118 119 120 121 |
# File 'lib/slatedb/database.rb', line 115 def delete(key, await_durable: nil) if await_durable.nil? _delete(key) else (key, { await_durable: await_durable }) end end |
#get(key, durability_filter: nil, dirty: nil, cache_blocks: nil) ⇒ String?
Get a value by key.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/slatedb/database.rb', line 61 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 |
#put(key, value, ttl: nil, await_durable: nil) ⇒ void
This method returns an undefined value.
Store a key-value pair.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/slatedb/database.rb', line 91 def put(key, value, ttl: nil, await_durable: nil) opts = {} opts[:ttl] = ttl if ttl opts[:await_durable] = await_durable unless await_durable.nil? 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) ⇒ Iterator
Scan a range of keys.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/slatedb/database.rb', line 149 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 (start_key, end_key, opts) end if block_given? iter.each(&) else iter end end |
#snapshot {|snapshot| ... } ⇒ Snapshot, Object
Create a snapshot for consistent reads.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/slatedb/database.rb', line 294 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/slatedb/database.rb', line 262 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) ⇒ void
This method returns an undefined value.
Write a batch of operations atomically.
190 191 192 193 194 195 196 |
# File 'lib/slatedb/database.rb', line 190 def write(batch, await_durable: nil) if await_durable.nil? _write(batch) else (batch, { await_durable: await_durable }) end end |