sekka 0.8.8 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sekka/kvs.rb CHANGED
@@ -31,19 +31,47 @@
31
31
  #
32
32
  # $Id:
33
33
  #
34
- require 'tokyocabinet'
35
34
  require 'memcache'
36
35
 
37
-
38
36
  class Kvs
39
37
  def initialize( dbtype )
38
+ @tcFlag = true
39
+ begin
40
+ require 'tokyocabinet'
41
+ rescue LoadError
42
+ @tcFlag = false
43
+ end
44
+
45
+ @dbmFlag = true
46
+ begin
47
+ require 'dbm'
48
+ rescue LoadError
49
+ @dbmFlag = false
50
+ end
51
+
40
52
  @dbtype = dbtype
41
53
  case dbtype
42
54
  when :tokyocabinet
43
- @db = TokyoCabinet::HDB.new( )
44
- # @db.setxmsiz(512 * 1024 * 1024) # expand memory
55
+ if @tcFlag
56
+ @db = TokyoCabinet::HDB.new( )
57
+ else
58
+ raise RuntimeError, "Kvs.new() missed require( 'tokyocabinet' )."
59
+ end
60
+
45
61
  when :memcache
46
62
  # do nothing
63
+
64
+ when :dbm
65
+ if @dbmFlag
66
+ # do nothing
67
+ else
68
+ raise RuntimeError, "Kvs.new() missed require( 'dbm' )."
69
+ end
70
+
71
+ # do nothing
72
+
73
+ when :pure
74
+ # do nothing
47
75
  else
48
76
  raise ArgumentError, "Kvs.new() requires reserved DB typename"
49
77
  end
@@ -57,6 +85,18 @@ class Kvs
57
85
  end
58
86
  when :memcache
59
87
  @db = MemCache.new( name )
88
+ when :dbm
89
+ @db = DBM.new( name )
90
+ when :pure
91
+ @name = name
92
+ if File.exist?( @name )
93
+ File.open( @name ) {|f|
94
+ @db = eval( f.read() )
95
+ }
96
+ @db
97
+ else
98
+ @db = Hash.new
99
+ end
60
100
  else
61
101
  raise RuntimeError
62
102
  end
@@ -79,10 +119,12 @@ class Kvs
79
119
  def pure_put!( key, value, timeout = 0 )
80
120
  if 0 < key.size
81
121
  case @dbtype
82
- when :tokyocabinet
122
+ when :tokyocabinet, :dbm
83
123
  @db[ key.force_encoding("ASCII-8BIT") ] = value.force_encoding("ASCII-8BIT")
84
124
  when :memcache
85
125
  @db.set( key.force_encoding("ASCII-8BIT"), value.force_encoding("ASCII-8BIT"), timeout )
126
+ when :pure
127
+ @db[ key ] = value
86
128
  else
87
129
  raise RuntimeError
88
130
  end
@@ -94,7 +136,7 @@ class Kvs
94
136
  if 0 == key.size
95
137
  fallback
96
138
  else
97
- val = @db.get( key )
139
+ val = @db[ key ]
98
140
  if val
99
141
  val.force_encoding("UTF-8")
100
142
  else
@@ -109,7 +151,7 @@ class Kvs
109
151
 
110
152
  def clear()
111
153
  case @dbtype
112
- when :tokyocabinet
154
+ when :tokyocabinet, :dbm, :pure
113
155
  @db.clear
114
156
  when :memcache
115
157
  # do nothing
@@ -121,12 +163,14 @@ class Kvs
121
163
  # return array of key string
122
164
  def keys()
123
165
  case @dbtype
124
- when :tokyocabinet
166
+ when :tokyocabinet, :dbm
125
167
  @db.keys.map { |k|
126
168
  k.force_encoding("UTF-8")
127
169
  }
128
170
  when :memcache
129
171
  raise RuntimeError, "Kvs#keys method was not implemented for memcache."
172
+ when :pure
173
+ @db.keys
130
174
  else
131
175
  raise RuntimeError
132
176
  end
@@ -140,6 +184,10 @@ class Kvs
140
184
  }
141
185
  when :memcache
142
186
  raise RuntimeError, "Kvs#forward_match_keys method was not implemented for memcache."
187
+ when :dbm, :pure
188
+ self.keys( ).select {|key|
189
+ key.match( "^" + prefix )
190
+ }
143
191
  else
144
192
  raise RuntimeError
145
193
  end
@@ -147,10 +195,14 @@ class Kvs
147
195
 
148
196
  def close()
149
197
  case @dbtype
150
- when :tokyocabinet
198
+ when :tokyocabinet, :dbm
151
199
  @db.close
152
200
  when :memcache
