transit-ruby 0.8.572 → 0.8.586

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
  SHA1:
3
- metadata.gz: 5fbd02765bf5e56918772bd7f95227aaa3a93d2a
4
- data.tar.gz: 6a4c50abf34ccb4b3b8a7968d3e0284041a5cb81
3
+ metadata.gz: d01cddb9a52ad5f336423f8bad2a06bd46483886
4
+ data.tar.gz: abbc67aed1cde0c0a2e3abedd3b35b8f5644c11e
5
5
  SHA512:
6
- metadata.gz: 86c8d24ffa6d87f8327fdb530b5a47e332eb81cbcb0dcf92debb145caef32ee03d259d9ad1dda826435fa125d55dcc505989eb5d30bb8c2b36ebd04290290695
7
- data.tar.gz: 4b5198fda8bf5016cf94c2a8399a7be5786ba89b9b2a7c609c1fe81ebb15ef0ab505814defe2adf0c5d91e0f691a8fd653da58ecdc2d6549b2a61a7089b19c0b
6
+ metadata.gz: 484fe240df8ee5cdde7b0daa55957f50e06a8689e5470fe4c306afb7924c3dfe826fb8d7bc7888e9fec48e4c84df71580b4aeba86c618da9cb97e4fb15f94fef
7
+ data.tar.gz: 2f8ae4b32c5a95e0af41840cd816227503b5a59657d6ca404e7d3978ddfdac7c27d39c1027427306f632755ea124660d87fa343290e09cc7d425904bd47f0203
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ### 0.8.586 / 2015-03-13
2
+
3
+ * Add handler caching for MRI
4
+ * Bump to transit-java-0.8.285 for handler caching in JRuby
5
+
6
+ ### 0.8.572 / 2015-01-15
7
+
8
+ * Marshal int map keys as ints in msgpack
9
+
1
10
  ### 0.8.569 / 2014-12-03
2
11
 
3
12
  * ByteArray#to_s forces default encoding for platform
@@ -16,6 +16,9 @@ module Transit
16
16
  # Converts a transit value to an instance of a type
17
17
  # @api private
18
18
  class Decoder
19
+ MUTEX = Mutex.new
20
+ HANDLER_CACHE = {}
21
+
19
22
  ESC_ESC = "#{ESC}#{ESC}"
20
23
  ESC_SUB = "#{ESC}#{SUB}"
21
24
  ESC_RES = "#{ESC}#{RES}"
@@ -27,7 +30,14 @@ module Transit
27
30
  def initialize(options={})
28
31
  custom_handlers = options[:handlers] || {}
29
32
  custom_handlers.each {|k,v| validate_handler(k,v)}
30
- @handlers = ReadHandlers::DEFAULT_READ_HANDLERS.merge(custom_handlers)
33
+ MUTEX.synchronize do
34
+ if HANDLER_CACHE.has_key?(custom_handlers)
35
+ @handlers = HANDLER_CACHE[custom_handlers]
36
+ else
37
+ @handlers = ReadHandlers::DEFAULT_READ_HANDLERS.merge(custom_handlers)
38
+ end
39
+
40
+ end
31
41
  @default_handler = options[:default_handler] || ReadHandlers::DEFAULT_READ_HANDLER
32
42
  end
33
43
 
@@ -17,21 +17,49 @@ module Transit
17
17
  # @see https://github.com/cognitect/transit-format
18
18
  module Marshaler
19
19
 
20
+ HANDLER_CACHE = {}
21
+ VERBOSE_HANDLER_CACHE = {}
22
+ MUTEX = Mutex.new
23
+
24
+ # @api private
25
+ # Included in VerboseJson subclasses. Defined here to make it
26
+ # available in CRuby and JRuby environments.
27
+ module VerboseHandlers
28
+ def build_handlers(custom_handlers)
29
+ if VERBOSE_HANDLER_CACHE.has_key?(custom_handlers)
30
+ VERBOSE_HANDLER_CACHE[custom_handlers]
31
+ else
32
+ handlers = super(custom_handlers).reduce({}) do |h, (k,v)|
33
+ if v.respond_to?(:verbose_handler) && vh = v.verbose_handler
34
+ h.store(k, vh)
35
+ else
36
+ h.store(k, v)
37
+ end
38
+ h
39
+ end
40
+ VERBOSE_HANDLER_CACHE[custom_handlers] = handlers
41
+ handlers
42
+ end
43
+ end
44
+ end
45
+
20
46
  # @api private
