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,55 @@
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/options"
18
+
19
+ require_relative "response_converter/kv"
20
+
21
+ module Couchbase
22
+ module Protostellar
23
+ class BinaryCollection
24
+ def initialize(collection)
25
+ @collection = collection
26
+ @client = collection.instance_variable_get(:@client)
27
+ @kv_request_generator = collection.instance_variable_get(:@kv_request_generator)
28
+ end
29
+
30
+ def append(id, content, options = Options::Append::DEFAULT)
31
+ req = @kv_request_generator.append_request(id, content, options)
32
+ resp = @client.send_request(req)
33
+ ResponseConverter::KV.to_mutation_result(resp)
34
+ end
35
+
36
+ def prepend(id, content, options = Options::Prepend::DEFAULT)
37
+ req = @kv_request_generator.prepend_request(id, content, options)
38
+ resp = @client.send_request(req)
39
+ ResponseConverter::KV.to_mutation_result(resp)
40
+ end
41
+
42
+ def increment(id, options = Options::Increment::DEFAULT)
43
+ req = @kv_request_generator.increment_request(id, options)
44
+ resp = @client.send_request(req)
45
+ ResponseConverter::KV.to_counter_result(resp)
46
+ end
47
+
48
+ def decrement(id, options = Options::Decrement::DEFAULT)
49
+ req = @kv_request_generator.decrement_request(id, options)
50
+ resp = @client.send_request(req)
51
+ ResponseConverter::KV.to_counter_result(resp)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,51 @@
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 "scope"
18
+ require_relative "management"
19
+
20
+ module Couchbase
21
+ module Protostellar
22
+ class Bucket
23
+ attr_reader :name
24
+
25
+ def initialize(client, name)
26
+ @client = client
27
+ @name = name
28
+ end
29
+
30
+ def scope(name)
31
+ Scope.new(@client, @name, name)
32
+ end
33
+
34
+ def collection(name)
35
+ default_scope.collection(name)
36
+ end
37
+
38
+ def default_scope
39
+ Scope.new(@client, @name, "_default")
40
+ end
41
+
42
+ def default_collection
43
+ default_scope.collection("_default")
44
+ end
45
+
46
+ def collections
47
+ Management::CollectionManager.new(client: @client, bucket_name: @name)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,99 @@
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 "error_handling"
18
+
19
+ require_relative "generated/routing/v1/routing_services_pb"
20
+ require_relative "generated/kv/v1/kv_services_pb"
21
+ require_relative "generated/query/v1/query_services_pb"
22
+ require_relative "generated/search/v1/search_services_pb"
23
+ require_relative "generated/analytics/v1/analytics_services_pb"
24
+ require_relative "generated/view/v1/view_services_pb"
25
+ require_relative "generated/admin/collection/v1/collection_services_pb"
26
+ require_relative "generated/admin/bucket/v1/bucket_services_pb"
27
+ require_relative "generated/admin/query/v1/query_services_pb"
28
+ require_relative "generated/admin/search/v1/search_services_pb"
29
+
30
+ module Couchbase
31
+ module Protostellar
32
+ # @api private
33
+ class Client
34
+ def initialize(host:, credentials:, channel_args:, call_metadata:, timeouts:)
35
+ @channel = GRPC::Core::Channel.new(host, channel_args, credentials)
36
+ @call_metadata = call_metadata
37
+ @timeouts = timeouts
38
+
39
+ @stubs = {
40
+ routing: Generated::Routing::V1::RoutingService::Stub.new(host, credentials, channel_override: @channel),
41
+ kv: Generated::KV::V1::KvService::Stub.new(host, credentials, channel_override: @channel),
42
+ query: Generated::Query::V1::QueryService::Stub.new(host, credentials, channel_override: @channel),
43
+ search: Generated::Search::V1::SearchService::Stub.new(host, credentials, channel_override: @channel),
44
+ analytics: Generated::Analytics::V1::AnalyticsService::Stub.new(host, credentials, channel_override: @channel),
45
+ view: Generated::View::V1::ViewService::Stub.new(host, credentials, channel_override: @channel),
46
+ bucket_admin: Generated::Admin::Bucket::V1::BucketAdminService::Stub.new(host, credentials, channel_override: @channel),
47
+ collection_admin: Generated::Admin::Collection::V1::CollectionAdminService::Stub.new(host, credentials,
48
+ channel_override: @channel),
49
+ query_admin: Generated::Admin::Query::V1::QueryAdminService::Stub.new(host, credentials, channel_override: @channel),
50
+ search_admin: Generated::Admin::Search::V1::SearchAdminService::Stub.new(host, credentials, channel_override: @channel),
51
+ }
52
+ end
53
+
54
+ def close
55
+ @channel.close
56
+ end
57
+
58
+ def send_request(request)
59
+ request.set_timeout_from_defaults(@timeouts)
60
+ loop do
61
+ resp = @stubs[request.service].public_send(request.rpc, request.proto_request, deadline: request.deadline,
62
+ metadata: @call_metadata)
63
+ return resp unless resp.respond_to?(:next)
64
+
65
+ # Streaming RPC - wrap it in an enumerator that handles any mid-stream errors
66
+ return Enumerator.new do |y|
67
+ loop do
68
+ y << resp.next
69
+ rescue GRPC::BadStatus => e
70
+ request_behaviour = ErrorHandling.handle_grpc_error(e, request)
71
+ raise request_behaviour.error unless request_behaviour.error.nil?
72
+
73
+ unless request_behaviour.retry_duration.nil?
74
+ raise Couchbase::Error::RequestCanceled.new("Error encountered mid-stream",
75
+ request.error_context)
76
+ end
77
+
78
+ next
79
+ end
80
+ end
81
+
82
+ # Simple RPC - just return it
83
+ rescue GRPC::BadStatus => e
84
+ request_behaviour = ErrorHandling.handle_grpc_error(e, request)
85
+
86
+ unless request_behaviour.error.nil? ^ request_behaviour.retry_duration.nil?
87
+ raise Couchbase::Error::CouchbaseError, "Either the error or the retry duration can be set"
88
+ end
89
+ raise request_behaviour.error unless request_behaviour.error.nil?
90
+
91
+ unless request_behaviour.retry_duration.nil?
92
+ sleep(0.001 * request_behaviour.retry_duration)
93
+ next
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,163 @@
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/options"
18
+
19
+ module Couchbase
20
+ module Protostellar
21
+ class Cluster
22
+ VALID_CONNECTION_STRING_PARAMS = [
23
+ "trust_certificate",
24
+ ].freeze
25
+
26
+ attr_reader :client
27
+
28
+ # Connect to the Couchbase cluster
29
+ #
30
+ # @overload connect(connection_string, options)
31
+ # @param [String] connection_string connection string used to locate the Couchbase Cluster
32
+ # @param [Options::Cluster] options custom options when creating the cluster connection
33
+ #
34
+ # @overload connect(connection_string, username, password, options)
35
+ # Shortcut for {PasswordAuthenticator}
36
+ # @param [String] connection_string connection string used to locate the Couchbase Cluster
37
+ # @param [String] username name of the user
38
+ # @param [String] password password of the user
39
+ # @param [Options::Cluster, nil] options custom options when creating the cluster connection
40
+ #
41
+ # @overload connect(configuration, options)
42
+ # @param [Configuration] configuration configuration object
43
+ # @param [Options::Cluster, nil] options custom options when creating the cluster connection
44
+ def self.connect(connection_string_or_config, *args)
45
+ require_relative "connect_options"
46
+ require_relative "bucket"
47
+ require_relative "client"
48
+ require_relative "timeouts"
49
+ require_relative "management/bucket_manager"
50
+ require_relative "request_generator/query"
51
+ require_relative "request_generator/search"
52
+ require_relative "response_converter/query"
53
+ require_relative "response_converter/search"
54
+ require_relative "management/query_index_manager"
55
+
56
+ connection_string = nil
57
+ username = nil
58
+ password = nil
59
+ options = nil
60
+
61
+ if connection_string_or_config.is_a?(Couchbase::Configuration)
62
+ connection_string = connection_string_or_config.connection_string
63
+ username = connection_string_or_config.username
64
+ password = connection_string_or_config.password
65
+ options = args[0] || Couchbase::Options::Cluster.new
66
+ else
67
+ connection_string = connection_string_or_config
68
+ case args[0]
69
+ when String
70
+ username = args[0]
71
+ password = args[1]
72
+ options = args[2] || Couchbase::Options::Cluster.new
73
+ when Couchbase::Options::Cluster
74
+ options = args[0]
75
+ case options.authenticator
76
+ when Couchbase::PasswordAuthenticator
77
+ username = options.authenticator.username
78
+ password = options.authenticator.password
79
+ when Couchbase::CertificateAuthenticator
80
+ raise Couchbase::Error::FeatureNotAvailable,
81
+ "The #{Couchbase::Protostellar::NAME} protocol does not support the CertificateAuthenticator"
82
+ else
83
+ raise ArgumentError, "options must have authenticator configured"
84
+ end
85
+ else
86
+ raise ArgumentError, "unexpected second argument, have to be String or Couchbase::Options::Cluster"
87
+ end
88
+ end
89
+
90
+ raise ArgumentError, "missing username" unless username
91
+ raise ArgumentError, "missing password" unless password
92
+
93
+ params = parse_connection_string_params(connection_string)
94
+ connect_options = ConnectOptions.new(
95
+ username: username,
96
+ password: password,
97
+ timeouts: Protostellar::Timeouts.from_cluster_options(options),
98
+ root_certificates: params.key?("trust_certificate") ? File.read(params["trust_certificate"]) : nil
99
+ )
100
+ new(connection_string.split("://")[1].split("?")[0], connect_options)
101
+ end
102
+
103
+ def self.parse_connection_string_params(connection_string)
104
+ params =
105
+ if connection_string.include?("?")
106
+ connection_string.split("?")[1].split("&").to_h { |p| p.split("=") }
107
+ else
108
+ {}
109
+ end
110
+
111
+ # Show warnings for the connection string parameters that are not supported
112
+ params.each do |k, v|
113
+ warn "Unsupported parameter '#{k}' in connection string (value '#{v}')" unless VALID_CONNECTION_STRING_PARAMS.include?(k)
114
+ end
115
+
116
+ params
117
+ end
118
+
119
+ def disconnect
120
+ @client.close
121
+ end
122
+
123
+ def bucket(name)
124
+ Bucket.new(@client, name)
125
+ end
126
+
127
+ def buckets
128
+ Management::BucketManager.new(@client)
129
+ end
130
+
131
+ def query_indexes
132
+ Management::QueryIndexManager.new(client: @client)
133
+ end
134
+
135
+ def query(statement, options = Couchbase::Options::Query::DEFAULT)
136
+ req = @query_request_generator.query_request(statement, options)
137
+ resps = @client.send_request(req)
138
+ ResponseConverter::Query.to_query_result(resps)
139
+ end
140
+
141
+ def search_query(index_name, query, options = Couchbase::Options::Search::DEFAULT)
142
+ req = @search_request_generator.search_query_request(index_name, query, options)
143
+ resp = @client.send_request(req)
144
+ ResponseConverter::Search.to_search_result(resp, options)
145
+ end
146
+
147
+ private
148
+
149
+ def initialize(host, options = ConnectOptions.new)
150
+ @client = Client.new(
151
+ host: host.include?(":") ? host : "#{host}:18098",
152
+ credentials: options.grpc_credentials,
153
+ channel_args: options.grpc_channel_args,
154
+ call_metadata: options.grpc_call_metadata,
155
+ timeouts: options.timeouts
156
+ )
157
+
158
+ @query_request_generator = RequestGenerator::Query.new
159
+ @search_request_generator = RequestGenerator::Search.new
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,152 @@
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/options"
18
+ require "couchbase/errors"
19
+ require "couchbase/collection_options"
20
+
21
+ require_relative "request_generator/kv"
22
+ require_relative "response_converter/kv"
23
+ require_relative "binary_collection"
24
+ require_relative "management/collection_query_index_manager"
25
+
26
+ require "google/protobuf/well_known_types"
27
+
28
+ module Couchbase
29
+ module Protostellar
30
+ class Collection
31
+ attr_reader :bucket_name
32
+ attr_reader :scope_name
33
+ attr_reader :name
34
+
35
+ def initialize(client, bucket_name, scope_name, name)
36
+ @client = client
37
+ @bucket_name = bucket_name
38
+ @scope_name = scope_name
39
+ @name = name
40
+ @kv_request_generator = RequestGenerator::KV.new(@bucket_name, @scope_name, @name)
41
+ end
42
+
43
+ def binary
44
+ BinaryCollection.new(self)
45
+ end
46
+
47
+ def query_indexes
48
+ Management::CollectionQueryIndexManager.new(
49
+ client: @client,
50
+ bucket_name: @bucket_name,
51
+ scope_name: @scope_name,
52
+ collection_name: @name
53
+ )
54
+ end
55
+
56
+ def get(id, options = Couchbase::Options::Get::DEFAULT)
57
+ req = @kv_request_generator.get_request(id, options)
58
+ resp = @client.send_request(req)
59
+ ResponseConverter::KV.to_get_result(resp, options)
60
+ end
61
+
62
+ def get_and_touch(id, expiry, options = Couchbase::Options::GetAndTouch::DEFAULT)
63
+ req = @kv_request_generator.get_and_touch_request(id, expiry, options)
64
+ resp = @client.send_request(req)
65
+ ResponseConverter::KV.to_get_result(resp, options)
66
+ end
67
+
68
+ def get_and_lock(id, lock_time, options = Couchbase::Options::GetAndLock::DEFAULT)
69
+ req = @kv_request_generator.get_and_lock_request(id, lock_time, options)
70
+ resp = @client.send_request(req)
71
+ ResponseConverter::KV.to_get_result(resp, options)
72
+ end
73
+
74
+ def unlock(id, cas, options = Couchbase::Options::Unlock::DEFAULT)
75
+ req = @kv_request_generator.unlock_request(id, cas, options)
76
+ @client.send_request(req)
77
+ end
78
+
79
+ def touch(id, expiry, options = Couchbase::Options::Touch::DEFAULT)
80
+ req = @kv_request_generator.touch_request(id, expiry, options)
81
+ resp = @client.send_request(req)
82
+ ResponseConverter::KV.to_mutation_result(resp)
83
+ end
84
+
85
+ def upsert(id, content, options = Couchbase::Options::Upsert::DEFAULT)
86
+ req = @kv_request_generator.upsert_request(id, content, options)
87
+ resp = @client.send_request(req)
88
+ ResponseConverter::KV.to_mutation_result(resp)
89
+ end
90
+
91
+ def remove(id, options = Couchbase::Options::Remove::DEFAULT)
92
+ req = @kv_request_generator.remove_request(id, options)
93
+ resp = @client.send_request(req)
94
+ ResponseConverter::KV.to_mutation_result(resp)
95
+ end
96
+
97
+ def insert(id, content, options = Couchbase::Options::Insert::DEFAULT)
98
+ req = @kv_request_generator.insert_request(id, content, options)
99
+ resp = @client.send_request(req)
100
+ ResponseConverter::KV.to_mutation_result(resp)
101
+ end
102
+
103
+ def replace(id, content, options = Couchbase::Options::Replace::DEFAULT)
104
+ req = @kv_request_generator.replace_request(id, content, options)
105
+ resp = @client.send_request(req)
106
+ ResponseConverter::KV.to_mutation_result(resp)
107
+ end
108
+
109
+ def exists(id, options = Couchbase::Options::Exists::DEFAULT)
110
+ req = @kv_request_generator.exists_request(id, options)
111
+ resp = @client.send_request(req)
112
+ ResponseConverter::KV.to_exists_result(resp)
113
+ end
114
+
115
+ def lookup_in(id, specs, options = Couchbase::Options::LookupIn::DEFAULT)
116
+ req = @kv_request_generator.lookup_in_request(id, specs, options)
117
+ resp = @client.send_request(req)
118
+ ResponseConverter::KV.to_lookup_in_result(resp, specs, options, req)
119
+ end
120
+
121
+ def lookup_in_any_replica(_id, _specs, _options = Couchbase::Options::LookupInAnyReplica::DEFAULT)
122
+ raise Couchbase::Error::FeatureNotAvailable, "The #{Protostellar::NAME} protocol does not support lookup in any replica"
123
+ end
124
+
125
+ def lookup_in_all_replicas(_id, _specs, _options = Couchbase::Options::LookupInAllReplicas::DEFAULT)
126
+ raise Couchbase::Error::FeatureNotAvailable, "The #{Protostellar::NAME} protocol does not support lookup in all replicas"
127
+ end
128
+
129
+ def mutate_in(id, specs, options = Couchbase::Options::MutateIn::DEFAULT)
130
+ req = @kv_request_generator.mutate_in_request(id, specs, options)
131
+ resp = @client.send_request(req)
132
+ ResponseConverter::KV.to_mutate_in_result(resp, specs, options)
133
+ end
134
+
135
+ def get_any_replica(id, options = Couchbase::Options::GetAnyReplica::DEFAULT)
136
+ req = @kv_request_generator.get_any_replica_request(id, options)
137
+ resp = @client.send_request(req)
138
+ ResponseConverter::KV.to_get_any_replica_result(resp, options)
139
+ end
140
+
141
+ def get_all_replicas(id, options = Couchbase::Options::GetAllReplicas::DEFAULT)
142
+ req = @kv_request_generator.get_all_replicas_request(id, options)
143
+ resp = @client.send_request(req)
144
+ ResponseConverter::KV.to_get_all_replicas_result(resp, options)
145
+ end
146
+
147
+ def scan(_scan_type, _options = Options::Scan::DEFAULT)
148
+ raise Couchbase::Error::FeatureNotAvailable, "The #{Protostellar::NAME} protocol does not support KV scans"
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,63 @@
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 "base64"
18
+
19
+ require_relative "timeouts"
20
+
21
+ module Couchbase
22
+ module Protostellar
23
+ # @api private
24
+ class ConnectOptions
25
+ attr_reader :timeouts
26
+
27
+ def initialize(timeouts: Timeouts.new,
28
+ username: nil,
29
+ password: nil,
30
+ root_certificates: nil,
31
+ client_certificate: nil,
32
+ private_key: nil)
33
+ @timeouts = timeouts
34
+ @username = username
35
+ @password = password
36
+ @root_certificates = root_certificates
37
+ @client_certificate = client_certificate
38
+ @private_key = private_key
39
+ end
40
+
41
+ def grpc_credentials
42
+ if @root_certificates
43
+ GRPC::Core::ChannelCredentials.new(@root_certificates, @client_certificate, @private_key)
44
+ else
45
+ # Use default certificates
46
+ GRPC::Core::ChannelCredentials.new(nil, @client_certificate, @private_key)
47
+ end
48
+ end
49
+
50
+ def grpc_call_metadata
51
+ if @username && @password
52
+ {"authorization" => ["Basic #{Base64.strict_encode64("#{@username}:#{@password}")}"]}
53
+ else
54
+ {}
55
+ end
56
+ end
57
+
58
+ def grpc_channel_args
59
+ {}
60
+ end
61
+ end
62
+ end
63
+ end