couchbase 3.5.3-arm64-darwin

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 +342 -0
  6. data/lib/couchbase/3.1/libcouchbase.bundle +0 -0
  7. data/lib/couchbase/3.2/libcouchbase.bundle +0 -0
  8. data/lib/couchbase/3.3/libcouchbase.bundle +0 -0
  9. data/lib/couchbase/analytics_options.rb +109 -0
  10. data/lib/couchbase/authenticator.rb +66 -0
  11. data/lib/couchbase/binary_collection.rb +130 -0
  12. data/lib/couchbase/binary_collection_options.rb +26 -0
  13. data/lib/couchbase/bucket.rb +146 -0
  14. data/lib/couchbase/cluster.rb +462 -0
  15. data/lib/couchbase/cluster_registry.rb +49 -0
  16. data/lib/couchbase/collection.rb +707 -0
  17. data/lib/couchbase/collection_options.rb +401 -0
  18. data/lib/couchbase/config_profiles.rb +57 -0
  19. data/lib/couchbase/configuration.rb +58 -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 +26 -0
  25. data/lib/couchbase/diagnostics.rb +183 -0
  26. data/lib/couchbase/errors.rb +414 -0
  27. data/lib/couchbase/json_transcoder.rb +41 -0
  28. data/lib/couchbase/key_value_scan.rb +119 -0
  29. data/lib/couchbase/libcouchbase.rb +6 -0
  30. data/lib/couchbase/logger.rb +87 -0
  31. data/lib/couchbase/management/analytics_index_manager.rb +1129 -0
  32. data/lib/couchbase/management/bucket_manager.rb +445 -0
  33. data/lib/couchbase/management/collection_manager.rb +472 -0
  34. data/lib/couchbase/management/collection_query_index_manager.rb +224 -0
  35. data/lib/couchbase/management/query_index_manager.rb +619 -0
  36. data/lib/couchbase/management/scope_search_index_manager.rb +200 -0
  37. data/lib/couchbase/management/search_index_manager.rb +426 -0
  38. data/lib/couchbase/management/user_manager.rb +470 -0
  39. data/lib/couchbase/management/view_index_manager.rb +239 -0
  40. data/lib/couchbase/management.rb +31 -0
  41. data/lib/couchbase/mutation_state.rb +65 -0
  42. data/lib/couchbase/options.rb +2846 -0
  43. data/lib/couchbase/protostellar/binary_collection.rb +55 -0
  44. data/lib/couchbase/protostellar/bucket.rb +55 -0
  45. data/lib/couchbase/protostellar/client.rb +99 -0
  46. data/lib/couchbase/protostellar/cluster.rb +171 -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 +122 -0
  109. data/lib/couchbase/railtie.rb +47 -0
  110. data/lib/couchbase/raw_binary_transcoder.rb +39 -0
  111. data/lib/couchbase/raw_json_transcoder.rb +40 -0
  112. data/lib/couchbase/raw_string_transcoder.rb +42 -0
  113. data/lib/couchbase/scope.rb +258 -0
  114. data/lib/couchbase/search_options.rb +1650 -0
  115. data/lib/couchbase/subdoc.rb +293 -0
  116. data/lib/couchbase/transcoder_flags.rb +64 -0
  117. data/lib/couchbase/utils/generic_logger_adapter.rb +40 -0
  118. data/lib/couchbase/utils/stdlib_logger_adapter.rb +67 -0
  119. data/lib/couchbase/utils/time.rb +71 -0
  120. data/lib/couchbase/utils.rb +23 -0
  121. data/lib/couchbase/version.rb +25 -0
  122. data/lib/couchbase/view_options.rb +67 -0
  123. data/lib/couchbase.rb +30 -0
  124. data/lib/rails/generators/couchbase/config/config_generator.rb +29 -0
  125. metadata +190 -0
