Skip to content

Commit

Permalink
Etcd#get_prefix returns a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg committed Jul 30, 2024
1 parent 12f73a2 commit 3990f4f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions spec/etcd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe LavinMQ::Etcd do
etcd.put("foo/a", "bar")
etcd.put("foo/b", "bar")
etcd.put("fou/c", "bar")
etcd.get_prefix("foo").should eq %w(bar bar)
etcd.get_prefix("foo").should eq Hash{"foo/a" => "bar", "foo/b" => "bar"}
end
end

Expand All @@ -33,7 +33,7 @@ describe LavinMQ::Etcd do
etcd.put("foo/b", "bar")
etcd.put("fou/c", "bar")
etcd.del_prefix("foo").should eq 2
etcd.get_prefix("foo").should eq %w()
etcd.get_prefix("foo").empty?.should be_true
etcd.get("foo/a").should be_nil
end
end
Expand Down
12 changes: 7 additions & 5 deletions src/lavinmq/etcd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module LavinMQ
end

# Get all keys with a prefix
def get_prefix(prefix) : Array(String)
def get_prefix(prefix) : Hash(String, String)
range_end = prefix.to_slice.dup
len = range_end.bytesize
(len - 1).downto(0) do |i|
Expand All @@ -28,13 +28,15 @@ module LavinMQ
end
range_end = len.zero? ? Bytes[0] : range_end[0, len] # drop ending null values
json = post("/v3/kv/range", %({"key":"#{Base64.strict_encode prefix}","range_end":"#{Base64.strict_encode range_end}","limit":0,"serializable":true}))
result = Hash(String, String).new
if kvs = json["kvs"]?
kvs.as_a.map do |kv|
Base64.decode_string kv["value"].as_s
kvs.as_a.each do |kv|
key = Base64.decode_string kv["key"].as_s
value = Base64.decode_string kv["value"].as_s
result[key] = value
end
else
Array(String).new(0)
end
result
end

def put(key, value) : String?
Expand Down

0 comments on commit 3990f4f

Please sign in to comment.