forked from couchbase/couchbase-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
kv.rb
151 lines (136 loc) · 5.24 KB
/
kv.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2023. Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# frozen_string_literal: true
require "couchbase/collection_options"
require "couchbase/binary_collection_options"
module Couchbase
module Protostellar
module ResponseConverter
class KV
def self.to_get_result(resp, options)
Couchbase::Collection::GetResult.new do |res|
res.transcoder = options.transcoder
res.cas = resp.cas
res.expiry = extract_expiry_time(resp) if options.respond_to?(:with_expiry) && options.with_expiry
res.encoded = if resp.content_uncompressed == "null" && !options.projections.empty?
"{}"
else
resp.content_uncompressed
end
res.flags = resp.content_flags
end
end
def self.to_mutation_result(resp)
Couchbase::Collection::MutationResult.new do |res|
res.cas = resp.cas
res.mutation_token = extract_mutation_token(resp)
end
end
def self.to_exists_result(resp)
Couchbase::Collection::ExistsResult.new do |res|
res.cas = resp.cas
res.exists = resp.result
end
end
def self.to_lookup_in_result(resp, specs, options, request)
Couchbase::Collection::LookupInResult.new do |res|
res.cas = resp.cas
res.transcoder = options.transcoder
res.encoded = resp.specs.each_with_index.map do |s, idx|
Couchbase::Collection::SubDocumentField.new do |f|
# TODO: What to do with the status?
error = s.status.nil? ? nil : ErrorHandling.convert_rpc_status(s.status, request)
f.error = error unless error.nil?
f.index = idx
f.path = specs[idx].path
if specs[idx].type == :exists
f.exists = s.content == "true"
f.value = s.content
elsif s.content.empty?
f.value = nil
f.exists = false
else
f.value = s.content
f.exists = true
end
end
end
end
end
def self.to_get_any_replica_result(resps, options)
begin
entry = resps.next
rescue StopIteration
raise Couchbase::Error::DocumentIrretrievable, "unable to get replica of the document"
end
Couchbase::Collection::GetReplicaResult.new do |res|
res.transcoder = options.transcoder
res.cas = entry.cas
res.flags = entry.content_flags
res.encoded = entry.content
res.is_replica = entry.is_replica
end
end
def self.to_get_all_replicas_result(resps, options)
resps.map do |entry|
Couchbase::Collection::GetReplicaResult.new do |res|
res.transcoder = options.transcoder
res.cas = entry.cas
res.flags = entry.content_flags
res.encoded = entry.content
res.is_replica = entry.is_replica
end
end
end
def self.to_mutate_in_result(resp, specs, options)
Couchbase::Collection::MutateInResult.new do |res|
res.cas = resp.cas
res.transcoder = options.transcoder
res.deleted = nil # TODO: gRPC response has no deleted field
res.mutation_token = extract_mutation_token(resp)
res.encoded = resp.specs.each_with_index.map do |s, idx|
Couchbase::Collection::SubDocumentField.new do |f|
f.index = idx
f.path = specs[idx].path
f.value = s.content
end
end
end
end
def self.to_counter_result(resp)
Couchbase::BinaryCollection::CounterResult.new do |res|
res.cas = resp.cas
res.content = resp.content
res.mutation_token = extract_mutation_token(resp)
end
end
def self.extract_mutation_token(resp)
proto_token = resp.mutation_token
return nil if proto_token.nil?
Couchbase::MutationToken.new do |token|
token.bucket_name = proto_token.bucket_name
token.partition_id = proto_token.vbucket_id
token.partition_uuid = proto_token.vbucket_uuid
token.sequence_number = proto_token.seq_no
end
end
def self.extract_expiry_time(resp)
timestamp = resp.expiry
return nil if timestamp.nil?
Time.at(timestamp.seconds, timestamp.nanos, :nsec)
end
end
end
end
end