Class: Garcon::Stash::Store
- Includes:
- Enumerable
- Defined in:
- lib/garcon/stash/store.rb
Overview
Stash::Store contains the public api for Stash. It includes Enumerable for functional goodies like map, each, reduce and friends.
Instance Attribute Summary collapse
-
#default(key = nil) ⇒ Object
Return default value belonging to key.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get)
Retrieve a value at key from the stash.
-
#[]=(key, value) ⇒ Object
(also: #set)
Set a key in the stash to be written at some future date.
-
#bytesize ⇒ Fixnum
Utility method that will return the size of the stash in bytes, useful for determining when to compact.
-
#clear ⇒ Stash
Remove all keys and values from the stash.
-
#close ⇒ Object
Close the stash for reading and writing.
-
#closed? ⇒ Boolean
Check to see if we’ve already closed the stash.
-
#compact ⇒ Stash
Compact the stash to remove stale commits and reduce the file size.
-
#delete(key) ⇒ Object
Delete a key from the stash.
-
#delete!(key) ⇒ Object
Immediately delete the key on disk.
-
#each {|key, value| ... } ⇒ Object
Iterate over the key, value pairs in the stash.
-
#empty? ⇒ Boolean
Return true if stash is empty.
-
#file ⇒ String
Stash store file name.
-
#flush ⇒ Stash
Flush all changes to disk.
-
#has_key?(key) ⇒ Boolean
(also: #key?, #include?, #member?)
Does this stash have this key?.
-
#has_value?(value) ⇒ Boolean
(also: #value?)
Does this stash have this value?.
-
#initialize(file, opts = {}) {|key| ... } ⇒ Store
constructor
Create a new Stash::Store.
-
#keys ⇒ Array<String>
Return the keys in the stash.
-
#load ⇒ Stash
(also: #sunrise)
Sync the stash with what is on disk, by first flushing changes, and then loading the new records if necessary.
-
#lock {|stash| ... } ⇒ Object
Lock the stash for an exclusive commit across processes and threads.
-
#logsize ⇒ Fixnum
Counter of how many records are in the journal.
-
#set!(key, value) ⇒ Object
Flushes data immediately to disk.
-
#size ⇒ Fixnum
(also: #length)
Return the number of stored items.
-
#synchronize {|stash| ... } ⇒ Object
Synchronize access to the stash from multiple threads.
-
#update(hash) ⇒ Stash
Update stash with hash (fast batch update).
-
#update!(hash) ⇒ Stash
Updata stash and flush data to disk.
Methods included from Enumerable
Constructor Details
#initialize(file, opts = {}) {|key| ... } ⇒ Store
Create a new Stash::Store. The second argument is the default value to store when accessing a previously unset key, this follows the Hash standard.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/garcon/stash/store.rb', line 79 def initialize(file, opts = {}, &block) opts = { serializer: opts.fetch(:serializer, Serializer::Default), format: opts.fetch(:format, Format), default: opts.fetch(:default, nil) } @format = (opts[:format]).new @serializer = (opts[:serializer]).new @table = Hash.new(&method(:hash_default)) @journal = Journal.new(file, @format, @serializer) do |record| if !record @table.clear elsif record.size == 1 @table.delete(record.first) else @table[record.first] = @serializer.load(record.last) end end @default = block ? block : opts[:default] @mutex = Mutex.new @@stashs_mutex.synchronize { @@stashs << self } end |
Instance Attribute Details
#default(key = nil) ⇒ Object
Return default value belonging to key.
120 121 122 |
# File 'lib/garcon/stash/store.rb', line 120 def default(key = nil) @table.default(@serializer.key_for(key)) end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
Retrieve a value at key from the stash. If the default value was specified when this stash was created, that value will be set and returned. Aliased as ‘#get`.
134 135 136 |
# File 'lib/garcon/stash/store.rb', line 134 def [](key) @table[@serializer.key_for(key)] end |
#[]=(key, value) ⇒ Object Also known as: set
Set a key in the stash to be written at some future date. If the data needs to be persisted immediately, call ‘#store.set(key, value, true)`.
151 152 153 154 155 |
# File 'lib/garcon/stash/store.rb', line 151 def []=(key, value) key = @serializer.key_for(key) @journal << [key, value] @table[key] = value end |
#bytesize ⇒ Fixnum
Utility method that will return the size of the stash in bytes, useful for determining when to compact.
271 272 273 |
# File 'lib/garcon/stash/store.rb', line 271 def bytesize @journal.bytesize end |
#clear ⇒ Stash
Remove all keys and values from the stash.
364 365 366 367 368 |
# File 'lib/garcon/stash/store.rb', line 364 def clear @table.clear @journal.clear self end |
#close ⇒ Object
Close the stash for reading and writing.
383 384 385 386 387 |
# File 'lib/garcon/stash/store.rb', line 383 def close @journal.close @@stashs_mutex.synchronize { @@stashs.delete(self) } nil end |
#closed? ⇒ Boolean
Check to see if we’ve already closed the stash.
393 394 395 |
# File 'lib/garcon/stash/store.rb', line 393 def closed? @journal.closed? end |
#compact ⇒ Stash
Compact the stash to remove stale commits and reduce the file size.
374 375 376 377 |
# File 'lib/garcon/stash/store.rb', line 374 def compact @journal.compact { @table } self end |
#delete(key) ⇒ Object
Delete a key from the stash.
183 184 185 186 187 |
# File 'lib/garcon/stash/store.rb', line 183 def delete(key) key = @serializer.key_for(key) @journal << [key] @table.delete(key) end |
#delete!(key) ⇒ Object
Immediately delete the key on disk.
197 198 199 200 201 |
# File 'lib/garcon/stash/store.rb', line 197 def delete!(key) value = delete(key) flush value end |
#each {|key, value| ... } ⇒ Object
Iterate over the key, value pairs in the stash.
300 301 302 |
# File 'lib/garcon/stash/store.rb', line 300 def each(&block) @table.each(&block) end |
#empty? ⇒ Boolean
Return true if stash is empty.
287 288 289 |
# File 'lib/garcon/stash/store.rb', line 287 def empty? @table.empty? end |
#file ⇒ String
Stash store file name.
108 109 110 |
# File 'lib/garcon/stash/store.rb', line 108 def file @journal.file end |
#flush ⇒ Stash
Flush all changes to disk.
316 317 318 319 |
# File 'lib/garcon/stash/store.rb', line 316 def flush @journal.flush self end |
#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?
Does this stash have this key?
238 239 240 |
# File 'lib/garcon/stash/store.rb', line 238 def has_key?(key) @table.has_key?(@serializer.key_for(key)) end |
#has_value?(value) ⇒ Boolean Also known as: value?
Does this stash have this value?
252 253 254 |
# File 'lib/garcon/stash/store.rb', line 252 def has_value?(value) @table.has_value?(value) end |
#keys ⇒ Array<String>
Return the keys in the stash.
308 309 310 |
# File 'lib/garcon/stash/store.rb', line 308 def keys @table.keys end |
#load ⇒ Stash Also known as: sunrise
Sync the stash with what is on disk, by first flushing changes, and then loading the new records if necessary.
326 327 328 329 |
# File 'lib/garcon/stash/store.rb', line 326 def load @journal.load self end |
#lock {|stash| ... } ⇒ Object
This method performs an expensive locking over process boundaries.
Lock the stash for an exclusive commit across processes and threads. If you want to synchronize only between threads, use ‘#synchronize`.
341 342 343 |
# File 'lib/garcon/stash/store.rb', line 341 def lock synchronize { @journal.lock { yield self } } end |
#logsize ⇒ Fixnum
Counter of how many records are in the journal.
279 280 281 |
# File 'lib/garcon/stash/store.rb', line 279 def logsize @journal.size end |
#set!(key, value) ⇒ Object
Flushes data immediately to disk.
169 170 171 172 173 |
# File 'lib/garcon/stash/store.rb', line 169 def set!(key, value) set(key, value) flush value end |
#size ⇒ Fixnum Also known as: length
Return the number of stored items.
261 262 263 |
# File 'lib/garcon/stash/store.rb', line 261 def size @table.size end |
#synchronize {|stash| ... } ⇒ Object
Stash is not thread safe, if you want to access it from multiple
Synchronize access to the stash from multiple threads. threads, all accesses have to be in the #synchronize block.
356 357 358 |
# File 'lib/garcon/stash/store.rb', line 356 def synchronize @mutex.synchronize { yield self } end |
#update(hash) ⇒ Stash
Update stash with hash (fast batch update).
210 211 212 213 214 215 216 |
# File 'lib/garcon/stash/store.rb', line 210 def update(hash) shash = {} hash.each { |key, value| shash[@serializer.key_for(key)] = value } @journal << shash @table.update(shash) self end |
#update!(hash) ⇒ Stash
Updata stash and flush data to disk.
225 226 227 228 |
# File 'lib/garcon/stash/store.rb', line 225 def update!(hash) update(hash) @journal.flush end |