Class: AnyStyle::Dictionary::LMDB

Inherits:
AnyStyle::Dictionary show all
Defined in:
lib/anystyle/dictionary/lmdb.rb

Instance Attribute Summary collapse

Attributes inherited from AnyStyle::Dictionary

#db, #options

Instance Method Summary collapse

Methods inherited from AnyStyle::Dictionary

create, instance, #populate!, #tag_counts, #tags

Constructor Details

#initialize(options = {}) ⇒ LMDB

Returns a new instance of LMDB.



15
16
17
# File 'lib/anystyle/dictionary/lmdb.rb', line 15

def initialize(options = {})
  super(self.class.defaults.merge(options))
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/anystyle/dictionary/lmdb.rb', line 13

def env
  @env
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/anystyle/dictionary/lmdb.rb', line 30

def close
  env.close if open?
end

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/anystyle/dictionary/lmdb.rb', line 38

def empty?
  open? and db.size == 0
end

#get(key) ⇒ Object



50
51
52
# File 'lib/anystyle/dictionary/lmdb.rb', line 50

def get(key)
  db[key.to_s].to_i
end

#lmdb_optionsObject



62
63
64
# File 'lib/anystyle/dictionary/lmdb.rb', line 62

def lmdb_options
  options.reject { |k| [:path, :source].include?(k) }
end

#openObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/anystyle/dictionary/lmdb.rb', line 19

def open
  unless open?
    @env = ::LMDB.new(path, lmdb_options)
    @db = @env.database create: true
  end

  self
ensure
  populate! if empty?
end

#open?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/anystyle/dictionary/lmdb.rb', line 34

def open?
  !db.nil?
end

#pathObject



58
59
60
# File 'lib/anystyle/dictionary/lmdb.rb', line 58

def path
  options[:path]
end

#put(key, value) ⇒ Object



54
55
56
# File 'lib/anystyle/dictionary/lmdb.rb', line 54

def put(key, value)
  db[key.to_s] = value.to_i.to_s
end

#truncateObject



42
43
44
45
46
47
48
# File 'lib/anystyle/dictionary/lmdb.rb', line 42

def truncate
  close
  %w{ data.mdb lock.mdb }.each do |mdb|
    mdb = File.join(path, mdb)
    File.unlink(mdb) if File.exists?(mdb)
  end
end