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'
INT32 =
'l>'
UINT32 =
'N'
INT64 =
'q>'
UINT64 =
'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) ⇒ 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 = "%0#{size}d" % 0

  @slice_end = @buf.bytesize
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



32
33
34
# File 'lib/aerospike/utils/buffer.rb', line 32

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(from = nil, to = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/aerospike/utils/buffer.rb', line 159

def dump(from=nil, to=nil)
  from ||= 0
  to ||= @slice_end - 1

  @buf.bytes[from...to].each do |c|
    print c.ord.to_s(16)
    putc ' '
  end
end

#read(offset, len = nil) ⇒ Object



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

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

#read_double(offset) ⇒ Object



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

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

#read_int16(offset) ⇒ Object



124
125
126
127
# File 'lib/aerospike/utils/buffer.rb', line 124

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

#read_int32(offset) ⇒ Object



129
130
131
132
# File 'lib/aerospike/utils/buffer.rb', line 129

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

#read_int64(offset) ⇒ Object



134
135
136
137
# File 'lib/aerospike/utils/buffer.rb', line 134

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

#read_var_int64(offset, len) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/aerospike/utils/buffer.rb', line 139

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

#resize(length) ⇒ Object



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

def resize(length)
  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



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

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

#write_binary(data, offset) ⇒ Object



76
77
78
79
# File 'lib/aerospike/utils/buffer.rb', line 76

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

#write_byte(byte, offset) ⇒ Object



71
72
73
74
# File 'lib/aerospike/utils/buffer.rb', line 71

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

#write_double(f, offset) ⇒ Object



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

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

#write_int16(i, offset) ⇒ Object



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

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

#write_int32(i, offset) ⇒ Object



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

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

#write_int64(i, offset) ⇒ Object



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

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

#write_uint16(i, offset) ⇒ Object



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

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

#write_uint32(i, offset) ⇒ Object



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

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

#write_uint64(i, offset) ⇒ Object



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

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