Class: Iriq::Storage::Json
Overview
Json wraps Memory with load-from-file at open and save-to-file at close. Same JSON shape as the pre-Storage release, so files round-trip across versions.
Constant Summary
Constants inherited from Memory
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Attributes inherited from Memory
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path: nil, **opts) ⇒ Json
constructor
A new instance of Json.
-
#load!(path) ⇒ Object
Refuses (Iriq::CorpusError) anything that isn't a corpus dump, so a mistyped --corpus path can't overwrite an unrelated JSON file: it must be an object with at least one corpus key, or
{}. -
#save(path = nil) ⇒ Object
save writes atomically (unique tmp + rename).
Methods inherited from Memory
#activated_recognizer_count, #add_to_cluster, #batch, #begin_rebuild, #clear_materialized_views, #close, #cluster_for, #cluster_size, #clusters, #discard_rebuild, #each_activated_recognizer, #each_observed_iri, #each_observed_iri_since, #each_position_stats, #fingerprint_counts, #flush, #host_counts, #increment_fingerprint, #increment_host, #increment_path_length, #increment_raw_shape, #install_rebuild, #load_dump!, #observe_position, #observed_iri_count, #param_stats, #path_length_counts, #position_evidence, #position_stats, #raw_shape_counts, #record_activated_recognizer, #record_observation, #to_dump, #transaction, #turn_over?
Constructor Details
#initialize(path: nil, **opts) ⇒ Json
11 12 13 14 |
# File 'lib/iriq/storage/json.rb', line 11 def initialize(path: nil, **opts) super(**opts) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/iriq/storage/json.rb', line 9 def path @path end |
Class Method Details
.open(path, **opts) ⇒ Object
16 17 18 19 20 |
# File 'lib/iriq/storage/json.rb', line 16 def self.open(path, **opts) s = new(path: path, **opts) s.load!(path) if File.exist?(path) && File.size(path).positive? s end |
Instance Method Details
#load!(path) ⇒ Object
Refuses (Iriq::CorpusError) anything that isn't a corpus dump, so a
mistyped --corpus path can't overwrite an unrelated JSON file: it must
be an object with at least one corpus key, or {}.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/iriq/storage/json.rb', line 25 def load!(path) data = begin File.read(path) rescue SystemCallError => e raise CorpusError, "corpus #{path}: #{Iriq.(e)}" end return self if data.empty? dump = begin JSON.parse(data) rescue JSON::ParserError raise CorpusError, "corpus #{path}: not valid JSON" end unless dump.is_a?(Hash) && (dump.empty? || dump.keys.intersect?(DUMP_KEYS)) raise CorpusError, "corpus #{path}: not an iriq corpus (no corpus keys at the top level)" end load_dump!(dump) @path = path self end |
#save(path = nil) ⇒ Object
save writes atomically (unique tmp + rename). Defaults to the path passed at open(); pass an explicit path to write elsewhere.
49 50 51 52 53 54 |
# File 'lib/iriq/storage/json.rb', line 49 def save(path = nil) target = path || @path raise ArgumentError, "no path provided" unless target Storage.write_atomically(target, JSON.generate(to_dump)) end |