Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement CLEAR

  • Loading branch information...
commit ff55af69b311df9d7780a9c20d7f6515eb2757ae 1 parent e65c81a
@txus authored
View
12 src/shitdb/db.c
@@ -16,6 +16,18 @@ DB_create()
return NULL;
}
+static int destroy_all(HashmapNode *node)
+{
+ Object_destroy((Object*)node);
+ return 0;
+}
+
+void
+DB_clear(DB *db)
+{
+ Hashmap_traverse(db->map, destroy_all);
+}
+
void
DB_set(DB *db, bstring key, Object *value)
{
View
1  src/shitdb/db.h
@@ -13,6 +13,7 @@ DB* DB_create();
#define DB_destroy(db) Hashmap_destroy(db->map); free(db);
void DB_set(DB *db, bstring key, Object *value);
Object* DB_get(DB *db, bstring key);
+void DB_clear(DB *db);
// Array
void DB_apush(DB *db, bstring key, Object *value);
View
22 src/shitdb/object.c
@@ -15,6 +15,28 @@ Object_allocate()
return NULL;
}
+void
+Object_destroy(Object *object)
+{
+ switch(object->type) {
+ case tString:
+ bdestroy(object->value.as_string);
+ break;
+ case tArray: {
+ DArray *ary = object->value.as_array;
+ Object *obj = NULL;
+ while((obj = (Object*)DArray_pop(ary)) != NULL) {
+ Object_destroy(obj);
+ }
+ break;
+ }
+ default:
+ break;
+ }
+
+ free(object);
+}
+
Object*
Object_create_integer(int value)
{
View
2  src/shitdb/object.h
@@ -28,7 +28,7 @@ Object *Object_create_integer(int value);
Object *Object_create_string(bstring value);
Object *Object_create_array(DArray *array);
Object *Object_create_hash(Hashmap *hash);
-Object *Object_destroy(Object *object);
+void Object_destroy(Object *object);
bstring Object_to_string(Object *object);
Object *String_to_object(bstring string);
View
4 src/shitdb/server.c
@@ -27,6 +27,8 @@ int Command_arity(bstring cmd)
return 3;
} else if (eql(cmd, "HGET")) {
return 2;
+ } else if (eql(cmd, "CLEAR")) {
+ return 0;
} else if (eql(cmd, "QUIT")) {
return 0;
} else {
@@ -99,6 +101,8 @@ int Server_execute(DB *db, bstring command, Object *result)
Object *ret = DB_hget(db, key, hkey);
memcpy(result, ret, sizeof(Object));
+ } else if (eql(cmd, "CLEAR")) {
+ DB_clear(db);
} else if (eql(cmd, "QUIT")) {
return -1;
}
Please sign in to comment.
Something went wrong with that request. Please try again.