Class: TokyoCacheCow::Server

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/tokyo_cache_cow/server.rb

Constant Summary collapse

Terminator =
"\r\n"
SetCommand =

set

/(\S+) +(\d+) +(\d+) +(\d+)( +noreply)?/
CasCommand =
/(\S+) +(\d+) +(\d+) +(\d+) +(\d+)( +noreply)?/
StoredReply =
"STORED\r\n"
NotStoredReply =
"NOT_STORED\r\n"
ExistsReply =
"EXISTS\r\n"
NotFoundReply =
"NOT_FOUND\r\n"
GetValueReply =
"VALUE %s %d %d\r\n"
CasValueReply =
"VALUE %d %d %d %d\r\n"
EndReply =
"END\r\n"
DeleteCommand =

delete

/(\S+) *(noreply)?/
DeleteWithTimeoutCommand =
/(\S+) +(\d+) *(noreply)?/
DeletedReply =
"DELETED\r\n"
NotDeletedReply =
"NOT_DELETED\r\n"
DeleteMatchCommand =

delete_match

/(\S+)( +noreply)?/
IncrementDecrementCommand =

Increment/Decrement

/(\S+) +(\d+)( +noreply)?/
ValueReply =
"%d\r\n"
OK =

errors

"OK\r\n"
Error =
"ERROR\r\n"
ClientError =
"CLIENT_ERROR %s\r\n"
ServerError =
"SERVER_ERROR %s\r\n"
TerminatorRegex =
/\r\n/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



45
46
47
# File 'lib/tokyo_cache_cow/server.rb', line 45

def cache
  @cache
end

Instance Method Details

#receive_data(data) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
164
165
166
167
168
169
170
171
172
173
# File 'lib/tokyo_cache_cow/server.rb', line 69

def receive_data(data)
  ss = StringScanner.new(data)
  
  while part = ss.scan_until(TerminatorRegex)
    begin
      command_argument_separator_index = part.index(/\s/)
      command = part[0, command_argument_separator_index]
      args = part[command_argument_separator_index + 1, part.size - command_argument_separator_index - 3]
      case command
      when 'get', 'gets'
        keys = args.split(/\s+/)
        keys.each do |k|
          next unless validate_key(k)
          if data = @cache.get(k)
            if command == 'get'
              send_data(GetValueReply % [k, data[:flags], data[:value].size])
            else
              send_data(CasValueReply % [k, data[:flags], data[:value].size, data[:value].hash])
            end
            send_data(data[:value])
            send_data(Terminator)
          end
        end
        send_data(EndReply)
      when 'set'
        SetCommand.match(args) or (send_client_error and next)
        (key, flags, expires, bytes, noreply) = [$1, Integer($2), Integer($3), Integer($4), !$5.nil?]
        next unless validate_key(key)
        send_data(@cache.set(key, ss.rest[0, bytes.to_i], :flags => flags, :expires => expires) ?
          StoredReply : NotStoredReply)
        ss.pos += bytes + 2 
      when 'add'
        SetCommand.match(args)
        (key, flags, expires, bytes, noreply) = [$1, $2.to_i, $3.to_i, $4, !$5.nil?]
        send_data(@cache.add(key, ss.rest[0, bytes.to_i], :flags => flags, :expires => expires) ?
          StoredReply : NotStoredReply)
        ss.pos += bytes + 2 
      when 'replace'
        SetCommand.match(args)
        (key, flags, expires, bytes, noreply) = [$1, $2.to_i, $3.to_i, $4, !$5.nil?]
        send_data(@cache.replace(key, ss.rest[0, bytes.to_i], :flags => flags, :expires => expires) ?
          StoredReply : NotStoredReply)
        ss.pos += bytes + 2 
      when 'append'
        SetCommand.match(args)
        (key, flags, expires, bytes, noreply) = [$1, $2.to_i, $3.to_i, $4, !$5.nil?]
        send_data(@cache.append(key, ss.rest[0, bytes.to_i], :flags => flags, :expires => expires) ?
          StoredReply : NotStoredReply)
        ss.pos += bytes + 2 
      when 'prepend'
        SetCommand.match(args)
        (key, flags, expires, bytes, noreply) = [$1, $2.to_i, $3.to_i, $4, !$5.nil?]
        send_data(@cache.prepend(key, ss.rest[0, bytes.to_i], :flags => flags, :expires => expires) ?
          StoredReply : NotStoredReply)
        ss.pos += bytes + 2 
      when 'cas'
        # do something
      when 'delete'
        case args
        when DeleteWithTimeoutCommand
          (key, timeout, noreply) = [$1.chomp, $2, !$3.nil?]
          next unless validate_key(key)
          send_data(@cache.delete_expire(key, timeout) ?
            DeletedReply : NotDeletedReply)
        when DeleteCommand
          (key, noreply) = [$1.chomp, !$2.nil?]
          next unless validate_key(key)
          send_data @cache.delete(key) ?
            DeletedReply : NotDeletedReply
        end
      when 'delete_match'
        DeleteMatchCommand.match(args)
        (key, noreply) = [$1.chomp, !$2.nil?]
        next unless validate_key(key)
        @cache.delete_match(key)
        send_data(DeletedReply)
      when 'incr', 'decr'
        IncrementDecrementCommand.match(args)
        (key, value, noreply) = [$1, $2.to_i, !$3.nil?]
        next unless validate_key(key)
        send_data(if d = @cache.get(key)
          value = -value if command == 'decr'
          d['data'] = (val = (d['data'].to_i + value)).to_s
          @cache.put(key, d)
          ValueReply % val
        else
          NotFoundReply
        end)
      when 'stats'
        send_data(Error)
      when 'flush_all'
        send_data(@cache.flush_all ? OK : Error)
      when 'version'
        send_data(Error)
      when 'quit'
        close_connection_after_writing
      else
        send_data(Error)
      end
    rescue
      send_server_error($!)
    end
  end
  
end

#send_client_error(message = "invalid arguments") ⇒ Object



47
48
49
# File 'lib/tokyo_cache_cow/server.rb', line 47

def send_client_error(message = "invalid arguments")
  send_data(ClientError % message.to_s)
end

#send_server_error(message = "there was a problem") ⇒ Object



51
52
53
# File 'lib/tokyo_cache_cow/server.rb', line 51

def send_server_error(message = "there was a problem")
  send_data(ServerError % message.to_s)
end

#validate_key(key) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tokyo_cache_cow/server.rb', line 55

def validate_key(key)
  if key.nil?
    send_data(ClientError % "key cannot be blank")
  elsif key && key.index(' ')
    send_data(ClientError % "key cannot contain spaces")
    nil
  elsif key.size > 250
    send_data(ClientError % "key must be less than 250 characters")
    nil
  else
    key
  end
end