| Class | Rddb::DocumentStore::PartitionedFileDocumentStore |
| In: |
lib/rddb/document_store/partitioned_file_document_store.rb
|
| Parent: | Base |
DocumentStore implementation that stores documents in partitioned files.
Initialize the datastore.
Options:
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 17
17: def initialize(options={})
18: @options = options
19: @options[:basedir] ||= 'data'
20: load_index
21: end
The number of documents in the datastore
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 69
69: def count
70: index.length
71: end
Delete the document at the given path.
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 52
52: def delete(id)
53: raise RuntimeError, "File datastore does not support document deletion"
54: end
Yield each document from the datastore to the given block
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 74
74: def each(partition=nil, &block)
75: if partition.nil?
76: index.each do |id, info|
77: yield find(id)
78: end
79: else
80: f = open_read(partition)
81: f.rewind
82: until f.eof?
83: yield Marshal.load(f)
84: end
85: end
86: end
Return true if the document exists.
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 57
57: def exists?(id)
58: !index[id.to_s].nil?
59: end
Find the document for the given id.
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 24
24: def find(id)
25: id = id.to_s
26: return nil unless index[id]
27: return cache[id] if cache? && cache.key?(id)
28: p = index[id][:partition]
29: f = open_read(p)
30: f.seek(index[id][:offset])
31: data = f.read(index[id][:length])
32: document = Marshal.load(data)
33: cache[id] = document if cache?
34: document
35: end
Store the document.
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 38
38: def store(document)
39: id = document.id.to_s
40: p = partition_for(document)
41: index[id] = {:partition => p}
42: f = open_write(p)
43: f.seek(0, IO::SEEK_END)
44: index[id][:offset] = f.pos
45: Marshal.dump(document, f)
46: index[id][:length] = f.pos - index[id][:offset]
47: cache[id] = document if cache?
48: document
49: end
Returns true
# File lib/rddb/document_store/partitioned_file_document_store.rb, line 98
98: def supports_partitioning?
99: true
100: end