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

Commit

Permalink
Merge pull request #79 from cipherstash/more-refactorings
Browse files Browse the repository at this point in the history
Introduce new index DSL
  • Loading branch information
freshtonic authored Sep 13, 2022
2 parents 8c933b8 + dbd2132 commit 9314d56
Show file tree
Hide file tree
Showing 38 changed files with 731 additions and 536 deletions.
6 changes: 4 additions & 2 deletions lib/active_stash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
require "active_stash/collection_proxy"
require "active_stash/schema_builder"
require "active_stash/query_builder"
require "active_stash/reflection"
require "active_stash/model_reflection"
require "active_stash/relation"
require "active_stash/index"
require "active_stash/stash_indexes"
require "active_stash/index_dsl"
require "active_stash/index_lookup"
require "active_stash/finalized_index_config"
require "active_stash/logger"
require "active_stash/railtie" if defined?(Rails::Railtie)

Expand Down
21 changes: 16 additions & 5 deletions lib/active_stash/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ def collection(skip_consistency_check: false)
end

def consistency_check!(collection)
reflection = ActiveStash::Reflection.new(collection)

if reflection.indexes.size != stash_indexes.all.size
if indexes(collection).size != stash_indexes.indexes.size
raise CollectionDivergedError, name: collection_name
end

stash_indexes.all.each do |target|
if !reflection.has_index?(target)
stash_indexes.indexes.each do |target|
if !has_index?(collection, target)
raise CollectionDivergedError, name: collection_name
end
end
Expand All @@ -67,6 +65,19 @@ def client
)
end

def indexes(collection)
collection.instance_variable_get("@indexes")
end

def has_index?(collection, target)
self.indexes(collection).any? do |index|
mapping = index.instance_variable_get("@settings")["mapping"]
meta = index.instance_variable_get("@settings")["meta"]
# TODO: Check other fields, too
target.name == meta["$indexName"]
end
end

def logger
ActiveStash::Logger
end
Expand Down
14 changes: 14 additions & 0 deletions lib/active_stash/finalized_index_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module ActiveStash
class FinalizedIndexConfig
attr_reader :indexes

def initialize(indexes, callback_registration_handlers)
@indexes = ActiveStash::IndexLookup.new(indexes)
@callback_registration_handlers = callback_registration_handlers
end

def register_callbacks
@callback_registration_handlers.each(&:call)
end
end
end
47 changes: 37 additions & 10 deletions lib/active_stash/index.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
module ActiveStash
class Index
attr_accessor :type, :valid_ops, :options
attr_accessor :type, :valid_ops, :options, :name, :unique
attr_reader :field

RANGE_TYPES = [:timestamp, :date, :datetime, :float, :decimal, :integer]
# TODO: boolean supports range
# TODO: string and text support ranges

# These index types support uniqueness.
# Note that when using :auto indexing with :unique, only the generated
# :exact index will have the uniqueness validation.
INDEX_TYPES_WITH_UNIQUE_SUPPORT = [:exact, :range]

FIELD_TYPE_TO_SUPPORTED_INDEX_TYPES = {
:timestamp => [:range],
:date => [:range],
:datetime => [:range],
:float => [:range],
:decimal => [:range],
:integer => [:range],
:string => [:range, :exact, :match],
:text => [:range, :exact, :match],
:boolean => [:range],
:uuid => [:range],
}

RANGE_OPS = [:lt, :lte, :gt, :gte, :eq, :between]
EXACT_OPS = [:eq]
Expand All @@ -29,7 +48,23 @@ def initialize(field, type, **opts)
@field = field
@type = type
@options = opts
@name = @options[:name] || @field.to_s
@options.delete(:name)
@valid_ops = INDEX_TYPE_TO_OPS[type]
@unique = @options[:unique] || false
@options.delete(:unique)
end

def self.valid_index_type_for_field_type?(index_type, field_type)
applicable_index_types(field_type).include?(index_type)
end

def self.applicable_index_types(type)
FIELD_TYPE_TO_SUPPORTED_INDEX_TYPES[type] || []
end

def make_unique!
@unique = true
end

def self.exact(field, **opts)
Expand All @@ -52,14 +87,6 @@ def self.dynamic_match(field, **opts)
new(field, :match, name: "#{field}_dynamic_match", **opts)
end

def name
@options[:name] || @field.to_s
end

def unique
@options[:unique]
end

def valid_op?(op)
@valid_ops.include?(op)
end
Expand Down
Loading

0 comments on commit 9314d56

Please sign in to comment.