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>'
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.



41
42
43
44
45
# File 'lib/aerospike/utils/buffer.rb', line 41

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.



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

def buf
  @buf
end

Class Method Details

.getObject



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

def self.get
  @@buf_pool.poll
end

.put(buffer) ⇒ Object



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

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

Instance Method Details

#dump(from = nil, to = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/aerospike/utils/buffer.rb', line 147

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



107
108
109
110
111
112
113
114
115
# File 'lib/aerospike/utils/buffer.rb', line 107

def read(offset, len=nil)
  start = offset

  if len
    @buf[start, len]
  else
    @buf.getbyte(start)
  end
end

#read_int16(offset) ⇒ Object



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

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

#read_int32(offset) ⇒ Object



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

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

#read_int64(offset) ⇒ Object



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

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

#read_var_int64(offset, len) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/aerospike/utils/buffer.rb', line 132

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



60
61
62
63
64
65
# File 'lib/aerospike/utils/buffer.rb', line 60

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



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

def size
  @buf.bytesize
end

#to_sObject



143
144
145
# File 'lib/aerospike/utils/buffer.rb', line 143

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

#write_binary(data, offset) ⇒ Object



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

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

#write_byte(byte, offset) ⇒ Object



67
68
69
70
# File 'lib/aerospike/utils/buffer.rb', line 67

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

#write_int16(i, offset) ⇒ Object



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

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

#write_int32(i, offset) ⇒ Object



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

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

#write_int64(i, offset) ⇒ Object



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

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

#write_uint16(i, offset) ⇒ Object



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

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

#write_uint32(i, offset) ⇒ Object



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

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

#write_uint64(i, offset) ⇒ Object



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

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