Class: LS4::LocalTokyoCabinetMDS

Inherits:
MDS
  • Object
show all
Defined in:
lib/ls4/service/mds_tc.rb

Defined Under Namespace

Classes: HADB

Constant Summary collapse

COL_PK =
''
COL_KEY =
'_key'
COL_VTIME =
'_time'
COL_RSID =
'_rsid'
COL_VNAME =
'_vname'
COL_REMOVED =
'_removed'
COLS_RESERVED =
[COL_PK, COL_KEY, COL_VTIME, COL_RSID, COL_VNAME, COL_REMOVED]
COLS_REQUIRED =
[COL_PK, COL_KEY, COL_VTIME, COL_RSID]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalTokyoCabinetMDS

Returns a new instance of LocalTokyoCabinetMDS.



102
103
104
105
106
# File 'lib/ls4/service/mds_tc.rb', line 102

def initialize
	self.class.define_consts
	@random = Random.new
	@pid = Process.pid
end

Class Method Details

.define_constsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ls4/service/mds_tc.rb', line 34

def self.define_consts
	unless const_defined?(:QRY)
		require 'tokyocabinet'
		const_set(:QRY, TokyoCabinet::TDBQRY)
		HADB.const_set(:TDB, TokyoCabinet::TDB)
		HADB.const_set(:FATAL_ERROR, [
			TokyoCabinet::TDB::ETHREAD,
			TokyoCabinet::TDB::EINVALID,
			TokyoCabinet::TDB::ENOFILE,
			TokyoCabinet::TDB::ENOPERM,
			TokyoCabinet::TDB::EMETA,
			TokyoCabinet::TDB::ERHEAD,
			TokyoCabinet::TDB::EOPEN,
			TokyoCabinet::TDB::ECLOSE,
			TokyoCabinet::TDB::ETRUNC,
			TokyoCabinet::TDB::ESYNC,
			TokyoCabinet::TDB::ESTAT,
			TokyoCabinet::TDB::ESEEK,
			TokyoCabinet::TDB::EREAD,
			TokyoCabinet::TDB::EWRITE,
			TokyoCabinet::TDB::EMMAP,
			TokyoCabinet::TDB::ELOCK,
			TokyoCabinet::TDB::EUNLINK,
			TokyoCabinet::TDB::ERENAME,
			TokyoCabinet::TDB::EMKDIR,
			TokyoCabinet::TDB::ERMDIR,
			#TokyoCabinet::TDB::EKEEP,
			#TokyoCabinet::TDB::ENOREC,
			TokyoCabinet::TDB::EMISC
		])
	end
end

Instance Method Details

#add(key, attrs = {}, vname = nil, &cb) ⇒ Object



166
167
168
169
170
171
# File 'lib/ls4/service/mds_tc.rb', line 166

def add(key, attrs={}, vname=nil, &cb)
	okey = add_impl(key, attrs, vname)
	cb.call(okey, nil) rescue nil
rescue
	cb.call(nil, $!) rescue nil
end

#closeObject



125
126
127
# File 'lib/ls4/service/mds_tc.rb', line 125

def close
	@hadb.close
end

#delete(key, version = nil, &cb) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ls4/service/mds_tc.rb', line 219

def delete(key, version=nil, &cb)
	map = get_impl(key, version, COLS_RESERVED)
	if map && !is_removed(map)
		okey = to_okey(map)

		pk = map[COL_PK]
		@hadb.write(key) {|rdb| rdb.delete(pk) }

		cb.call(okey, nil) rescue nil

	else
		cb.call(nil, nil) rescue nil
	end
rescue
	cb.call(nil, $!) rescue nil
end

#get_attrs(key, version = nil, &cb) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ls4/service/mds_tc.rb', line 141

def get_attrs(key, version=nil, &cb)
	map = get_impl(key, version)
	if map && !is_removed(map)
		attrs = to_attrs(map)
		cb.call(attrs, nil) rescue nil
	else
		cb.call(nil, nil) rescue nil
	end
rescue
	cb.call(nil, $!) rescue nil
end

#get_okey(key, version = nil, &cb) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ls4/service/mds_tc.rb', line 129

def get_okey(key, version=nil, &cb)
	map = get_impl(key, version, COLS_RESERVED)
	if map && !is_removed(map)
		okey = to_okey(map)
		cb.call(okey, nil) rescue nil
	else
		cb.call(nil, nil) rescue nil
	end
rescue
	cb.call(nil, $!) rescue nil
end

#get_okey_attrs(key, version = nil, &cb) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ls4/service/mds_tc.rb', line 153

def get_okey_attrs(key, version=nil, &cb)
	map = get_impl(key, version)
	if map && !is_removed(map)
		okey = to_okey(map)
		attrs = to_attrs(map)
		cb.call([okey, attrs], nil) rescue nil
	else
		cb.call(nil, nil) rescue nil
	end
rescue
	cb.call(nil, $!) rescue nil
end

#open(expr) ⇒ Object



121
122
123
# File 'lib/ls4/service/mds_tc.rb', line 121

def open(expr)
	@hadb = HADB.new(expr)
end

#remove(key, &cb) ⇒ Object

def merge_attrs(key, attrs, &cb) okey = update_impl(key) {|old_attrs| old_attrs.merge(attrs) } cb.call(okey, nil) rescue nil rescue cb.call(nil, $!) rescue nil end



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ls4/service/mds_tc.rb', line 191

def remove(key, &cb)
	map = get_impl_head(key, COLS_REQUIRED)
	if map && !is_removed(map)
		okey = to_okey(map)

		# optional: inherit rsid
		rsid = map[COL_RSID].to_i

		# get current vtime later than old vtime
		vtime = get_current_vtime(map[COL_VTIME].to_i)

		remove_okey = new_okey(key, vtime, rsid)

		# insert
		pk = new_pk(map[COL_PK])
		map = to_map({}, remove_okey, nil, true)
		@hadb.write(key) {|rdb| rdb.put(pk, map) }

		cb.call(okey, nil) rescue nil

	else
		cb.call(nil, nil) rescue nil
	end

rescue
	cb.call(nil, $!) rescue nil
end

#update_attrs(key, attrs, &cb) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/ls4/service/mds_tc.rb', line 173

def update_attrs(key, attrs, &cb)
	okey = update_impl(key) {|old_attrs|
		attrs
	}
	cb.call(okey, nil) rescue nil
rescue
	cb.call(nil, $!) rescue nil
end

#util_locate(key, &cb) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ls4/service/mds_tc.rb', line 236

def util_locate(key, &cb)
	array = @hadb.read(key) {|rdb|
		qry = QRY.new(rdb)
		qry.addcond(COL_KEY, QRY::QCSTREQ, key)
		qry.search.map {|key|
			rdb.get(key)
			map[COL_PK] = key
			map
		}
	}

	array.reject! {|map|
		!is_valid_map(map) || is_removed(map)
	}

	array.map! {|map|
		[to_okey(map), map[COL_VNAME]]
	}

	cb.call(array, nil) rescue nil

rescue
	cb.call(nil, $!) rescue nil
end