protobuf 2.4.2-java → 2.4.3-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,15 @@ require 'protobuf/field/field_array'
4
4
  module Protobuf
5
5
  module Field
6
6
  class BaseField
7
+ ##
8
+ # Constants
9
+ #
10
+ PACKED_TYPES = [
11
+ ::Protobuf::WireType::VARINT,
12
+ ::Protobuf::WireType::FIXED32,
13
+ ::Protobuf::WireType::FIXED64
14
+ ].freeze
15
+
7
16
  ##
8
17
  # Attributes
9
18
  #
@@ -29,24 +38,11 @@ module Protobuf
29
38
  @extension = options.delete(:extension)
30
39
  @packed = repeated? && options.delete(:packed)
31
40
  @deprecated = options.delete(:deprecated)
32
- unless options.empty?
33
- warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
34
- end
35
-
36
- if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
37
- raise "Can't use packed encoding for `#{@type}' type"
38
- end
39
-
40
- @default_value = \
41
- case @rule
42
- when :repeated then
43
- ::Protobuf::Field::FieldArray.new(self).freeze
44
- when :required then
45
- nil
46
- when :optional then
47
- typed_default_value
48
- end
49
41
 
42
+ set_rule_predicates
43
+ set_default_value
44
+ warn_excess_options(options) unless options.empty?
45
+ validate_packed_field if packed?
50
46
  define_accessor
51
47
  end
52
48
 
@@ -58,13 +54,11 @@ module Protobuf
58
54
  end
59
55
 
60
56
  def enum?
61
- return @_enum unless @_enum.nil?
62
- @_enum = (self.class < ::Protobuf::Field::EnumField)
57
+ false
63
58
  end
64
59
 
65
60
  def message?
66
- return @_message unless @_message.nil?
67
- @_message = (self.class < ::Protobuf::Field::MessageField)
61
+ false
68
62
  end
69
63
 
70
64
  # Decode +bytes+ and pass to +message_instance+.
@@ -103,25 +97,22 @@ module Protobuf
103
97
  end
104
98
 
105
99
  def extension?
106
- !!@extension
100
+ !! @extension
107
101
  end
108
102
 
109
103
  # Is this a repeated field?
110
104
  def repeated?
111
- return @_repeated unless @_repeated.nil?
112
- @_repeated = (@rule == :repeated)
105
+ !! @repeated
113
106
  end
114
107
 
115
108
  # Is this a required field?
116
109
  def required?
117
- return @_required unless @_required.nil?
118
- @_required = (@rule == :required)
110
+ !! @required
119
111
  end
120
112
 
121
113
  # Is this a optional field?
122
114
  def optional?
123
- return @_optional unless @_optional.nil?
124
- @_optional = (@rule == :optional)
115
+ !! @optional
125
116
  end
126
117
 
127
118
  # Is this a deprecated field?
@@ -184,8 +175,9 @@ module Protobuf
184
175
  @message_class.class_eval do
185
176
  define_method(field.setter_method_name) do |val|
186
177
  field.warn_if_deprecated
178
+ val.compact! if val.respond_to?(:compact!)
187
179
 
188
- if val.nil?
180
+ if val.nil? || (val.respond_to?(:empty?) && val.empty?)
189
181
  @values.delete(field.name)
190
182
  else
191
183
  @values[field.name] ||= ::Protobuf::Field::FieldArray.new(field)
@@ -211,7 +203,7 @@ module Protobuf
211
203
  define_method(field.setter_method_name) do |val|
212
204
  field.warn_if_deprecated
213
205
 
214
- if val.nil?
206
+ if val.nil? || (val.respond_to?(:empty?) && val.empty?)
215
207
  @values.delete(field.name)
216
208
  elsif field.acceptable?(val)
217
209
  @values[field.name] = val
@@ -222,6 +214,28 @@ module Protobuf
222
214
  end
223
215
  end
224
216
 
217
+ def set_default_value
218
+ @default_value = case
219
+ when repeated? then ::Protobuf::Field::FieldArray.new(self).freeze
220
+ when required? then nil
221
+ when optional? then typed_default_value
222
+ end
223
+ end
224
+
225
+ def set_rule_predicates
226
+ case @rule
227
+ when :repeated then
228
+ @required = @optional = false
229
+ @repeated = true
230
+ when :required then
231
+ @repeated = @optional = false
232
+ @required = true
233
+ when :optional then
234
+ @repeated = @required = false
235
+ @optional = true
236
+ end
237
+ end
238
+
225
239
  def typed_default_value
