Class: RubyFFDB::StorageEngine
- Inherits:
-
Object
- Object
- RubyFFDB::StorageEngine
- Defined in:
- lib/rffdb/storage_engine.rb
Overview
Generic Storage Engine definition. Any subclass must implement or inherit the methods defined here (if any).
Direct Known Subclasses
RubyFFDB::StorageEngines::JsonEngine, RubyFFDB::StorageEngines::YamlEngine
Class Method Summary collapse
-
.all(_type) ⇒ Array
Return all known instances of a Document This method should be overridden in subclasses.
-
.cache(type) ⇒ CacheProvider
Allow access to the cache instance directly (kind of dangerous but helpful for troubleshooting).
-
.cache_lookup(type, object_id) ⇒ Object
Attempt to retrieve an item from the Document type’s cache instance.
-
.cache_provider(document_type, cache_provider_class) ⇒ Object
Set the cache provider to use for a document type This completely flushes all cache.
-
.cache_size(type, size) ⇒ Object
Set the maximum size of a cache, based on Document type.
-
.cache_store(type, object_id, data) ⇒ Boolean
Store some data in the cache for the Document type.
-
.file_path(_type, _object_id) ⇒ Object
The full path to a stored (or would-be stored) Document This method should be overridden in subclasses.
-
.flush ⇒ Object
Flush all changes to disk (usually done automatically) This method should be overridden in subclasses.
-
.index(type, column) ⇒ Object
Allows direct access to an index for a Document type and column.
-
.next_id(type) ⇒ Fixnum
Determine the next unique identifier available for a Document type.
-
.read_lock(type, &block) ⇒ Object
Read locking by document type.
-
.retrieve(_type, _object_id, _use_caching = true) ⇒ Object
Retrieve some stored data This method should be overridden in subclasses.
-
.store(_type, _object_id, _data) ⇒ Object
Store data This method should be overridden in subclasses.
-
.write_lock(type, &block) ⇒ Object
Write locking by document type, with implicit read locking.
Class Method Details
.all(_type) ⇒ Array
Return all known instances of a Document This method should be overridden in subclasses.
61 62 63 |
# File 'lib/rffdb/storage_engine.rb', line 61 def self.all(_type) [] end |
.cache(type) ⇒ CacheProvider
Allow access to the cache instance directly (kind of dangerous but helpful for troubleshooting)
129 130 131 132 |
# File 'lib/rffdb/storage_engine.rb', line 129 def self.cache(type) @caches ||= {} @caches[type] ||= CacheProviders::LRUCache.new end |
.cache_lookup(type, object_id) ⇒ Object
Attempt to retrieve an item from the Document type’s cache instance
107 108 109 110 111 |
# File 'lib/rffdb/storage_engine.rb', line 107 def self.cache_lookup(type, object_id) @caches ||= {} @caches[type] ||= CacheProviders::LRUCache.new @caches[type][object_id.to_s] end |
.cache_provider(document_type, cache_provider_class) ⇒ Object
Set the cache provider to use for a document type This completely flushes all cache.
83 84 85 86 87 88 89 90 |
# File 'lib/rffdb/storage_engine.rb', line 83 def self.cache_provider(document_type, cache_provider_class) unless cache_provider_class.instance_of?(Class) && cache_provider_class.ancestors.include?(CacheProvider) fail Exceptions::InvalidCacheProvider end @caches ||= {} @caches[document_type] = cache_provider_class.new end |
.cache_size(type, size) ⇒ Object
Set the maximum size of a cache, based on Document type
95 96 97 98 99 100 101 102 |
# File 'lib/rffdb/storage_engine.rb', line 95 def self.cache_size(type, size) @caches ||= {} if @caches.key?(type) @caches[type] = @caches[type].class.new(size) else @caches[type] = CacheProviders::LRUCache.new(size) end end |
.cache_store(type, object_id, data) ⇒ Boolean
Store some data in the cache for the Document type
118 119 120 121 122 123 |
# File 'lib/rffdb/storage_engine.rb', line 118 def self.cache_store(type, object_id, data) @caches ||= {} @caches[type] ||= CacheProviders::LRUCache.new @caches[type][object_id.to_s] = data true end |
.file_path(_type, _object_id) ⇒ Object
The full path to a stored (or would-be stored) Document This method should be overridden in subclasses.
53 54 55 |
# File 'lib/rffdb/storage_engine.rb', line 53 def self.file_path(_type, _object_id) false end |
.flush ⇒ Object
Flush all changes to disk (usually done automatically) This method should be overridden in subclasses.
45 46 47 |
# File 'lib/rffdb/storage_engine.rb', line 45 def self.flush false end |
.index(type, column) ⇒ Object
Allows direct access to an index for a Document type and column
137 138 139 140 141 |
# File 'lib/rffdb/storage_engine.rb', line 137 def self.index(type, column) @indexes ||= {} @indexes[type] ||= {} @indexes[type][column.to_sym] ||= Index.new(type, column.to_sym) end |
.next_id(type) ⇒ Fixnum
Determine the next unique identifier available for a Document type
68 69 70 71 72 73 74 75 76 |
# File 'lib/rffdb/storage_engine.rb', line 68 def self.next_id(type) last_id = all(type)[-1] next_key = last_id.nil? ? 1 : (last_id + 1) if @highest_known_key && @highest_known_key >= next_key write_lock(type) { @highest_known_key += 1 } else write_lock(type) { @highest_known_key = next_key } end end |
.read_lock(type, &block) ⇒ Object
Read locking by document type
8 9 10 11 12 |
# File 'lib/rffdb/storage_engine.rb', line 8 def self.read_lock(type, &block) @read_mutexes ||= {} @read_mutexes[type] ||= Mutex.new @read_mutexes[type].synchronize(&block) end |
.retrieve(_type, _object_id, _use_caching = true) ⇒ Object
Retrieve some stored data This method should be overridden in subclasses.
39 40 41 |
# File 'lib/rffdb/storage_engine.rb', line 39 def self.retrieve(_type, _object_id, _use_caching = true) false end |
.store(_type, _object_id, _data) ⇒ Object
Store data This method should be overridden in subclasses.
30 31 32 |
# File 'lib/rffdb/storage_engine.rb', line 30 def self.store(_type, _object_id, _data) false end |
.write_lock(type, &block) ⇒ Object
Write locking by document type, with implicit read locking
17 18 19 20 21 22 23 |
# File 'lib/rffdb/storage_engine.rb', line 17 def self.write_lock(type, &block) @write_mutexes ||= {} @write_mutexes[type] ||= Mutex.new @write_mutexes[type].synchronize do read_lock(type, &block) end end |