Class: EM::Tycoon::Protocol::Binary::SetMessage

Inherits:
Message
  • Object
show all
Defined in:
lib/em/tycoon/protocol/binary/set_message.rb

Constant Summary

Constants inherited from Message

Message::DEFAULT_OPTS, Message::FLAGS, Message::KV_PACK_FMT, Message::MAGIC, Message::MSG_TYPES, Message::NO_EXPIRATION_TIME, Message::NO_XT_HEX, Message::PARSE_PHASES

Instance Attribute Summary

Attributes inherited from Message

#buffer, #bytes_expected, #data, #item_count, #keysize, #magic, #parse_phase, #parsed, #type, #valuesize

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#[], check_msg_type, message_for, msg_type_for, #parse, #parse_chunk, #parsed?

Constructor Details

#initialize(data = {}, opts = {}) ⇒ SetMessage

Returns a new instance of SetMessage.



7
8
9
# File 'lib/em/tycoon/protocol/binary/set_message.rb', line 7

def initialize(data={},opts={})
  super(:set,data)
end

Class Method Details

.generate(data, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/em/tycoon/protocol/binary/set_message.rb', line 11

def self.generate(data,opts={})
 raise ArgumentError.new("Unsupported data type : #{data.class.name}") unless data.kind_of?(Hash)
 msg_array = [MAGIC[:set]]
 optflags = 0
 opts.each_pair do |optkey,optval|
    optflags |= FLAGS[optkey] if (FLAGS.has_key?(optkey) and (optval == true))
 end
 msg_array << optflags
 msg_array << data.keys.length
 data.each_pair do |key,value|
   xt = NO_XT_HEX
   if value.kind_of?(Hash)
     if value.has_key?(:xt)
       xt = ("%016X" % (value[:xt].kind_of?(Time) ? (value[:xt] - Time.now).to_i : value[:xt].to_i))
     end
     value = value[:value]
   end
   msg_array << 0 # dbidx
   msg_array << key.bytesize
   msg_array << value.bytesize
   msg_array << xt
   msg_array << key
   msg_array << value
 end
 return msg_array.pack("CNN#{KV_PACK_FMT*data.keys.length}")
end