rbbt-util 5.34.11 → 5.34.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32b17775f7faf2220a471559cf7faf7db5f2515001a19e1fe5d70f3b588c06dd
4
- data.tar.gz: '09b9518c2dadcdff12799cb9ece8b91a1f096c3953e695854fdf4b937868a345'
3
+ metadata.gz: 1d36e35e1495c3c319e8aa0485851b870e985bc34ea282f21c8e744adc302f50
4
+ data.tar.gz: e4df8738231ce9e1f8ebf70090595413fef4555ad9bedde4b52b36ebc219e39e
5
5
  SHA512:
6
- metadata.gz: 56976a81e8aca514a836fd260d8e57c074252f9ef1e03bfb54496c1107bd6ecf27fa33f9e7d9f4c8ca39f5e8a4a2cc28c560e15cb26ec81855202aea8a53d41c
7
- data.tar.gz: b195a055a913b56dd3cb70dc1da1bede56462a7eb55bce0ff437a53abdf556cee39dad8f8e49a6246705116990a666adc24e354345d5759d1f0d3690df2908dd
6
+ metadata.gz: 3ebc6c5ea53567b06a58854ab0b58c83777d939bb67a80cc28dbe7ebaa992269f59c20c2dba28d799402bfded6cbd9771ad1036e31ebbb365e3995500cd37f44
7
+ data.tar.gz: 2d3870b76a07c096ee1df74593b6bd641c0f1f8aab68c09827bae57cf64ee97e237b22ee6d16edb81ddcc83b7d8b6f4cb6298da8d203c63f8cdebc408b44fb84
@@ -61,8 +61,8 @@ module AnnotatedArray
61
61
 
62
62
  value = annotate(value)
63
63
 
64
- value.container = self
65
- value.container_index = pos
64
+ value.container = self if value.respond_to?(:container)
65
+ value.container_index = pos if value.respond_to?(:container_index)
66
66
 
67
67
  pos += 1
68
68
 
@@ -217,46 +217,66 @@ module Annotated
217
217
  end
218
218
  end
219
219
 
220
- def marshal_dump
221
- Annotated.purge(self).to_sym.to_s
222
- end
223
-
224
220
  def self.to_hash(e)
225
221
  hash = {}
226
- if Array === e && AnntatedArray === e
222
+ if Array === e && AnnotatedArray === e
227
223
  hash[:literal] = Annotated.purge(e)
228
224
  hash[:info] = e.info
229
225
  elsif Array === e
230
226
  hash = e.collect do |_e|
231
227
  _hash = {}
232
- _hash[:literal] = _e
233
- _hash[:info] = _e.info
228
+ _hash[:literal] = _e.dup
229
+ _hash[:info] = _e.info if _e.respond_to?(:info)
234
230
  _hash
235
231
  end
236
232
  else
237
- hash[:literal] = e
233
+ hash[:literal] = e.dup
238
234
  hash[:info] = e.info
239
235
  end
240
236
  hash
241
237
  end
242
238
 
243
239
  def self.load_hash(hash)
244
- literal = hash[:literal]
245
- info = hash[:info]
246
- info[:annotation_types].each do |type|
247
- type = Kernel.const_get(type) if String === type
248
- type.setup(literal, info)
240
+ if Array === hash
241
+ hash.collect{|h| load_hash(h) }
242
+ else
243
+ literal = hash[:literal]
244
+ info = hash[:info]
245
+ info[:annotation_types].each do |type|
246
+ type = Kernel.const_get(type) if String === type
247
+ type.setup(literal, info)
248
+ end
249
+ literal
249
250
  end
250
- literal
251
+ end
252
+
253
+ def marshal_dump
254
+ Annotated.to_hash(self)
251
255
  end
252
256
  end
253
257
 
254
258
  class String
255
- def marshal_load(str)
256
- self.replace str
259
+ def marshal_load(hash)
260
+ if Hash === hash
261
+ e = Annotated.load_hash(hash)
262
+ self.replace e
263
+ e.annotate(self)
264
+ self
265
+ else
266
+ self.replace hash
267
+ end
257
268
  end
