Class: LS4::TokyoTyrantMDS

Inherits:
MDS
  • Object
show all
Defined in:
lib/ls4/service/mds_tt.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

#initializeTokyoTyrantMDS

Returns a new instance of TokyoTyrantMDS.



83
84
85
86
87
# File 'lib/ls4/service/mds_tt.rb', line 83

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
# File 'lib/ls4/service/mds_tt.rb', line 34

def self.define_consts
	unless const_defined?(:QRY)
		require 'tokyotyrant'
		const_set(:QRY, TokyoTyrant::RDBQRY)
		HADB.const_set(:TBL, TokyoTyrant::RDBTBL)
		HADB.const_set(:FATAL_ERROR, [
			TokyoTyrant::RDB::EINVALID,
			TokyoTyrant::RDB::ENOHOST,
			TokyoTyrant::RDB::EREFUSED,
			TokyoTyrant::RDB::ESEND,
			TokyoTyrant::RDB::ERECV,
			TokyoTyrant::RDB::EMISC
		])
	end
end

Instance Method Details

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



147
148
149
150
151
152
# File 'lib/ls4/service/mds_tt.rb', line 147

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



106
107
108
# File 'lib/ls4/service/mds_tt.rb', line 106

def close
	@hadb.close
end

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ls4/service/mds_tt.rb', line 200

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



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ls4/service/mds_tt.rb', line 122

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ls4/service/mds_tt.rb', line 110

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



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ls4/service/mds_tt.rb', line 134

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



102
103
104
# File 'lib/ls4/service/mds_tt.rb', line 102

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ls4/service/mds_tt.rb', line 172

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



154
155
156
157
158
159
160
161
# File 'lib/ls4/service/mds_tt.rb', line 154

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



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

def util_locate(key, &cb)
	array = @hadb.read(key) {|rdb|
		qry = QRY.new(rdb)
		qry.addcond(COL_KEY, QRY::QCSTREQ, key)
		qry.searchget(COLS_RESERVED)
	}

	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