Class: RubyFFDB::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/rffdb/index.rb

Instance Method Summary collapse

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



34
35
36
37
38
39
# File 'lib/rffdb/index.rb', line 34

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_pathObject



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

#keysObject



47
48
49
50
51
# File 'lib/rffdb/index.rb', line 47

def keys
  GDBM.open(file_path, 0664, GDBM::READER) do |index|
    index.keys
  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

#truncate(key) ⇒ Object



41
42
43
44
45
# File 'lib/rffdb/index.rb', line 41

def truncate(key)
  GDBM.open(file_path, 0664, GDBM::WRCREAT) do |index|
    index.delete(key.to_s)
  end
end