21
47
  module Base
22
48
  def parse_options(opts)
23
- @cache_enabled = !opts[:verbose]
24
- @prefer_strings = opts[:prefer_strings]
25
- @max_int = opts[:max_int]
26
- @min_int = opts[:min_int]
27
-
28
- handlers = WriteHandlers::DEFAULT_WRITE_HANDLERS.dup
29
- handlers = handlers.merge!(opts[:handlers]) if opts[:handlers]
30
- @handlers = (opts[:verbose] ? verbose_handlers(handlers) : handlers)
31
- @handlers.values.each do |h|
32
- if h.respond_to?(:handlers=)
33
- h.handlers=(@handlers)
34
- end
49
+ MUTEX.synchronize do
50
+ @handlers = build_handlers(opts[:handlers])
51
+ end
52
+ @handlers.values.each { |h| h.handlers=(@handlers) if h.respond_to?(:handlers=) }
53
+ end
54
+
55
+ def build_handlers(custom_handlers)
56
+ if HANDLER_CACHE.has_key?(custom_handlers)
57
+ HANDLER_CACHE[custom_handlers]
58
+ else
59
+ handlers = WriteHandlers::DEFAULT_WRITE_HANDLERS.dup
60
+ handlers.merge!(custom_handlers) if custom_handlers
61
+ HANDLER_CACHE[custom_handlers] = handlers
62
+ handlers
35
63
  end
36
64
  end
37
65
 
@@ -44,15 +72,6 @@ module Transit
44
72
  nil
45
73
  end
46
74
 
47
- def verbose_handlers(handlers)
48
- handlers.each do |k, v|
49
- if v.respond_to?(:verbose_handler) && vh = v.verbose_handler
50
- handlers.store(k, vh)
51
- end
52
- end
53
- handlers
54
- end
55
-
56
75
  def escape(s)
57
76
  if s.start_with?(SUB,ESC,RES) && s != "#{SUB} "
58
77
  "#{ESC}#{s}"
@@ -67,7 +86,7 @@ module Transit
67
86
 
68
87
  def emit_string(prefix, tag, value, as_map_key, cache)
69
88
  encoded = "#{prefix}#{tag}#{value}"
70
- if @cache_enabled && cache.cacheable?(encoded, as_map_key)
89
+ if cache.cacheable?(encoded, as_map_key)
71
90
  emit_value(cache.write(encoded), as_map_key)
72
91
  else
73
92
  emit_value(encoded, as_map_key)
@@ -19,16 +19,13 @@ module Transit
19
19
  class BaseJson
20
20
  include Transit::Marshaler::Base
21
21
 
22
- def default_opts
23
- {:prefer_strings => true,
24
- :max_int => JSON_MAX_INT,
25
- :min_int => JSON_MIN_INT}
26
- end
27
-
28
22
  def initialize(io, opts)
29
23
  @oj = Oj::StreamWriter.new(io,opts.delete(:oj_opts) || {})
30
- parse_options(default_opts.merge(opts))
31
24
  @state = []
25
+ @max_int = JSON_MAX_INT
26
+ @min_int = JSON_MIN_INT
27
+ @prefer_strings = true
28
+ parse_options(opts)
32
29
  end
33
30
 
34
31
  def emit_array_start(size)
@@ -87,6 +84,8 @@ module Transit
87
84
 
88
85
  # @api private
89
86
  class VerboseJson < BaseJson
87
+ include Transit::Marshaler::VerboseHandlers
88
+
90
89
  def emit_string(prefix, tag, value, as_map_key, cache)
91
90
  emit_value("#{prefix}#{tag}#{value}", as_map_key)
92
91
  end
@@ -19,16 +19,13 @@ module Transit
19
19
  class MessagePack
20
20
  include Transit::Marshaler::Base
21
21
 
22
- def default_opts
23
- {:prefer_strings => false,
24
- :max_int => MAX_INT,
25
- :min_int => MIN_INT}
26
- end
27
-
28
22
  def initialize(io, opts)
29
23
  @io = io
30
24
  @packer = ::MessagePack::Packer.new(io)
31
- parse_options(default_opts.merge(opts))
25
+ @max_int = MAX_INT
26
+ @min_int = MIN_INT
27
+ @prefer_strings = false
28
+ parse_options(opts)
32
29
  end
