Show
Ignore:
Timestamp:
04/01/08 15:16:49 (8 months ago)
Author:
ryan@…
Branch:
default
Message:

Added: cas/gets, and noreply option on sets

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • jmemcached-core/src/main/java/com/thimbleware/jmemcached/ServerSessionHandler.java

    r10 r14  
    2222import org.slf4j.LoggerFactory; 
    2323 
     24import static java.lang.Integer.parseInt; 
     25import static java.lang.String.valueOf; 
    2426import java.nio.charset.CharacterCodingException; 
    2527import java.nio.charset.Charset; 
    2628import java.nio.charset.CharsetEncoder; 
    2729import java.util.Iterator; 
    28 import static java.lang.String.valueOf; 
    29 import static java.lang.Integer.*; 
    3030 
    3131/** 
     
    5050    public boolean verbose; 
    5151 
    52     public static CharsetEncoder ENCODER  = Charset.forName("US-ASCII").newEncoder(); 
     52    public static CharsetEncoder ENCODER = Charset.forName("US-ASCII").newEncoder(); 
    5353 
    5454    /** 
     
    5959     * Construct the server session handler 
    6060     * 
    61      * @param cache the cache to use 
     61     * @param cache            the cache to use 
    6262     * @param memcachedVersion the version string to return to clients 
    63      * @param verbosity verbosity level for debugging 
    64      * @param idle how long sessions can be idle for 
     63     * @param verbosity        verbosity level for debugging 
     64     * @param idle             how long sessions can be idle for 
    6565     */ 
    6666    public ServerSessionHandler(Cache cache, String memcachedVersion, boolean verbosity, int idle) { 
     
    6868 
    6969        this.cache = cache; 
    70          
     70 
    7171        started = Now(); 
    7272        version = memcachedVersion; 
     
    141141 
    142142        ResponseMessage r = new ResponseMessage(); 
    143         if (cmd == Commands.GET) { 
     143        if (cmd == Commands.GET || cmd == Commands.GETS) { 
    144144            for (int i = 0; i < cmdKeysSize; i++) { 
    145145                MCElement result = get(command.keys.get(i)); 
    146146                if (result != null) { 
    147                     r.out.putString("VALUE " + result.keystring + " " + result.flags + " " + result.data_length + "\r\n", ENCODER); 
     147                    r.out.putString("VALUE " + result.keystring + " " + result.flags + " " + result.data_length + (cmd == Commands.GETS ? " " + result.cas_unique : "") + "\r\n", ENCODER); 
    148148                    r.out.put(result.data, 0, result.data_length); 
    149149                    r.out.putString("\r\n", ENCODER); 
     
    153153            r.out.putString("END\r\n", ENCODER); 
    154154        } else if (cmd == Commands.SET) { 
    155             r.out.putString(set(command.element), ENCODER); 
     155            String ret = set(command.element); 
     156            if (!command.noreply) 
     157                r.out.putString(ret, ENCODER); 
     158        } else if (cmd == Commands.CAS) { 
     159            String ret = cas(command.cas_key, command.element); 
     160            if (!command.noreply) 
     161                r.out.putString(ret, ENCODER); 
    156162        } else if (cmd == Commands.ADD) { 
    157             r.out.putString(add(command.element), ENCODER); 
     163            String ret = add(command.element); 
     164            if (!command.noreply) 
     165                r.out.putString(ret, ENCODER); 
    158166        } else if (cmd == Commands.REPLACE) { 
    159             r.out.putString(replace(command.element), ENCODER); 
     167            String ret = replace(command.element); 
     168            if (!command.noreply) 
     169                r.out.putString(ret, ENCODER); 
    160170        } else if (cmd == Commands.INCR) { 
    161171            r.out.putString(get_add(command.keys.get(0), parseInt(command.keys.get(1))), ENCODER); 
     
    207217    /** 
    208218     * Triggered when a session has gone idle. 
     219     * 
    209220     * @param session the MINA session 
    210      * @param status the idle status 
     221     * @param status  the idle status 
    211222     */ 
    212223    public void sessionIdle(IoSession session, IdleStatus status) { 
     
    217228    /** 
    218229     * Triggered when an exception is caught by the protocol handler 
     230     * 
    219231     * @param session the MINA session 
    220      * @param cause the exception 
     232     * @param cause   the exception 
    221233     */ 
    222234    public void exceptionCaught(IoSession session, Throwable cause) { 
     
    230242     * Handle the deletion of an item from the cache. 
    231243     * 
    232      * @param key the key for the item 
     244     * @param key  the key for the item 
    233245     * @param time only delete the element if time (time in seconds) 
    234246     * @return the message response 
     
    246258     */ 
    247259    protected String add(MCElement e) { 
    248         if (cache.add(e)) return "STORED\r\n"; 
     260        if (cache.add(e) == Cache.StoreResponse.STORED) return "STORED\r\n"; 
    249261        else return "NOT_STORED\r\n"; 
     262    } 
     263 
     264    protected String getStoreResponeString(Cache.StoreResponse storeResponse) { 
     265        switch (storeResponse) { 
     266            case EXISTS: 
     267                return "EXISTS\r\n"; 
     268            case NOT_FOUND: 
     269                return "NOT_FOUND\r\n"; 
     270            case NOT_STORED: 
     271                return "NOT_STORED\r\n"; 
     272            case STORED: 
     273                return "STORED\r\n"; 
     274        } 
     275        return null; 
    250276    } 
    251277 
     
    257283     */ 
    258284    protected String replace(MCElement e) { 
    259         if (cache.replace(e)) return "STORED\r\n"; 
    260         else return "NOT_STORED\r\n"; 
     285        return getStoreResponeString(cache.replace(e)); 
    261286    } 
    262287 
     
    268293     */ 
    269294    protected String set(MCElement e) { 
    270         if (cache.set(e)) return "STORED\r\n"; 
    271         else return "NOT_STORED\r\n"; 
    272     } 
    273  
    274     /** 
    275      * Increment an (integer) element inthe cache 
     295        return getStoreResponeString(cache.set(e)); 
     296    } 
     297 
     298    /** 
     299     * Check and set an element in the cache 
     300     * 
     301     * @param cas_key 
     302     * @param e       the element to set @return the message response string 
     303     */ 
     304    protected String cas(Long cas_key, MCElement e) { 
     305        return getStoreResponeString(cache.cas(cas_key, e)); 
     306    } 
     307 
     308    /** 
     309     * Increment an (integer) element in the cache 
     310     * 
    276311     * @param key the key to increment 
    277312     * @param mod the amount to add to the value 
     
    289324    /** 
    290325     * Check whether an element is in the cache and non-expired 
     326     * 
    291327     * @param key the key for the element to lookup 
    292328     * @return whether the element is in the cache and is live 
     
    298334    /** 
    299335     * Get an element from the cache 
     336     * 
    300337     * @param key the key for the element to lookup 
    301338     * @return the element, or 'null' in case of cache miss. 
     
    326363    /** 
    327364     * Return runtime statistics 
     365     * 
    328366     * @param arg additional arguments to the stats command 
    329367     * @return the full command response 
     
    372410    /** 
    373411     * Flush all cache entries 
     412     * 
    374413     * @return command response 
    375414     */ 
     
    380419    /** 
    381420     * Flush all cache entries with a timestamp after a given expiration time 
     421     * 
    382422     * @param expire the flush time in seconds 
    383423     * @return command response 
     
    388428 
    389429 
    390  
    391  
    392  
    393430}