Class: Garcon::Stash::Store

Inherits:
Object show all
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

Instance Method Summary collapse

Methods included from Enumerable

#exclude?

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.

Parameters:

  • file (String)

    The path to the Stash Store file.

  • opts (Hash) (defaults to: {})

    Options hash for creating a new stash.

Options Hash (opts):

  • :serializer (Class)

    Serializer class

  • :format (Class)

    Format class

  • :default (Object)

    Default value

Yields:

  • (key)

    a block that will return the default value to store.

Yield Parameters:

  • key (String)

    the key to be stored.



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.

Parameters:

  • key (Object) (defaults to: nil)

    the default value to retrieve.

Returns:

  • (Object)

    value the default value



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`.

Parameters:

  • key (Object)

    the value to retrieve from the stash

Returns:



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)`.

Parameters:

  • key (Object)

    the key of the storage slot in the stash

  • value (Object)

    the value to store

Returns:



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

#bytesizeFixnum

Utility method that will return the size of the stash in bytes, useful for determining when to compact.

Returns:

  • (Fixnum)


271
272
273
# File 'lib/garcon/stash/store.rb', line 271

def bytesize
  @journal.bytesize
end

#clearStash

Remove all keys and values from the stash.

Returns:



364
365
366
367
368
# File 'lib/garcon/stash/store.rb', line 364

def clear
  @table.clear
  @journal.clear
  self
end

#closeObject

Close the stash for reading and writing.

Returns:

  • nil



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.

Returns:



393
394
395
# File 'lib/garcon/stash/store.rb', line 393

def closed?
  @journal.closed?
end

#compactStash

Compact the stash to remove stale commits and reduce the file size.

Returns:



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.

Parameters:

  • key (Object)

    the key of the storage slot in the stash

Returns:



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.

Parameters:

  • key (Object)

    the key of the storage slot in the stash

Returns:



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.

Yields:

  • (key, value)

    block the iterator for each key value pair

Yield Parameters:

  • key

    the key.

  • value

    the value from 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.

Returns:



287
288
289
# File 'lib/garcon/stash/store.rb', line 287

def empty?
  @table.empty?
end

#fileString

Stash store file name.

Returns:

  • (String)

    stash store file name



108
109
110
# File 'lib/garcon/stash/store.rb', line 108

def file
  @journal.file
end

#flushStash

Flush all changes to disk.

Returns:



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?

Parameters:

  • key (Object)

    the key to check if the stash has it

Returns:



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?

Parameters:

  • value (Object)

    the value to check if the Stash has it

Returns:



252
253
254
# File 'lib/garcon/stash/store.rb', line 252

def has_value?(value)
  @table.has_value?(value)
end

#keysArray<String>

Return the keys in the stash.

Returns:



308
309
310
# File 'lib/garcon/stash/store.rb', line 308

def keys
  @table.keys
end

#loadStash Also known as: sunrise

Sync the stash with what is on disk, by first flushing changes, and then loading the new records if necessary.

Returns:



326
327
328
329
# File 'lib/garcon/stash/store.rb', line 326

def load
  @journal.load
  self
end

#lock {|stash| ... } ⇒ Object

Note:

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`.

Yields:

  • a block where every change to the stash is synced

Yield Parameters:

Returns:

  • result of the block

See Also:



341
342
343
# File 'lib/garcon/stash/store.rb', line 341

def lock
  synchronize { @journal.lock { yield self } }
end

#logsizeFixnum

Counter of how many records are in the journal.

Returns:

  • (Fixnum)


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.

Parameters:

  • key (Object)

    the key of the storage slot in the stash

  • value (Object)

    the value to store

Returns:



169
170
171
172
173
# File 'lib/garcon/stash/store.rb', line 169

def set!(key, value)
  set(key, value)
  flush
  value
end

#sizeFixnum Also known as: length

Return the number of stored items.

Returns:

  • (Fixnum)


261
262
263
# File 'lib/garcon/stash/store.rb', line 261

def size
  @table.size
end

#synchronize {|stash| ... } ⇒ Object

Note:

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.

Yields:

  • a block where every change to the stash is synced

Yield Parameters:

Returns:

  • result of the block

See Also:



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).

Parameters:

  • hash (Hash)

    the key/value hash

Returns:



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.

Parameters:

  • hash (Hash)

    the key/value hash

Returns:



225
226
227
228
# File 'lib/garcon/stash/store.rb', line 225

def update!(hash)
  update(hash)
  @journal.flush
end