258
269
  end
259
270
 
260
-
261
-
262
-
271
+ class Array
272
+ def marshal_load(hash)
273
+ if Hash === hash
274
+ e = Annotated.load_hash(hash)
275
+ self.replace e
276
+ e.annotate(self)
277
+ self
278
+ else
279
+ self.replace hash
280
+ end
281
+ end
282
+ end
data/lib/rbbt/entity.rb CHANGED
@@ -5,7 +5,7 @@ module Entity
5
5
 
6
6
  UNPERSISTED_PREFIX = "entity_unpersisted_property_"
7
7
 
8
- class MultipleEntity < Exception; end
8
+ class MultipleEntity < TryAgain; end
9
9
 
10
10
  class DontPersist < Exception
11
11
  attr_accessor :payload
@@ -120,10 +120,6 @@ module Entity
120
120
  coder.scalar = clean_annotations
121
121
  end
122
122
 
123
- def marshal_dump
124
- clean_annotations
125
- end
126
-
127
123
  def consolidate
128
124
  self.inject(nil){|acc,e|
129
125
  if acc.nil?
@@ -203,9 +199,11 @@ module Entity
203
199
  end
204
200
 
205
201
  define_method name do |*args|
202
+
206
203
  if self.instance_variable_get("@multiple_result_" + name.to_s)
207
204
  return self.instance_variable_get("@multiple_result_" + name.to_s)
208
205
  end
206
+
209
207
  obj = if Array === self
210
208
  self
211
209
  elsif self.respond_to?(:container) && Array === self.container
@@ -217,7 +215,8 @@ module Entity
217
215
  missing = []
218
216
  obj.each do |e|
219
217
  begin
220
- e.send(multi_name)
218
+ res = e.send(multi_name)
219
+ e.instance_variable_set("@multiple_result_" + name.to_s, res)
221
220
  rescue MultipleEntity
222
221
  missing << e
223
222
  end
@@ -227,20 +226,26 @@ module Entity
227
226
  case res
228
227
  when Array
229
228
  missing.zip(res).each do |o,res|
230
- raise "Multiple function #{name} result nil for element #{o}" if res.nil?
229
+ raise "Multiple function '#{name}' result 'nil' for element '#{o}'" if res.nil?
231
230
  o.instance_variable_set("@multiple_result_" + name.to_s, res)
231
+ o.send(multi_name)
232
232
  end
233
233
  when Hash
234
234
  res.each do |o,res|
235
- raise "Multiple function #{name} result nil for element #{o}" if res.nil?
235
+ raise "Multiple function '#{name}' result 'nil' for element '#{o}'" if res.nil?
236
236
  o.instance_variable_set("@multiple_result_" + name.to_s, res)
237
+ o.send(multi_name)
237
238
  end
238
239
  end
239
240
 
240
241
  if Array === self
241
- self.collect{|o| o.send(multi_name)}
242
+ res = self.collect{|o| o.send(multi_name)}
243
+ self.instance_variable_set("@multiple_result_" + name.to_s, res)
244
+ res
242
245
  else
243
- self.send(multi_name)
246
+ res = self.send(multi_name)
247
+ self.instance_variable_set("@multiple_result_" + name.to_s, res)
248
+ res
244
249
  end
245
250
  end
246
251
 
@@ -193,8 +193,12 @@ module Persist
193
193
  end
194
194
 
195
195
  def range(*args)
196
- self.read_lock do
197
- super(*args)
196
+ begin
197
+ self.read_lock do
198
+ super(*args)
199
+ end
200
+ rescue
201
+ []
198
202
  end
199
203
  end
200
204
 
@@ -266,5 +270,6 @@ module Persist
266
270
  end
267
271
  end
268
272
  end
273
+
269
274
  end
270
275
  end
@@ -0,0 +1,22 @@
1
+ module Persist
2
+ module TCAdapter
3
+ def marshal_dump
4
+ [persistence_path, tokyocabinet_class]
5
+ end
6
+ end
7
+ end
8
+
9
+ class TokyoCabinet::BDB
10
+ def marshal_load(values)
11
+ persistence_path, tokyocabinet_class = values
12
+ Persist::TCAdapter.open persistence_path, false, tokyocabinet_class
13
+ end
14
+ end
15
+
16
+ class TokyoCabinet::HDB
17
+ def marshal_load(values)
18
+ persistence_path, tokyocabinet_class = values
19
+ Persist::TCAdapter.open persistence_path, false, tokyocabinet_class
20
+ end
21
+ end
22
+
@@ -101,3 +101,5 @@ module Persist
101
101
  database
102
102
  end
103
103
  end
104
+
105
+ require 'rbbt/persist/tsv/tokyocabinet/marshal'
data/lib/rbbt/persist.rb CHANGED
@@ -359,6 +359,12 @@ module Persist
359
359
  Log.exception $!
360
360
  sleep 1 + rand(2)
361
361
  retry
362
+ rescue TryAgain
363
+ begin
364
+ Open.rm path
365
+ rescue
366
+ end if Open.exists? path
367
+ raise $!
362
368
  rescue Exception
363
369
  Log.medium "Error in persist: #{path}#{Open.exists?(path) ? Log.color(:red, " Erasing") : ""}"
364
370
 
@@ -369,13 +369,14 @@ module TSV
369
369
  def select(method = nil, invert = false, &block)
370
370
  new = TSV.setup({}, :key_field => key_field, :fields => fields, :type => type, :filename => filename, :identifiers => identifiers)
371
371
 
372
- new.key_field = key_field
373
- new.fields = fields.dup unless fields.nil?
374
- new.type = type
375
- new.filename = filename
376
- new.namespace = namespace
377
- new.entity_options = entity_options
378
- new.entity_templates = entity_templates
372
+ self.annotate(new)
373
+ #new.key_field = key_field
374
+ #new.fields = fields.dup unless fields.nil?
375
+ #new.type = type
376
+ #new.filename = filename
377
+ #new.namespace = namespace
378
+ #new.entity_options = entity_options
379
+ #new.entity_templates = entity_templates
379
380
 
380
381
  case
381
382
  when (method.nil? and block_given?)
@@ -0,0 +1,17 @@
1
+ module TSV
2
+ def marshal_dump
3
+ if defined?(Persist::TCAdapter) && Persist::TCAdapter === self
4
+ super
5
+ else
6
+ [info, to_hash]
7
+ end
8
+ end
9
+ end
10
+
11
+ class Hash
12
+ def marshal_load(array)
13
+ info, to_hash = array
14
+ self.merge! to_hash
15
+ TSV.setup(self)
16
+ end
17
+ end
data/lib/rbbt/tsv/util.rb CHANGED
@@ -388,9 +388,6 @@ module TSV
388
388
  new
389
389
  end
390
390
 
391
- def marshal_dump
392
- [info, to_hash]
393
- end
394
391
 
395
392
  def to_onehot(boolean = false)
396
393
  all_values = values.flatten.uniq.collect{|v| v.to_s}.sort
@@ -405,10 +402,3 @@ module TSV
405
402
  end
406
403
  end
407
404
 
408
- class Hash
409
- def marshal_load(array)
410
- info, to_hash = array
411
- self.merge! to_hash
412
- TSV.setup(self)
413
- end
414
- end
data/lib/rbbt/tsv.rb CHANGED
@@ -18,6 +18,7 @@ require 'rbbt/tsv/parallel'
18
18
  require 'rbbt/tsv/stream'
19
19
  require 'rbbt/tsv/melt'
20
20
  require 'rbbt/tsv/matrix'
21
+ require 'rbbt/tsv/marshal'
21
22
 
22
23
  module TSV
23
24
  class << self
@@ -29,6 +29,10 @@ class RbbtProcessQueue
29
29
 
30
30
  def dump(obj, stream)
31
31
  case obj
32
+ when Annotated
33
+ payload = @serializer.dump(obj)
34
+ size_head = [payload.bytesize,"S"].pack 'La'
35
+ str = size_head << payload
32
36
  when String
33
37
  payload = obj
34
38
  size_head = [payload.bytesize,"C"].pack 'La'
@@ -90,7 +94,7 @@ class RbbtProcessQueue
90
94
  def push(obj)
91
95
  RbbtSemaphore.synchronize(@write_sem) do
92
96
  multiple = MultipleResult === obj
93
- obj = Annotated.purge(obj)
97
+ #obj = Annotated.purge(obj)
94
98
  obj.extend MultipleResult if multiple
95
99
  self.dump(obj, @swrite)
96
100
  end
@@ -413,4 +413,15 @@ def self.add_libdir(dir=nil)
413
413
  CMD.cmd("ssh '#{server}' 'shopt -s expand_aliases; bash -l -c \"ruby\"' ", :in => script, :log => true).read
414
414
  end
415
415
 
416
+ def self.timeout_insist(time, msg = nil, &block)
417
+ Misc.insist do
418
+ begin
419
+ Timeout.timeout(time, TryAgain, msg, &block)
420
+ rescue TryAgain
421
+ Log.low "Timeout detected after #{time} seconds"
422
+ raise $!
423
+ end
424
+ end
425
+ end
426
+
416
427
  end
@@ -27,10 +27,11 @@ module Misc
27
27
  params.merge!(:format => _format) unless _format.nil? or (params.include?(:format) and not ((f = params[:format]).nil? or (String === f and f.empty?)))
28
28
 
29
29
  mod = Entity === field ? field : Entity.formats[field]
30
- entity = mod.setup(
31
- ((entity.frozen? and not entity.nil?) ? entity.dup : ((Array === entity and dup_array) ? entity.collect{|e| e.nil? ? e : e.dup} : entity) ),
32
- params
33
- )
30
+
31
+ entity = entity.dup
32
+ entity = (entity.frozen? and not entity.nil?) ? entity.dup : ((Array === entity and dup_array) ? entity.collect{|e| e.nil? ? e : e.dup} : entity)
33
+
34
+ entity = mod.setup(entity, params)
34
35
  end
35
36
 
36
37
  entity
@@ -9,9 +9,19 @@ end
9
9
  class TestAnnotation < Test::Unit::TestCase
10
10
  def test_marshal
11
11
  a = "STRING"
12
- a.extend Annotated
12
+ a = TestEntityString.setup(a)
13
13
 
14
- assert !(Annotated === Marshal.load(Marshal.dump(a)))
14
+ #assert !(Annotated === Marshal.load(Marshal.dump(a)))
15
+ assert_equal a, Marshal.load(Marshal.dump(a))
16
+ end
17
+
18
+ def test_marshal_array
19
+ a = ["STRING"]
20
+ TestEntityString.setup(a)
21
+
22
+ #assert !(Annotated === Marshal.load(Marshal.dump(a)))
23
+ assert Annotated === a
24
+ assert Annotated === a.first
15
25
  assert_equal a, Marshal.load(Marshal.dump(a))
16
26
  end
17
27
 
@@ -20,5 +30,14 @@ class TestAnnotation < Test::Unit::TestCase
20
30
  assert_equal "TEST", Annotated.to_hash(e)[:literal]
21
31
  assert_equal 10, Annotated.to_hash(e)[:info][:code]
22
32
  end
33
+
34
+ def test_traverse_cpus
35
+ strings = (1..10).to_a.collect{|n| n.to_s }
36
+ res = TSV.traverse strings, :cpus => 2, :into => [] do |s|
37
+ TestEntityString.setup(s, :code => s.to_i)
38
+ end
39
+
40
+ assert TestEntityString === res.first
41
+ end
23
42
  end
24
43
 
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/tsv'
3
+ require 'rbbt/tsv/marshal'
4
+ require 'rbbt/util/tmpfile'
5
+ require 'test/unit'
6
+
7
+ class TestTSVUtil < Test::Unit::TestCase
8
+
9
+ def test_marshal
10
+ content =<<-EOF
11
+ #Id ValueA ValueB OtherID
12
+ row1 a|aa|aaa b Id1|Id2
13
+ row2 A B Id3
14
+ EOF
15
+
16
+ TmpFile.with_file(content) do |filename|
17
+ tsv = TSV.open(filename, :sep => /\s+/)
18
+
19
+ assert TSV === Marshal.load(Marshal.dump(tsv))
20
+ assert_equal tsv.to_hash, Marshal.load(Marshal.dump(tsv)).to_hash
21
+ assert_equal({1 => 1}, Marshal.load(Marshal.dump({1 => 1})))
22
+ end
23
+ end
24
+ end
@@ -21,22 +21,6 @@ row2 A B Id3
21
21
  end
22
22
  end
23
23
 
24
- def test_marshal
25
- content =<<-EOF
26
- #Id ValueA ValueB OtherID
27
- row1 a|aa|aaa b Id1|Id2
28
- row2 A B Id3
29
- EOF
30
-
31
- TmpFile.with_file(content) do |filename|
32
- tsv = TSV.open(filename, :sep => /\s+/)
33
-
34
- assert TSV === Marshal.load(Marshal.dump(tsv))
35
- assert_equal tsv.to_hash, Marshal.load(Marshal.dump(tsv)).to_hash
36
- assert_equal({1 => 1}, Marshal.load(Marshal.dump({1 => 1})))
37
- end
38
- end
39
-
40
24
  def test_replicates
41
25
  content =<<-EOF
42
26
  #Id ValueA ValueB OtherID
@@ -7,5 +7,16 @@ class TestMiscDevelopment < Test::Unit::TestCase
7
7
  puts "hola"
8
8
  end
9
9
  end
10
+
11
+ def __test_timeout
12
+ Misc.timeout_insist(2) do
13
+ puts "Start"
14
+ 3.times do
15
+ puts "ping"
16
+ sleep rand(4)
17
+ puts "pong"
18
+ end
19
+ end
20
+ end
10
21
  end
11
22
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-util
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.34.11
4
+ version: 5.34.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -255,6 +255,7 @@ files:
255
255
  - lib/rbbt/persist/tsv/packed_index.rb
256
256
  - lib/rbbt/persist/tsv/sharder.rb
257
257
  - lib/rbbt/persist/tsv/tokyocabinet.rb
258
+ - lib/rbbt/persist/tsv/tokyocabinet/marshal.rb
258
259
  - lib/rbbt/resource.rb
259
260
  - lib/rbbt/resource/path.rb
260
261
  - lib/rbbt/resource/rake.rb
@@ -272,6 +273,7 @@ files:
272
273
  - lib/rbbt/tsv/filter.rb
273
274
  - lib/rbbt/tsv/index.rb
274
275
  - lib/rbbt/tsv/manipulate.rb
276
+ - lib/rbbt/tsv/marshal.rb
275
277
  - lib/rbbt/tsv/matrix.rb
276
278
  - lib/rbbt/tsv/melt.rb
277
279
  - lib/rbbt/tsv/parallel.rb
@@ -536,6 +538,7 @@ files:
536
538
  - test/rbbt/tsv/test_filter.rb
537
539
  - test/rbbt/tsv/test_index.rb
538
540
  - test/rbbt/tsv/test_manipulate.rb
541
+ - test/rbbt/tsv/test_marshal.rb
539
542
  - test/rbbt/tsv/test_matrix.rb
540
543
  - test/rbbt/tsv/test_parallel.rb
541
544
  - test/rbbt/tsv/test_parser.rb
@@ -667,6 +670,7 @@ test_files:
667
670
  - test/rbbt/tsv/test_change_id.rb
668
671
  - test/rbbt/tsv/test_attach.rb
669
672
  - test/rbbt/tsv/test_filter.rb
673
+ - test/rbbt/tsv/test_marshal.rb
670
674
  - test/rbbt/tsv/test_parser.rb
671
675
  - test/rbbt/tsv/test_csv.rb
672
676
  - test/rbbt/tsv/test_accessor.rb