Method: EventMachine::Protocols::Couchbase::Packet.build

Defined in:
lib/em-couchbase/packet.rb

.build(opaque, vbucket, opcode, *args) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/em-couchbase/packet.rb', line 85

def self.build(opaque, vbucket, opcode, *args)
  case opcode
  when :set
    key, value, flags, expiration, cas = args.shift(5)
    bodylen = key.size + value.size + 8
    [
      0x80,             # uint8_t   magic
      CMD_SET,          # uint8_t   opcode
      key.size,         # uint16_t  keylen
      8,                # uint8_t   extlen (flags + expiration)
      0,                # uint8_t   datatype
      vbucket,          # uint16_t  vbucket
      bodylen,          # uint32_t  bodylen
      opaque || 0,      # uint32_t  opaque
      cas || 0,         # uint64_t  cas
      flags || 0,       # uint32_t  flags
      expiration || 0,  # uint32_t  expiration
      key,
      value
    ].pack("#{REQUEST_HEADER_FMT}NNa*a*")
  when :get
    key = args.shift
    bodylen = key.size
    [
      0x80,             # uint8_t   magic
      CMD_GET,          # uint8_t   opcode
      key.size,         # uint16_t  keylen
      0,                # uint8_t   extlen
      0,                # uint8_t   datatype
      vbucket,          # uint16_t  vbucket
      bodylen,          # uint32_t  bodylen
      opaque || 0,      # uint32_t  opaque
      0,                # uint64_t  cas
      key
    ].pack("#{REQUEST_HEADER_FMT}a*")
  when :incr, :decr
    cmd_id = opcode == :incr ? CMD_INCREMENT : CMD_DECREMENT
    key, delta, initial, expiration, cas = args.shift(5)
    delta ||= 1
    initial ||= 0
    bodylen = key.size + 20
    [
      0x80,                   # uint8_t   magic
      cmd_id,                 # uint8_t   opcode
      key.size,               # uint16_t  keylen
      20,                     # uint8_t   extlen (delta + initial + expiration)
      0,                      # uint8_t   datatype
      vbucket,                # uint16_t  vbucket
      bodylen,                # uint32_t  bodylen
      opaque || 0,            # uint32_t  opaque
      cas || 0,               # uint64_t  cas
      delta >> 32,            # uint64_t
      delta & 0xffffffff,     #
      initial >> 32,          # uint64_t
      initial & 0xffffffff,   #
      expiration || 0,        # uint32_t
      key
    ].pack("#{REQUEST_HEADER_FMT}NNNNNa*")
  when :sasl_auth
    mech, username, password = args.shift(3)
    value = "\0#{username}\0#{password}"
    bodylen = mech.size + value.size
    [
      0x80,             # uint8_t   magic
      CMD_SASL_AUTH,    # uint8_t   opcode
      mech.size,        # uint16_t  keylen
      0,                # uint8_t   extlen
      0,                # uint8_t   datatype
      0,                # uint16_t  vbucket
      bodylen,          # uint32_t  bodylen
      0,                # uint32_t  opaque
      0,                # uint64_t  cas
      mech,
      value
    ].pack("#{REQUEST_HEADER_FMT}a*a*")
  else
    raise Couchbase::Error::UnknownCommand, [opcode, *args].inspect
  end
end