Class: Roma::Storage::GroongaStorage::GroongaHash

Inherits:
Object
  • Object
show all
Defined in:
lib/roma/storage/groonga_storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ GroongaHash

Returns a new instance of GroongaHash.



34
35
36
# File 'lib/roma/storage/groonga_storage.rb', line 34

def initialize(fname)
  @fname = fname
end

Instance Method Details

#closeObject



93
94
95
96
97
# File 'lib/roma/storage/groonga_storage.rb', line 93

def close
  @database.close
  @context.close
  @hash = @value = @database = @context = nil
end

#eachObject



67
68
69
70
71
# File 'lib/roma/storage/groonga_storage.rb', line 67

def each
  @hash.each do |record|
    yield(record.key, @value[record.id])
  end
end

#get(key) ⇒ Object



47
48
49
50
51
# File 'lib/roma/storage/groonga_storage.rb', line 47

def get(key)
  record = @hash[key]
  return nil if record.nil?
  @value[record.id]
end

#openObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roma/storage/groonga_storage.rb', line 73

def open
  @context = Groonga::Context.new(:encoding => :none)
  if File.exist?(@fname)
    @database = Groonga::Database.new(@fname, :context => @context)
  else
    @database = Groonga::Database.create(:context => @context,
                                         :path => @fname)
    Groonga::Schema.define(:context => @context) do |schema|
      schema.create_table("hash",
                          :type => :hash,
                          :key_type => "ShortText") do |table|
        table.text("value")
      end
    end
  end

  @hash = @context["hash"]
  @value = @hash.column("value")
end

#out(key) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/roma/storage/groonga_storage.rb', line 53

def out(key)
  record = @hash[key]
  if record
    record.delete
    true
  else
    false
  end
end

#pathObject



38
39
40
# File 'lib/roma/storage/groonga_storage.rb', line 38

def path
  @hash.path
end

#put(key, value) ⇒ Object



42
43
44
45
# File 'lib/roma/storage/groonga_storage.rb', line 42

def put(key, value)
  record = @hash.add(key)
  @value[record.id] = value
end

#rnumObject



63
64
65
# File 'lib/roma/storage/groonga_storage.rb', line 63

def rnum
  @hash.count
end