tros 1.7.6.1 → 1.7.6.2
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/.gitignore +1 -0
- data/Gemfile.lock +2 -1
- data/lib/tros.rb +2 -2
- data/lib/tros/data_file.rb +1 -1
- data/lib/tros/io.rb +14 -16
- data/lib/tros/ipc.rb +12 -12
- data/lib/tros/protocol.rb +1 -1
- data/lib/tros/schema.rb +20 -9
- data/lib/tros/version.rb +1 -1
- metadata +7 -1
data/Gemfile.lock
CHANGED
data/lib/tros.rb
CHANGED
@@ -22,9 +22,9 @@ require 'stringio'
|
|
22
22
|
require 'zlib'
|
23
23
|
|
24
24
|
module Tros
|
25
|
-
class
|
25
|
+
class AvroError < StandardError; end
|
26
26
|
|
27
|
-
class
|
27
|
+
class AvroTypeError < Tros::AvroError
|
28
28
|
def initialize(schm=nil, datum=nil, msg=nil)
|
29
29
|
msg ||= "Not a #{schm.to_s}: #{datum}"
|
30
30
|
super(msg)
|
data/lib/tros/data_file.rb
CHANGED
@@ -26,7 +26,7 @@ module Tros
|
|
26
26
|
META_SCHEMA = Schema.parse('{"type": "map", "values": "bytes"}')
|
27
27
|
VALID_ENCODINGS = ['binary'] # not used yet
|
28
28
|
|
29
|
-
class DataFileError <
|
29
|
+
class DataFileError < AvroError; end
|
30
30
|
|
31
31
|
def self.open(file_path, mode='r', schema=nil, codec=nil)
|
32
32
|
schema = Tros::Schema.parse(schema) if schema
|
data/lib/tros/io.rb
CHANGED
@@ -17,14 +17,14 @@
|
|
17
17
|
module Tros
|
18
18
|
module IO
|
19
19
|
# Raised when datum is not an example of schema
|
20
|
-
class AvroTypeError <
|
20
|
+
class AvroTypeError < AvroError
|
21
21
|
def initialize(expected_schema, datum)
|
22
22
|
super("The datum #{datum.inspect} is not an example of schema #{expected_schema}")
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
# Raised when writer's and reader's schema do not match
|
27
|
-
class SchemaMatchException <
|
27
|
+
class SchemaMatchException < AvroError
|
28
28
|
def initialize(writers_schema, readers_schema)
|
29
29
|
super("Writer's schema #{writers_schema} and Reader's schema " +
|
30
30
|
"#{readers_schema} do not match.")
|
@@ -205,7 +205,7 @@ module Tros
|
|
205
205
|
# A string is encoded as a long followed by that many bytes of
|
206
206
|
# UTF-8 encoded character data
|
207
207
|
def write_string(datum)
|
208
|
-
write_bytes(datum.encode('UTF-8'))
|
208
|
+
write_bytes(datum.encode('UTF-8').force_encoding('binary'))
|
209
209
|
end
|
210
210
|
|
211
211
|
# Write an arbritary datum.
|
@@ -304,7 +304,7 @@ module Tros
|
|
304
304
|
when :union; read_union(writers_schema, readers_schema, decoder)
|
305
305
|
when :record, :error, :request; read_record(writers_schema, readers_schema, decoder)
|
306
306
|
else
|
307
|
-
raise
|
307
|
+
raise AvroError, "Cannot read unknown schema type: #{writers_schema.type}"
|
308
308
|
end
|
309
309
|
end
|
310
310
|
|
@@ -441,7 +441,7 @@ module Tros
|
|
441
441
|
return read_record
|
442
442
|
else
|
443
443
|
fail_msg = "Unknown type: #{field_schema.type}"
|
444
|
-
raise
|
444
|
+
raise AvroError, fail_msg
|
445
445
|
end
|
446
446
|
end
|
447
447
|
|
@@ -476,7 +476,7 @@ module Tros
|
|
476
476
|
when :record, :error, :request
|
477
477
|
skip_record(writers_schema, decoder)
|
478
478
|
else
|
479
|
-
raise
|
479
|
+
raise AvroError, "Unknown schema type: #{writers_schema.type}"
|
480
480
|
end
|
481
481
|
end
|
482
482
|
|
@@ -535,7 +535,7 @@ module Tros
|
|
535
535
|
|
536
536
|
def write_data(writers_schema, datum, encoder)
|
537
537
|
unless Schema.validate(writers_schema, datum)
|
538
|
-
raise
|
538
|
+
raise AvroTypeError.new(writers_schema, datum)
|
539
539
|
end
|
540
540
|
|
541
541
|
# function dispatch to write datum
|
@@ -555,7 +555,7 @@ module Tros
|
|
555
555
|
when :union; write_union(writers_schema, datum, encoder)
|
556
556
|
when :record, :error, :request; write_record(writers_schema, datum, encoder)
|
557
557
|
else
|
558
|
-
raise
|
558
|
+
raise AvroError.new("Unknown type: #{writers_schema.type}")
|
559
559
|
end
|
560
560
|
end
|
561
561
|
|
@@ -590,14 +590,12 @@ module Tros
|
|
590
590
|
end
|
591
591
|
|
592
592
|
def write_union(writers_schema, datum, encoder)
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
encoder.write_long(index_of_schema)
|
600
|
-
write_data(writers_schema.schemas[index_of_schema], datum, encoder)
|
593
|
+
index = writers_schema.schemas.find_index { |s| Schema.validate_strictly(s, datum) }
|
594
|
+
index ||= writers_schema.schemas.find_index { |s| Schema.validate(s, datum) }
|
595
|
+
raise AvroTypeError.new(writers_schema, datum) if index.nil?
|
596
|
+
|
597
|
+
encoder.write_long(index)
|
598
|
+
write_data(writers_schema.schemas[index], datum, encoder)
|
601
599
|
end
|
602
600
|
|
603
601
|
def write_record(writers_schema, datum, encoder)
|
data/lib/tros/ipc.rb
CHANGED
@@ -18,7 +18,7 @@ require "net/http"
|
|
18
18
|
|
19
19
|
module Tros::IPC
|
20
20
|
|
21
|
-
class TrosRemoteError < Tros::
|
21
|
+
class TrosRemoteError < Tros::AvroError; end
|
22
22
|
|
23
23
|
HANDSHAKE_REQUEST_SCHEMA = Tros::Schema.parse <<-JSON
|
24
24
|
{
|
@@ -70,9 +70,9 @@ module Tros::IPC
|
|
70
70
|
BUFFER_SIZE = 8192
|
71
71
|
|
72
72
|
# Raised when an error message is sent by an Tros requestor or responder.
|
73
|
-
class TrosRemoteException < Tros::
|
73
|
+
class TrosRemoteException < Tros::AvroError; end
|
74
74
|
|
75
|
-
class ConnectionClosedException < Tros::
|
75
|
+
class ConnectionClosedException < Tros::AvroError; end
|
76
76
|
|
77
77
|
class Requestor
|
78
78
|
"""Base class for the client side of a protocol interaction."""
|
@@ -149,7 +149,7 @@ module Tros::IPC
|
|
149
149
|
|
150
150
|
message = local_protocol.messages[message_name]
|
151
151
|
unless message
|
152
|
-
raise
|
152
|
+
raise AvroError, "Unknown message: #{message_name}"
|
153
153
|
end
|
154
154
|
encoder.write_string(message.name)
|
155
155
|
|
@@ -170,18 +170,18 @@ module Tros::IPC
|
|
170
170
|
self.send_protocol = false
|
171
171
|
we_have_matching_schema = true
|
172
172
|
when 'CLIENT'
|
173
|
-
raise
|
173
|
+
raise AvroError.new('Handshake failure. match == CLIENT') if send_protocol
|
174
174
|
self.remote_protocol = Tros::Protocol.parse(handshake_response['serverProtocol'])
|
175
175
|
self.remote_hash = handshake_response['serverHash']
|
176
176
|
self.send_protocol = false
|
177
177
|
we_have_matching_schema = true
|
178
178
|
when 'NONE'
|
179
|
-
raise
|
179
|
+
raise AvroError.new('Handshake failure. match == NONE') if send_protocol
|
180
180
|
self.remote_protocol = Tros::Protocol.parse(handshake_response['serverProtocol'])
|
181
181
|
self.remote_hash = handshake_response['serverHash']
|
182
182
|
self.send_protocol = true
|
183
183
|
else
|
184
|
-
raise
|
184
|
+
raise AvroError.new("Unexpected match: #{match}")
|
185
185
|
end
|
186
186
|
|
187
187
|
return we_have_matching_schema
|
@@ -199,12 +199,12 @@ module Tros::IPC
|
|
199
199
|
|
200
200
|
# remote response schema
|
201
201
|
remote_message_schema = remote_protocol.messages[message_name]
|
202
|
-
raise
|
202
|
+
raise AvroError.new("Unknown remote message: #{message_name}") unless remote_message_schema
|
203
203
|
|
204
204
|
# local response schema
|
205
205
|
local_message_schema = local_protocol.messages[message_name]
|
206
206
|
unless local_message_schema
|
207
|
-
raise
|
207
|
+
raise AvroError.new("Unknown local message: #{message_name}")
|
208
208
|
end
|
209
209
|
|
210
210
|
# error flag
|
@@ -264,11 +264,11 @@ module Tros::IPC
|
|
264
264
|
# schema resolution (one fine day)
|
265
265
|
remote_message = remote_protocol.messages[remote_message_name]
|
266
266
|
unless remote_message
|
267
|
-
raise
|
267
|
+
raise AvroError.new("Unknown remote message: #{remote_message_name}")
|
268
268
|
end
|
269
269
|
local_message = local_protocol.messages[remote_message_name]
|
270
270
|
unless local_message
|
271
|
-
raise
|
271
|
+
raise AvroError.new("Unknown local message: #{remote_message_name}")
|
272
272
|
end
|
273
273
|
writers_schema = remote_message.request
|
274
274
|
readers_schema = local_message.request
|
@@ -292,7 +292,7 @@ module Tros::IPC
|
|
292
292
|
writers_schema = local_message.errors || SYSTEM_ERROR_SCHEMA
|
293
293
|
write_error(writers_schema, error, buffer_encoder)
|
294
294
|
end
|
295
|
-
rescue Tros::
|
295
|
+
rescue Tros::AvroError => e
|
296
296
|
error = TrosRemoteException.new(e.to_s)
|
297
297
|
buffer_encoder = Tros::IO::BinaryEncoder.new(StringIO.new)
|
298
298
|
META_WRITER.write(response_metadata, buffer_encoder)
|
data/lib/tros/protocol.rb
CHANGED
@@ -18,7 +18,7 @@ module Tros
|
|
18
18
|
class Protocol
|
19
19
|
VALID_TYPE_SCHEMA_TYPES = Set.new(%w[enum record error fixed])
|
20
20
|
VALID_TYPE_SCHEMA_TYPES_SYM = Set.new(VALID_TYPE_SCHEMA_TYPES.map(&:to_sym))
|
21
|
-
class ProtocolParseError < Tros::
|
21
|
+
class ProtocolParseError < Tros::AvroError; end
|
22
22
|
|
23
23
|
attr_reader :name, :namespace, :types, :messages, :md5
|
24
24
|
def self.parse(protocol_string)
|
data/lib/tros/schema.rb
CHANGED
@@ -93,6 +93,17 @@ module Tros
|
|
93
93
|
|
94
94
|
# Determine if a ruby datum is an instance of a schema
|
95
95
|
def self.validate(expected_schema, datum)
|
96
|
+
return true if validate_strictly(expected_schema, datum)
|
97
|
+
case expected_schema.type_sym
|
98
|
+
when :float, :double
|
99
|
+
datum.is_a?(Float) || datum.is_a?(Fixnum) || datum.is_a?(Bignum)
|
100
|
+
else
|
101
|
+
return false
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Determine if a ruby datum is an instance of a schema
|
106
|
+
def self.validate_strictly(expected_schema, datum)
|
96
107
|
case expected_schema.type_sym
|
97
108
|
when :null
|
98
109
|
datum.nil?
|
@@ -107,24 +118,24 @@ module Tros
|
|
107
118
|
(datum.is_a?(Fixnum) || datum.is_a?(Bignum)) &&
|
108
119
|
(LONG_MIN_VALUE <= datum) && (datum <= LONG_MAX_VALUE)
|
109
120
|
when :float, :double
|
110
|
-
datum.is_a?(Float)
|
121
|
+
datum.is_a?(Float)
|
111
122
|
when :fixed
|
112
123
|
datum.is_a?(String) && datum.size == expected_schema.size
|
113
124
|
when :enum
|
114
125
|
expected_schema.symbols.include? datum
|
115
126
|
when :array
|
116
127
|
datum.is_a?(Array) &&
|
117
|
-
datum.all?{|d|
|
128
|
+
datum.all?{|d| validate_strictly(expected_schema.items, d) }
|
118
129
|
when :map
|
119
130
|
datum.keys.all?{|k| k.is_a? String } &&
|
120
|
-
datum.values.all?{|v|
|
131
|
+
datum.values.all?{|v| validate_strictly(expected_schema.values, v) }
|
121
132
|
when :union
|
122
|
-
expected_schema.schemas.any?{|s|
|
133
|
+
expected_schema.schemas.any?{|s| validate_strictly(s, datum) }
|
123
134
|
when :record, :error, :request
|
124
135
|
datum.is_a?(Hash) &&
|
125
|
-
expected_schema.fields.all?{|f|
|
136
|
+
expected_schema.fields.all?{|f| validate_strictly(f.type, datum[f.name]) }
|
126
137
|
else
|
127
|
-
raise "
|
138
|
+
raise TypeError, "#{expected_schema.inspect} is not recognized as type."
|
128
139
|
end
|
129
140
|
end
|
130
141
|
|
@@ -322,7 +333,7 @@ module Tros
|
|
322
333
|
elsif PRIMITIVE_TYPES.include?(type)
|
323
334
|
super(type.to_sym)
|
324
335
|
else
|
325
|
-
raise
|
336
|
+
raise AvroError.new("#{type} is not a valid primitive type.")
|
326
337
|
end
|
327
338
|
end
|
328
339
|
|
@@ -337,7 +348,7 @@ module Tros
|
|
337
348
|
def initialize(name, space, size, names=nil)
|
338
349
|
# Ensure valid cto args
|
339
350
|
unless size.is_a?(Fixnum) || size.is_a?(Bignum)
|
340
|
-
raise
|
351
|
+
raise AvroError, 'Fixed Schema requires a valid integer for size property.'
|
341
352
|
end
|
342
353
|
super(:fixed, name, space, names)
|
343
354
|
@size = size
|
@@ -368,7 +379,7 @@ module Tros
|
|
368
379
|
end
|
369
380
|
end
|
370
381
|
|
371
|
-
class SchemaParseError <
|
382
|
+
class SchemaParseError < AvroError; end
|
372
383
|
|
373
384
|
module Name
|
374
385
|
def self.extract_namespace(name, namespace)
|
data/lib/tros/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tros
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.6.
|
4
|
+
version: 1.7.6.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -103,12 +103,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
103
|
- - ! '>='
|
104
104
|
- !ruby/object:Gem::Version
|
105
105
|
version: '0'
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
hash: 141493864149925103
|
106
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
110
|
none: false
|
108
111
|
requirements:
|
109
112
|
- - ! '>='
|
110
113
|
- !ruby/object:Gem::Version
|
111
114
|
version: '0'
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
hash: 141493864149925103
|
112
118
|
requirements: []
|
113
119
|
rubyforge_project:
|
114
120
|
rubygems_version: 1.8.23
|