Skip to content

Commit 9e06b9d

Browse files
authored
RCBC-450: Set value of subdoc exists to true or false if result is success or path-not-found (#120)
1 parent d23002b commit 9e06b9d

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

ext/couchbase

ext/couchbase.cxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3517,7 +3517,7 @@ cb_Backend_document_lookup_in(VALUE self, VALUE bucket, VALUE scope, VALUE colle
35173517
if (!resp_entry.value.empty()) {
35183518
rb_hash_aset(entry, value_property, cb_str_new(resp_entry.value));
35193519
}
3520-
if (resp_entry.ec && resp_entry.ec != couchbase::errc::key_value::path_not_found) {
3520+
if (resp_entry.ec) {
35213521
rb_hash_aset(entry,
35223522
error_property,
35233523
cb_map_error_code(resp_entry.ec,
@@ -3628,7 +3628,7 @@ cb_Backend_document_lookup_in_any_replica(VALUE self, VALUE bucket, VALUE scope,
36283628
if (!resp_entry.value.empty()) {
36293629
rb_hash_aset(entry, value_property, cb_str_new(resp_entry.value));
36303630
}
3631-
if (resp_entry.ec && resp_entry.ec != couchbase::errc::key_value::path_not_found) {
3631+
if (resp_entry.ec) {
36323632
rb_hash_aset(entry,
36333633
error_property,
36343634
cb_map_error_code(resp_entry.ec,
@@ -3744,7 +3744,7 @@ cb_Backend_document_lookup_in_all_replicas(VALUE self, VALUE bucket, VALUE scope
37443744
if (!field_entry.value.empty()) {
37453745
rb_hash_aset(entry, value_property, cb_str_new(field_entry.value));
37463746
}
3747-
if (field_entry.ec && field_entry.ec != couchbase::errc::key_value::path_not_found) {
3747+
if (field_entry.ec) {
37483748
rb_hash_aset(
37493749
entry,
37503750
error_property,

lib/couchbase/collection_options.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def content(path_or_index, transcoder = self.transcoder)
169169
field = get_field_at_index(path_or_index)
170170

171171
raise field.error unless field.error.nil?
172-
raise Error::PathNotFound, "Path is not found: #{path_or_index}" unless field.exists
173172

174173
transcoder.decode(field.value, :json)
175174
end
@@ -191,7 +190,7 @@ def exists?(path_or_index)
191190
end
192191
return false unless field
193192

194-
raise field.error unless field.error.nil?
193+
raise field.error unless field.error.nil? || field.error.is_a?(Error::PathNotFound)
195194

196195
field.exists
197196
end

test/subdoc_test.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,25 @@ def test_exists_single
145145

146146
@collection.upsert(doc_id, {"foo" => "bar"})
147147

148+
res = @collection.lookup_in(doc_id, [
149+
LookupInSpec.exists("foo"),
150+
])
151+
152+
assert res.exists?(0)
153+
assert res.content(0)
154+
end
155+
156+
def test_exists_single_does_not_exist
157+
doc_id = uniq_id(:foo)
158+
159+
@collection.upsert(doc_id, {"foo" => "bar"})
160+
148161
res = @collection.lookup_in(doc_id, [
149162
LookupInSpec.exists("does_not_exist"),
150163
])
151164

152165
refute res.exists?(0)
153-
assert_raises Error::PathNotFound do
154-
res.content(0)
155-
end
166+
refute res.content(0)
156167
end
157168

158169
def test_exists_multi
@@ -166,9 +177,7 @@ def test_exists_multi
166177
])
167178

168179
refute res.exists?(0)
169-
assert_raises Error::PathNotFound do
170-
res.content(0)
171-
end
180+
refute res.content(0)
172181

173182
assert res.exists?(1)
174183
assert_equal "bar", res.content(1)
@@ -1399,6 +1408,8 @@ def test_lookup_in_any_replica_exists
13991408

14001409
assert res.exists?(0)
14011410
refute res.exists?(1)
1411+
assert res.content(0)
1412+
refute res.content(1)
14021413
assert_respond_to res, :replica?
14031414
end
14041415

@@ -1421,6 +1432,8 @@ def test_lookup_in_all_replicas_exist
14211432
res.each do |entry|
14221433
assert entry.exists?(0)
14231434
refute entry.exists?(1)
1435+
assert entry.content(0)
1436+
refute entry.content(1)
14241437
assert_respond_to entry, :replica?
14251438
end
14261439
end

0 commit comments

Comments
 (0)