Class: Steenzout::DAO::TokyoCabinetDAO

Inherits:
Object
  • Object
show all
Defined in:
lib/dao/tokyocabinet.rb

Overview

Generic TokyoCabinet Data Access Object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, configuration) ⇒ TokyoCabinetDAO

Constructs a generic TokyoCabinet DAO.

Parameters:

  • name:

    the name of the implemented DAO.

  • configuration:

    hash containing the DAO configuration. :kvstore => the TokyoCabinet database implementation. :location => the TokyoCabinet database location.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dao/tokyocabinet.rb', line 22

def initialize(name, configuration)

  @name = name

  raise ArgumentError, "TokyoCabinetDAO name is nil!" \
    if name.nil?
  raise ArgumentError, "TokyoCabinetDAO configuration nil!" \
    if configuration.nil?
  raise ArgumentError, "TokyoCabinetDAO configuration for #{@name} is missing the :kvstore property!" \
    if !configuration.has_key? :kvstore
  raise ArgumentError, "TokyoCabinetDAO configuration for #{@name} is missing the :location property!" \
    if !configuration.has_key? :location


  case configuration[:kvstore]
    when :hdb, :hash
      @database = HDB::new
    when :bdb, :btree
      @database = BDB::new
    when :fdb, :'fixed-length'
      @database = FDB::new
    when :tdb, :table
      @database = TDB::new
    when :adb, :abstract
      @database = ADB::new
    else
      raise ArgumentError, "Unknown TokyoCabinet database #{configuration[:kvstore]}!"
  end

  @location = configuration[:location]

end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



13
14
15
# File 'lib/dao/tokyocabinet.rb', line 13

def database
  @database
end

Instance Method Details

#closeObject



63
64
65
66
67
68
69
# File 'lib/dao/tokyocabinet.rb', line 63

def close()

  if !@database.nil?
    LOGGER.error('steenzout-dao.TokyoCabinetDAO[#{@name}') {"close error: #{@database.errmsg(@database.ecode)}"} if !@database.close
  end

end

#delete(key) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/dao/tokyocabinet.rb', line 73

def delete(key)

  @database.out key

  LOGGER.info('steenzout-dao.TokyoCabinetDAO[#{@name}') {"deleted #{key} mapping."}

  nil

end

#get(key) ⇒ Object



93
94
95
96
97
# File 'lib/dao/tokyocabinet.rb', line 93

def get(key)

  @database[key]

end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/dao/tokyocabinet.rb', line 85

def has?(key)

  !@database[key].nil?

end

#listObject



101
102
103
104
105
106
107
108
# File 'lib/dao/tokyocabinet.rb', line 101

def list()

  @database.iterinit
  while key = @database.iternext
    yield key, get(key)
  end

end

#map(key, value) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dao/tokyocabinet.rb', line 112

def map(key, value)

  status = @database.put key, value
  if !status
    LOGGER.debug('steenzout-dao.TokyoCabinetDAO][#{@name}') {"#{@database.ecode} : #{@database.errmsg(@database.ecode)}"}
    error = "failed mapping #{key}->#{value}!"
    LOGGER.error('steenzout-dao.TokyoCabinetDAO][#{@name}') {error}
    raise Exception, error
  end
  LOGGER.info('steenzout-dao.TokyoCabinetDAO][#{@name}') {"created #{key}->#{value} mapping."}

end

#openObject



55
56
57
58
59
60
61
# File 'lib/dao/tokyocabinet.rb', line 55

def open()

  if !@database.open(@location, HDB::OWRITER | HDB::OCREAT)
    LOGGER.error('steenzout-dao.TokyoCabinetDAO][#{@name}') {"open error: #{@database.errmsg(@database.ecode)}"} if LOGGER.error?
  end

end

#update(key, value) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/dao/tokyocabinet.rb', line 127

def update(key, value)

  if !has? key
    raise ArgumentError, "There is no mapping for the given #{key} key."
  end

  map(key, value)

end