226
240
  if @default.nil?
227
241
  self.class.default
@@ -230,6 +244,16 @@ module Protobuf
230
244
  end
231
245
  end
232
246
 
247
+ def validate_packed_field
248
+ if packed? && ! ::Protobuf::Field::BaseField::PACKED_TYPES.include?(wire_type)
249
+ raise "Can't use packed encoding for `#{@type}' type"
250
+ end
251
+ end
252
+
253
+ def warn_excess_options(options)
254
+ warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
255
+ end
256
+
233
257
  end
234
258
  end
235
259
  end
@@ -1,14 +1,14 @@
1
+ require 'protobuf/wire_type'
2
+
1
3
  module Protobuf
2
4
  module Field
3
5
  class BytesField < BaseField
6
+ BYTES_ENCODING = "ASCII-8BIT".freeze
7
+
4
8
  def self.default
5
9
  ''
6
10
  end
7
11
 
8
- def wire_type
9
- WireType::LENGTH_DELIMITED
10
- end
11
-
12
12
  def acceptable?(val)
13
13
  if val.nil? || val.is_a?(::Protobuf::Message) || val.instance_of?(String)
14
14
  return true
@@ -18,20 +18,21 @@ module Protobuf
18
18
  end
19
19
 
20
20
  def decode(bytes)
21
- bytes.force_encoding(::Protobuf::Message::STRING_ENCODING) if bytes.respond_to?(:force_encoding)
21
+ bytes.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
22
22
  bytes
23
23
  end
24
24
 
25
25
  def encode(value)
26
- if value.is_a?(::Protobuf::Message)
27
- value = value.serialize_to_string
28
- else
29
- value.force_encoding(::Protobuf::Message::STRING_ENCODING) if value.respond_to?(:force_encoding)
30
- end
26
+ value = value.serialize_to_string if value.is_a?(::Protobuf::Message)
27
+ value.force_encoding(::Protobuf::Field::BytesField::BYTES_ENCODING)
31
28
 
32
- string_size = VarintField.encode(value.size)
29
+ string_size = ::Protobuf::Field::VarintField.encode(value.size)
33
30
  string_size << value
34
31
  end
32
+
33
+ def wire_type
34
+ ::Protobuf::WireType::LENGTH_DELIMITED
35
+ end
35
36
  end
36
37
  end
37
38
  end
@@ -19,6 +19,10 @@ module Protobuf
19
19
  super(value.to_i)
20
20
  end
21
21
 
22
+ def enum?
23
+ true
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def typed_default_value
@@ -28,7 +28,7 @@ module Protobuf
28
28
  end
29
29
 
30
30
  def replace(val)
31
- raise TypeError unless val.is_a?(Array)
31
+ raise_type_error(val) unless val.is_a?(Array)
32
32
  val = val.map { |v| normalize(v) }
33
33
  super(val)
34
34
  end
@@ -60,6 +60,15 @@ module Protobuf
60
60
  value
61
61
  end
62
62
  end
63
+
64
+ def raise_type_error(val)
65
+ error_text = <<-TYPE_ERROR
66
+ Expected value of type '#{@field.type}'
67
+ Got '#{val.class}' for protobuf field #{@field.name}
68
+ TYPE_ERROR
69
+
70
+ raise TypeError, error_text
71
+ end
63
72
  end
64
73
  end
65
74
  end
@@ -21,10 +21,14 @@ module Protobuf
21
21
 
22
22
  def encode(value)
23
23
  bytes = value.serialize_to_string
24
- result = VarintField.encode(bytes.size)
24
+ result = ::Protobuf::Field::VarintField.encode(bytes.size)
25
25
  result << bytes
26
26
  end
27
27
 
28
+ def message?
29
+ true
30
+ end
31
+
28
32
  def wire_type
29
33
  ::Protobuf::WireType::LENGTH_DELIMITED
30
34
  end
@@ -6,9 +6,15 @@ module Protobuf
6
6
  ENCODING = 'UTF-8'.freeze
7
7
 
8
8
  def decode(bytes)
