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,124 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require "couchbase/management/query_index_manager"
18
+
19
+ require_relative "../request_generator/admin/query"
20
+ require_relative "../response_converter/admin/query"
21
+
22
+ module Couchbase
23
+ module Protostellar
24
+ module Management
25
+ class CollectionQueryIndexManager
26
+ def initialize(client:, bucket_name:, scope_name:, collection_name:)
27
+ @client = client
28
+ @bucket_name = bucket_name
29
+ @scope_name = scope_name
30
+ @collection_name = collection_name
31
+ @request_generator = RequestGenerator::Admin::Query.new(
32
+ bucket_name: @bucket_name, scope_name: @scope_name, collection_name: @collection_name
33
+ )
34
+ end
35
+
36
+ def get_all_indexes(options = Couchbase::Management::Options::Query::GetAllIndexes.new)
37
+ validate_options(options)
38
+
39
+ req = @request_generator.get_all_indexes_request(options)
40
+ resp = @client.send_request(req)
41
+ ResponseConverter::Admin::Query.to_query_index_array(resp)
42
+ end
43
+
44
+ def create_index(index_name, fields, options = Couchbase::Management::Options::Query::CreateIndex.new)
45
+ validate_options(options)
46
+
47
+ req = @request_generator.create_index_request(index_name, fields, options)
48
+ @client.send_request(req)
49
+ end
50
+
51
+ def create_primary_index(options = Couchbase::Management::Options::Query::CreatePrimaryIndex.new)
52
+ validate_options(options)
53
+
54
+ req = @request_generator.create_primary_index_request(options)
55
+ @client.send_request(req)
56
+ end
57
+
58
+ def drop_index(index_name, options = Couchbase::Management::Options::Query::DropIndex.new)
59
+ validate_options(options)
60
+
61
+ req = @request_generator.drop_index_request(index_name, options)
62
+ @client.send_request(req)
63
+ end
64
+
65
+ def drop_primary_index(options = Couchbase::Management::Options::Query::DropPrimaryIndex.new)
66
+ validate_options(options)
67
+
68
+ req = @request_generator.drop_primary_index_request(options)
69
+ @client.send_request(req)
70
+ end
71
+
72
+ def build_deferred_indexes(options = Couchbase::Management::Options::Query::BuildDeferredIndexes.new)
73
+ validate_options(options)
74
+
75
+ req = @request_generator.build_deferred_indexes_request(options)
76
+ @client.send_request(req)
77
+ end
78
+
79
+ def watch_indexes(index_names, timeout, options = Couchbase::Management::Options::Query::WatchIndexes.new)
80
+ validate_options(options)
81
+
82
+ # TODO: Will this be implemented in the gateway instead?
83
+
84
+ index_names.append("#primary") if options.watch_primary
85
+
86
+ interval_millis = 50
87
+ deadline = Time.now + (Utils::Time.extract_duration(timeout) * 0.001)
88
+
89
+ while Time.now <= deadline
90
+ get_all_opts = Couchbase::Management::Options::Query::GetAllIndexes.new(timeout: ((deadline - Time.now) * 1000).round)
91
+ indexes = get_all_indexes(get_all_opts).select { |idx| index_names.include? idx.name }
92
+ indexes_not_found = index_names - indexes.map(&:name)
93
+ unless indexes_not_found.empty?
94
+ raise Couchbase::Error::IndexNotFound,
95
+ "Failed to find the indexes: #{indexes_not_found.join(', ')}"
96
+ end
97
+
98
+ all_online = indexes.all? { |idx| idx.state == :online }
99
+ return if all_online
100
+
101
+ sleep(interval_millis / 1000)
102
+ interval_millis += 500
103
+ interval_millis = 1000 if interval_millis > 1000
104
+ end
105
+ raise Couchbase::Error::UnambiguousTimeout, "Failed to find all indexes online within the allotted time"
106
+ end
107
+
108
+ private
109
+
110
+ def validate_options(options)
111
+ unless options.scope_name.nil?
112
+ raise Couchbase::Error::InvalidArgument,
113
+ "Scope name cannot be set in the options when using the Query Index manager at the collection level"
114
+ end
115
+
116
+ return if options.collection_name.nil?
117
+
118
+ raise Couchbase::Error::InvalidArgument,
119
+ "Collection name cannot be set in the options when using the Query Index manager at the collection level"
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,112 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require "couchbase/management/query_index_manager"
18
+
19
+ require_relative "../request_generator/admin/query"
20
+ require_relative "../response_converter/admin/query"
21
+
22
+ module Couchbase
23
+ module Protostellar
24
+ module Management
25
+ class QueryIndexManager
26
+ def initialize(client:)
27
+ @client = client
28
+ @request_generator = RequestGenerator::Admin::Query.new
29
+ end
30
+
31
+ def get_all_indexes(bucket_name, options = Couchbase::Management::Options::Query::GetAllIndexes.new)
32
+ validate_options(options)
33
+
34
+ req = @request_generator.get_all_indexes_request(options, bucket_name)
35
+ resp = @client.send_request(req)
36
+ ResponseConverter::Admin::Query.to_query_index_array(resp)
37
+ end
38
+
39
+ def create_index(bucket_name, index_name, fields, options = Couchbase::Management::Options::Query::CreateIndex.new)
40
+ validate_options(options)
41
+
42
+ req = @request_generator.create_index_request(index_name, fields, options, bucket_name)
43
+ @client.send_request(req)
44
+ end
45
+
46
+ def create_primary_index(bucket_name, options = Couchbase::Management::Options::Query::CreatePrimaryIndex.new)
47
+ validate_options(options)
48
+
49
+ req = @request_generator.create_primary_index_request(options, bucket_name)
50
+ @client.send_request(req)
51
+ end
52
+
53
+ def drop_index(bucket_name, index_name, options = Couchbase::Management::Options::Query::DropIndex.new)
54
+ validate_options(options)
55
+
56
+ req = @request_generator.drop_index_request(index_name, options, bucket_name)
57
+ @client.send_request(req)
58
+ end
59
+
60
+ def drop_primary_index(bucket_name, options = Couchbase::Management::Options::Query::DropPrimaryIndex.new)
61
+ validate_options(options)
62
+
63
+ req = @request_generator.drop_primary_index_request(options, bucket_name)
64
+ @client.send_request(req)
65
+ end
66
+
67
+ def build_deferred_indexes(bucket_name, options = Couchbase::Management::Options::Query::BuildDeferredIndexes.new)
68
+ validate_options(options)
69
+
70
+ req = @request_generator.build_deferred_indexes_request(options, bucket_name)
71
+ @client.send_request(req)
72
+ end
73
+
74
+ def watch_indexes(bucket_name, index_names, timeout, options = Couchbase::Management::Options::Query::WatchIndexes.new)
75
+ validate_options(options)
76
+
77
+ # TODO: Will this be implemented in the gateway instead?
78
+
79
+ index_names.append("#primary") if options.watch_primary
80
+
81
+ interval_millis = 50
82
+ deadline = Time.now + (Utils::Time.extract_duration(timeout) * 0.001)
83
+ while Time.now <= deadline
84
+ get_all_opts = Couchbase::Management::Options::Query::GetAllIndexes.new(timeout: ((deadline - Time.now) * 1000).round)
85
+ indexes = get_all_indexes(bucket_name, get_all_opts).select { |idx| index_names.include? idx.name }
86
+ indexes_not_found = index_names - indexes.map(&:name)
87
+ unless indexes_not_found.empty?
88
+ raise Couchbase::Error::IndexNotFound,
89
+ "Failed to find the indexes: #{indexes_not_found.join(', ')}"
90
+ end
91
+
92
+ all_online = indexes.all? { |idx| idx.state == :online }
93
+ return if all_online
94
+
95
+ sleep(interval_millis / 1000)
96
+ interval_millis += 500
97
+ interval_millis = 1000 if interval_millis > 1000
98
+ end
99
+ raise Couchbase::Error::UnambiguousTimeout, "Failed to find all indexes online within the allotted time"
100
+ end
101
+
102
+ private
103
+
104
+ def validate_options(options)
105
+ return if options.scope_name.nil? && options.collection_name.nil?
106
+
107
+ warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require_relative "management/collection_manager"
18
+
19
+ module Couchbase
20
+ module Protostellar
21
+ module Management
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,78 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require "couchbase/utils/time"
18
+ require_relative "retry/strategies/best_effort"
19
+
20
+ module Couchbase
21
+ module Protostellar
22
+ # @api private
23
+ class Request
24
+ attr_reader :service
25
+ attr_reader :rpc
26
+ attr_reader :timeout
27
+ attr_reader :proto_request
28
+ attr_reader :idempotent
29
+ attr_reader :retry_attempts
30
+ attr_reader :retry_reasons
31
+ attr_reader :retry_strategy
32
+ attr_accessor :context
33
+
34
+ def initialize(
35
+ service:,
36
+ rpc:,
37
+ proto_request:,
38
+ timeout:,
39
+ retry_strategy: Retry::Strategies::BestEffort::DEFAULT,
40
+ idempotent: false
41
+ )
42
+ @service = service
43
+ @rpc = rpc
44
+ @timeout = timeout
45
+ @deadline = nil
46
+ @proto_request = proto_request
47
+ @idempotent = idempotent
48
+ @retry_attempts = 0
49
+ @retry_reasons = Set.new
50
+ @retry_strategy = retry_strategy
51
+ @context = {}
52
+ end
53
+
54
+ def deadline
55
+ if @deadline.nil?
56
+ timeout_secs = 0.001 * Utils::Time.extract_duration(@timeout)
57
+ @deadline = Time.now + timeout_secs
58
+ end
59
+ @deadline
60
+ end
61
+
62
+ def add_retry_attempt(reason)
63
+ @retry_reasons.add(reason)
64
+ @retry_attempts += 1
65
+ end
66
+
67
+ def error_context
68
+ @context.merge({retry_reasons: @retry_reasons.to_a, retry_attempts: @retry_attempts})
69
+ end
70
+
71
+ def set_timeout_from_defaults(timeouts)
72
+ return unless @timeout.nil?
73
+
74
+ @timeout = timeouts.timeout_for_request(self)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ module Couchbase
18
+ module Protostellar
19
+ # @api private
20
+ class RequestBehaviour
21
+ attr_reader :retry_duration
22
+ attr_reader :error
23
+
24
+ def initialize(retry_duration: nil, error: nil)
25
+ @retry_duration = retry_duration
26
+ @error = error
27
+ end
28
+
29
+ def self.fail(error)
30
+ RequestBehaviour.new(error: error)
31
+ end
32
+
33
+ def self.retry(retry_duration)
34
+ RequestBehaviour.new(retry_duration: retry_duration)
35
+ end
36
+
37
+ def self.success
38
+ RequestBehaviour.new(retry_duration: nil, error: nil)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,124 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require_relative "../../request"
18
+ require_relative "../../generated/admin/bucket/v1/bucket_pb"
19
+ require_relative "../kv"
20
+
21
+ module Couchbase
22
+ module Protostellar
23
+ module RequestGenerator
24
+ module Admin
25
+ class Bucket
26
+ BUCKET_TYPE_MAP = {
27
+ :couchbase => :BUCKET_TYPE_COUCHBASE,
28
+ :memcached => :BUCKET_TYPE_MEMCACHED,
29
+ :ephemeral => :BUCKET_TYPE_EPHEMERAL,
30
+ }.freeze
31
+
32
+ EVICTION_MODE_MAP = {
33
+ :full => :EVICTION_MODE_FULL,
34
+ :value_only => :EVICTION_MODE_VALUE_ONLY,
35
+ :no_eviction => :EVICTION_MODE_NONE,
36
+ :not_recently_used => :EVICTION_MODE_NOT_RECENTLY_USED,
37
+ }.freeze
38
+
39
+ COMPRESSION_MODE_MAP = {
40
+ :off => :COMPRESSION_MODE_OFF,
41
+ :passive => :COMPRESSION_MODE_PASSIVE,
42
+ :active => :COMPRESSION_MODE_ACTIVE,
43
+ }.freeze
44
+
45
+ STORAGE_BACKEND_MAP = {
46
+ :couchstore => :STORAGE_BACKEND_COUCHSTORE,
47
+ :magma => :STORAGE_BACKEND_MAGMA,
48
+ }.freeze
49
+
50
+ CONFLICT_RESOLUTION_TYPE_MAP = {
51
+ :timestamp => :CONFLICT_RESOLUTION_TYPE_TIMESTAMP,
52
+ :sequence_number => :CONFLICT_RESOLUTION_TYPE_SEQUENCE_NUMBER,
53
+ :custom => :CONFLICT_RESOLUTION_TYPE_CUSTOM,
54
+ }.freeze
55
+
56
+ DURABILITY_LEVEL_MAP = RequestGenerator::KV::DURABILITY_LEVEL_MAP
57
+
58
+ def list_buckets_request(options)
59
+ proto_req = Generated::Admin::Bucket::V1::ListBucketsRequest.new
60
+ create_request(proto_req, :list_buckets, options, idempotent: true)
61
+ end
62
+
63
+ def create_bucket_request(settings, options)
64
+ proto_req = Generated::Admin::Bucket::V1::CreateBucketRequest.new(
65
+ extract_bucket_settings(settings, create: true)
66
+ )
67
+ create_request(proto_req, :create_bucket, options)
68
+ end
69
+
70
+ def update_bucket_request(settings, options)
71
+ proto_req = Generated::Admin::Bucket::V1::UpdateBucketRequest.new(
72
+ extract_bucket_settings(settings, create: false)
73
+ )
74
+ create_request(proto_req, :update_bucket, options)
75
+ end
76
+
77
+ def delete_bucket_request(bucket_name, options)
78
+ proto_req = Generated::Admin::Bucket::V1::DeleteBucketRequest.new(
79
+ bucket_name: bucket_name
80
+ )
81
+ create_request(proto_req, :delete_bucket, options)
82
+ end
83
+
84
+ private
85
+
86
+ def extract_bucket_settings(settings, create: false)
87
+ s = {bucket_name: settings.name}
88
+
89
+ if create
90
+ s[:bucket_type] = BUCKET_TYPE_MAP[settings.bucket_type]
91
+ s[:storage_backend] = STORAGE_BACKEND_MAP[settings.storage_backend] unless settings.storage_backend.nil?
92
+ unless settings.conflict_resolution_type.nil?
93
+ s[:conflict_resolution_type] = CONFLICT_RESOLUTION_TYPE_MAP[settings.conflict_resolution_type]
94
+ end
95
+ s[:replica_indexes] = settings.replica_indexes unless settings.replica_indexes.nil?
96
+ end
97
+
98
+ s[:ram_quota_mb] = settings.ram_quota_mb unless settings.ram_quota_mb.nil?
99
+ s[:num_replicas] = settings.num_replicas unless settings.num_replicas.nil?
100
+ s[:flush_enabled] = settings.flush_enabled unless settings.flush_enabled.nil?
101
+ s[:eviction_mode] = EVICTION_MODE_MAP[settings.eviction_policy] unless settings.eviction_policy.nil?
102
+ s[:max_expiry_secs] = settings.max_expiry unless settings.max_expiry.nil?
103
+ s[:compression_mode] = COMPRESSION_MODE_MAP[settings.compression_mode] unless settings.compression_mode.nil?
104
+ unless settings.minimum_durability_level.nil?
105
+ s[:minimum_durability_level] = DURABILITY_LEVEL_MAP[settings.minimum_durability_level]
106
+ end
107
+
108
+ s
109
+ end
110
+
111
+ def create_request(proto_request, rpc, options, idempotent: false)
112
+ Request.new(
113
+ service: :bucket_admin,
114
+ rpc: rpc,
115
+ proto_request: proto_request,
116
+ idempotent: idempotent,
117
+ timeout: options.timeout
118
+ )
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,94 @@
1
+ # Copyright 2023. 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
+ # frozen_string_literal: true
16
+
17
+ require "couchbase/protostellar/request"
18
+ require "couchbase/protostellar/generated/admin/collection/v1/collection_pb"
19
+
20
+ module Couchbase
21
+ module Protostellar
22
+ module RequestGenerator
23
+ module Admin
24
+ class Collection
25
+ attr_reader :client
26
+
27
+ def initialize(bucket_name:)
28
+ @bucket_name = bucket_name
29
+ end
30
+
31
+ def list_collections_request(options)
32
+ proto_req = Generated::Admin::Collection::V1::ListCollectionsRequest.new(
33
+ bucket_name: @bucket_name
34
+ )
35
+ create_request(proto_req, :list_collections, options, idempotent: true)
36
+ end
37
+
38
+ def create_scope_request(scope_name, options)
39
+ proto_req = Generated::Admin::Collection::V1::CreateScopeRequest.new(
40
+ bucket_name: @bucket_name,
41
+ scope_name: scope_name
42
+ )
43
+ create_request(proto_req, :create_scope, options)
44
+ end
45
+
46
+ def delete_scope_request(scope_name, options)
47
+ proto_req = Generated::Admin::Collection::V1::DeleteScopeRequest.new(
48
+ bucket_name: @bucket_name,
49
+ scope_name: scope_name
50
+ )
51
+ create_request(proto_req, :delete_scope, options)
52
+ end
53
+
54
+ def create_collection_request(scope_name, collection_name, settings, options)
55
+ proto_opts = {}
56
+ proto_opts[:max_expiry_secs] = settings.max_expiry unless settings.max_expiry.nil?
57
+ unless settings.history.nil?
58
+ raise Couchbase::Error::FeatureNotAvailable, "The #{Protostellar::NAME} protocol does not support the history setting"
59
+ end
60
+
61
+ proto_req = Generated::Admin::Collection::V1::CreateCollectionRequest.new(
62
+ bucket_name: @bucket_name,
63
+ scope_name: scope_name,
64
+ collection_name: collection_name,
65
+ **proto_opts
66
+ )
67
+ create_request(proto_req, :create_collection, options)
68
+ end
69
+
70
+ def delete_collection_request(scope_name, collection_name, options)
71
+ proto_req = Generated::Admin::Collection::V1::DeleteCollectionRequest.new(
72
+ bucket_name: @bucket_name,
73
+ scope_name: scope_name,
74
+ collection_name: collection_name
75
+ )
76
+ create_request(proto_req, :delete_collection, options)
77
+ end
78
+
79
+ private
80
+
81
+ def create_request(proto_request, rpc, options, idempotent: false)
82
+ Request.new(
83
+ service: :collection_admin,
84
+ rpc: rpc,
85
+ proto_request: proto_request,
86
+ idempotent: idempotent,
87
+ timeout: options.timeout
88
+ )
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end