Class: Aerospike::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/aerospike/utils/buffer.rb

Overview

Buffer class to ease the work around

Constant Summary collapse

INT16 =
"s>"
UINT16 =
"n"
UINT16LE =
"v"
INT32 =
"l>"
UINT32 =
"N"
INT64 =
"q>"
UINT64 =
"Q>"
UINT64LE =
"Q"
DOUBLE =
"G"
DEFAULT_BUFFER_SIZE =
16 * 1024
MAX_BUFFER_SIZE =
10 * 1024 * 1024
@@buf_pool =

:nodoc:

Pool.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = DEFAULT_BUFFER_SIZE, buf = nil) ⇒ Buffer

Returns a new instance of Buffer.



45
46
47
48
49
# File 'lib/aerospike/utils/buffer.rb', line 45

def initialize(size = DEFAULT_BUFFER_SIZE, buf = nil)
  @buf = (buf ? buf : ("%0#{size}d" % 0))
  @buf.force_encoding("binary")
  @slice_end = @buf.bytesize
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



30
31
32
# File 'lib/aerospike/utils/buffer.rb', line 30

def buf
  @buf
end

Class Method Details

.getObject



51
52
53
# File 'lib/aerospike/utils/buffer.rb', line 51

def self.get
  @@buf_pool.poll
end

.put(buffer) ⇒ Object



55
56
57
# File 'lib/aerospike/utils/buffer.rb', line 55

def self.put(buffer)
  @@buf_pool.offer(buffer)
end

Instance Method Details

#dump(start = 0, finish = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/aerospike/utils/buffer.rb', line 210

def dump(start = 0, finish = nil)
  buf ||= @buf.bytes
  finish ||= @slice_end - 1
  width = 16

  ascii = "|"
  counter = 0

  print "%08x  " % start
  @buf.bytes[start...finish].each do |c|
    if counter >= start
      print "%02x " % c
      ascii << (c.between?(32, 126) ? c : ?.)
      print " " if ascii.length == (width / 2 + 1)
      if ascii.length > width
        ascii << "|"
        puts ascii
        ascii = "|"
        print "%08x  " % (counter + 1)
      end
    end
    counter += 1
  end

  # print the remainder in buffer
  if ascii.length.positive?
    fill_size = ((width - ascii.length + 1) * 3)
    fill_size += 1 if ascii.length <= (width / 2)
    filler = " " * fill_size
    print filler
    ascii << "|"
    puts ascii
  end
end

#eat!(n) ⇒ Object



65
66
67
# File 'lib/aerospike/utils/buffer.rb', line 65

def eat!(n)
  @buf.replace(@buf[n..-1])
end

#read(offset, len = nil) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/aerospike/utils/buffer.rb', line 137

def read(offset, len = nil)
  if len
    @buf[offset, len]
  else
    @buf.getbyte(offset)
  end
end

#read_bool(offset, length) ⇒ Object



196
197
198
# File 'lib/aerospike/utils/buffer.rb', line 196

def read_bool(offset, length)
  length <= 0 ? false : @buf[offset].ord != 0
end

#read_double(offset) ⇒ Object



191
192
193
194
# File 'lib/aerospike/utils/buffer.rb', line 191

def read_double(offset)
  vals = @buf[offset..offset + 7]
  vals.unpack(DOUBLE)[0]
end

#read_int16(offset) ⇒ Object



145
146
147
148
# File 'lib/aerospike/utils/buffer.rb', line 145

def read_int16(offset)
  vals = @buf[offset..offset + 1]
  vals.unpack(INT16)[0]
end

#read_int32(offset) ⇒ Object



155
156
157
158
# File 'lib/aerospike/utils/buffer.rb', line 155

def read_int32(offset)
  vals = @buf[offset..offset + 3]
  vals.unpack(INT32)[0]
end

#read_int64(offset) ⇒ Object



165
166
167
168
# File 'lib/aerospike/utils/buffer.rb', line 165

def read_int64(offset)
  vals = @buf[offset..offset + 7]
  vals.unpack(INT64)[0]
end

#read_uint16(offset) ⇒ Object



150
151
152
153
# File 'lib/aerospike/utils/buffer.rb', line 150

def read_uint16(offset)
  vals = @buf[offset..offset + 1]
  vals.unpack(UINT16)[0]
end

#read_uint32(offset) ⇒ Object



160
161
162
163
# File 'lib/aerospike/utils/buffer.rb', line 160

def read_uint32(offset)
  vals = @buf[offset..offset + 3]
  vals.unpack(UINT32)[0]