9
- bytes.force_encoding(::Protobuf::Field::StringField::ENCODING) if bytes.respond_to?(:force_encoding)
9
+ bytes.force_encoding(::Protobuf::Field::StringField::ENCODING)
10
10
  bytes
11
11
  end
12
+
13
+ def encode(value)
14
+ value.force_encoding(::Protobuf::Field::StringField::ENCODING)
15
+ string_size = ::Protobuf::Field::VarintField.encode(value.size)
16
+ string_size << value
17
+ end
12
18
  end
13
19
  end
14
20
  end
@@ -49,7 +49,15 @@ module Protobuf
49
49
  end
50
50
 
51
51
  def encode(value)
52
- self.class.encode(value)
52
+ return [value].pack('C') if value < 128
53
+
54
+ bytes = []
55
+ until value == 0
56
+ bytes << (0x80 | (value & 0x7f))
57
+ value >>= 7
58
+ end
59
+ bytes[-1] &= 0x7f
60
+ bytes.pack('C*')
53
61
  end
54
62
 
55
63
  def wire_type
@@ -6,10 +6,6 @@ require 'protobuf/message/decoder'
6
6
 
7
7
  module Protobuf
8
8
  class Message
9
- ##
10
- # Constants
11
- #
12
- STRING_ENCODING = "ASCII-8BIT".freeze
13
9
 
14
10
  ##
15
11
  # Class Methods
@@ -129,7 +125,7 @@ module Protobuf
129
125
 
130
126
  def clear!
131
127
  @values.delete_if do |_, value|
132
- if value.is_a?(Field::FieldArray)
128
+ if value.is_a?(::Protobuf::Field::FieldArray)
133
129
  value.clear
134
130
  false
135
131
  else
@@ -160,7 +156,7 @@ module Protobuf
160
156
 
161
157
  def each_field_for_serialization
162
158
  all_fields.each do |field|
163
- next unless _field_must_be_serialized?(field)
159
+ next unless __field_must_be_serialized__?(field)
164
160
 
165
161
  value = @values[field.name]
166
162
 
@@ -225,15 +221,17 @@ module Protobuf
225
221
  (self.__send__(key).present? || [true, false].include?(self.__send__(key)))
226
222
  end
227
223
 
228
- def serialize_to(stream)
224
+ def serialize_to_string
225
+ stream = ""
226
+
229
227
  each_field_for_serialization do |field, value|
230
228
  if field.repeated?
231
229
  if field.packed?
232
230
  key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
233
231
  packed_value = value.map { |val| field.encode(val) }.join
234
- stream.write(::Protobuf::Field::VarintField.encode(key))
235
- stream.write(::Protobuf::Field::VarintField.encode(packed_value.size))
236
- stream.write(packed_value)
232
+ stream << ::Protobuf::Field::VarintField.encode(key)
233
+ stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
234
+ stream << packed_value
237
235
  else
238
236
  value.each { |val| write_pair(stream, field, val) }
239
237
  end
@@ -241,14 +239,8 @@ module Protobuf
241
239
  write_pair(stream, field, value)
242
240
  end
243
241
  end
244
- end
245
242
 
246
- def serialize_to_string(string = '')
247
- io = StringIO.new(string)
248
- serialize_to(io)
249
- result = io.string
250
- result.force_encoding(::Protobuf::Message::STRING_ENCODING) if result.respond_to?(:force_encoding)
251
- result
243
+ return stream
252
244
  end
253
245
 
254
246
  def set_field(tag, bytes)
@@ -273,6 +265,10 @@ module Protobuf
273
265
  to_hash.to_json
274
266
  end
275
267
 
268
+ def to_proto
269
+ self
270
+ end
271
+
276
272
  def ==(obj)
277
273
  return false unless obj.is_a?(self.class)
278
274
  each_field do |field, value|
@@ -301,6 +297,7 @@ module Protobuf
301
297
  # Instance Aliases
302
298
  #
303
299
  alias_method :to_hash_value, :to_hash
300
+ alias_method :to_proto_hash, :to_hash
304
301
  alias_method :to_s, :serialize_to_string
305
302
  alias_method :bytes, :serialize_to_string
306
303
  alias_method :serialize, :serialize_to_string
@@ -329,7 +326,7 @@ module Protobuf
329
326
 
