couchbase 3.5.0-x86_64-linux-musl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +202 -0
  3. data/README.md +154 -0
  4. data/ext/extconf.rb +0 -0
  5. data/lib/active_support/cache/couchbase_store.rb +339 -0
  6. data/lib/couchbase/3.1/libcouchbase.so +0 -0
  7. data/lib/couchbase/3.2/libcouchbase.so +0 -0
  8. data/lib/couchbase/3.3/libcouchbase.so +0 -0
  9. data/lib/couchbase/analytics_options.rb +107 -0
  10. data/lib/couchbase/authenticator.rb +64 -0
  11. data/lib/couchbase/binary_collection.rb +128 -0
  12. data/lib/couchbase/binary_collection_options.rb +24 -0
  13. data/lib/couchbase/bucket.rb +144 -0
  14. data/lib/couchbase/cluster.rb +460 -0
  15. data/lib/couchbase/cluster_registry.rb +49 -0
  16. data/lib/couchbase/collection.rb +705 -0
  17. data/lib/couchbase/collection_options.rb +399 -0
  18. data/lib/couchbase/config_profiles.rb +55 -0
  19. data/lib/couchbase/configuration.rb +56 -0
  20. data/lib/couchbase/datastructures/couchbase_list.rb +160 -0
  21. data/lib/couchbase/datastructures/couchbase_map.rb +194 -0
  22. data/lib/couchbase/datastructures/couchbase_queue.rb +134 -0
  23. data/lib/couchbase/datastructures/couchbase_set.rb +128 -0
  24. data/lib/couchbase/datastructures.rb +24 -0
  25. data/lib/couchbase/diagnostics.rb +181 -0
  26. data/lib/couchbase/errors.rb +376 -0
  27. data/lib/couchbase/json_transcoder.rb +39 -0
  28. data/lib/couchbase/key_value_scan.rb +117 -0
  29. data/lib/couchbase/libcouchbase.rb +6 -0
  30. data/lib/couchbase/logger.rb +85 -0
  31. data/lib/couchbase/management/analytics_index_manager.rb +1127 -0
  32. data/lib/couchbase/management/bucket_manager.rb +443 -0
  33. data/lib/couchbase/management/collection_manager.rb +470 -0
  34. data/lib/couchbase/management/collection_query_index_manager.rb +222 -0
  35. data/lib/couchbase/management/query_index_manager.rb +617 -0
  36. data/lib/couchbase/management/scope_search_index_manager.rb +198 -0
  37. data/lib/couchbase/management/search_index_manager.rb +424 -0
  38. data/lib/couchbase/management/user_manager.rb +468 -0
  39. data/lib/couchbase/management/view_index_manager.rb +237 -0
  40. data/lib/couchbase/management.rb +29 -0
  41. data/lib/couchbase/mutation_state.rb +63 -0
  42. data/lib/couchbase/options.rb +2837 -0
  43. data/lib/couchbase/protostellar/binary_collection.rb +55 -0
  44. data/lib/couchbase/protostellar/bucket.rb +51 -0
  45. data/lib/couchbase/protostellar/client.rb +99 -0
  46. data/lib/couchbase/protostellar/cluster.rb +163 -0
  47. data/lib/couchbase/protostellar/collection.rb +152 -0
  48. data/lib/couchbase/protostellar/connect_options.rb +63 -0
  49. data/lib/couchbase/protostellar/error_handling.rb +203 -0
  50. data/lib/couchbase/protostellar/generated/admin/bucket/v1/bucket_pb.rb +61 -0
  51. data/lib/couchbase/protostellar/generated/admin/bucket/v1/bucket_services_pb.rb +35 -0
  52. data/lib/couchbase/protostellar/generated/admin/collection/v1/collection_pb.rb +57 -0
  53. data/lib/couchbase/protostellar/generated/admin/collection/v1/collection_services_pb.rb +36 -0
  54. data/lib/couchbase/protostellar/generated/admin/query/v1/query_pb.rb +61 -0
  55. data/lib/couchbase/protostellar/generated/admin/query/v1/query_services_pb.rb +37 -0
  56. data/lib/couchbase/protostellar/generated/admin/search/v1/search_pb.rb +72 -0
  57. data/lib/couchbase/protostellar/generated/admin/search/v1/search_services_pb.rb +44 -0
  58. data/lib/couchbase/protostellar/generated/analytics/v1/analytics_pb.rb +52 -0
  59. data/lib/couchbase/protostellar/generated/analytics/v1/analytics_services_pb.rb +30 -0
  60. data/lib/couchbase/protostellar/generated/internal/hooks/v1/hooks_pb.rb +70 -0
  61. data/lib/couchbase/protostellar/generated/internal/hooks/v1/hooks_services_pb.rb +36 -0
  62. data/lib/couchbase/protostellar/generated/kv/v1/kv_pb.rb +97 -0
  63. data/lib/couchbase/protostellar/generated/kv/v1/kv_services_pb.rb +46 -0
  64. data/lib/couchbase/protostellar/generated/query/v1/query_pb.rb +57 -0
  65. data/lib/couchbase/protostellar/generated/query/v1/query_services_pb.rb +30 -0
  66. data/lib/couchbase/protostellar/generated/routing/v1/routing_pb.rb +52 -0
  67. data/lib/couchbase/protostellar/generated/routing/v1/routing_services_pb.rb +30 -0
  68. data/lib/couchbase/protostellar/generated/search/v1/search_pb.rb +99 -0
  69. data/lib/couchbase/protostellar/generated/search/v1/search_services_pb.rb +30 -0
  70. data/lib/couchbase/protostellar/generated/transactions/v1/transactions_pb.rb +57 -0
  71. data/lib/couchbase/protostellar/generated/transactions/v1/transactions_services_pb.rb +36 -0
  72. data/lib/couchbase/protostellar/generated/view/v1/view_pb.rb +51 -0
  73. data/lib/couchbase/protostellar/generated/view/v1/view_services_pb.rb +30 -0
  74. data/lib/couchbase/protostellar/generated.rb +9 -0
  75. data/lib/couchbase/protostellar/management/bucket_manager.rb +67 -0
  76. data/lib/couchbase/protostellar/management/collection_manager.rb +94 -0
  77. data/lib/couchbase/protostellar/management/collection_query_index_manager.rb +124 -0
  78. data/lib/couchbase/protostellar/management/query_index_manager.rb +112 -0
  79. data/lib/couchbase/protostellar/management.rb +24 -0
  80. data/lib/couchbase/protostellar/request.rb +78 -0
  81. data/lib/couchbase/protostellar/request_behaviour.rb +42 -0
  82. data/lib/couchbase/protostellar/request_generator/admin/bucket.rb +124 -0
  83. data/lib/couchbase/protostellar/request_generator/admin/collection.rb +94 -0
  84. data/lib/couchbase/protostellar/request_generator/admin/query.rb +130 -0
  85. data/lib/couchbase/protostellar/request_generator/admin.rb +24 -0
  86. data/lib/couchbase/protostellar/request_generator/kv.rb +474 -0
  87. data/lib/couchbase/protostellar/request_generator/query.rb +133 -0
  88. data/lib/couchbase/protostellar/request_generator/search.rb +387 -0
  89. data/lib/couchbase/protostellar/request_generator.rb +26 -0
  90. data/lib/couchbase/protostellar/response_converter/admin/bucket.rb +55 -0
  91. data/lib/couchbase/protostellar/response_converter/admin/collection.rb +42 -0
  92. data/lib/couchbase/protostellar/response_converter/admin/query.rb +59 -0
  93. data/lib/couchbase/protostellar/response_converter/admin.rb +24 -0
  94. data/lib/couchbase/protostellar/response_converter/kv.rb +151 -0
  95. data/lib/couchbase/protostellar/response_converter/query.rb +84 -0
  96. data/lib/couchbase/protostellar/response_converter/search.rb +136 -0
  97. data/lib/couchbase/protostellar/response_converter.rb +26 -0
  98. data/lib/couchbase/protostellar/retry/action.rb +38 -0
  99. data/lib/couchbase/protostellar/retry/orchestrator.rb +60 -0
  100. data/lib/couchbase/protostellar/retry/reason.rb +67 -0
  101. data/lib/couchbase/protostellar/retry/strategies/best_effort.rb +49 -0
  102. data/lib/couchbase/protostellar/retry/strategies.rb +26 -0
  103. data/lib/couchbase/protostellar/retry.rb +28 -0
  104. data/lib/couchbase/protostellar/scope.rb +57 -0
  105. data/lib/couchbase/protostellar/timeout_defaults.rb +30 -0
  106. data/lib/couchbase/protostellar/timeouts.rb +83 -0
  107. data/lib/couchbase/protostellar.rb +29 -0
  108. data/lib/couchbase/query_options.rb +120 -0
  109. data/lib/couchbase/railtie.rb +45 -0
  110. data/lib/couchbase/raw_binary_transcoder.rb +37 -0
  111. data/lib/couchbase/raw_json_transcoder.rb +38 -0
  112. data/lib/couchbase/raw_string_transcoder.rb +40 -0
  113. data/lib/couchbase/scope.rb +256 -0
  114. data/lib/couchbase/search_options.rb +1622 -0
  115. data/lib/couchbase/subdoc.rb +290 -0
  116. data/lib/couchbase/transcoder_flags.rb +62 -0
  117. data/lib/couchbase/utils/generic_logger_adapter.rb +38 -0
  118. data/lib/couchbase/utils/stdlib_logger_adapter.rb +65 -0
  119. data/lib/couchbase/utils/time.rb +69 -0
  120. data/lib/couchbase/utils.rb +21 -0
  121. data/lib/couchbase/version.rb +23 -0
  122. data/lib/couchbase/view_options.rb +65 -0
  123. data/lib/couchbase.rb +28 -0
  124. data/lib/rails/generators/couchbase/config/config_generator.rb +27 -0
  125. metadata +190 -0
