[memcached] sgrimm, r430: Multithreading prep: Refactor item updat...

commits at code.sixapart.com commits at code.sixapart.com
Fri Nov 10 00:34:53 UTC 2006


Multithreading prep: Refactor item update code into a separate function.

This intermediate commit probably breaks item freeing; the next one will
fix it again, but this is broken out for easier reading of the changes.


U   branches/multithreaded/server/memcached.c
U   branches/multithreaded/server/memcached.h


Modified: branches/multithreaded/server/memcached.c
===================================================================
--- branches/multithreaded/server/memcached.c	2006-11-09 23:29:09 UTC (rev 429)
+++ branches/multithreaded/server/memcached.c	2006-11-10 00:34:53 UTC (rev 430)
@@ -552,56 +552,58 @@
 void complete_nread(conn *c) {
     item *it = c->item;
     int comm = c->item_comm;
-    item *old_it;
-    int delete_locked = 0;
-    char *key = ITEM_key(it);
 
     stats.set_cmds++;
 
     if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) != 0) {
         out_string(c, "CLIENT_ERROR bad data chunk");
-        goto err;
+    } else {
+        if (store_item(it, comm)) {
+            out_string(c, "STORED");
+        } else {
+            out_string(c, "NOT_STORED");
+        }
     }
 
-    old_it = item_get_notedeleted(key, it->nkey, &delete_locked);
+    c->item = 0;
+}
 
+/*
+ * Stores an item in the cache according to the semantics of one of the set
+ * commands. In threaded mode, this is protected by the cache lock.
+ *
+ * Returns true if the item was stored.
+ */
+int store_item(item *it, int comm) {
+    char *key = ITEM_key(it);
+    int delete_locked = 0;
+    item *old_it = item_get_notedeleted(key, it->nkey, &delete_locked);
+    int stored = 0;
+
     if (old_it && comm == NREAD_ADD) {
-        item_update(old_it);  /* touches item, promotes to head of LRU */
-        out_string(c, "NOT_STORED");
-        goto err;
-    }
+        /* add only adds a nonexistent item, but promote to head of LRU */
+        item_update(old_it);
+    } else if (!old_it && comm == NREAD_REPLACE) {
+        /* replace only replaces an existing value; don't store */
+    } else if (delete_locked && (comm == NREAD_REPLACE || comm == NREAD_ADD)) {
+        /* replace and add can't override delete locks; don't store */
+    } else {
+        /* "set" commands can override the delete lock
+           window... in which case we have to find the old hidden item
+           that's in the namespace/LRU but wasn't returned by
+           item_get.... because we need to replace it */
+        if (delete_locked)
+            old_it = item_get_nocheck(key, it->nkey);
 
-    if (!old_it && comm == NREAD_REPLACE) {
-        out_string(c, "NOT_STORED");
-        goto err;
-    }
+        if (old_it)
+            item_replace(old_it, it);
+        else
+            item_link(it);
 
-    if (delete_locked) {
-        if (comm == NREAD_REPLACE || comm == NREAD_ADD) {
-            out_string(c, "NOT_STORED");
-            goto err;
-        }
+        stored = 1;
+     }
 
-        /* but "set" commands can override the delete lock
-         window... in which case we have to find the old hidden item
-         that's in the namespace/LRU but wasn't returned by
-         item_get.... because we need to replace it (below) */
-        old_it = item_get_nocheck(key, it->nkey);
-    }
-
-    if (old_it)
-        item_replace(old_it, it);
-    else
-        item_link(it);
-
-    c->item = 0;
-    out_string(c, "STORED");
-    return;
-
-err:
-     item_free(it);
-     c->item = 0;
-     return;
+     return stored;
 }
 
 typedef struct token_s {

Modified: branches/multithreaded/server/memcached.h
===================================================================
--- branches/multithreaded/server/memcached.h	2006-11-09 23:29:09 UTC (rev 429)
+++ branches/multithreaded/server/memcached.h	2006-11-10 00:34:53 UTC (rev 430)
@@ -258,6 +258,7 @@
 int add_iov(conn *c, const void *buf, int len);
 int add_msghdr(conn *c);
 char *add_delta(item *item, int incr, unsigned int delta, char *buf);
+int store_item(item *item, int comm);
 /* stats */
 void stats_reset(void);
 void stats_init(void);




More information about the memcached-commits mailing list