sekka 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/sekka/kvs.rb ADDED
@@ -0,0 +1,135 @@
1
+ # kvs.rb - "wrapper library for Key-Value-Store"
2
+ #
3
+ # Copyright (c) 2010 Kiyoka Nishiyama <kiyoka@sumibi.org>
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions
7
+ # are met:
8
+ #
9
+ # 1. Redistributions of source code must retain the above copyright
10
+ # notice, this list of conditions and the following disclaimer.
11
+ #
12
+ # 2. Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # 3. Neither the name of the authors nor the names of its contributors
17
+ # may be used to endorse or promote products derived from this
18
+ # software without specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26
+ # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ #
32
+ # $Id:
33
+ #
34
+ require 'tokyocabinet'
35
+ require 'memcache'
36
+
37
+
38
+ class Kvs
39
+ def initialize( dbtype )
40
+ @dbtype = dbtype
41
+ case dbtype
42
+ when :tokyocabinet
43
+ @db = TokyoCabinet::HDB.new( )
44
+ when :memcache
45
+ # do nothing
46
+ else
47
+ raise ArgumentError, "Kvs.new() requires reserved DB typename"
48
+ end
49
+ end
50
+
51
+ def open( name )
52
+ case @dbtype
53
+ when :tokyocabinet
54
+ if not @db.open( name, TokyoCabinet::HDB::OWRITER | TokyoCabinet::HDB::OCREAT )
55
+ raise RuntimeError, sprintf( "TokyoCabinet::HDB.open error: file=%s", name )
56
+ end
57
+ when :memcache
58
+ @db = MemCache.new( name )
59
+ else
60
+ raise RuntimeError
61
+ end
62
+ end
63
+
64
+ def put!( key, value, timeout = 0 )
65
+ if 0 < key.size
66
+ #p "put! " + key + ":" + value
67
+ case @dbtype
68
+ when :tokyocabinet
69
+ @db[ key.force_encoding("ASCII-8BIT") ] = value.force_encoding("ASCII-8BIT")
70
+ when :memcache
71
+ @db.set( key.force_encoding("ASCII-8BIT"), value.force_encoding("ASCII-8BIT"), timeout )
72
+ else
73
+ raise RuntimeError
74
+ end
75
+ end
76
+ end
77
+
78
+ def get( key, fallback = false )
79
+ if 0 == key.size
80
+ fallback
81
+ else
82
+ val = @db.get( key )
83
+ if val
84
+ val.force_encoding("UTF-8")
85
+ else
86
+ fallback
87
+ end
88
+ end
89
+ end
90
+
91
+ def delete( key )
92
+ @db.delete( key )
93
+ end
94
+
95
+ def clear()
96
+ case @dbtype
97
+ when :tokyocabinet
98
+ @db.clear
99
+ when :memcache
100
+ # do nothing
101
+ else
102
+ raise RuntimeError
103
+ end
104
+ end
105
+
106
+ # return array of key string
107
+ def keys()
108
+ case @dbtype
109
+ when :tokyocabinet
110
+ @db.keys.map { |k|
111
+ k.force_encoding("UTF-8")
112
+ }
113
+ when :memcache
114
+ raise RuntimeError, "Kvs#keys method was not implemented for memcache."
115
+ else
116
+ raise RuntimeError
117
+ end
118
+ end
119
+
120
+ def close()
121
+ case @dbtype
122
+ when :tokyocabinet
123
+ @db.close
124
+ when :memcache
125
+ # do nothign
126
+ else
127
+ raise RuntimeError
128
+ end
129
+ end
130
+
131
+ ## for testing
132
+ def _db()
133
+ @db
134
+ end
135
+ end