end

#read_uint64(offset) ⇒ Object



175
176
177
178
# File 'lib/aerospike/utils/buffer.rb', line 175

def read_uint64(offset)
  vals = @buf[offset..offset + 7]
  vals.unpack(UINT64)[0]
end

#read_uint64_little_endian(offset) ⇒ Object



170
171
172
173
# File 'lib/aerospike/utils/buffer.rb', line 170

def read_uint64_little_endian(offset)
  vals = @buf[offset..offset + 7]
  vals.unpack(UINT64LE)[0]
end

#read_var_int64(offset, len) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/aerospike/utils/buffer.rb', line 180

def read_var_int64(offset, len)
  val = 0
  i = 0
  while i < len
    val <<= 8
    val |= @buf[offset + i].ord & 0xFF
    i = i.succ
  end
  val
end

#resetObject



204
205
206
207
208
# File 'lib/aerospike/utils/buffer.rb', line 204

def reset
  for i in 0..@buf.size - 1
    @buf[i] = " "
  end
end

#resize(length) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aerospike/utils/buffer.rb', line 69

def resize(length)
  # Corrupted data streams can result in a hug.length.
  # Do a sanity check here.
  if length > MAX_BUFFER_SIZE
    raise Aerospike::Exceptions::Parse.new("Invalid size for buffer: #{length}")
  end

  if @buf.bytesize < length
    @buf.concat("%0#{length - @buf.bytesize}d" % 0)
  end
  @slice_end = length
end

#sizeObject Also known as: length



59
60
61
# File 'lib/aerospike/utils/buffer.rb', line 59

def size
  @buf.bytesize
end

#to_sObject



200
201
202
# File 'lib/aerospike/utils/buffer.rb', line 200

def to_s
  @buf[0..@slice_end - 1]
end

#write_binary(data, offset) ⇒ Object



87
88
89
90
# File 'lib/aerospike/utils/buffer.rb', line 87

def write_binary(data, offset)
  @buf[offset, data.bytesize] = data
  data.bytesize
end

#write_byte(byte, offset) ⇒ Object



82
83
84
85
# File 'lib/aerospike/utils/buffer.rb', line 82

def write_byte(byte, offset)
  @buf.setbyte(offset, byte.ord)
  1
end

#write_double(f, offset) ⇒ Object



132
133
134
135
# File 'lib/aerospike/utils/buffer.rb', line 132

def write_double(f, offset)
  @buf[offset, 8] = [f].pack(DOUBLE)
  8
end

#write_int16(i, offset) ⇒ Object



92
93
94
95
# File 'lib/aerospike/utils/buffer.rb', line 92

def write_int16(i, offset)
  @buf[offset, 2] = [i].pack(INT16)
  2
end

#write_int32(i, offset) ⇒ Object



107
108
109
110
# File 'lib/aerospike/utils/buffer.rb', line 107

def write_int32(i, offset)
  @buf[offset, 4] = [i].pack(INT32)
  4
end

#write_int64(i, offset) ⇒ Object



117
118
119
120
# File 'lib/aerospike/utils/buffer.rb', line 117

def write_int64(i, offset)
  @buf[offset, 8] = [i].pack(INT64)
  8
end

#write_uint16(i, offset) ⇒ Object



97
98
99
100
# File 'lib/aerospike/utils/buffer.rb', line 97

def write_uint16(i, offset)
  @buf[offset, 2] = [i].pack(UINT16)
  2
end

#write_uint16_little_endian(i, offset) ⇒ Object



102
103
104
105
# File 'lib/aerospike/utils/buffer.rb', line 102

def write_uint16_little_endian(i, offset)
  @buf[offset, 2] = [i].pack(UINT16LE)
  2
end

#write_uint32(i, offset) ⇒ Object



112
113
114
115
# File 'lib/aerospike/utils/buffer.rb', line 112

def write_uint32(i, offset)
  @buf[offset, 4] = [i].pack(UINT32)
  4
end

#write_uint64(i, offset) ⇒ Object



122
123
124
125
# File 'lib/aerospike/utils/buffer.rb', line 122

def write_uint64(i, offset)
  @buf[offset, 8] = [i].pack(UINT64)
  8
end

#write_uint64_little_endian(i, offset) ⇒ Object



127
128
129
130
# File 'lib/aerospike/utils/buffer.rb', line 127

def write_uint64_little_endian(i, offset)
  @buf[offset, 8] = [i].pack(UINT64LE)
  8
end