avro 1.7.4 → 1.7.5
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/Rakefile +4 -0
- data/avro.gemspec +4 -4
- data/interop/test_interop.rb +1 -1
- data/lib/avro.rb +1 -0
- data/lib/avro/data_file.rb +90 -25
- data/lib/avro/io.rb +78 -86
- data/lib/avro/protocol.rb +24 -28
- data/lib/avro/schema.rb +132 -151
- data/test/random_data.rb +16 -16
- data/test/test_datafile.rb +31 -0
- data/test/test_protocol.rb +7 -0
- data/test/test_schema.rb +134 -0
- metadata +6 -4
data/test/random_data.rb
CHANGED
@@ -27,52 +27,52 @@ class RandomData
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def nextdata(schm, d=0)
|
30
|
-
case schm.
|
31
|
-
when
|
30
|
+
case schm.type_sym
|
31
|
+
when :boolean
|
32
32
|
rand > 0.5
|
33
|
-
when
|
33
|
+
when :string
|
34
34
|
randstr()
|
35
|
-
when
|
35
|
+
when :int
|
36
36
|
rand(Avro::Schema::INT_MAX_VALUE - Avro::Schema::INT_MIN_VALUE) + Avro::Schema::INT_MIN_VALUE
|
37
|
-
when
|
37
|
+
when :long
|
38
38
|
rand(Avro::Schema::LONG_MAX_VALUE - Avro::Schema::LONG_MIN_VALUE) + Avro::Schema::LONG_MIN_VALUE
|
39
|
-
when
|
39
|
+
when :float
|
40
40
|
(-1024 + 2048 * rand).round.to_f
|
41
|
-
when
|
41
|
+
when :double
|
42
42
|
Avro::Schema::LONG_MIN_VALUE + (Avro::Schema::LONG_MAX_VALUE - Avro::Schema::LONG_MIN_VALUE) * rand
|
43
|
-
when
|
43
|
+
when :bytes
|
44
44
|
randstr(BYTEPOOL)
|
45
|
-
when
|
45
|
+
when :null
|
46
46
|
nil
|
47
|
-
when
|
47
|
+
when :array
|
48
48
|
arr = []
|
49
49
|
len = rand(5) + 2 - d
|
50
50
|
len = 0 if len < 0
|
51
51
|
len.times{ arr << nextdata(schm.items, d+1) }
|
52
52
|
arr
|
53
|
-
when
|
53
|
+
when :map
|
54
54
|
map = {}
|
55
55
|
len = rand(5) + 2 - d
|
56
56
|
len = 0 if len < 0
|
57
57
|
len.times do
|
58
|
-
map[nextdata(Avro::Schema::PrimitiveSchema.new(
|
58
|
+
map[nextdata(Avro::Schema::PrimitiveSchema.new(:string))] = nextdata(schm.values, d+1)
|
59
59
|
end
|
60
60
|
map
|
61
|
-
when
|
61
|
+
when :record, :error
|
62
62
|
m = {}
|
63
63
|
schm.fields.each do |field|
|
64
64
|
m[field.name] = nextdata(field.type, d+1)
|
65
65
|
end
|
66
66
|
m
|
67
|
-
when
|
67
|
+
when :union
|
68
68
|
types = schm.schemas
|
69
69
|
nextdata(types[rand(types.size)], d)
|
70
|
-
when
|
70
|
+
when :enum
|
71
71
|
symbols = schm.symbols
|
72
72
|
len = symbols.size
|
73
73
|
return nil if len == 0
|
74
74
|
symbols[rand(len)]
|
75
|
-
when
|
75
|
+
when :fixed
|
76
76
|
f = ""
|
77
77
|
schm.size.times { f << BYTEPOOL[rand(BYTEPOOL.size), 1] }
|
78
78
|
f
|
data/test/test_datafile.rb
CHANGED
@@ -154,4 +154,35 @@ JSON
|
|
154
154
|
datafile.close
|
155
155
|
end
|
156
156
|
|
157
|
+
def test_deflate
|
158
|
+
Avro::DataFile.open('data.avr', 'w', '"string"', :deflate) do |writer|
|
159
|
+
writer << 'a' * 10_000
|
160
|
+
end
|
161
|
+
assert(File.size('data.avr') < 500)
|
162
|
+
|
163
|
+
records = []
|
164
|
+
Avro::DataFile.open('data.avr') do |reader|
|
165
|
+
reader.each {|record| records << record }
|
166
|
+
end
|
167
|
+
assert_equal records, ['a' * 10_000]
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_append_to_deflated_file
|
171
|
+
schema = Avro::Schema.parse('"string"')
|
172
|
+
writer = Avro::IO::DatumWriter.new(schema)
|
173
|
+
file = Avro::DataFile::Writer.new(File.open('data.avr', 'wb'), writer, schema, :deflate)
|
174
|
+
file << 'a' * 10_000
|
175
|
+
file.close
|
176
|
+
|
177
|
+
file = Avro::DataFile::Writer.new(File.open('data.avr', 'a+b'), writer)
|
178
|
+
file << 'b' * 10_000
|
179
|
+
file.close
|
180
|
+
assert(File.size('data.avr') < 1_000)
|
181
|
+
|
182
|
+
records = []
|
183
|
+
Avro::DataFile.open('data.avr') do |reader|
|
184
|
+
reader.each {|record| records << record }
|
185
|
+
end
|
186
|
+
assert_equal records, ['a' * 10_000, 'b' * 10_000]
|
187
|
+
end
|
157
188
|
end
|
data/test/test_protocol.rb
CHANGED
@@ -189,4 +189,11 @@ EOS
|
|
189
189
|
assert_equal original, round_trip
|
190
190
|
end
|
191
191
|
end
|
192
|
+
|
193
|
+
def test_namespaces
|
194
|
+
protocol = Protocol.parse(EXAMPLES.first.protocol_string)
|
195
|
+
protocol.types.each do |type|
|
196
|
+
assert_equal type.namespace, 'com.acme'
|
197
|
+
end
|
198
|
+
end
|
192
199
|
end
|
data/test/test_schema.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
2
|
+
# or more contributor license agreements. See the NOTICE file
|
3
|
+
# distributed with this work for additional information
|
4
|
+
# regarding copyright ownership. The ASF licenses this file
|
5
|
+
# to you under the Apache License, Version 2.0 (the
|
6
|
+
# "License"); you may not use this file except in compliance
|
7
|
+
# with the License. You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'test_help'
|
18
|
+
|
19
|
+
class TestSchema < Test::Unit::TestCase
|
20
|
+
def test_default_namespace
|
21
|
+
schema = Avro::Schema.parse <<-SCHEMA
|
22
|
+
{"type": "record", "name": "OuterRecord", "fields": [
|
23
|
+
{"name": "field1", "type": {
|
24
|
+
"type": "record", "name": "InnerRecord", "fields": []
|
25
|
+
}},
|
26
|
+
{"name": "field2", "type": "InnerRecord"}
|
27
|
+
]}
|
28
|
+
SCHEMA
|
29
|
+
|
30
|
+
assert_equal schema.name, 'OuterRecord'
|
31
|
+
assert_equal schema.fullname, 'OuterRecord'
|
32
|
+
assert_nil schema.namespace
|
33
|
+
|
34
|
+
schema.fields.each do |field|
|
35
|
+
assert_equal field.type.name, 'InnerRecord'
|
36
|
+
assert_equal field.type.fullname, 'InnerRecord'
|
37
|
+
assert_nil field.type.namespace
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_inherited_namespace
|
42
|
+
schema = Avro::Schema.parse <<-SCHEMA
|
43
|
+
{"type": "record", "name": "OuterRecord", "namespace": "my.name.space",
|
44
|
+
"fields": [
|
45
|
+
{"name": "definition", "type": {
|
46
|
+
"type": "record", "name": "InnerRecord", "fields": []
|
47
|
+
}},
|
48
|
+
{"name": "relativeReference", "type": "InnerRecord"},
|
49
|
+
{"name": "absoluteReference", "type": "my.name.space.InnerRecord"}
|
50
|
+
]}
|
51
|
+
SCHEMA
|
52
|
+
|
53
|
+
assert_equal schema.name, 'OuterRecord'
|
54
|
+
assert_equal schema.fullname, 'my.name.space.OuterRecord'
|
55
|
+
assert_equal schema.namespace, 'my.name.space'
|
56
|
+
schema.fields.each do |field|
|
57
|
+
assert_equal field.type.name, 'InnerRecord'
|
58
|
+
assert_equal field.type.fullname, 'my.name.space.InnerRecord'
|
59
|
+
assert_equal field.type.namespace, 'my.name.space'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_inherited_namespace_from_dotted_name
|
64
|
+
schema = Avro::Schema.parse <<-SCHEMA
|
65
|
+
{"type": "record", "name": "my.name.space.OuterRecord", "fields": [
|
66
|
+
{"name": "definition", "type": {
|
67
|
+
"type": "enum", "name": "InnerEnum", "symbols": ["HELLO", "WORLD"]
|
68
|
+
}},
|
69
|
+
{"name": "relativeReference", "type": "InnerEnum"},
|
70
|
+
{"name": "absoluteReference", "type": "my.name.space.InnerEnum"}
|
71
|
+
]}
|
72
|
+
SCHEMA
|
73
|
+
|
74
|
+
assert_equal schema.name, 'OuterRecord'
|
75
|
+
assert_equal schema.fullname, 'my.name.space.OuterRecord'
|
76
|
+
assert_equal schema.namespace, 'my.name.space'
|
77
|
+
schema.fields.each do |field|
|
78
|
+
assert_equal field.type.name, 'InnerEnum'
|
79
|
+
assert_equal field.type.fullname, 'my.name.space.InnerEnum'
|
80
|
+
assert_equal field.type.namespace, 'my.name.space'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_nested_namespaces
|
85
|
+
schema = Avro::Schema.parse <<-SCHEMA
|
86
|
+
{"type": "record", "name": "outer.OuterRecord", "fields": [
|
87
|
+
{"name": "middle", "type": {
|
88
|
+
"type": "record", "name": "middle.MiddleRecord", "fields": [
|
89
|
+
{"name": "inner", "type": {
|
90
|
+
"type": "record", "name": "InnerRecord", "fields": [
|
91
|
+
{"name": "recursive", "type": "MiddleRecord"}
|
92
|
+
]
|
93
|
+
}}
|
94
|
+
]
|
95
|
+
}}
|
96
|
+
]}
|
97
|
+
SCHEMA
|
98
|
+
|
99
|
+
assert_equal schema.name, 'OuterRecord'
|
100
|
+
assert_equal schema.fullname, 'outer.OuterRecord'
|
101
|
+
assert_equal schema.namespace, 'outer'
|
102
|
+
middle = schema.fields.first.type
|
103
|
+
assert_equal middle.name, 'MiddleRecord'
|
104
|
+
assert_equal middle.fullname, 'middle.MiddleRecord'
|
105
|
+
assert_equal middle.namespace, 'middle'
|
106
|
+
inner = middle.fields.first.type
|
107
|
+
assert_equal inner.name, 'InnerRecord'
|
108
|
+
assert_equal inner.fullname, 'middle.InnerRecord'
|
109
|
+
assert_equal inner.namespace, 'middle'
|
110
|
+
assert_equal inner.fields.first.type, middle
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_to_avro_includes_namespaces
|
114
|
+
schema = Avro::Schema.parse <<-SCHEMA
|
115
|
+
{"type": "record", "name": "my.name.space.OuterRecord", "fields": [
|
116
|
+
{"name": "definition", "type": {
|
117
|
+
"type": "fixed", "name": "InnerFixed", "size": 16
|
118
|
+
}},
|
119
|
+
{"name": "reference", "type": "InnerFixed"}
|
120
|
+
]}
|
121
|
+
SCHEMA
|
122
|
+
|
123
|
+
assert_equal schema.to_avro, {
|
124
|
+
'type' => 'record', 'name' => 'OuterRecord', 'namespace' => 'my.name.space',
|
125
|
+
'fields' => [
|
126
|
+
{'name' => 'definition', 'type' => {
|
127
|
+
'type' => 'fixed', 'name' => 'InnerFixed', 'namespace' => 'my.name.space',
|
128
|
+
'size' => 16
|
129
|
+
}},
|
130
|
+
{'name' => 'reference', 'type' => 'my.name.space.InnerFixed'}
|
131
|
+
]
|
132
|
+
}
|
133
|
+
end
|
134
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: avro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 1.7.
|
9
|
+
- 5
|
10
|
+
version: 1.7.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Apache Software Foundation
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-08-19 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: yajl-ruby
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- test/test_protocol.rb
|
71
71
|
- test/test_socket_transport.rb
|
72
72
|
- test/tool.rb
|
73
|
+
- test/test_schema.rb
|
73
74
|
homepage: http://hadoop.apache.org/avro/
|
74
75
|
licenses: []
|
75
76
|
|
@@ -112,4 +113,5 @@ test_files:
|
|
112
113
|
- test/test_help.rb
|
113
114
|
- test/test_protocol.rb
|
114
115
|
- test/test_socket_transport.rb
|
116
|
+
- test/test_schema.rb
|
115
117
|
- test/test_io.rb
|