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'- 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_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_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.
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
#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
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
169 170 171 172 173 174 175 176 177 |
# File 'lib/aerospike/utils/buffer.rb', line 169 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
64 65 66 |
# File 'lib/aerospike/utils/buffer.rb', line 64 def eat!(n) @buf.replace(@buf[n..-1]) end |
#read(offset, len = nil) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/aerospike/utils/buffer.rb', line 120 def read(offset, len=nil) if len @buf[offset, len] else @buf.getbyte(offset) end end |
#read_double(offset) ⇒ Object
154 155 156 157 |
# File 'lib/aerospike/utils/buffer.rb', line 154 def read_double(offset) vals = @buf[offset..offset+7] vals.unpack(DOUBLE)[0] end |
#read_int16(offset) ⇒ Object
128 129 130 131 |
# File 'lib/aerospike/utils/buffer.rb', line 128 def read_int16(offset) vals = @buf[offset..offset+1] vals.unpack(INT16)[0] end |
#read_int32(offset) ⇒ Object
133 134 135 136 |
# File 'lib/aerospike/utils/buffer.rb', line 133 def read_int32(offset) vals = @buf[offset..offset+3] vals.unpack(INT32)[0] end |
#read_int64(offset) ⇒ Object
138 139 140 141 |
# File 'lib/aerospike/utils/buffer.rb', line 138 def read_int64(offset) vals = @buf[offset..offset+7] vals.unpack(INT64)[0] end |
#read_var_int64(offset, len) ⇒ Object
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/aerospike/utils/buffer.rb', line 143 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
163 164 165 166 167 |
# File 'lib/aerospike/utils/buffer.rb', line 163 def reset for i in 0..@buf.size-1 @buf[i] = ' ' end end |
#resize(length) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/aerospike/utils/buffer.rb', line 68 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
59 60 61 |
# File 'lib/aerospike/utils/buffer.rb', line 59 def size @buf.bytesize end |
#to_s ⇒ Object
159 160 161 |
# File 'lib/aerospike/utils/buffer.rb', line 159 def to_s @buf[0..@slice_end-1] end |
#write_binary(data, offset) ⇒ Object
80 81 82 83 |
# File 'lib/aerospike/utils/buffer.rb', line 80 def write_binary(data, offset) @buf[offset, data.bytesize] = data data.bytesize end |
#write_byte(byte, offset) ⇒ Object
75 76 77 78 |
# File 'lib/aerospike/utils/buffer.rb', line 75 def write_byte(byte, offset) @buf.setbyte(offset, byte.ord) 1 end |
#write_double(f, offset) ⇒ Object
115 116 117 118 |
# File 'lib/aerospike/utils/buffer.rb', line 115 def write_double(f, offset) @buf[offset, 8] = [f].pack(DOUBLE) 8 end |
#write_int16(i, offset) ⇒ Object
85 86 87 88 |
# File 'lib/aerospike/utils/buffer.rb', line 85 def write_int16(i, offset) @buf[offset, 2] = [i].pack(INT16) 2 end |
#write_int32(i, offset) ⇒ Object
95 96 97 98 |
# File 'lib/aerospike/utils/buffer.rb', line 95 def write_int32(i, offset) @buf[offset, 4] = [i].pack(INT32) 4 end |
#write_int64(i, offset) ⇒ Object
105 106 107 108 |
# File 'lib/aerospike/utils/buffer.rb', line 105 def write_int64(i, offset) @buf[offset, 8] = [i].pack(INT64) 8 end |
#write_uint16(i, offset) ⇒ Object
90 91 92 93 |
# File 'lib/aerospike/utils/buffer.rb', line 90 def write_uint16(i, offset) @buf[offset, 2] = [i].pack(UINT16) 2 end |
#write_uint32(i, offset) ⇒ Object
100 101 102 103 |
# File 'lib/aerospike/utils/buffer.rb', line 100 def write_uint32(i, offset) @buf[offset, 4] = [i].pack(UINT32) 4 end |
#write_uint64(i, offset) ⇒ Object
110 111 112 113 |
# File 'lib/aerospike/utils/buffer.rb', line 110 def write_uint64(i, offset) @buf[offset, 8] = [i].pack(UINT64) 8 end |