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.



4
5
6
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 4

def persistence_class
  @persistence_class
end

#persistence_pathObject

Returns the value of attribute persistence_path.



4
5
6
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 4

def persistence_path
  @persistence_path
end

Class Method Details

.importtsv(database, stream) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 113

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 134

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



6
7
8
9
10
11
12
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
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 6

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



51
52
53
54
55
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 51

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

#closed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 75

def closed?
  @closed
end

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



137
138
139
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 137

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

#read(force = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 57

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 80

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)


71
72
73
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 71

def write?
  @writable
end

#write_and_closeObject



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

def write_and_close
  begin
    write
    yield
  ensure
    close
  end
end

#write_and_readObject



95
96
97
98
99
100
101
102
# File 'lib/scout/persist/engine/tokyocabinet.rb', line 95

def write_and_read
  begin
    write
    yield
  ensure
    read
  end
end