Module: NATS::KeyValue::Manager

Defined in:
lib/nats/io/kv.rb

Instance Method Summary collapse

Instance Method Details

#create_key_value(config) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nats/io/kv.rb', line 133

def create_key_value(config)
  config = if not config.is_a?(JetStream::API::StreamConfig)
             KeyValue::API::KeyValueConfig.new(config)
           else
             config
           end
  config.history ||= 1
  config.replicas ||= 1
  if config.ttl
    config.ttl = config.ttl * 1_000_000_000
  end

  stream = JetStream::API::StreamConfig.new(
    name: "KV_#{config.bucket}",
    subjects: ["$KV.#{config.bucket}.>"],
    max_msgs_per_subject: config.history,
    max_bytes: config.max_bytes,
    max_age: config.ttl,
    max_msg_size: config.max_value_size,
    storage: config.storage,
    num_replicas: config.replicas,
    allow_rollup_hdrs: true,
    deny_delete: true,
  )
  resp = add_stream(stream)

  KeyValue.new(
    name: config.bucket,
    stream: stream.name,
    pre: "$KV.#{config.bucket}.",
    js: self,
  )
end

#delete_key_value(bucket) ⇒ Object



167
168
169
# File 'lib/nats/io/kv.rb', line 167

def delete_key_value(bucket)
  delete_stream("KV_#{bucket}")
end

#key_value(bucket) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nats/io/kv.rb', line 114

def key_value(bucket)
  stream = "KV_#{bucket}"
  begin
    si = stream_info(stream)
  rescue NATS::JetStream::Error::NotFound
    raise BucketNotFoundError.new("nats: bucket not found")
  end
  if si.config.max_msgs_per_subject < 1
    raise BadBucketError.new("nats: bad bucket")
  end

  KeyValue.new(
    name: bucket,
    stream: stream,
    pre: "$KV.#{bucket}.",
    js: self,
  )
end