153
201
  # do nothign
202
+ when :pure
203
+ File.open( @name, "w" ) { |f|
204
+ f.print( @db )
205
+ }
154
206
  else
155
207
  raise RuntimeError
156
208
  end
@@ -1,6 +1,6 @@
1
1
  class SekkaVersion
2
2
  include Singleton
3
3
  def self.version
4
- "0.8.8"
4
+ "0.9.0"
5
5
  end
6
6
  end
@@ -38,7 +38,7 @@
38
38
  (use sekka.henkan)
39
39
  (load "./test/common.nnd")
40
40
 
41
- (define dbtype (string->symbol (first *argv*)))
41
+ (define dbtype 'tokyocabinet)
42
42
 
43
43
  (test-start "aprorimate bench")
44
44
  ;;===================================================================
@@ -72,12 +72,14 @@
72
72
 
73
73
  (test-section "bench start 1")
74
74
  (require "benchmark")
75
+
75
76
  (.puts Benchmark::CAPTION)
76
77
  (.puts (Benchmark.measure
77
78
  (&block ()
78
- (. (Range.new 0 1000) each
79
- (&block (n)
80
- (a-search.search "uuuu" kvs "henkan" #f))))))
79
+ (for-each
80
+ (lambda (n)
81
+ (approximate-search "uuuu" kvs "henkan" "k" 0))
82
+ (Range.new 0 100)))))
81
83
 
82
84
  ;;===================================================================
83
85
  (test-end)
@@ -0,0 +1,85 @@
1
+ ;;-*- mode: nendo; syntax: scheme -*-
2
+ ;;;
3
+ ;;; approximate-bench.nnd - 漢字変換のベンチマークテスト
4
+ ;;;
5
+ ;;; Copyright (c) 2011 Kiyoka Nishiyama <kiyoka@sumibi.org>
6
+ ;;;
7
+ ;;; Redistribution and use in source and binary forms, with or without
8
+ ;;; modification, are permitted provided that the following conditions
9
+ ;;; are met:
10
+ ;;;
11
+ ;;; 1. Redistributions of source code must retain the above copyright
12
+ ;;; notice, this list of conditions and the following disclaimer.
13
+ ;;;
14
+ ;;; 2. Redistributions in binary form must reproduce the above copyright
15
+ ;;; notice, this list of conditions and the following disclaimer in the
16
+ ;;; documentation and/or other materials provided with the distribution.
17
+ ;;;
18
+ ;;; 3. Neither the name of the authors nor the names of its contributors
19
+ ;;; may be used to endorse or promote products derived from this
20
+ ;;; software without specific prior written permission.
21
+ ;;;
22
+ ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
+ ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
+ ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
+ ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
+ ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28
+ ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29
+ ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30
+ ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
+ ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
+ ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
+ ;;;
34
+ ;;; $Id:
35
+ ;;;
36
+ (require "sekka/kvs")
37
+ (require "ruby-prof")
38
+ (require "benchmark")
39
+ (use sekka.henkan)
40
+ (load "./test/common.nnd")
41
+
42
+ ;; ------------------ macro --------------------
43
+ (define bench-task
44
+ (macro (profile-flag title proc)
45
+ `(begin
46
+ (.puts ,title)
47
+ (.puts Benchmark::CAPTION)
48
+ ,(if profile-flag
49
+ `(let* ((result (RubyProf.profile
50
+ (&block ()
51
+ ,proc)))
52
+ (printer (RubyProf::GraphPrinter.new result)))
53
+ (printer.print STDOUT))
54
+ `(.puts (Benchmark.measure
55
+ (&block ()
56
+ ,proc))))
57
+ (.puts ""))))
58
+
59
+
60
+ ;;-------------------------------------------------------------------
61
+ (let1 kvs (Kvs.new 'dbm)
62
+ (kvs.open "./test")
63
+
64
+ (bench-task #f
65
+ "henkan-okuri-nashi (DBM)"
66
+ (for-each
67
+ (lambda (n)
68
+ (henkan-okuri-nashi "uuuu" kvs "henkan" 0))
69
+ (Range.new 0 100)))
70
+ (kvs.close))
71
+
72
+ ;;-------------------------------------------------------------------
73
+ (let1 kvs (Kvs.new 'tokyocabinet)
74
+ (kvs.open "./test.tch")
75
+
76
+ (bench-task #f
77
+ "henkan-okuri-nashi (Tokyo Cabinet)"
78
+ (for-each
79
+ (lambda (n)
80
+ (henkan-okuri-nashi "uuuu" kvs "henkan" 0))
81
+ (Range.new 0 100)))
82
+ (kvs.close))
83
+
84
+
85
+ ;;===================================================================