33
30
 
34
31
  def emit_array_start(size)
@@ -39,21 +39,12 @@ module Transit
39
39
  def initialize(format, io, opts={})
40
40
  @marshaler = case format
41
41
  when :json
42
- Marshaler::Json.new(io,
43
- {:prefer_strings => true,
44
- :verbose => false,
45
- :handlers => {},
46
- :oj_opts => {:indent => -1}}.merge(opts))
42
+ Marshaler::Json.new(io, {:handlers => {},
43
+ :oj_opts => {:indent => -1}}.merge(opts))
47
44
  when :json_verbose
48
- Marshaler::VerboseJson.new(io,
49
- {:prefer_strings => true,
50
- :verbose => true,
51
- :handlers => {}}.merge(opts))
45
+ Marshaler::VerboseJson.new(io, {:handlers => {}}.merge(opts))
52
46
  else
53
- Marshaler::MessagePack.new(io,
54
- {:prefer_strings => false,
55
- :verbose => false,
56
- :handlers => {}}.merge(opts))
47
+ Marshaler::MessagePack.new(io, {:handlers => {}}.merge(opts))
57
48
  end
58
49
  end
59
50
 
@@ -0,0 +1,30 @@
1
+ module Transit
2
+ describe Marshaler do
3
+ it "caches non-verbose handlers" do
4
+ io = StringIO.new
5
+ first = Transit::Marshaler::Json.new(io,{}).instance_variable_get("@handlers")
6
+ second = Transit::Marshaler::Json.new(io,{}).instance_variable_get("@handlers")
7
+ third = Transit::Marshaler::MessagePack.new(io,{}).instance_variable_get("@handlers")
8
+ assert { first }
9
+ assert { first.equal?(second) }
10
+ assert { second.equal?(third) }
11
+ end
12
+
13
+ it "caches verbose handlers" do
14
+ io = StringIO.new
15
+ first = Transit::Marshaler::VerboseJson.new(io,{}).instance_variable_get("@handlers")
16
+ second = Transit::Marshaler::VerboseJson.new(io,{}).instance_variable_get("@handlers")
17
+ assert { first }
18
+ assert { first.equal?(second) }
19
+ end
20
+
21
+ it "caches verbose and non-verbose handlers separately" do
22
+ io = StringIO.new
23
+ first = Transit::Marshaler::Json.new(io,{}).instance_variable_get("@handlers")
24
+ second = Transit::Marshaler::VerboseJson.new(io,{}).instance_variable_get("@handlers")
25
+ assert { first }
26
+ assert { second }
27
+ assert { !first.equal?(second) }
28
+ end
29
+ end
30
+ end
@@ -47,7 +47,12 @@ module Transit
47
47
  writer = Transit::Writer.new(type, io)
48
48
  inputs.each {|i| writer.write(i)}
49
49
  reader = Transit::Reader.new(type, StringIO.new(io.string))
50
- reader.read {|val| outputs << val}
50
+ if Transit::jruby?
51
+ # Ignore expected EOFException raised after the StringIO is exhausted
52
+ reader.read {|val| outputs << val} rescue nil
53
+ else
54
+ reader.read {|val| outputs << val}
55
+ end
51
56
 
52
57
  assert { outputs == inputs }
53
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transit-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.572
4
+ version: 0.8.586
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russ Olsen
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-01-16 00:00:00.000000000 Z
13
+ date: 2015-03-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: oj
@@ -170,6 +170,7 @@ files:
170
170
  - spec/transit/date_time_util_spec.rb
171
171
  - spec/transit/decoder_spec.rb
172
172
  - spec/transit/exemplar_spec.rb
173
+ - spec/transit/marshaler_spec.rb
173
174
  - spec/transit/reader_spec.rb
174
175
  - spec/transit/rolling_cache_spec.rb
175
176
  - spec/transit/round_trip_spec.rb
@@ -204,6 +205,7 @@ test_files:
204
205
  - spec/transit/date_time_util_spec.rb
205
206
  - spec/transit/decoder_spec.rb
206
207
  - spec/transit/exemplar_spec.rb
208
+ - spec/transit/marshaler_spec.rb
207
209
  - spec/transit/reader_spec.rb
208
210
  - spec/transit/rolling_cache_spec.rb
209
211
  - spec/transit/round_trip_spec.rb