Class: Aerospike::Buffer
- Inherits:
-
Object
- Object
- Aerospike::Buffer
- 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>'- DOUBLE =
'G'- DEFAULT_BUFFER_SIZE =
16 * 1024
- MAX_BUFFER_SIZE =
10 * 1024 * 1024
- @@buf_pool =
:nodoc:
Pool.new
Instance Attribute Summary collapse
-
#buf ⇒ Object
Returns the value of attribute buf.
Class Method Summary collapse
Instance Method Summary collapse
- #dump(from = nil, to = nil) ⇒ Object
- #eat!(n) ⇒ Object
-
#initialize(size = DEFAULT_BUFFER_SIZE, buf = nil) ⇒ Buffer
constructor
A new instance of Buffer.
- #read(offset, len = nil) ⇒ Object
- #read_bool(offset, length) ⇒ Object
- #read_double(offset) ⇒ Object
- #read_int16(offset) ⇒ Object
- #read_int32(offset) ⇒ Object
- #read_int64(offset) ⇒ Object
- #read_var_int64(offset, len) ⇒ Object
- #reset ⇒ Object
- #resize(length) ⇒ Object
- #size ⇒ Object (also: #length)
- #to_s ⇒ Object
- #write_binary(data, offset) ⇒ Object
- #write_byte(byte, offset) ⇒ Object
- #write_double(f, offset) ⇒ Object
- #write_int16(i, offset) ⇒ Object
- #write_int32(i, offset) ⇒ Object
- #write_int64(i, offset) ⇒ Object
- #write_uint16(i, offset) ⇒ Object
- #write_uint16_little_endian(i, offset) ⇒ Object
- #write_uint32(i, offset) ⇒ Object
- #write_uint64(i, offset) ⇒ Object
Constructor Details
#initialize(size = DEFAULT_BUFFER_SIZE, buf = nil) ⇒ Buffer
Returns a new instance of Buffer.
46 47 48 49 50 |
# File 'lib/aerospike/utils/buffer.rb', line 46 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
#buf ⇒ Object
Returns the value of attribute buf.
32 33 34 |
# File 'lib/aerospike/utils/buffer.rb', line 32 def buf @buf end |
Class Method Details
.get ⇒ Object
52 53 54 |
# File 'lib/aerospike/utils/buffer.rb', line 52 def self.get @@buf_pool.poll end |
.put(buffer) ⇒ Object
56 57 58 |
# File 'lib/aerospike/utils/buffer.rb', line 56 def self.put(buffer) @@buf_pool.offer(buffer) end |
Instance Method Details
#dump(from = nil, to = nil) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/aerospike/utils/buffer.rb', line 179 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 |
#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
126 127 128 129 130 131 132 |
# File 'lib/aerospike/utils/buffer.rb', line 126 def read(offset, len=nil) if len @buf[offset, len] else @buf.getbyte(offset) end end |
#read_bool(offset, length) ⇒ Object
165 166 167 |
# File 'lib/aerospike/utils/buffer.rb', line 165 def read_bool(offset, length) length <= 0 ? false : @buf[offset].ord != 0 end |
#read_double(offset) ⇒ Object
160 161 162 163 |
# File 'lib/aerospike/utils/buffer.rb', line 160 def read_double(offset) vals = @buf[offset..offset+7] vals.unpack(DOUBLE)[0] end |
#read_int16(offset) ⇒ Object
134 135 136 137 |
# File 'lib/aerospike/utils/buffer.rb', line 134 def read_int16(offset) vals = @buf[offset..offset+1] vals.unpack(INT16)[0] end |
#read_int32(offset) ⇒ Object
139 140 141 142 |
# File 'lib/aerospike/utils/buffer.rb', line 139 def read_int32(offset) vals = @buf[offset..offset+3] vals.unpack(INT32)[0] end |
#read_int64(offset) ⇒ Object
144 145 146 147 |
# File 'lib/aerospike/utils/buffer.rb', line 144 def read_int64(offset) vals = @buf[offset..offset+7] vals.unpack(INT64)[0] end |
#read_var_int64(offset, len) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/aerospike/utils/buffer.rb', line 149 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 |
#reset ⇒ Object
173 174 175 176 177 |
# File 'lib/aerospike/utils/buffer.rb', line 173 def reset for i in 0..@buf.size-1 @buf[i] = ' ' end end |
#resize(length) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/aerospike/utils/buffer.rb', line 69 def resize(length) if @buf.bytesize < length @buf.concat("%0#{length - @buf.bytesize}d" % 0) end @slice_end = length end |
#size ⇒ Object Also known as: length
60 61 62 |
# File 'lib/aerospike/utils/buffer.rb', line 60 def size @buf.bytesize end |
#to_s ⇒ Object
169 170 171 |
# File 'lib/aerospike/utils/buffer.rb', line 169 def to_s @buf[0..@slice_end-1] end |
#write_binary(data, offset) ⇒ Object
81 82 83 84 |
# File 'lib/aerospike/utils/buffer.rb', line 81 def write_binary(data, offset) @buf[offset, data.bytesize] = data data.bytesize end |
#write_byte(byte, offset) ⇒ Object
76 77 78 79 |
# File 'lib/aerospike/utils/buffer.rb', line 76 def write_byte(byte, offset) @buf.setbyte(offset, byte.ord) 1 end |
#write_double(f, offset) ⇒ Object
121 122 123 124 |
# File 'lib/aerospike/utils/buffer.rb', line 121 def write_double(f, offset) @buf[offset, 8] = [f].pack(DOUBLE) 8 end |
#write_int16(i, offset) ⇒ Object
86 87 88 89 |
# File 'lib/aerospike/utils/buffer.rb', line 86 def write_int16(i, offset) @buf[offset, 2] = [i].pack(INT16) 2 end |
#write_int32(i, offset) ⇒ Object
101 102 103 104 |
# File 'lib/aerospike/utils/buffer.rb', line 101 def write_int32(i, offset) @buf[offset, 4] = [i].pack(INT32) 4 end |
#write_int64(i, offset) ⇒ Object
111 112 113 114 |
# File 'lib/aerospike/utils/buffer.rb', line 111 def write_int64(i, offset) @buf[offset, 8] = [i].pack(INT64) 8 end |
#write_uint16(i, offset) ⇒ Object
91 92 93 94 |
# File 'lib/aerospike/utils/buffer.rb', line 91 def write_uint16(i, offset) @buf[offset, 2] = [i].pack(UINT16) 2 end |
#write_uint16_little_endian(i, offset) ⇒ Object
96 97 98 99 |
# File 'lib/aerospike/utils/buffer.rb', line 96 def write_uint16_little_endian(i, offset) @buf[offset, 2] = [i].pack(UINT16LE) 2 end |
#write_uint32(i, offset) ⇒ Object
106 107 108 109 |
# File 'lib/aerospike/utils/buffer.rb', line 106 def write_uint32(i, offset) @buf[offset, 4] = [i].pack(UINT32) 4 end |
#write_uint64(i, offset) ⇒ Object
116 117 118 119 |
# File 'lib/aerospike/utils/buffer.rb', line 116 def write_uint64(i, offset) @buf[offset, 8] = [i].pack(UINT64) 8 end |