Class: RubyFFDB::StorageEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/rffdb/storage_engine.rb

Overview

Generic Storage Engine definition. Any subclass must implement or inherit the methods defined here (if any).

Class Method Summary collapse

Class Method Details

.all(_type) ⇒ Array

Return all known instances of a Document This method should be overridden in subclasses.

Parameters:

Returns:

  • (Array)


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)

Parameters:

Returns:



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

Parameters:

  • type (Document)

    the document type

  • object_id (Object)

    unique identifier for the document



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.

Parameters:



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

Parameters:

  • type (Document)

    the document type

  • size (Fixnum)

    the maximum size of the cache



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

Parameters:

  • type (Document)

    the document type

  • object_id (Object)

    unique identifier for the document

  • data (Object)

    data to be stored

Returns:

  • (Boolean)


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.

Parameters:

  • type (Document)

    the document type

  • object_id (Object)

    unique identifier for the document



53
54
55
# File 'lib/rffdb/storage_engine.rb', line 53

def self.file_path(_type, _object_id)
  false
end

.flushObject

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

Parameters:

  • type (Document)

    the document type

  • column (String, Symbol)

    the column / attribute for the index



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

Parameters:

Returns:

  • (Fixnum)


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

Parameters:

  • type (Document)

    implements the equivalent of table-level read-locking on this 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.

Parameters:

  • type (Document)

    type of Document to retrieve

  • object_id (Object)

    unique identifier for the stored data

  • use_caching (Boolean)

    attempt to pull the data from cache (or not)



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.

Parameters:

  • type (Document)

    type of Document to store

  • object_id (Object)

    unique identifier for the data to store

  • data (Object)

    data to be stored



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

Parameters:

  • type (Document)

    implements the equivalent of table-level write-locking on this Document type



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