@@ -0,0 +1,470 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020-2021 Couchbase, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "couchbase/errors"
18
+
19
+ require "time"
20
+
21
+ module Couchbase
22
+ module Management
23
+ class UserManager
24
+ alias inspect to_s
25
+
26
+ # @param [Couchbase::Backend] backend
27
+ def initialize(backend)
28
+ @backend = backend
29
+ end
30
+
31
+ # Get a user
32
+ #
33
+ # @param [String] username ID of the user
34
+ # @param [GetUserOptions] options
35
+ #
36
+ # @return [UserAndMetadata]
37
+ #
38
+ # @raise [ArgumentError]
39
+ # @raise [Error::UserNotFound]
40
+ def get_user(username, options = GetUserOptions.new)
41
+ resp = @backend.user_get(options.domain, username, options.timeout)
42
+ extract_user(resp)
43
+ end
44
+
45
+ # Gets all users
46
+ #
47
+ # @return [Array<UserAndMetadata>]
48
+ def get_all_users(options = GetAllUsersOptions.new)
49
+ resp = @backend.user_get_all(options.domain, options.timeout)
50
+ resp.map { |entry| extract_user(entry) }
51
+ end
52
+
53
+ # Creates or updates a user
54
+ #
55
+ # @param [User] user the new version of the user
56
+ # @param [UpsertUserOptions] options
57
+ #
58
+ # @raise [ArgumentError]
59
+ def upsert_user(user, options = UpsertUserOptions.new)
60
+ @backend.user_upsert(
61
+ options.domain,
62
+ {
63
+ username: user.username,
64
+ display_name: user.display_name,
65
+ groups: user.groups,
66
+ password: user.password,
67
+ roles: user.roles.map do |role|
68
+ {
69
+ name: role.name,
70
+ bucket: role.bucket,
71
+ scope: role.scope,
72
+ collection: role.collection,
73
+ }
74
+ end,
75
+ }, options.timeout
76
+ )
77
+ end
78
+
79
+ # Removes a user
80
+ #
81
+ # @param [String] username ID of the user
82
+ # @param [DropUserOptions] options
83
+ def drop_user(username, options = DropUserOptions.new)
84
+ @backend.user_drop(options.domain, username, options.timeout)
85
+ end
86
+
87
+ # Gets all roles supported by the server
88
+ #
89
+ # @param [GetRolesOptions] options
90
+ #
91
+ # @return [Array<RoleAndDescription>]
92
+ def get_roles(options = GetRolesOptions.new)
93
+ resp = @backend.role_get_all(options.timeout)
94
+ resp.map do |r|
95
+ RoleAndDescription.new do |role|
96
+ role.name = r[:name]
97
+ role.display_name = r[:display_name]
98
+ role.description = r[:description]
99
+ role.bucket = r[:bucket]
100
+ role.scope = r[:scope]
101
+ role.collection = r[:collection]
102
+ end
103
+ end
104
+ end
105
+
106
+ # Changes the password of the currently authenticated user
107
+ #
108
+ # @param [ChangePasswordOptions] options
109
+ #
110
+ # @raise [ArgumentError]
111
+ def change_password(new_password, options = ChangePasswordOptions.new)
112
+ @backend.change_password(new_password, options.timeout)
113
+ end
114
+
115
+ # Gets a group
116
+ #
117
+ # @param [String] group_name name of the group to get
118
+ #
119
+ # @return [Group]
120
+ #
121
+ # @raise [ArgumentError]
122
+ # @raise [Error::GroupNotFound]
123
+ def get_group(group_name, options = GetGroupOptions.new)
124
+ resp = @backend.group_get(group_name, options.timeout)
125
+ extract_group(resp)
126
+ end
127
+
128
+ # Gets all groups
129
+ #
130
+ # @param [GetAllGroupsOptions] options
131
+ #
132
+ # @return [Array<Group>]
133
+ def get_all_groups(options = GetAllGroupsOptions.new)
134
+ resp = @backend.group_get_all(options.timeout)
135
+ resp.map { |entry| extract_group(entry) }
136
+ end
137
+
138
+ # Creates or updates a group
139
+ #
140
+ # @param [Group] group the new version of the group
141
+ # @param [UpsertGroupOptions] options
142
+ #
143
+ # @raise [ArgumentError]
144
+ # @raise [Error::GroupNotFound]
145
+ def upsert_group(group, options = UpsertGroupOptions.new)
146
+ @backend.group_upsert({
147
+ name: group.name,
148
+ description: group.description,
149
+ ldap_group_reference: group.ldap_group_reference,
150
+ roles: group.roles.map do |role|
151
+ {
152
+ name: role.name,
153
+ bucket: role.bucket,
154
+ scope: role.scope,
155
+ collection: role.collection,
156
+ }
157
+ end,
158
+ }, options.timeout)
159
+ end
160
+
161
+ # Removes a group
162
+ #
163
+ # @param [String] group_name name of the group
164
+ # @param [DropGroupOptions] options
165
+ #
166
+ # @raise [Error::GroupNotFound]
167
+ def drop_group(group_name, options = DropGroupOptions.new)
168
+ @backend.group_drop(group_name, options.timeout)
169
+ end
170
+
171
+ class GetUserOptions
172
+ # @return [:local, :external] Name of the user's domain. Defaults to +:local+
173
+ attr_accessor :domain
174
+
175
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
176
+ attr_accessor :timeout
177
+
178
+ # @yieldparam [GetUserOptions] self
179
+ def initialize
180
+ @domain = :local
181
+ yield self if block_given?
182
+ end
183
+ end
184
+
185
+ class GetAllUsersOptions
186
+ # @return [:local, :external] Name of the user's domain. Defaults to +:local+
187
+ attr_accessor :domain
188
+
189
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
190
+ attr_accessor :timeout
191
+
192
+ # @yieldparam [GetAllUsersOptions] self
193
+ def initialize
194
+ @domain = :local
195
+ yield self if block_given?
196
+ end
197
+ end
198
+
199
+ class UpsertUserOptions
200
+ # @return [:local, :external] Name of the user's domain. Defaults to +:local+
201
+ attr_accessor :domain
202
+
203
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
204
+ attr_accessor :timeout
205
+
206
+ # @yieldparam [UpsertUserOptions] self
207
+ def initialize
208
+ @domain = :local
209
+ yield self if block_given?
210
+ end
211
+ end
212
+
213
+ class DropUserOptions
214
+ # @return [:local, :external] Name of the user's domain. Defaults to +:local+
215
+ attr_accessor :domain
216
+
217
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
218
+ attr_accessor :timeout
219
+
220
+ # @yieldparam [DropUserOptions] self
221
+ def initialize
222
+ @domain = :local
223
+ yield self if block_given?
224
+ end
225
+ end
226
+
227
+ class ChangePasswordOptions
228
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
229
+ attr_accessor :timeout
230
+
231
+ # @yieldparam [ChangePasswordOptions] self
232
+ def initialize
233
+ yield self if block_given?
234
+ end
235
+ end
236
+
237
+ class GetRolesOptions
238
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
239
+ attr_accessor :timeout
240
+
241
+ # @yieldparam [GetRolesOptions] self
242
+ def initialize
243
+ yield self if block_given?
244
+ end
245
+ end
246
+
247
+ class GetGroupOptions
248
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
249
+ attr_accessor :timeout
250
+
251
+ # @yieldparam [GetGroupOptions] self
252
+ def initialize
253
+ yield self if block_given?
254
+ end
255
+ end
256
+
257
+ class GetAllGroupsOptions
258
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
259
+ attr_accessor :timeout
260
+
261
+ # @yieldparam [GetAllGroupsOptions] self
262
+ def initialize
263
+ yield self if block_given?
264
+ end
265
+ end
266
+
267
+ class UpsertGroupOptions
268
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
269
+ attr_accessor :timeout
270
+
271
+ # @yieldparam [UpsertGroupOptions] self
272
+ def initialize
273
+ yield self if block_given?
274
+ end
275
+ end
276
+
277
+ class DropGroupOptions
278
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
279
+ attr_accessor :timeout
280
+
281
+ # @yieldparam [DropGroupOptions] self
282
+ def initialize
283
+ yield self if block_given?
284
+ end
285
+ end
286
+
287
+ private
288
+
289
+ def extract_group(resp)
290
+ Group.new do |group|
291
+ group.name = resp[:name]
292
+ group.description = resp[:description]
293
+ group.ldap_group_reference = resp[:ldap_group_reference]
294
+ if resp[:roles]
295
+ group.roles = resp[:roles].map do |r|
296
+ Role.new do |role|
297
+ role.name = r[:name]
298
+ role.bucket = r[:bucket]
299
+ role.scope = r[:scope]
300
+ role.collection = r[:collection]
301
+ end
302
+ end
303
+ end
304
+ end
305
+ end
306
+
307
+ def extract_user(resp)
308
+ UserAndMetadata.new do |user|
309
+ user.domain = resp[:domain]
310
+ user.username = resp[:username]
311
+ user.display_name = resp[:display_name]
312
+ user.groups = resp[:groups]
313
+ user.external_groups = resp[:external_groups]
314
+ user.password_changed = Time.parse(resp[:password_changed]) if resp[:password_changed]
315
+ if resp[:roles]
316
+ user.roles = resp[:roles].map do |r|
317
+ Role.new do |role|
318
+ role.name = r[:name]
319
+ role.bucket = r[:bucket]
320
+ role.scope = r[:scope]
321
+ role.collection = r[:collection]
322
+ end
323
+ end
324
+ end
325
+ if resp[:effective_roles]
326
+ user.effective_roles = resp[:effective_roles].map do |r|
327
+ RoleAndOrigins.new do |role|
328
+ role.name = r[:name]
329
+ role.bucket = r[:bucket]
330
+ role.scope = r[:scope]
331
+ role.collection = r[:collection]
332
+ if r[:origins]
333
+ role.origins = r[:origins].map do |o|
334
+ Origin.new do |origin|
335
+ origin.type = o[:type]
336
+ origin.name = o[:name]
337
+ end
338
+ end
339
+ end
340
+ end
341
+ end
342
+ end
343
+ end
344
+ end
345
+ end
346
+
347
+ class Role
348
+ # @return [String]
349
+ attr_accessor :name
350
+
351
+ # @return [String]
352
+ attr_accessor :bucket
353
+
354
+ # @return [String]
355
+ attr_accessor :scope
356
+
357
+ # @return [String]
358
+ attr_accessor :collection
359
+
360
+ # @yieldparam [Role] self
361
+ def initialize
362
+ yield self if block_given?
363
+ end
364
+ end
365
+
366
+ class RoleAndDescription < Role
367
+ # @return [String]
368
+ attr_accessor :display_name
369
+
370
+ # @return [String]
371
+ attr_accessor :description
372
+
373
+ # @yieldparam [RoleAndDescription] self
374
+ def initialize
375
+ super
376
+ yield self if block_given?
377
+ end
378
+ end
379
+
380
+ class Origin
381
+ # @return [String]
382
+ attr_writer :type
383
+
384
+ # @return [String]
385
+ attr_writer :name
386
+
387
+ # @yieldparam [Origin] self
388
+ def initialize
389
+ yield self if block_given?
390
+ end
391
+ end
392
+
393
+ class RoleAndOrigins < Role
394
+ # @return [Array<Origin>]
395
+ attr_writer :origins
396
+
397
+ # @yieldparam [RoleAndOrigins] self
398
+ def initialize
399
+ super
400
+ @origins = []
401
+ yield self if block_given?
402
+ end
403
+ end
404
+
405
+ class User
406
+ # @return [String]
407
+ attr_accessor :username
408
+
409
+ # @return [String]
410
+ attr_accessor :display_name
411
+
412
+ # @return [Array<String>]
413
+ attr_accessor :groups
414
+
415
+ # @return [Array<Role>]
416
+ attr_accessor :roles
417
+
418
+ # @return [String]
419
+ attr_accessor :password
420
+
421
+ # @yieldparam [User] self
422
+ def initialize
423
+ @groups = []
424
+ @roles = []
425
+ yield self if block_given?
426
+ end
427
+ end
428
+
429
+ class UserAndMetadata < User
430
+ # @return [:local, :external]
431
+ attr_accessor :domain
432
+
433
+ # @return [Array<RoleAndOrigins>]
434
+ attr_accessor :effective_roles
435
+
436
+ # @return [Time]
437
+ attr_accessor :password_changed
438
+
439
+ # @return [Array<String>]
440
+ attr_accessor :external_groups
441
+
442
+ # @yieldparam [UserAndMetadata] self
443
+ def initialize
444
+ super
445
+ @effective_roles = []
446
+ yield self if block_given?
447
+ end
448
+ end
449
+
450
+ class Group
451
+ # @return [String]
452
+ attr_accessor :name
453
+
454
+ # @return [String]
455
+ attr_accessor :description
456
+
457
+ # @return [Array<Role>]
458
+ attr_accessor :roles
459
+
460
+ # @return [String]
461
+ attr_accessor :ldap_group_reference
462
+
463
+ # @yieldparam [Group] self
464
+ def initialize
465
+ @roles = []
466
+ yield self if block_given?
467
+ end
468
+ end
469
+ end
470
+ end
@@ -0,0 +1,239 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020-2021 Couchbase, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "couchbase/errors"
18
+
19
+ module Couchbase
20
+ module Management
21
+ # The View Index Manager interface contains the means for managing design documents used for views.
22
+ #
23
+ # A design document belongs to either the "development" or "production" namespace. A development document has a name
24
+ # that starts with "dev_". This is an implementation detail we've chosen to hide from consumers of this API.
25
+ # Document names presented to the user (returned from the "get" and "get all" methods, for example) always have the
26
+ # "dev_" prefix stripped.
27
+ #
28
+ # Whenever the user passes a design document name to any method of this API, the user may refer to the document
29
+ # using the "dev_" prefix regardless of whether the user is referring to a development document or production
30
+ # document. The "dev_" prefix is always stripped from user input, and the actual document name passed to the server
31
+ # is determined by the "namespace" argument.
32
+ #
33
+ # All methods (except publish) have a required "namespace" argument indicating whether the operation targets a
34
+ # development document or a production document. The type of this argument is [Symbol] with allowed values
35
+ # +:production+ and +:development+.
36
+ class ViewIndexManager
37
+ alias inspect to_s
38
+
39
+ # @return [String] name of the bucket
40
+ attr_accessor :bucket_name
41
+
42
+ # @param [Couchbase::Backend] backend
43
+ # @param [String] bucket_name
44
+ def initialize(backend, bucket_name)
45
+ @backend = backend
46
+ @bucket_name = bucket_name
47
+ end
48
+
49
+ # Fetches a design document from the server
50
+ #
51
+ # @param [String] name the name of the design document
52
+ # @param [:production, :development] namespace the namespace
53
+ # @param [GetDesignDocumentOptions] options
54
+ #
55
+ # @return [DesignDocument]
56
+ #
57
+ # @raise [Error::DesignDocumentNotFound]
58
+ def get_design_document(name, namespace, options = GetDesignDocumentOptions.new)
59
+ resp = @backend.view_index_get(@bucket_name, name, namespace, options.timeout)
60
+ extract_design_document(resp)
61
+ end
62
+
63
+ # Fetches all design documents from the server
64
+ #
65
+ # @param [:production, :development] namespace the namespace
66
+ # @param [GetAllDesignDocumentsOptions] options
67
+ #
68
+ # @return [Array<DesignDocument>]
69
+ def get_all_design_documents(namespace, options = GetAllDesignDocumentsOptions.new)
70
+ resp = @backend.view_index_get_all(@bucket_name, namespace, options.timeout)
71
+ resp.map do |entry|
72
+ extract_design_document(entry)
73
+ end
74
+ end
75
+
76
+ # Updates or inserts the design document
77
+ #
78
+ # @param [DesignDocument] document
79
+ # @param [:production, :development] namespace the namespace
80
+ # @param [UpsertDesignDocumentOptions] options
81
+ #
82
+ # @return [void]
83
+ def upsert_design_document(document, namespace, options = UpsertDesignDocumentOptions.new)
84
+ @backend.view_index_upsert(@bucket_name, {
85
+ name: document.name,
86
+ views: document.views.map do |name, view|
87
+ {
88
+ name: name,
89
+ map: view.map_function,
90
+ reduce: view.reduce_function,
91
+ }
92
+ end,
93
+ }, namespace, options.timeout)
94
+ end
95
+
96
+ # Removes the design document
97
+ #
98
+ # @param [String] name design document name
99
+ # @param [:production, :development] namespace the namespace
100
+ # @param [DropDesignDocumentOptions] options
101
+ #
102
+ # @return [void]
103
+ #
104
+ # @raise [Error::DesignDocumentNotFound]
105
+ def drop_design_document(name, namespace, options = DropDesignDocumentOptions.new)
106
+ @backend.view_index_drop(@bucket_name, name, namespace, options.timeout)
107
+ end
108
+
109
+ # Publishes the design document.
110
+ #
111
+ # This method is equivalent to getting a document from the development namespace and upserting
112
+ # it to the production namespace.
113
+ #
114
+ # @param [String] name design document name
115
+ # @param [PublishDesignDocumentOptions] options
116
+ #
117
+ # @return [void]
118
+ #
119
+ # @raise [ArgumentError]
120
+ # @raise [Error::DesignDocumentNotFound]
121
+ def publish_design_document(name, options = PublishDesignDocumentOptions.new)
122
+ document = get_design_document(name, :development, GetDesignDocumentOptions.new { |o| o.timeout = options.timeout })
123
+ upsert_design_document(document, :production, UpsertDesignDocumentOptions.new { |o| o.timeout = options.timeout })
124
+ end
125
+
126
+ class GetDesignDocumentOptions
127
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
128
+ attr_accessor :timeout
129
+
130
+ # @yieldparam [GetDesignDocumentOptions] self
131
+ def initialize
132
+ yield self if block_given?
133
+ end
134
+ end
135
+
136
+ class GetAllDesignDocumentsOptions
137
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
138
+ attr_accessor :timeout
139
+
140
+ # @yieldparam [GetAllDesignDocumentsOptions] self
141
+ def initialize
142
+ yield self if block_given?
143
+ end
144
+ end
145
+
146
+ class UpsertDesignDocumentOptions
147
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
148
+ attr_accessor :timeout
149
+
150
+ # @yieldparam [UpsertDesignDocumentOptions] self
151
+ def initialize
152
+ yield self if block_given?
153
+ end
154
+ end
155
+
156
+ class DropDesignDocumentOptions
157
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
158
+ attr_accessor :timeout
159
+
160
+ # @yieldparam [DropDesignDocumentOptions] self
161
+ def initialize
162
+ yield self if block_given?
163
+ end
164
+ end
165
+
166
+ class PublishDesignDocumentOptions
167
+ # @return [Integer] the time in milliseconds allowed for the operation to complete
168
+ attr_accessor :timeout
169
+
170
+ # @yieldparam [PublishDesignDocumentOptions] self
171
+ def initialize
172
+ yield self if block_given?
173
+ end
174
+ end
175
+
176
+ private
177
+
178
+ def extract_design_document(resp)
179
+ DesignDocument.new do |design_document|
180
+ design_document.name = resp[:name]
181
+ design_document.namespace = resp[:namespace]
182
+ resp[:views].each do |name, view_resp|
183
+ design_document.views[name] = View.new(view_resp[:map], view_resp[:reduce])
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ class View
190
+ # @return [String] name of the view
191
+ attr_accessor :view
192
+
193
+ # @return [String] map function in javascript as String
194
+ attr_accessor :map_function
195
+ alias map map_function
196
+
197
+ # @return [String] reduce function in javascript as String
198
+ attr_accessor :reduce_function
199
+ alias reduce reduce_function
200
+
201
+ # @return [Boolean] true if map function is defined
202
+ def has_map?
203
+ !@map_function.nil?
204
+ end
205
+
206
+ # @return [Boolean] true if map function is defined
207
+ def has_reduce?
208
+ !@reduce_function.nil?
209
+ end
210
+
211
+ # @param [String] map
212
+ # @param [String] reduce
213
+ #
214
+ # @yieldparam [View] self
215
+ def initialize(map = nil, reduce = nil)
216
+ @map_function = map
217
+ @reduce_function = reduce
218
+ yield self if block_given?
219
+ end
220
+ end
221
+
222
+ class DesignDocument
223
+ # @return [String] name
224
+ attr_accessor :name
225
+
226
+ # @return [Hash<String => View>]
227
+ attr_accessor :views
228
+
229
+ # @return [:production, :development]
230
+ attr_accessor :namespace
231
+
232
+ # @yieldparam [DesignDocument] self
233
+ def initialize
234
+ @views = {}
235
+ yield self if block_given?
236
+ end
237
+ end
238
+ end
239
+ end