Skip to content

Commit

Permalink
Merge pull request #602 from toptal/specification-storage
Browse files Browse the repository at this point in the history
Specification stored as json
  • Loading branch information
pyromaniac authored Nov 6, 2017
2 parents 1509dde + 8c2e70c commit 1a13f75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/chewy/index/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(index)
def lock!
Chewy::Stash::Specification.import!([
id: @index.derivable_name,
value: current.to_json
specification: current
], journal: false)
end

Expand All @@ -34,7 +34,9 @@ def lock!
# @return [Hash] hash produced with JSON parser
def locked
filter = {ids: {values: [@index.derivable_name]}}
JSON.parse(Chewy::Stash::Specification.filter(filter).first.try!(:value) || '{}')
document = Chewy::Stash::Specification.filter(filter).first
return {} unless document
document.specification || JSON.parse(document.value || '{}')
end

# Simply returns `Chewy::Index.specification_hash`, but
Expand Down
1 change: 1 addition & 0 deletions lib/chewy/stash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Stash < Chewy::Index
default_import_options journal: false

field :value, index: 'no'
field :specification, type: 'object', enabled: false
end

define_type :journal do # rubocop:disable Metrics/BlockLength
Expand Down
15 changes: 12 additions & 3 deletions spec/chewy/index/specification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
'_type' => 'specification',
'_id' => 'places',
'_score' => 1.0,
'_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"name":{"type":"string"}}}}}'}
'_source' => {'specification' => {
'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
}, 'value' => nil}
}])
end

Expand All @@ -72,13 +75,19 @@
'_type' => 'specification',
'_id' => 'places',
'_score' => 1.0,
'_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"name":{"type":"string"}}}}}'}
'_source' => {'specification' => {
'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
}, 'value' => nil}
}, {
'_index' => 'chewy_stash',
'_type' => 'specification',
'_id' => 'namespace/cities',
'_score' => 1.0,
'_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"population":{"type":"integer"}}}}}'}
'_source' => {'specification' => {
'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
'mappings' => {'city' => {'properties' => {'population' => {'type' => 'integer'}}}}
}, 'value' => nil}
}])
end
end
Expand Down

0 comments on commit 1a13f75

Please sign in to comment.