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
-
#initialize(size = DEFAULT_BUFFER_SIZE) ⇒ 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
- #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) ⇒ Buffer
Returns a new instance of Buffer.
42 43 44 45 46 |
# File 'lib/aerospike/utils/buffer.rb', line 42 def initialize(size=DEFAULT_BUFFER_SIZE) @buf = "%0#{size}d" % 0 @slice_end = @buf.bytesize end |
Instance Attribute Details
#buf ⇒ Object
Returns the value of attribute buf.
29 30 31 |
# File 'lib/aerospike/utils/buffer.rb', line 29 def buf @buf end |
Class Method Details
.get ⇒ Object
48 49 50 |
# File 'lib/aerospike/utils/buffer.rb', line 48 def self.get @@buf_pool.poll end |
.put(buffer) ⇒ Object
52 53 54 |
# File 'lib/aerospike/utils/buffer.rb', line 52 def self.put(buffer) @@buf_pool.offer(buffer) end |
Instance Method Details
#dump(from = nil, to = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/aerospike/utils/buffer.rb', line 158 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
113 114 115 116 117 118 119 120 121 |
# File 'lib/aerospike/utils/buffer.rb', line 113 def read(offset, len=nil) start = offset if len @buf[start, len] else @buf.getbyte(start) end end |
#read_double(offset) ⇒ Object
149 150 151 152 |
# File 'lib/aerospike/utils/buffer.rb', line 149 def read_double(offset) vals = @buf[offset..offset+7] vals.unpack(DOUBLE)[0] end |
#read_int16(offset) ⇒ Object
123 124 125 126 |
# File 'lib/aerospike/utils/buffer.rb', line 123 def read_int16(offset) vals = @buf[offset..offset+1] vals.unpack(INT16)[0] end |
#read_int32(offset) ⇒ Object
128 129 130 131 |
# File 'lib/aerospike/utils/buffer.rb', line 128 def read_int32(offset) vals = @buf[offset..offset+3] vals.unpack(INT32)[0] end |
#read_int64(offset) ⇒ Object
133 134 135 136 |
# File 'lib/aerospike/utils/buffer.rb', line 133 def read_int64(offset) vals = @buf[offset..offset+7] vals.unpack(INT64)[0] end |
#read_var_int64(offset, len) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/aerospike/utils/buffer.rb', line 138 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
61 62 63 64 65 66 |
# File 'lib/aerospike/utils/buffer.rb', line 61 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
56 57 58 |
# File 'lib/aerospike/utils/buffer.rb', line 56 def size @buf.bytesize end |
#to_s ⇒ Object
154 155 156 |
# File 'lib/aerospike/utils/buffer.rb', line 154 def to_s @buf[0..@slice_end-1] end |
#write_binary(data, offset) ⇒ Object
73 74 75 76 |
# File 'lib/aerospike/utils/buffer.rb', line 73 def write_binary(data, offset) @buf[offset, data.bytesize] = data data.bytesize end |
#write_byte(byte, offset) ⇒ Object
68 69 70 71 |
# File 'lib/aerospike/utils/buffer.rb', line 68 def write_byte(byte, offset) @buf.setbyte(offset, byte.ord) 1 end |
#write_double(f, offset) ⇒ Object
108 109 110 111 |
# File 'lib/aerospike/utils/buffer.rb', line 108 def write_double(f, offset) @buf[offset, 8] = [f].pack(DOUBLE) 8 end |
#write_int16(i, offset) ⇒ Object
78 79 80 81 |
# File 'lib/aerospike/utils/buffer.rb', line 78 def write_int16(i, offset) @buf[offset, 2] = [i].pack(INT16) 2 end |
#write_int32(i, offset) ⇒ Object
88 89 90 91 |
# File 'lib/aerospike/utils/buffer.rb', line 88 def write_int32(i, offset) @buf[offset, 4] = [i].pack(INT32) 4 end |
#write_int64(i, offset) ⇒ Object
98 99 100 101 |
# File 'lib/aerospike/utils/buffer.rb', line 98 def write_int64(i, offset) @buf[offset, 8] = [i].pack(INT64) 8 end |
#write_uint16(i, offset) ⇒ Object
83 84 85 86 |
# File 'lib/aerospike/utils/buffer.rb', line 83 def write_uint16(i, offset) @buf[offset, 2] = [i].pack(UINT16) 2 end |
#write_uint32(i, offset) ⇒ Object
93 94 95 96 |
# File 'lib/aerospike/utils/buffer.rb', line 93 def write_uint32(i, offset) @buf[offset, 4] = [i].pack(UINT32) 4 end |
#write_uint64(i, offset) ⇒ Object
103 104 105 106 |
# File 'lib/aerospike/utils/buffer.rb', line 103 def write_uint64(i, offset) @buf[offset, 8] = [i].pack(UINT64) 8 end |