330
327
  object.__send__(:initialize)
331
328
  @values.each do |name, value|
332
- if value.is_a?(Field::FieldArray)
329
+ if value.is_a?(::Protobuf::Field::FieldArray)
333
330
  object.__send__(name).replace(value.map {|v| duplicate.call(v)})
334
331
  else
335
332
  object.__send__("#{name}=", duplicate.call(value))
@@ -338,24 +335,20 @@ module Protobuf
338
335
  object
339
336
  end
340
337
 
341
- def _field_must_be_serialized?(field)
342
- return true if field.required?
343
-
344
- if field.repeated? && has_field?(field.name)
345
- @values[field.name].compact!
346
- @values.delete(field.name) if @values[field.name].empty?
347
- end
338
+ def __field_must_be_serialized__?(field)
339
+ field.required? || __field_value_is_present__?(field)
340
+ end
348
341
 
349
- return has_field?(field.name)
342
+ def __field_value_is_present__?(field)
343
+ value = @values[field.name]
344
+ !value.nil? && (value.present? || [true, false].include?(value))
350
345
  end
351
346
 
352
347
  # Encode key and value, and write to +stream+.
353
348
  def write_pair(stream, field, value)
354
349
  key = (field.tag << 3) | field.wire_type
355
- key_bytes = ::Protobuf::Field::VarintField.encode(key)
356
- stream.write(key_bytes)
357
- bytes = field.encode(value)
358
- stream.write(bytes)
350
+ stream << ::Protobuf::Field::VarintField.encode(key)
351
+ stream << field.encode(value)
359
352
  end
360
353
 
361
354
  end
@@ -43,10 +43,9 @@ module Protobuf
43
43
 
44
44
  # Read varint integer value from +stream+.
45
45
  def read_varint(stream)
46
- read_method = stream.respond_to?(:readbyte) ? :readbyte : :readchar
47
46
  value = index = 0
48
47
  begin
49
- byte = stream.__send__(read_method)
48
+ byte = stream.readbyte
50
49
  value |= (byte & 0x7f) << (7 * index)
51
50
  index += 1
52
51
  end while (byte & 0x80).nonzero?
@@ -44,7 +44,7 @@ module Protobuf
44
44
  end
45
45
 
46
46
  def set_data(data)
47
- @data = data.is_a?(::Protobuf::Message) ? data.serialize_to_string : data.to_s
47
+ @data = data.to_s
48
48
  @size = @data.size
49
49
  end
50
50
 
@@ -10,7 +10,7 @@ module Protobuf
10
10
  extend Forwardable
11
11
  include Protobuf::Logger::LogMethods
12
12
 
13
- delegate [:options, :complete_cb, :success_cb, :failure_cb] => :@connector
13
+ def_delegators :@connector, :options, :complete_cb, :success_cb, :failure_cb
14
14
  attr_reader :connector
15
15
 
16
16
  # Create a new client with default options (defined in ClientConnection)
@@ -104,10 +104,10 @@ module Protobuf
104
104
  def rpc_request_data
105
105
  validate_request_type
106
106
 
