Class: Iriq::Storage::Memory
- Inherits:
-
Object
- Object
- Iriq::Storage::Memory
- Defined in:
- lib/iriq/storage/memory.rb
Overview
Memory is the canonical backend — every other backend either wraps it (Json) or implements the same surface against an external store (Sqlite).
The contract is small enough to enumerate up top:
increment_host(host)
increment_path_length(length)
increment_raw_shape(shape)
increment_fingerprint(shape)
observe_position(host, prefix, value, type)
add_to_cluster(key, host, scheme, shape, identifier)
host_counts / path_length_counts / raw_shape_counts / fingerprint_counts
position_stats(host, prefix)
clusters / cluster_size
transaction { ... } # backends may batch within
flush # commit pending writes (no-op for Memory)
close # release resources
Direct Known Subclasses
Instance Attribute Summary collapse
-
#max_values_per_position ⇒ Object
readonly
Returns the value of attribute max_values_per_position.
Instance Method Summary collapse
- #add_to_cluster(key, host, scheme, shape, identifier) ⇒ Object
- #batch ⇒ Object
- #close ⇒ Object
- #cluster_size ⇒ Object
- #clusters ⇒ Object
- #each_position_stats(&block) ⇒ Object
- #fingerprint_counts ⇒ Object
- #flush ⇒ Object
-
#host_counts ⇒ Object
— Reads ————————————————————.
- #increment_fingerprint(shape) ⇒ Object
-
#increment_host(host) ⇒ Object
— Increments ——————————————————-.
- #increment_path_length(length) ⇒ Object
- #increment_raw_shape(shape) ⇒ Object
-
#initialize(classifier: SegmentClassifier::DEFAULT, max_values_per_position: PositionStats::DEFAULT_MAX_VALUES) ⇒ Memory
constructor
A new instance of Memory.
-
#load_dump!(h) ⇒ Object
— Bulk load (used by JSON backend) ——————————–.
- #observe_position(host, prefix, value, type) ⇒ Object
-
#path ⇒ Object
Path of the underlying file, if any.
- #path_length_counts ⇒ Object
- #position_stats(host, prefix) ⇒ Object
- #raw_shape_counts ⇒ Object
-
#save(path = nil) ⇒ Object
No-op for in-memory; subclasses override.
- #to_dump ⇒ Object
- #transaction {|_self| ... } ⇒ Object
Constructor Details
#initialize(classifier: SegmentClassifier::DEFAULT, max_values_per_position: PositionStats::DEFAULT_MAX_VALUES) ⇒ Memory
Returns a new instance of Memory.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/iriq/storage/memory.rb', line 29 def initialize(classifier: SegmentClassifier::DEFAULT, max_values_per_position: PositionStats::DEFAULT_MAX_VALUES) @classifier = classifier @max_values_per_position = max_values_per_position @host_counts = Hash.new(0) @path_length_counts = Hash.new(0) @raw_shape_counts = Hash.new(0) @fingerprint_counts = Hash.new(0) @position_stats = {} @clusters = {} end |
Instance Attribute Details
#max_values_per_position ⇒ Object (readonly)
Returns the value of attribute max_values_per_position.
23 24 25 |
# File 'lib/iriq/storage/memory.rb', line 23 def max_values_per_position @max_values_per_position end |
Instance Method Details
#add_to_cluster(key, host, scheme, shape, identifier) ⇒ Object
78 79 80 81 82 |
# File 'lib/iriq/storage/memory.rb', line 78 def add_to_cluster(key, host, scheme, shape, identifier) cluster = @clusters[key] ||= Cluster.new(key: key, host: host, scheme: scheme, shape: shape) cluster.add(identifier) cluster end |
#batch ⇒ Object
45 46 47 |
# File 'lib/iriq/storage/memory.rb', line 45 def batch yield end |
#close ⇒ Object
50 |
# File 'lib/iriq/storage/memory.rb', line 50 def close; end |
#cluster_size ⇒ Object
103 104 105 |
# File 'lib/iriq/storage/memory.rb', line 103 def cluster_size @clusters.size end |
#clusters ⇒ Object
99 100 101 |
# File 'lib/iriq/storage/memory.rb', line 99 def clusters @clusters.values end |
#each_position_stats(&block) ⇒ Object
95 96 97 |
# File 'lib/iriq/storage/memory.rb', line 95 def each_position_stats(&block) @position_stats.each(&block) end |
#fingerprint_counts ⇒ Object
89 |
# File 'lib/iriq/storage/memory.rb', line 89 def fingerprint_counts; @fingerprint_counts; end |
#flush ⇒ Object
49 |
# File 'lib/iriq/storage/memory.rb', line 49 def flush; end |
#host_counts ⇒ Object
— Reads ————————————————————
86 |
# File 'lib/iriq/storage/memory.rb', line 86 def host_counts; @host_counts; end |
#increment_fingerprint(shape) ⇒ Object
69 70 71 |
# File 'lib/iriq/storage/memory.rb', line 69 def increment_fingerprint(shape) @fingerprint_counts[shape] += 1 end |
#increment_host(host) ⇒ Object
— Increments ——————————————————-
57 58 59 |
# File 'lib/iriq/storage/memory.rb', line 57 def increment_host(host) @host_counts[host] += 1 if host end |
#increment_path_length(length) ⇒ Object
61 62 63 |
# File 'lib/iriq/storage/memory.rb', line 61 def increment_path_length(length) @path_length_counts[length] += 1 end |
#increment_raw_shape(shape) ⇒ Object
65 66 67 |
# File 'lib/iriq/storage/memory.rb', line 65 def increment_raw_shape(shape) @raw_shape_counts[shape] += 1 end |
#load_dump!(h) ⇒ Object
— Bulk load (used by JSON backend) ——————————–
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/iriq/storage/memory.rb', line 109 def load_dump!(h) @host_counts = Hash.new(0).merge(h["host_counts"]) @path_length_counts = Hash.new(0).merge(h["path_length_counts"].transform_keys(&:to_i)) @raw_shape_counts = Hash.new(0).merge(h["raw_shape_counts"]) @fingerprint_counts = Hash.new(0).merge(h["fingerprint_counts"]) @max_values_per_position = h.fetch("max_values_per_position", PositionStats::DEFAULT_MAX_VALUES) @position_stats = h["position_stats"].each_with_object({}) do |(host, prefix, sdump), acc| acc[[host, prefix]] = PositionStats.from_dump(sdump) end cdump = h.fetch("clusterer", { "clusters" => {} }) @clusters = cdump["clusters"].transform_values { |c| Cluster.from_dump(c) } self end |
#observe_position(host, prefix, value, type) ⇒ Object
73 74 75 76 |
# File 'lib/iriq/storage/memory.rb', line 73 def observe_position(host, prefix, value, type) stats = @position_stats[[host, prefix]] ||= PositionStats.new(max_values: @max_values_per_position) stats.observe(value, type) end |
#path ⇒ Object
Path of the underlying file, if any. Memory backends are unpathed; Json/Sqlite override.
27 |
# File 'lib/iriq/storage/memory.rb', line 27 def path; nil; end |
#path_length_counts ⇒ Object
87 |
# File 'lib/iriq/storage/memory.rb', line 87 def path_length_counts; @path_length_counts; end |
#position_stats(host, prefix) ⇒ Object
91 92 93 |
# File 'lib/iriq/storage/memory.rb', line 91 def position_stats(host, prefix) @position_stats[[host, prefix]] end |
#raw_shape_counts ⇒ Object
88 |
# File 'lib/iriq/storage/memory.rb', line 88 def raw_shape_counts; @raw_shape_counts; end |
#save(path = nil) ⇒ Object
No-op for in-memory; subclasses override.
53 |
# File 'lib/iriq/storage/memory.rb', line 53 def save(path = nil); end |
#to_dump ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/iriq/storage/memory.rb', line 123 def to_dump { "host_counts" => @host_counts, "path_length_counts" => @path_length_counts.transform_keys(&:to_s), "raw_shape_counts" => @raw_shape_counts, "fingerprint_counts" => @fingerprint_counts, "max_values_per_position" => @max_values_per_position, "position_stats" => @position_stats.map { |(host, prefix), s| [host, prefix, s.dump] }, "clusterer" => { "clusters" => @clusters.transform_values(&:dump), }, } end |
#transaction {|_self| ... } ⇒ Object
41 42 43 |
# File 'lib/iriq/storage/memory.rb', line 41 def transaction yield self end |