struct NATS::KV::Bucket
- NATS::KV::Bucket
- Struct
- Value
- Object
Defined in:
kv.crInstance Method Summary
-
#[]=(key : String, value : Data)
Set the value of a key
-
#[]?(key : String) : Bytes | Nil
Get the value of a key, if it exists (not counting
Delete
operations), stripping away all metadata to return only the value. -
#create(key : String, value : String)
Creates the given
key
with the givenvalue
if and only if the key does not yet exist. - #created : Time
-
#delete(key : String)
Deletes the given
key
from the KV store. -
#description : String | Nil
An optional description of the purpose of this bucket
- #each_key(&)
-
#get(key : String, *, revision : Int | Nil = nil, ignore_deletes = false) : Entry | Nil
Get the entry for a key as a
KV::Entry
- returnsnil
if the key does not exist or if it's been deleted withignore_deletes
set totrue
. - #get!(key : String, *, revision : Int | Nil = nil, ignore_deletes = false) : Entry
-
#history(key : String)
Get the history
-
#history : Int64 | Nil
The number of revisions NATS will retain for this key
-
#keys(pattern : String = ">")
List all known keys for this bucket, returned as a
Set(String)
. - #last_update : Time
-
#max_bytes : Int64 | Nil
The maximum size in bytes of this bucket in memory or on disk
-
#max_value_size : Int32 | Nil
The maximum number of bytes in a value
-
#name : String
The name of this bucket
- #placement : JetStream::StreamConfig::Placement | Nil
-
#purge(key : String)
Purges the given
key
from the KV store. -
#put(key : String, value : Data)
Set the value of a key
-
#replicas : Int32
The number of NATS nodes to which this KV bucket will be replicated
-
#set(key : String, value : Data)
Assign
value
tokey
inbucket
asynchronously, not waiting for acknowledgement from the NATS server. - #size : Size
-
#storage : JetStream::StreamConfig::Storage
Where NATS stores the data for this bucket
-
#stream_name : String
The name of the underlying
JetStream
stream. -
#ttl : Time::Span | Nil
The maximum length of time NATS will retain a value or revision for a key
-
#update(key : String, value : String, revision : Int64)
Updates the given
key
with the givenvalue
if and only if it exists and is currently at the givenrevision
. - #values : Int64
-
#watch(key : String, *, ignore_deletes = false, include_history = true)
Watch the given key (or wildcard) for changes and yielding them to the block.
Instance Method Detail
Get the value of a key, if it exists (not counting Delete
operations),
stripping away all metadata to return only the value. If you need
metadata such as revision
, timestamp
, or operation
, or if you need
to be able to get deleted keys, you should use Bucket#get
instead.
Creates the given key
with the given value
if and only if the key
does not yet exist.
Deletes the given key
from the KV store. Inside the NATS server, this
is implemented as adding another message to the stream that signifies
deletion.
Get the entry for a key as a KV::Entry
- returns nil
if the key does
not exist or if it's been deleted with ignore_deletes
set to true
.
Important: If you do not set ignore_deletes
, you may get a deleted
key. This is because the keys are stored in a stream and deleting the
key sets an operation
flag (implemented in the NATS server as a
KV-Operation
message header) and this method retrieves the last entry
in the stream for this key. ignore_deletes
simply tells the client to
ignore deleted messages.
Get the value of a key as a KV::Entry
- raises KeyError
if the key
does not exist or if it's been deleted with ignore_deletes
set to
true
.
Purges the given key
from the KV store. Inside the NATS server, this
is implemented as rolling up all versions of this key into a single
message with its KV::Entry#operation
value (KV-Operation
header)
set to KV::Entry::Operation::Purge
.
Assign value
to key
in bucket
asynchronously, not waiting for
acknowledgement from the NATS server.
WARNING Without acknowledgement, there is no guarantee the server received the value. Use with caution.
The maximum length of time NATS will retain a value or revision for a key
Updates the given key
with the given value
if and only if it exists
and is currently at the given revision
. If you do not have the latest
revision, this method returns nil
so you can perform domain-specific
conflict resolution. If you need to set the key regardless of revision,
use Bucket#put
instead.
Watch the given key (or wildcard) for changes and yielding them to the
block. By default, this will also yield deleted messages. To avoid that,
pass ignore_deletes: true
.
bucket.watch("session.*") do |entry|
_prefix, session_id = entry.subject.split('.', 2)
if entry.operation.deleted?
# do deleted things
else
# the session was updated
end
end
This method blocks until the yielded Watch
is stopped (use
watch.stop
), so if you want to run it in the background, you will need
to run this method inside a spawn
block.
You can also use this method to wait for a specific key to change once:
watch = bucket.watch(my_key)
watch.each do |entry|
# react to the key change
watch.stop if entry.latest? # exit the block
end