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