Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit 9dc9260

Browse files
authored
Merge pull request #19 from cipherstash/unique-index-option
Add ability to specify unique constraint on exact and range indexes
2 parents f811f2a + 8e246b6 commit 9dc9260

14 files changed

Lines changed: 126 additions & 3 deletions

cipherstash-client.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
3232
s.add_runtime_dependency "aws-sdk-core", "~> 3.0"
3333
s.add_runtime_dependency "aws-sdk-kms", "~> 1.0"
3434
s.add_runtime_dependency 'cbor', '~> 0.5.9.6'
35-
s.add_runtime_dependency "cipherstash-grpc", "= 0.20220706.5"
35+
s.add_runtime_dependency "cipherstash-grpc", "= 0.20220801.1"
3636
s.add_runtime_dependency "enveloperb", "~> 0.0"
3737
s.add_runtime_dependency "launchy", "~> 2.5"
3838
s.add_runtime_dependency "ore-rs", "~> 0.0"

lib/cipherstash/client.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def collections
145145
#
146146
# @raise [CipherStash::Client::Error::DecryptionFailure] if there was a problem decrypting the naming key.
147147
#
148+
# @raise [CipherStash::Client::Error::InvalidSchemaError] if there was a problem with the format of the schema.
149+
#
148150
def create_collection(name, schema)
149151
@metrics.measure_client_call("create_collection") do
150152
metadata = {

lib/cipherstash/client/rpc.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def create_collection(name, metadata, indexes)
7979
id: blob_from_uuid(idx[:meta]["$indexId"]),
8080
settings: encrypt_blob(idx.to_cbor, "createCollection"),
8181
type: index_type(idx[:mapping]["kind"]),
82+
unique: idx[:mapping]["unique"]
8283
}
8384
end
8485
)
@@ -103,6 +104,7 @@ def migrate_collection(name, metadata, indexes, from_schema_version)
103104
id: blob_from_uuid(idx[:meta]["$indexId"]),
104105
settings: encrypt_blob(idx.to_cbor, "migrateCollection"),
105106
type: index_type(idx[:mapping]["kind"]),
107+
unique: idx[:mapping]["unique"]
106108
}
107109
end,
108110
fromSchemaVersion: from_schema_version
@@ -158,6 +160,8 @@ def put(collection, id, record, vectors)
158160
uuid_from_blob(id)
159161
rescue ::GRPC::NotFound
160162
raise Error::RecordPutFailure, "Collection '#{collection.name}' not found"
163+
rescue ::GRPC::InvalidArgument => ex
164+
raise Error::RecordPutFailure, "Error while putting record into collection '#{collection.name}': #{ex.message} (#{ex.class})"
161165
rescue ::GRPC::BadStatus => ex
162166
raise Error::RecordPutFailure, "Error while putting records into collection '#{collection.name}': #{ex.message} (#{ex.class})"
163167
end
@@ -278,7 +282,11 @@ def put_stream(collection, records)
278282

279283
res.numInserted
280284
rescue ::GRPC::NotFound
281-
raise Error::RecordDeleteFailure, "Collection '#{collection.name}' not found"
285+
raise Error::RecordPutFailure, "Collection '#{collection.name}' not found"
286+
rescue ::GRPC::InvalidArgument => ex
287+
raise Error::RecordPutFailure, "Error while putting records into collection '#{collection.name}': #{ex.message} (#{ex.class})"
288+
rescue ::GRPC::BadStatus => ex
289+
raise Error::RecordPutFailure, "Error while putting records into collection '#{collection.name}': #{ex.message} (#{ex.class})"
282290
end
283291

284292
def delete(collection, id)

lib/cipherstash/index.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def generate(id, settings, schema_versions)
4343
def settings(name, base_settings, schema)
4444
subclass = subclass_from_kind(base_settings["kind"])
4545

46+
if base_settings.key?("unique") && !subclass.uniqueness_supported?
47+
raise CipherStash::Client::Error::InvalidSchemaError, "Unique constraint not allowed on type #{base_settings["kind"].inspect}"
48+
end
49+
4650
{
4751
meta: subclass.meta(name),
4852
mapping: subclass.mapping(base_settings, schema),
@@ -131,6 +135,14 @@ def orderable?
131135
# that do
132136
false
133137
end
138+
139+
# Does this index support a unique constraint?
140+
#
141+
# @return [bool]
142+
#
143+
def uniqueness_supported?
144+
false
145+
end
134146

135147
# Examine the given record and send back index vectors.
136148
#

lib/cipherstash/index/dynamic_filter_match.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def self.meta(name)
2626
def orderable?
2727
false
2828
end
29+
30+
def self.uniqueness_supported?
31+
false
32+
end
2933

3034
def analyze(uuid, record)
3135
blid = blob_from_uuid(uuid)

lib/cipherstash/index/dynamic_ore_match.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def orderable?
2222
false
2323
end
2424

25+
def self.uniqueness_supported?
26+
false
27+
end
28+
2529
def analyze(uuid, record)
2630
blid = blob_from_uuid(uuid)
2731
raw_terms = collect_string_fields(record).map(&:last)

lib/cipherstash/index/exact.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def orderable?
2626
false
2727
end
2828

29+
def self.uniqueness_supported?
30+
true
31+
end
32+
2933
def analyze(uuid, record)
3034
blid = blob_from_uuid(uuid)
3135

lib/cipherstash/index/field_dynamic_exact.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def self.meta(name)
1818
self.ore_meta(name)
1919
end
2020

21+
def self.uniqueness_supported?
22+
false
23+
end
24+
2125
def analyze(uuid, record)
2226
blid = blob_from_uuid(uuid)
2327

lib/cipherstash/index/field_dynamic_filter_match.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def self.meta(name)
2323
self.filter_meta(name)
2424
end
2525

26+
def self.uniqueness_supported?
27+
false
28+
end
29+
2630
def analyze(uuid, record)
2731
blid = blob_from_uuid(uuid)
2832

lib/cipherstash/index/field_dynamic_ore_match.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def self.meta(name)
1818
self.ore_meta(name)
1919
end
2020

21+
def self.uniqueness_supported?
22+
false
23+
end
24+
2125
def analyze(uuid, record)
2226
blid = blob_from_uuid(uuid)
2327

0 commit comments

Comments
 (0)