Module: ScoutCabinet

Defined in:
lib/scout/persist/engine/tokyocabinet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#persistence_classObject

Returns the value of attribute persistence_class.



11
12
13
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 11

def persistence_class
  @persistence_class
end

#persistence_pathObject

Returns the value of attribute persistence_path.



11
12
13
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 11

def persistence_path
  @persistence_path
end

Class Method Details

.importtsv(database, stream) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 120

def self.importtsv(database, stream)
  begin
    bin = case database
          when TokyoCabinet::HDB
            'tchmgr'
          when TokyoCabinet::BDB
            'tcbmgr'
          else
            raise "Database not HDB or BDB: #{Log.fingerprint database}"
          end

    database.close
    CMD.cmd("#{bin} version", :log => false)
    FileUtils.mkdir_p File.dirname(database.persistence_path)
    CMD.cmd("#{bin} importtsv '#{database.persistence_path}'", :in => stream, :log => false, :dont_close_in => true)
  rescue
    Log.debug("tchmgr importtsv failed for: #{database.persistence_path}")
  end
end

.load_streamObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 141

def self.importtsv(database, stream)
  begin
    bin = case database
          when TokyoCabinet::HDB
            'tchmgr'
          when TokyoCabinet::BDB
            'tcbmgr'
          else
            raise "Database not HDB or BDB: #{Log.fingerprint database}"
          end

    database.close
    CMD.cmd("#{bin} version", :log => false)
    FileUtils.mkdir_p File.dirname(database.persistence_path)
    CMD.cmd("#{bin} importtsv '#{database.persistence_path}'", :in => stream, :log => false, :dont_close_in => true)
  rescue
    Log.debug("tchmgr importtsv failed for: #{database.persistence_path}")
  end
end

.open(path, write = true, tokyocabinet_class = TokyoCabinet::HDB) ⇒ Object



13
14
15
16
17
18
19
20
21
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
54
55
56
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 13

def self.open(path, write = true, tokyocabinet_class = TokyoCabinet::HDB)
  path = path.find if Path === path
  if String === tokyocabinet_class && tokyocabinet_class.include?(":big")
    big = true
    tokyocabinet_class = tokyocabinet_class.split(":").first
  else
    big = false
  end

  dir = File.dirname(File.expand_path(path))
  Open.mkdir(dir) unless File.exist?(dir)

  tokyocabinet_class = tokyocabinet_class.to_s if Symbol === tokyocabinet_class
  tokyocabinet_class = TokyoCabinet::HDB if tokyocabinet_class == "HDB" or tokyocabinet_class.nil?
  tokyocabinet_class = TokyoCabinet::BDB if tokyocabinet_class == "BDB"

  # Hack - Ignore warning: undefining the allocator of T_DATA class
  # TokyoCabinet::HDB_data
  database = Log.ignore_stderr do Persist::CONNECTIONS[path] ||= tokyocabinet_class.new end

  if big and not Open.exists?(path)
    database.tune(nil, nil, nil, tokyocabinet_class::TLARGE | tokyocabinet_class::TDEFLATE) 
  end

  flags = (write ? tokyocabinet_class::OWRITER | tokyocabinet_class::OCREAT : tokyocabinet_class::OREADER)
  database.close 

  if !database.open(path, flags)
    ecode = database.ecode
    raise "Open error: #{database.errmsg(ecode)}. Trying to open file #{path}"
  end

  database.extend ScoutCabinet
  database.persistence_path ||= path
  database.persistence_class = tokyocabinet_class

  database.open(path, tokyocabinet_class::OREADER)

  database.define_singleton_method(:fingerprint){ "#{self.persistence_class}:#{self.persistence_path}" }

  Persist::CONNECTIONS[path] = database

  database
end

Instance Method Details

#closeObject



58
59
60
61
62
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 58

def close
  @closed = true
  @writable = false
  super
end

#closed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 82

def closed?
  @closed
end

#importtsv(stream) ⇒ Object Also known as: load_stream



144
145
146
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 144

def importtsv(stream)
  ScoutCabinet.load_stream(self, stream)
end

#read(force = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 64

def read(force = false)
  return if ! @writable && ! @closed && ! force
  self.close
  if !self.open(@persistence_path, persistence_class::OREADER)
    ecode = self.ecode
    raise "Open error: #{self.errmsg(ecode)}. Trying to open file #{@persistence_path}"
  end

  @writable = false
  @closed = false

  self
end

#write(force = true) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 87

def write(force = true)
  return if write? && ! closed? && ! force
  self.close

  if !self.open(@persistence_path, persistence_class::OWRITER)
    ecode = self.ecode
    raise "Open error: #{self.errmsg(ecode)}. Trying to open file #{@persistence_path}"
  end

  @writable = true
  @closed = false

  self
end

#write?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 78

def write?
  @writable
end

#write_and_closeObject



111
112
113
114
115
116
117
118
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 111

def write_and_close
  begin
    write
    yield
  ensure
    close
  end
end

#write_and_readObject



102
103
104
105
106
107
108
109
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 102

def write_and_read
  begin
    write
    yield
  ensure
    read
  end
end