Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Fixing encoding of decimals ending in .0 or 0.0 #115

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/cql/protocol/cql_byte_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require 'bigdecimal'


module Cql
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid committing unrelated whitespace changes, it makes PRs so much harder to read. This is just a general comment, obviously a single line isn't a big deal.

module Protocol
class CqlByteBuffer < Ione::ByteBuffer
Expand Down Expand Up @@ -264,9 +263,12 @@ def append_varint(n)
end

def append_decimal(n)
sign, number_string, _, size = n.split
str = n.to_s('F')
size = str.index('.')
number_string = str.gsub('.', '')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's anything you can do here that avoids the string literals that would be good. String literals are allocated on each call, and too many of them in a hot path like the frame encoding can generate a lot of garbage (it obviously depends on the application, but this code could be called thousands of times per second).

Look at how some of the other methods in this class use constants instead of literal strings when doing things like the above.


num = number_string.to_i
raw = self.class.new.append_varint(sign * num)
raw = self.class.new.append_varint(num)
append_int(number_string.length - size)
append(raw)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/cql/protocol/cql_byte_buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,16 @@ module Protocol
buffer.should eql_bytes("\x00\x00\x00\x01\x00")
end

it 'encodes a BigDecimal ending in .0' do
buffer.append_decimal(BigDecimal.new('1042342234234.0'))
buffer.should eql_bytes("\x00\x00\x00\x01\tz\xE4b\xD4\xC4")
end

it 'appends a BigDecimal ending with 00.0' do
buffer.append_decimal(BigDecimal.new('12000.0'))
buffer.should eql_bytes("\x00\x00\x00\x01\x01\xD4\xC0")
end

it 'appends to the buffer' do
buffer << "\x99"
buffer.append_decimal(BigDecimal.new('1042342234234.123423435647768234'))
Expand Down