[sheepdog] [PATCH v5 4/4] sheep/http: check object beofre create or delete it

Robin Dong robin.k.dong at gmail.com
Fri Dec 13 11:55:17 CET 2013


From: Robin Dong <sanbai at taobao.com>

We add kv_find_object() to check whether the object is exists or not,
so it can decide how to update or delete it.

BTW, we discard data obj after deleting kv_onode.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheep/http/kv.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/sheep/http/kv.c b/sheep/http/kv.c
index f3dd716..e72e75c 100644
--- a/sheep/http/kv.c
+++ b/sheep/http/kv.c
@@ -809,6 +809,28 @@ out:
 	free(onode);
 }
 
+struct find_object_arg {
+	bool found;
+	const char *object_name;
+};
+
+static void find_object_cb(struct http_request *req, const char *bucket,
+			   const char *object, void *opaque)
+{
+	struct find_object_arg *foarg = (struct find_object_arg *)opaque;
+
+	if (!strncmp(foarg->object_name, object, SD_MAX_OBJECT_NAME))
+		foarg->found = true;
+}
+
+static bool kv_find_object(struct http_request *req, const char *account,
+		    const char *bucket, const char *object)
+{
+	struct find_object_arg arg = {false, object};
+	kv_list_objects(req, account, bucket, find_object_cb, &arg);
+	return arg.found;
+}
+
 #define KV_ONODE_INLINE_SIZE (SD_DATA_OBJ_SIZE - sizeof(struct kv_onode_hdr))
 
 static int kv_write_onode(struct sd_inode *inode, struct kv_onode *onode,
@@ -977,6 +999,15 @@ int kv_create_object(struct http_request *req, const char *account,
 	if (ret < 0)
 		return ret;
 
+	if (kv_find_object(req, account, bucket, name)) {
+		/* delete old object if it exists */
+		ret = kv_delete_object(req, account, bucket, name);
+		if (ret != SD_RES_SUCCESS) {
+			sd_err("Failed to delete exists object %s", name);
+			return ret;
+		}
+	}
+
 	onode = xzalloc(sizeof(*onode));
 
 	/* for inlined onode */
@@ -1131,6 +1162,9 @@ int kv_read_object(struct http_request *req, const char *account,
 	if (ret < 0)
 		goto out;
 
+	if (!kv_find_object(req, account, bucket, object))
+		return SD_RES_NO_OBJ;
+
 	onode = xzalloc(sizeof(*onode));
 
 	sys->cdrv->lock(vid);
@@ -1293,6 +1327,12 @@ static int do_kv_delete_object(struct http_request *req, const char *obj_name,
 			sd_err("failed to read onode extent %" PRIx64, oid);
 			goto out;
 		}
+		/* discard the data object which stores onode */
+		ret = discard_data_obj(oid);
+		if (ret != SD_RES_SUCCESS) {
+			sd_err("failed to discard oid %lu", oid);
+			goto out;
+		}
 		for (i = 0; i < onode->hdr.nr_extent; i++) {
 			ret = oalloc_free(onode->hdr.data_vid, ext[i].start,
 					  ext[i].count);
@@ -1321,6 +1361,9 @@ int kv_delete_object(struct http_request *req, const char *account,
 	if (ret < 0)
 		return ret;
 
+	if (!kv_find_object(req, account, bucket, object))
+		return SD_RES_NO_OBJ;
+
 	sys->cdrv->lock(vid);
 	hval = sd_hash(object, strlen(object));
 	for (i = 0; i < MAX_DATA_OBJS; i++) {
-- 
1.7.12.4




More information about the sheepdog mailing list