107
- return Protobuf::Socketrpc::Request.new(
107
+ return ::Protobuf::Socketrpc::Request.new(
108
108
  :service_name => @options[:service].name,
109
109
  :method_name => @options[:method].to_s,
110
- :request_proto => @options[:request].serialize_to_string
110
+ :request_proto => @options[:request]
111
111
  ).serialize_to_string
112
112
  rescue => e
113
113
  fail(:INVALID_REQUEST_PROTO, "Could not set request proto: #{e.message}")
@@ -86,4 +86,3 @@ module Protobuf
86
86
  end
87
87
  end
88
88
  end
89
-
@@ -9,7 +9,7 @@ module Protobuf
9
9
  include ::Protobuf::Rpc::Server
10
10
  include ::Protobuf::Logger::LogMethods
11
11
 
12
- AUTO_COLLECT_TIMEOUT = 20 # seconds
12
+ AUTO_COLLECT_TIMEOUT = 5 # seconds
13
13
 
14
14
  def self.cleanup?
15
15
  # every 10 connections run a cleanup routine after closing the response
@@ -49,7 +49,7 @@ module Protobuf
49
49
  end
50
50
 
51
51
  def send_data
52
- response_data = @response.is_a?(::Protobuf::Message) ? @response.serialize_to_string : @response.to_s
52
+ response_data = @response.to_s # to_s is aliases as serialize_to_string in Message
53
53
  @stats.response_size = response_data.size
54
54
  zmq_error_check(@socket.send_string(response_data))
55
55
  end
@@ -1,4 +1,4 @@
1
1
  module Protobuf
2
- VERSION = '2.4.2'
2
+ VERSION = '2.4.3'
3
3
  PROTOC_VERSION = '2.4.1'
4
4
  end
@@ -140,6 +140,17 @@ namespace :benchmark do
140
140
  puts args[:profile_output]
141
141
  end
142
142
 
143
+ desc "benchmark Protobuf Message #serialize"
144
+ task :profile_protobuf_serialize, [:number, :profile_output] do |t, args|
145
+ args.with_defaults(:number => 1000, :profile_output => "/tmp/profiler_new_#{Time.now.to_i}")
146
+ create_params = { :name => "The name that we set", :date_created => Time.now.to_i, :status => 2 }
147
+ ::PerfTools::CpuProfiler.start(args[:profile_output]) do
148
+ args[:number].to_i.times { Test::Resource.new(create_params).serialize }
149
+ end
150
+
151
+ puts args[:profile_output]
152
+ end
153
+
143
154
  desc "benchmark EventMachine client with EventMachine server"
144
155
  task :em_client_em_server, [:number, :length] do |t, args|
145
156
  args.with_defaults(:number => 1000, :length => 100)
@@ -23,25 +23,34 @@ describe Protobuf::Message do
23
23
  describe '#encode' do
24
24
  context "when there's no value for a required field" do
25
25
  let(:message) { ::Test::Resource.new }
26
- let(:stream) { StringIO.new }
27
26
 
28
27
  it "raises a 'message not initialized' error" do
29
28
  expect {
30
- message.serialize_to(stream)
29
+ message.serialize_to_string
31
30
  }.to raise_error(Protobuf::SerializationError, /required/i)
32
31
  end
33
32
  end
34
33
 
35
34
  context "repeated fields" do
36
35
  let(:message) { ::Test::Resource.new(:name => "something") }
37
- let(:stream) { StringIO.new }
38
36
 
39
37
  it "does not raise an error when repeated fields are []" do
40
38
  expect {
41
39
  message.repeated_enum = []
42
- message.serialize_to(stream)
40
+ message.serialize_to_string
43
41
  }.to_not raise_error
44
42
  end
43
+
44
+ it "sets the value to nil when empty array is passed" do
45
+ message.repeated_enum = []
46
+ message.instance_variable_get("@values")[:repeated_enum].should be_nil
47
+ end
48
+
49
+ it "raises TypeError when a non-array replaces it" do
50
+ expect {
51
+ message.repeated_enum = 2
52
+ }.to raise_error(/value of type/)
53
+ end
45
54
  end
46
55
  end
47
56
 
@@ -9,7 +9,7 @@ describe Protobuf::Rpc::Client do
9
9
  context "when using fiber based calls" do
10
10
  it "waits for response" do
11
11
  EventMachine.fiber_run do
12
- StubServer.new(:delay => 3) do |server|
12
+ StubServer.new(:delay => 1) do |server|
13
13
  client = Test::ResourceService.client
14
14
  start = now
15
15
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: protobuf
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.4.2
5
+ version: 2.4.3
6
6
  platform: java
7
7
  authors:
8
8
  - BJ Neilsen
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-11-16 00:00:00 Z
14
+ date: 2012-11-17 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -305,7 +305,6 @@ files:
305
305
  - lib/protobuf/logger.rb
306
306
  - lib/protobuf/message.rb
307
307
  - lib/protobuf/message/decoder.rb
308
- - lib/protobuf/message/extend.rb
309
308
  - lib/protobuf/message/message.rb
310
309
  - lib/protobuf/rpc/buffer.rb
311
310
  - lib/protobuf/rpc/client.rb
@@ -1,8 +0,0 @@
1
- require 'protobuf/message/message'
2
-
3
- module Protobuf
4
- class Extend < Message
5
- MIN = -1.0/0
6
- MAX = 1.0/0
7
- end
8
- end