Class: StrokeDB::FileStorage

Inherits:
Storage show all
Defined in:
lib/strokedb/stores/file_storage.rb

Instance Attribute Summary

Attributes inherited from Storage

#authoritative_source

Instance Method Summary collapse

Methods included from ChainableStorage

#add_chained_storage!, #has_chained_storage?, #remove_chained_storage!, #save_with_chained_storages!, #save_without_chained_storages!, #sync_chained_storage!, #sync_chained_storages!

Constructor Details

#initialize(options = {}) ⇒ FileStorage

Returns a new instance of FileStorage.



4
5
6
7
# File 'lib/strokedb/stores/file_storage.rb', line 4

def initialize(options = {})
  @options = options.stringify_keys
  initialize_files
end

Instance Method Details

#clear!Object



98
99
100
101
# File 'lib/strokedb/stores/file_storage.rb', line 98

def clear!
  FileUtils.rm_rf(@options['path'])
  initialize_files
end

#close!Object



103
104
105
106
# File 'lib/strokedb/stores/file_storage.rb', line 103

def close!
  @archive.close!
  @uindex.close!
end

#create_new_archive!Object



61
62
63
64
65
66
# File 'lib/strokedb/stores/file_storage.rb', line 61

def create_new_archive!
  @archive = ArchiveVolume.new(:path => @options['path'])
  File.open(File.join(@options['path'],'LAST'),'w') do |f|
    f.write(@archive.uuid)
  end
end

#each(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/strokedb/stores/file_storage.rb', line 40

def each(options = {})
  after = options[:after_timestamp]
  include_versions = options[:include_versions]
  @container.each do |key, value|
    next if after && (value[1] <= after)
    if uuid_match = key.match(/^#{UUID_RE}$/) || (include_versions && uuid_match = key.match(/#{UUID_RE}./) )
      yield Document.from_raw(options[:store],value[0])
    end
  end
end

#find(uuid, version = nil, opts = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/strokedb/stores/file_storage.rb', line 13

def find(uuid, version=nil, opts = {}, &block)
  uuid_version = uuid + (version ? ".#{version}" : "")
  key = uuid.to_raw_uuid + (version ? version.to_raw_uuid : RAW_FULL_UUID)
  if (ptr = @uindex.find(key)) && (ptr != "\x00" * 20) # no way ptr will be zero
    raw_doc = StrokeDB::deserialize(read_at_ptr(ptr))[0]
    unless opts[:no_instantiation]
      doc = Document.from_raw(opts[:store], raw_doc.freeze, &block) # FIXME: there should be a better source for store (probably)
      doc.extend(VersionedDocument) if version
      doc
    else
      raw_doc
    end
  end
end

#head_version(uuid, opts = {}) ⇒ Object



34
35
36
37
38
# File 'lib/strokedb/stores/file_storage.rb', line 34

def head_version(uuid, opts = {})
  if doc = find(uuid,nil,opts.merge({ :no_instantiation => true }))
    doc['version']
  end	 
end

#include?(uuid, version = nil) ⇒ Boolean Also known as: contains?

Returns:

  • (Boolean)


28
29
30
# File 'lib/strokedb/stores/file_storage.rb', line 28

def include?(uuid,version=nil)
  !!find(uuid,version,:no_instantiation => true)
end

#last_archive_uuidObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/strokedb/stores/file_storage.rb', line 68

def last_archive_uuid
  last_filename = File.join(@options['path'],'LAST')
  if File.exists?(last_filename)
    IO.read(last_filename) 
  else 
    uuid = Util.random_uuid
    File.open(last_filename,'w') do |f|
      f.write(uuid)
    end
    uuid
  end
end

#pathObject



94
95
96
# File 'lib/strokedb/stores/file_storage.rb', line 94

def path
  @options['path']
end

#perform_save!(document, timestamp, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/strokedb/stores/file_storage.rb', line 51

def perform_save!(document, timestamp, options = {})
  position = @archive.insert(StrokeDB::serialize([document,timestamp.counter]))
  ptr = DistributedPointer.pack(@archive.raw_uuid,position)
  uuid = document.raw_uuid
  @uindex.insert(uuid + RAW_FULL_UUID, ptr) if options[:head] || !document.is_a?(VersionedDocument)
  @uindex.insert(uuid + document.version.to_raw_uuid, ptr) unless options[:head]
rescue ArchiveVolume::VolumeCapacityExceeded	 
  create_new_archive!
end

#read_at_ptr(ptr) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/strokedb/stores/file_storage.rb', line 81

def read_at_ptr(ptr)
  dptr = DistributedPointer.unpack(ptr)
  volume_uuid = dptr.volume_uuid
  if volume_uuid == @archive.uuid.to_raw_uuid
    @archive.read(dptr.offset)
  else
    archive = ArchiveVolume.new(:path => @options['path'], :uuid => volume_uuid)
    result = archive.read(dptr.offset)
    archive.close!
    result
  end
end

#save_as_head!(document, timestamp) ⇒ Object



9
10
11
# File 'lib/strokedb/stores/file_storage.rb', line 9

def save_as_head!(document, timestamp)
  write(document.uuid, document, timestamp)
end