nats-pure 2.0.0.pre.rc1 → 2.0.0.pre.rc2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 210e29c9b6fcc68e5c89e841a068c1a389ac78a67cadcb639161d0d5c02c4bf0
4
- data.tar.gz: 2b181886b3a96277ae494cdda48b8bf16e9721666294ea69440976b85888b40c
3
+ metadata.gz: d16ee062f4cee20fd50abb3d9f78a2e2aa3c92648717ae4e6906e93255920d89
4
+ data.tar.gz: d4fa9ee8eb06ca8379493de124d7c125dd15a929550316c943be9bd2ae8d8743
5
5
  SHA512:
6
- metadata.gz: 910ffb697d692a3040fd6b40e835d1a2486f16525a5e814ea33dfdb2e732dd6ba3f1876b5e6dbc7dee07e9491154a6c181b41b2db08988434d9e48900ae3dcf0
7
- data.tar.gz: a4e4d9cc9be95d1c67331576c814841b7b7784986a6ac9503ec44846e5dfbf2af978990fe482c8a9ea1064112ab54b4c65cfc619a931a20904f712d2896547a1
6
+ metadata.gz: ec7aa2c9dd3ce098d7ebdd083ecabe581ebe22302d5cd452feb4e718bebf2b453c455ad6786a0f9bfb7a34f49e348ba9829184521a7f161fbd9ba96df6f39383
7
+ data.tar.gz: 8667b8373d87a4f905723f881991645c48a731bafd98a6a533316857fa6a6e56e0493edb753a4b9b01be62fa52b6a82c2a32269e337f4f82e105f48ed8602100
data/lib/nats/io/kv.rb ADDED
@@ -0,0 +1,172 @@
1
+ # Copyright 2021 The NATS Authors
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ #
14
+ require_relative 'errors'
15
+
16
+ module NATS
17
+ class KeyValue
18
+ KV_OP = "KV-Operation"
19
+ KV_DEL = "DEL"
20
+ KV_PURGE = "PURGE"
21
+ MSG_ROLLUP_SUBJECT = "sub"
22
+ MSG_ROLLUP_ALL = "all"
23
+
24
+ def initialize(opts={})
25
+ @name = opts[:name]
26
+ @stream = opts[:stream]
27
+ @pre = opts[:pre]
28
+ @js = opts[:js]
29
+ end
30
+
31
+ # When a key is not found because it was deleted.
32
+ class KeyDeletedError < NATS::Error; end
33
+
34
+ # When there was no bucket present.
35
+ class BucketNotFoundError < NATS::Error; end
36
+
37
+ # When it is an invalid bucket.
38
+ class BadBucketError < NATS::Error; end
39
+
40
+ # get returns the latest value for the key.
41
+ def get(key)
42
+ msg = @js.get_last_msg(@stream, "#{@pre}#{key}")
43
+ entry = Entry.new(bucket: @name, key: key, value: msg.data, revision: msg.seq)
44
+
45
+ if not msg.headers.nil?
46
+ op = msg.headers[KV_OP]
47
+ raise KeyDeletedError.new("nats: key was deleted") if op == KV_DEL or op == KV_PURGE
48
+ end
49
+
50
+ entry
51
+ end
52
+
53
+ # put will place the new value for the key into the store
54
+ # and return the revision number.
55
+ def put(key, value)
56
+ @js.publish("#{@pre}#{key}", value)
57
+ end
58
+
59
+ # delete will place a delete marker and remove all previous revisions.
60
+ def delete(key)
61
+ hdrs = {}
62
+ hdrs[KV_OP] = KV_DEL
63
+ @js.publish("#{@pre}#{key}", header: hdrs)
64
+ end
65
+
66
+ # status retrieves the status and configuration of a bucket.
67
+ def status
68
+ info = @js.stream_info(@stream)
69
+ BucketStatus.new(info, @name)
70
+ end
71
+
72
+ Entry = Struct.new(:bucket, :key, :value, :revision, keyword_init: true) do
73
+ def initialize(opts={})
74
+ rem = opts.keys - members
75
+ opts.delete_if { |k| rem.include?(k) }
76
+ super(opts)
77
+ end
78
+ end
79
+
80
+ class BucketStatus
81
+ attr_reader :bucket
82
+
83
+ def initialize(info, bucket)
84
+ @nfo = info
85
+ @bucket = bucket
86
+ end
87
+
88
+ def values
89
+ @nfo.state.messages
90
+ end
91
+
92
+ def history
93
+ @nfo.config.max_msgs_per_subject
94
+ end
95
+
96
+ def ttl
97
+ @nfo.config.max_age / 1_000_000_000
98
+ end
99
+ end
100
+
101
+ module API
102
+ KeyValueConfig = Struct.new(:bucket, :description, :max_value_size,
103
+ :history, :ttl, :max_bytes, :storage, :replicas,
104
+ keyword_init: true) do
105
+ def initialize(opts={})
106
+ rem = opts.keys - members
107
+ opts.delete_if { |k| rem.include?(k) }
108
+ super(opts)
109
+ end
110
+ end
111
+ end
112
+
113
+ module Manager
114
+ def key_value(bucket)
115
+ stream = "KV_#{bucket}"
116
+ begin
117
+ si = stream_info(stream)
118
+ rescue NATS::JetStream::Error::NotFound
119
+ raise BucketNotFoundError.new("nats: bucket not found")
120
+ end
121
+ if si.config.max_msgs_per_subject < 1
122
+ raise BadBucketError.new("nats: bad bucket")
123
+ end
124
+
125
+ KeyValue.new(
126
+ name: bucket,
127
+ stream: stream,
128
+ pre: "$KV.#{bucket}.",
129
+ js: self,
130
+ )
131
+ end
132
+
133
+ def create_key_value(config)
134
+ config = if not config.is_a?(JetStream::API::StreamConfig)
135
+ KeyValue::API::KeyValueConfig.new(config)
136
+ else
137
+ config
138
+ end
139
+ config.history ||= 1
140
+ config.replicas ||= 1
141
+ if config.ttl
142
+ config.ttl = config.ttl * 1_000_000_000
143
+ end
144
+
145
+ stream = JetStream::API::StreamConfig.new(
146
+ name: "KV_#{config.bucket}",
147
+ subjects: ["$KV.#{config.bucket}.>"],
148
+ max_msgs_per_subject: config.history,
149
+ max_bytes: config.max_bytes,
150
+ max_age: config.ttl,
151
+ max_msg_size: config.max_value_size,
152
+ storage: config.storage,
153
+ num_replicas: config.replicas,
154
+ allow_rollup_hdrs: true,
155
+ deny_delete: true,
156
+ )
157
+ resp = add_stream(stream)
158
+
159
+ KeyValue.new(
160
+ name: config.bucket,
161
+ stream: stream.name,
162
+ pre: "$KV.#{config.bucket}.",
163
+ js: self,
164
+ )
165
+ end
166
+
167
+ def delete_key_value(bucket)
168
+ delete_stream("KV_#{bucket}")
169
+ end
170
+ end
171
+ end
172
+ end
@@ -15,7 +15,7 @@
15
15
  module NATS
16
16
  module IO
17
17
  # VERSION is the version of the client announced on CONNECT to the server.
18
- VERSION = "2.0.0-rc1".freeze
18
+ VERSION = "2.0.0-rc2".freeze
19
19
 
20
20
  # LANG is the lang runtime of the client announced on CONNECT to the server.
21
21
  LANG = "#{RUBY_ENGINE}#{RUBY_VERSION}".freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nats-pure
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.rc1
4
+ version: 2.0.0.pre.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Waldemar Quevedo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-29 00:00:00.000000000 Z
11
+ date: 2022-01-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: NATS is an open-source, high-performance, lightweight cloud messaging
14
14
  system.
@@ -23,6 +23,7 @@ files:
23
23
  - lib/nats/io/client.rb
24
24
  - lib/nats/io/errors.rb
25
25
  - lib/nats/io/js.rb
26
+ - lib/nats/io/kv.rb
26
27
  - lib/nats/io/msg.rb
27
28
  - lib/nats/io/parser.rb
28
29
  - lib/nats/io/subscription.rb