Class: Universa::FSStore::FileStore

Inherits:
ChainStore show all
Defined in:
lib/universa/fs_store/file_store.rb

Overview

Simple file-based store that could be efficiently user with per-file cloud storages like Dropbox, Google Disk, NextCloud and like.

Notes to developers:

  • attributes are eager loaded: should always be contructed from contract or from file

  • contract is lazy loaded

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ChainStore

#<<, #find_by_id!, #find_by_parent

Constructor Details

#initialize(root_path) ⇒ FileStore

Construct store in the path supplied. If the path is not empty, it will be scanned for stored contracts.

Parameters:

  • root_path (String)

    of the store, must exist.



21
22
23
24
25
# File 'lib/universa/fs_store/file_store.rb', line 21

def initialize(root_path)
  @root = root_path
  @root = @root[0...-1] while (@root[-1] == '/')
  init_cache
end

Instance Attribute Details

#rootObject (readonly)

String

The file store root path



17
18
19
# File 'lib/universa/fs_store/file_store.rb', line 17

def root
  @root
end

Instance Method Details

#countObject

Count contracts in the store. This operation could be slow.



40
41
42
# File 'lib/universa/fs_store/file_store.rb', line 40

def count
  @cache.size
end

#find_by_id(hash_id) ⇒ Contract

Returns with the corresponding id or nil.

Parameters:

  • hash_id (HashId)

    instance to look for

Returns:

  • (Contract)

    with the corresponding id or nil



35
36
37
# File 'lib/universa/fs_store/file_store.rb', line 35

def find_by_id hash_id
  @cache[hash_id]
end

#store_contract(contract) ⇒ StoredContract

Save contract to the store. When this method returns, the contract must me already stored. If the contract with such hasId is already stored, just returns it.

Parameters:

  • contract (Object)

    to store

Returns:

  • (StoredContract)

    for this contract



28
29
30
31
32
# File 'lib/universa/fs_store/file_store.rb', line 28

def store_contract contract
  entry = FSStore::Entry.new(self)
  entry = entry.init_with_contract(contract)
  add_to_cache entry
end