@@ -0,0 +1,399 @@
1
+ # Copyright 2020-2021 Couchbase, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "rubygems/deprecate"
16
+
17
+ require "couchbase/json_transcoder"
18
+ require "couchbase/raw_string_transcoder"
19
+ require "couchbase/raw_json_transcoder"
20
+ require "couchbase/raw_binary_transcoder"
21
+ require "couchbase/subdoc"
22
+ require "couchbase/mutation_state"
23
+
24
+ module Couchbase
25
+ class Collection
26
+ class GetResult
27
+ extend Gem::Deprecate
28
+
29
+ # @return [Integer] holds the CAS value of the fetched document
30
+ attr_accessor :cas
31
+
32
+ # @return [Integer] the expiration if fetched and present
33
+ attr_writer :expiry
34
+
35
+ # @return [Error::CouchbaseError, nil] error associated with the result, or nil (used in {Collection#get_multi})
36
+ attr_accessor :error
37
+
38
+ # @return [String, nil] identifier of the document (used for {Collection#get_multi})
39
+ attr_accessor :id
40
+
41
+ # @return [Boolean] true if error was not associated with the result (useful for multi-operations)
42
+ def success?
43
+ !error
44
+ end
45
+
46
+ # @return [String] The encoded content when loading the document
47
+ # @api private
48
+ attr_accessor :encoded
49
+
50
+ # Decodes the content of the document using given (or default transcoder)
51
+ #
52
+ # @param [JsonTranscoder] transcoder custom transcoder
53
+ #
54
+ # @return [Object]
55
+ def content(transcoder = self.transcoder)
56
+ transcoder ? transcoder.decode(@encoded, @flags) : @encoded
57
+ end
58
+
59
+ # @return [Time] time when the document will expire
60
+ def expiry_time
61
+ Time.at(@expiry) if @expiry
62
+ end
63
+
64
+ # @yieldparam [GetResult] self
65
+ def initialize
66
+ @expiry = nil
67
+ @error = nil
68
+ @id = nil
69
+ yield self if block_given?
70
+ end
71
+
72
+ # @return [Integer] The flags from the operation
73
+ # @api private
74
+ attr_accessor :flags
75
+
76
+ # @return [JsonTranscoder] The default transcoder which should be used
77
+ attr_accessor :transcoder
78
+
79
+ # @deprecated Use {#expiry_time}
80
+ # @return [Integer] the expiration if fetched and present
81
+ def expiry # rubocop:disable Style/TrivialAccessors will be removed in next major release
82
+ @expiry
83
+ end
84
+
85
+ deprecate :expiry, :expiry_time, 2021, 1
86
+ end
87
+
88
+ class GetReplicaResult < GetResult
89
+ # @return [Boolean] true if this result came from a replica
90
+ attr_accessor :is_replica
91
+ alias replica? is_replica
92
+ end
93
+
94
+ class ExistsResult
95
+ # @return [Integer] holds the CAS value of the fetched document
96
+ attr_accessor :cas
97
+
98
+ # @return [Boolean] true if the document was deleted
99
+ attr_accessor :deleted
100
+
101
+ # @return [Boolean] true if the document exists
102
+ attr_accessor :exists
103
+ alias exists? exists
104
+
105
+ # @yieldparam [ExistsResult]
106
+ def initialize
107
+ yield self if block_given?
108
+ end
109
+
110
+ # @return [Integer] the expiration if fetched and present
111
+ attr_writer :expiry
112
+
113
+ # @return [Time] time when the document will expire
114
+ def expiry_time
115
+ Time.at(@expiry) if @expiry
116
+ end
117
+
118
+ # @api private
119
+ # @return [Integer] flags
120
+ attr_accessor :flags
121
+
122
+ # @api private
123
+ # @return [Integer] sequence_number
124
+ attr_accessor :sequence_number
125
+
126
+ # @api private
127
+ # @return [Integer] datatype
128
+ attr_accessor :datatype
129
+ end
130
+
131
+ class MutationResult
132
+ # @return [Integer] holds the CAS value of the document after the mutation
133
+ attr_accessor :cas
134
+
135
+ # @return [MutationToken] if returned, holds the mutation token of the document after the mutation
136
+ attr_accessor :mutation_token
137
+
138
+ # @return [Error::CouchbaseError, nil] error or nil (used in multi-operations like {Collection#upsert_multi},
139
+ # {Collection#remove_multi})
140
+ attr_accessor :error
141
+
142
+ # @return [String, nil] identifier of the document (used in multi-operations like {Collection#upsert_multi},
143
+ # {Collection#remove_multi})
144
+ attr_accessor :id
145
+
146
+ # @return [Boolean] true if error was not associated with the result (useful for multi-operations)
147
+ def success?
148
+ !error
149
+ end
150
+
151
+ # @yieldparam [MutationResult] self
152
+ def initialize
153
+ @error = nil
154
+ @id = nil
155
+ yield self if block_given?
156
+ end
157
+ end
158
+
159
+ class LookupInResult
160
+ # @return [Integer] holds the CAS value of the fetched document
161
+ attr_accessor :cas
162
+
163
+ # Decodes the content at the given index (or path)
164
+ #
165
+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to decode
166
+ #
167
+ # @return [Object] the decoded
168
+ def content(path_or_index, transcoder = self.transcoder)
169
+ field = get_field_at_index(path_or_index)
170
+
171
+ raise field.error unless field.error.nil?
172
+
173
+ transcoder.decode(field.value, :json)
174
+ end
175
+
176
+ # Allows to check if a value at the given index exists
177
+ #
178
+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to check
179
+ #
180
+ # @return [Boolean] true if a value is present at the index, false otherwise
181
+ def exists?(path_or_index)
182
+ field =
183
+ case path_or_index
184
+ when String
185
+ encoded.find { |f| f.path == path_or_index }
186
+ else
187
+ return false unless path_or_index >= 0 && path_or_index < encoded.size
188
+
189
+ encoded[path_or_index]
190
+ end
191
+ return false unless field
192
+
193
+ raise field.error unless field.error.nil? || field.error.is_a?(Error::PathNotFound)
194
+
195
+ field.exists
196
+ end
197
+
198
+ # @return [Array<SubDocumentField>] holds the encoded subdocument responses
199
+ attr_accessor :encoded
200
+
201
+ # @yieldparam [LookupInResult] self
202
+ def initialize
203
+ @deleted = false
204
+ yield self if block_given?
205
+ end
206
+
207
+ # @return [JsonTranscoder] The default transcoder which should be used
208
+ attr_accessor :transcoder
209
+
210
+ # @api private
211
+ #
212
+ # @see MutateInOptions#create_as_deleted
213
+ #
214
+ # @return [Boolean] true if the document is a tombstone (created in deleted state)
215
+ def deleted?
216
+ @deleted
217
+ end
218
+
219
+ # @api private
220
+ attr_accessor :deleted
221
+
222
+ private
223
+
224
+ def get_field_at_index(path_or_index)
225
+ case path_or_index
226
+ when String
227
+ encoded.find { |field| field.path == path_or_index } or raise Error::PathInvalid, "Path is not found: #{path_or_index}"
228
+ else
229
+ raise Error::PathInvalid, "Index is out of bounds: #{path_or_index}" unless path_or_index >= 0 && path_or_index < encoded.size
230
+
231
+ encoded[path_or_index]
232
+ end
233
+ end
234
+ end
235
+
236
+ class LookupInReplicaResult < LookupInResult
237
+ # @return [Boolean] true if the document was read from a replica node
238
+ attr_accessor :is_replica
239
+ alias replica? is_replica
240
+
241
+ # @yieldparam [LookupInReplicaResult] self
242
+ def initialize
243
+ super
244
+ yield self if block_given?
245
+ end
246
+ end
247
+
248
+ class MutateInResult < MutationResult
249
+ # Decodes the content at the given index
250
+ #
251
+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to decode
252
+ #
253
+ # @return [Object] the decoded
254
+ def content(path_or_index, transcoder = self.transcoder)
255
+ field = get_field_at_index(path_or_index)
256
+ transcoder.decode(field.value, :json)
257
+ end
258
+
259
+ # @yieldparam [MutateInResult] self
260
+ def initialize
261
+ super
262
+ yield self if block_given?
263
+ end
264
+
265
+ # @return [Array<SubDocumentField>] holds the encoded subdocument responses
266
+ # @api private
267
+ attr_accessor :encoded
268
+
269
+ # @return [JsonTranscoder] The default transcoder which should be used
270
+ attr_accessor :transcoder
271
+
272
+ # @api private
273
+ #
274
+ # @see MutateInOptions#create_as_deleted
275
+ #
276
+ # @return [Boolean] true if the document is a tombstone (created in deleted state)
277
+ def deleted?
278
+ @deleted
279
+ end
280
+
281
+ # @api private
282
+ attr_accessor :deleted
283
+
284
+ private
285
+
286
+ def get_field_at_index(path_or_index)
287
+ case path_or_index
288
+ when String
289
+ encoded.find { |field| field.path == path_or_index } or raise Error::PathInvalid, "Path is not found: #{path_or_index}"
290
+ else
291
+ raise Error::PathInvalid, "Index is out of bounds: #{path_or_index}" unless path_or_index >= 0 && path_or_index < encoded.size
292
+
293
+ encoded[path_or_index]
294
+ end
295
+ end
296
+ end
297
+
298
+ # @api private
299
+ class SubDocumentField
300
+ # @return [Boolean] true if the path exists in the document
301
+ attr_accessor :exists
302
+
303
+ # @return [String] value
304
+ attr_accessor :value
305
+
306
+ # @return [Integer] index
307
+ attr_accessor :index
308
+
309
+ # @return [String] path
310
+ attr_accessor :path
311
+
312
+ # @return [CouchbaseError] error
313
+ attr_accessor :error
314
+
315
+ # @yieldparam [SubDocumentField] self
316
+ def initialize
317
+ yield self if block_given?
318
+ end
319
+ end
320
+
321
+ class ScanResult
322
+ # @return [String] identifier of the document
323
+ attr_accessor :id
324
+
325
+ # @return [Boolean] whether only ids are returned from this scan
326
+ attr_accessor :id_only
327
+
328
+ # @return [Integer, nil] holds the CAS value of the fetched document
329
+ attr_accessor :cas
330
+
331
+ # @return [Integer, nil] the expiration if fetched and present
332
+ attr_accessor :expiry
333
+
334
+ # @return [JsonTranscoder, RawBinaryTranscoder, RawJsonTranscoder, RawStringTranscoder, #decode] The default
335
+ # transcoder which should be used
336
+ attr_accessor :transcoder
337
+
338
+ def initialize(id:, id_only:, cas: nil, expiry: nil, encoded: nil, flags: nil, transcoder: JsonTranscoder.new)
339
+ @id = id
340
+ @id_only = id_only
341
+ @cas = cas
342
+ @expiry = expiry
343
+ @encoded = encoded
344
+ @flags = flags
345
+ @transcoder = transcoder
346
+
347
+ yield self if block_given?
348
+ end
349
+
350
+ # Decodes the content of the document using given (or default transcoder)
351
+ #
352
+ # @param [JsonTranscoder, RawJsonTranscoder, RawBinaryTranscoder, RawStringTranscoder] transcoder custom transcoder
353
+ #
354
+ # @return [Object, nil]
355
+ def content(transcoder = self.transcoder)
356
+ return nil if @encoded.nil?
357
+
358
+ transcoder ? transcoder.decode(@encoded, @flags) : @encoded
359
+ end
360
+ end
361
+
362
+ class ScanResults
363
+ include Enumerable
364
+
365
+ def initialize(core_scan_result:, transcoder:)
366
+ @core_scan_result = core_scan_result
367
+ @transcoder = transcoder
368
+ end
369
+
370
+ def each
371
+ return enum_for(:each) unless block_given?
372
+
373
+ loop do
374
+ resp = @core_scan_result.next_item
375
+
376
+ break if resp.nil?
377
+
378
+ if resp[:id_only]
379
+ yield ScanResult.new(
380
+ id: resp[:id],
381
+ id_only: resp[:id_only],
382
+ transcoder: @transcoder
383
+ )
384
+ else
385
+ yield ScanResult.new(
386
+ id: resp[:id],
387
+ id_only: resp[:id_only],
388
+ cas: resp[:cas],
389
+ expiry: resp[:expiry],
390
+ encoded: resp[:encoded],
391
+ flags: resp[:flags],
392
+ transcoder: @transcoder
393
+ )
394
+ end
395
+ end
396
+ end
397
+ end
398
+ end
399
+ end
@@ -0,0 +1,55 @@
1
+ # Copyright 2020-2022 Couchbase, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Couchbase
16
+ module ConfigProfiles
17
+ class Profiles
18
+ attr_reader :profiles
19
+
20
+ def initialize
21
+ @profiles = {}
22
+ register_profile("wan_development", DevelopmentProfile.new)
23
+ end
24
+
25
+ def register_profile(name, profile)
26
+ @profiles[name] = profile
27
+ end
28
+
29
+ def apply(profile_name, options)
30
+ raise ArgumentError, "#{profile_name} is not a registered profile" unless @profiles.key?(profile_name)
31
+
32
+ @profiles[profile_name].apply(options)
33
+ end
34
+ end
35
+
36
+ class Profile
37
+ def apply(options); end
38
+ end
39
+
40
+ class DevelopmentProfile < Profile
41
+ def apply(options)
42
+ options.key_value_timeout = 20_000
43
+ # TODO: Add `options.key_value_durable_timeout = 20_000` when key_value_durable_timeout is added to Options::Cluster
44
+ options.connect_timeout = 20_000
45
+ options.view_timeout = 120_000
46
+ options.query_timeout = 120_000
47
+ options.analytics_timeout = 120_000
48
+ options.search_timeout = 120_000
49
+ options.management_timeout = 120_000
50
+ end
51
+ end
52
+
53
+ KNOWN_PROFILES = Profiles.new
54
+ end
55
+ end
@@ -0,0 +1,56 @@
1
+ # Copyright 2020-2021 Couchbase, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "yaml"
16
+ require "erb"
17
+
18
+ module Couchbase
19
+ class Configuration
20
+ attr_accessor :connection_string
21
+ attr_accessor :username
22
+ attr_accessor :password
23
+
24
+ def initialize
25
+ @connection_string = "couchbase://localhost"
26
+ end
27
+
28
+ def load!(path, environment = default_environment_name)
29
+ settings = load_yaml(path, environment)
30
+ load_configuration(settings)
31
+ end
32
+
33
+ private
34
+
35
+ def load_configuration(settings)
36
+ @connection_string = settings[:connection_string] || settings["connection_string"]
37
+ @username = settings[:username] || settings["username"]
38
+ @password = settings[:password] || settings["password"]
39
+ end
40
+
41
+ def load_yaml(path, environment)
42
+ file_content = ERB.new(File.read(path)).result
43
+ YAML.safe_load(file_content, aliases: :default)[environment]
44
+ end
45
+
46
+ def default_environment_name
47
+ if defined?(::Rails)
48
+ ::Rails.env
49
+ elsif defined?(::Sinatra)
50
+ ::Sinatra::Base.environment.to_s
51
+ else
52
+ ENV.fetch("RACK_ENV", nil) || ENV.fetch("COUCHBASE_ENV", nil) or raise ::Couchbase::Error::NoEnvironment
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,160 @@
1
+ # Copyright 2020-2021 Couchbase, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "couchbase/collection"
16
+ require "couchbase/errors"
17
+ require "couchbase/options"
18
+
19
+ module Couchbase
20
+ module Datastructures
21
+ # A {CouchbaseList} is implements +Enumerable+ interface and backed by {Collection} document (more specifically
22
+ # a JSON array).
23
+ #
24
+ # Note that as such, a {CouchbaseList} is restricted to the types that JSON array can contain.
25
+ class CouchbaseList
26
+ include Enumerable
27
+
28
+ # Create a new List, backed by the document identified by +id+ in +collection+.
29
+ #
30
+ # @param [String] id the id of the document to back the list.
31
+ # @param [Collection] collection the Couchbase collection through which to interact with the document.
32
+ # @param [Options::CouchbaseList] options customization of the datastructure
33
+ def initialize(id, collection, options = Options::CouchbaseList.new)
34
+ @id = id
35
+ @collection = collection
36
+ @options = options
37
+ @cas = 0
38
+ end
39
+
40
+ # Calls the given block once for each element in the list, passing that element as a parameter.
41
+ #
42
+ # @yieldparam [Object] item
43
+ #
44
+ # @return [CouchbaseList, Enumerable]
45
+ def each
46
+ if block_given?
47
+ begin
48
+ result = @collection.get(@id, @options.get_options)
49
+ current = result.content
50
+ @cas = result.cas
51
+ rescue Error::DocumentNotFound
52
+ current = []
53
+ @cas = 0
54
+ end
55
+ current.each do |entry|
56
+ yield entry
57
+ end
58
+ self
59
+ else
60
+ enum_for(:each)
61
+ end
62
+ end
63
+
64
+ # @return [Integer] returns the number of elements in the list.
65
+ def length
66
+ result = @collection.lookup_in(@id, [
67
+ LookupInSpec.count(""),
68
+ ], @options.lookup_in_options)
69
+ result.content(0)
70
+ rescue Error::DocumentNotFound
71
+ 0
72
+ end
73
+
74
+ alias size length
75
+
76
+ # @return [Boolean] returns true if list is empty
77
+ def empty?
78
+ size.zero?
79
+ end
80
+
81
+ # Appends the given object(s) on to the end of this error. This expression returns the array itself, so several
82
+ # appends may be chained together.
83
+ #
84
+ # @param [Object...] obj object(s) to append
85
+ # @return [CouchbaseList]
86
+ def push(*obj)
87
+ @collection.mutate_in(@id, [
88
+ MutateInSpec.array_append("", obj),
89
+ ], @options.mutate_in_options)
90
+ self
91
+ end
92
+
93
+ alias append push
94
+
95
+ # Prepends objects to the front of the list, moving other elements upwards
96
+ #
97
+ # @param [Object...] obj object(s) to prepend
98
+ # @return [CouchbaseList]
99
+ def unshift(*obj)
100
+ @collection.mutate_in(@id, [
101
+ MutateInSpec.array_prepend("", obj),
102
+ ], @options.mutate_in_options)
103
+ self
104
+ end
105
+
106
+ alias prepend unshift
107
+
108
+ # Inserts the given values before the element with the given +index+.
109
+ #
110
+ # @param [Integer] index
111
+ # @param [Object...] obj object(s) to insert
112
+ # @return [CouchbaseList]
113
+ def insert(index, *obj)
114
+ @collection.mutate_in(@id, [
115
+ MutateInSpec.array_insert("[#{index.to_i}]", obj),
116
+ ])
117
+ self
118
+ end
119
+
120
+ # Returns the element at +index+. A negative index counts from the end. Returns +nil+ if the index is out of range.
121
+ #
122
+ # @param [Integer] index
123
+ # @return [Object, nil]
124
+ def at(index)
125
+ result = @collection.lookup_in(@id, [
126
+ LookupInSpec.get("[#{index.to_i}]"),
127
+ ], @options.lookup_in_options)
128
+ result.exists?(0) ? result.content(0) : nil
129
+ rescue Error::DocumentNotFound
130
+ nil
131
+ end
132
+
133
+ alias [] at
134
+
135
+ # Deletes the element at the specified +index+, returning that element, or nil
136
+ #
137
+ # @param [Integer] index
138
+ # @return [CouchbaseList]
139
+ def delete_at(index)
140
+ @collection.mutate_in(@id, [
141
+ MutateInSpec.remove("[#{index.to_i}]"),
142
+ ])
143
+ self
144
+ rescue Error::DocumentNotFound
145
+ self
146
+ end
147
+
148
+ # Removes all elements from the list
149
+ def clear
150
+ @collection.remove(@id, @options.remove_options)
151
+ nil
152
+ rescue Error::DocumentNotFound
153
+ nil
154
+ end
155
+ end
156
+
157
+ # @api private
158
+ CouchbaseListOptions = ::Couchbase::Options::CouchbaseList
159
+ end
160
+ end