Class: RubyFFDB::Index
- Inherits:
-
Object
- Object
- RubyFFDB::Index
- Defined in:
- lib/rffdb/index.rb
Instance Method Summary collapse
-
#delete(key, value) ⇒ Object
Remove a specific Document association with a key.
- #file_path ⇒ Object
- #get(key) ⇒ Object
-
#initialize(type, column) ⇒ Index
constructor
A new instance of Index.
-
#keys ⇒ Array
All keys (column data) in the index.
-
#prune ⇒ Object
Evict keys (column data) with no associated Documents.
- #put(key, value) ⇒ Object
-
#query(q, operator = '==') ⇒ Array
Complex queries of the index can be done with this method.
-
#truncate(key) ⇒ Object
Remove all associations with a specific key (column data).
Constructor Details
#initialize(type, column) ⇒ Index
Returns a new instance of Index.
3 4 5 6 7 8 9 10 |
# File 'lib/rffdb/index.rb', line 3 def initialize(type, column) @type = type @column = column FileUtils.mkdir_p(File.dirname(file_path)) GDBM.open(file_path, 0664, GDBM::WRCREAT) do # Index initialized end end |
Instance Method Details
#delete(key, value) ⇒ Object
Remove a specific Document association with a key
35 36 37 38 39 40 |
# File 'lib/rffdb/index.rb', line 35 def delete(key, value) previous = get(key) GDBM.open(file_path, 0664, GDBM::WRCREAT) do |index| index[key.to_s] = Marshal.dump((previous - [value]).uniq) end end |
#file_path ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rffdb/index.rb', line 12 def file_path File.join( DB_DATA, @type.to_s.gsub('::', '__'), 'indexes', @column.to_s + '.index' ) end |
#get(key) ⇒ Object
21 22 23 24 25 |
# File 'lib/rffdb/index.rb', line 21 def get(key) GDBM.open(file_path, 0664, GDBM::READER) do |index| Marshal.load index.fetch(key.to_s, Marshal.dump([])) end end |
#keys ⇒ Array
All keys (column data) in the index
51 52 53 54 55 |
# File 'lib/rffdb/index.rb', line 51 def keys GDBM.open(file_path, 0664, GDBM::READER) do |index| index.keys end end |
#prune ⇒ Object
Evict keys (column data) with no associated Documents
70 71 72 73 74 75 |
# File 'lib/rffdb/index.rb', line 70 def prune GDBM.open(file_path, 0664, GDBM::WRCREAT) do |index| index.delete_if { |key, value| Marshal.load(value).empty? } index.reorganize # clear up wasted disk space end end |
#put(key, value) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/rffdb/index.rb', line 27 def put(key, value) previous = get(key) GDBM.open(file_path, 0664, GDBM::WRCREAT) do |index| index[key.to_s] = Marshal.dump((previous + [value]).uniq) end end |
#query(q, operator = '==') ⇒ Array
Complex queries of the index can be done with this method
59 60 61 62 63 64 65 66 67 |
# File 'lib/rffdb/index.rb', line 59 def query(q, operator = '==') datum = [] GDBM.open(file_path, 0664, GDBM::READER) do |index| index.each_pair do |key, value| datum += Marshal.load(value) if key.send(operator.to_sym, q) end end datum.uniq.compact end |
#truncate(key) ⇒ Object
Remove all associations with a specific key (column data)
43 44 45 46 47 |
# File 'lib/rffdb/index.rb', line 43 def truncate(key) GDBM.open(file_path, 0664, GDBM::WRCREAT) do |index| index.delete(key.to_s) end end |