Class Rddb::DocumentStore::PartitionedFileDocumentStore
In: lib/rddb/document_store/partitioned_file_document_store.rb
Parent: Base

DocumentStore implementation that stores documents in partitioned files.

Methods

Public Class methods

Initialize the datastore.

Options:

  • :basedir: The base directory for document storage
  • :partition_strategy: A Proc that defines partitioning
  • :cache: Provide a cache implementation (such as a Hash) to enable caching. If not specified then caching will not be used. Note that caching only works on direct ID lookups, not on queries. Queries should use materialized views for performance.

[Source]

    # 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

Public Instance methods

The number of documents in the datastore

[Source]

    # 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.

[Source]

    # 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

[Source]

    # 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

Yield each partition name

[Source]

    # File lib/rddb/document_store/partitioned_file_document_store.rb, line 89
89:       def each_partition(&block)
90:         Dir.glob("#{basedir}/*") do |f|
91:           f = File.basename(f)
92:           next if f == 'index'
93:           yield f
94:         end
95:       end

Return true if the document exists.

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # 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

[Source]

     # File lib/rddb/document_store/partitioned_file_document_store.rb, line 98
 98:       def supports_partitioning?
 99:         true
100:       end

Trigger indexes to be stored if necessary.

[Source]

    # File lib/rddb/document_store/partitioned_file_document_store.rb, line 62
62:       def write_indexes
63:         File.open(File.join(basedir, 'index'), 'w') do |f|
64:           Marshal.dump(index, f)
65:         end
66:       end

[Validate]