Permalink
Browse files

Add APUSH and APOP

  • Loading branch information...
1 parent e537bb2 commit ae322e9979a8abc3c1bb5cfd0cc3245985b49249 @txus committed Sep 16, 2012
Showing with 77 additions and 0 deletions.
  1. +17 −0 README.md
  2. +18 −0 src/shitdb/db.c
  3. +4 −0 src/shitdb/db.h
  4. +17 −0 src/shitdb/server.c
  5. +21 −0 tests/db_tests.c
View
@@ -21,6 +21,23 @@ Example session:
QUIT
Bye!
+## Basic types
+
+* Integer: 2834
+* String: "yuhuuuu"
+* Array: [42,"foo",9999]
+
+## Instructions
+
+* GET [key]: Returns the content of a key.
+* SET [key] [value]: Sets key to a value of any of the basic types.
+* QUIT: Ends the connection.
+
+### Array instructions
+
+* APUSH [key] [value]: Pushes value to the array located at key.
+* APOP [key]: Pops the array located at key and returns the popped value.
+
## Contributing
1. Fork it
View
@@ -26,3 +26,21 @@ DB_get(DB *db, bstring key)
{
return (Object*)Hashmap_get(db->map, key);
}
+
+/*
+ * Array operations
+ */
+
+void
+DB_apush(DB *db, bstring key, Object *value)
+{
+ Object *array = (Object*)Hashmap_get(db->map, key);
+ DArray_push(array->value.as_array, value);
+}
+
+Object*
+DB_apop(DB *db, bstring key)
+{
+ Object *array = (Object*)Hashmap_get(db->map, key);
+ return (Object*)DArray_pop(array->value.as_array);
+}
View
@@ -14,4 +14,8 @@ DB* DB_create();
void DB_set(DB *db, bstring key, Object *value);
Object* DB_get(DB *db, bstring key);
+// Array
+void DB_apush(DB *db, bstring key, Object *value);
+Object* DB_apop(DB *db, bstring key);
+
#endif
View
@@ -15,6 +15,10 @@ int Command_arity(bstring cmd)
return 1;
} else if (bstrcmp(cmd, bfromcstr("SET")) == 0) {
return 2;
+ } else if (bstrcmp(cmd, bfromcstr("APUSH")) == 0) {
+ return 2;
+ } else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
+ return 1;
} else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
return 0;
} else {
@@ -46,6 +50,19 @@ int Server_execute(DB *db, bstring command, Object *result)
check(value, "Invalid value to SET.");
DB_set(db, key, value);
+ } else if (bstrcmp(cmd, bfromcstr("APUSH")) == 0) {
+ bstring key = *ptr;
+ ptr++;
+ Object *value = String_to_object(*ptr);
+ check(value, "Invalid value to APUSH.");
+
+ DB_apush(db, key, value);
+ } else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
+ bstring key = *ptr;
+ ptr++;
+
+ Object *ret = DB_apop(db, key);
+ memcpy(result, ret, sizeof(Object));
} else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
return -1;
}
View
@@ -44,6 +44,25 @@ char *test_get()
return NULL;
}
+char *test_apush()
+{
+ Object *number = Object_create_integer(999);
+ DB_apush(db, bfromcstr("array"), number);
+ return NULL;
+}
+
+char *test_apop()
+{
+ Object *integer = DB_apop(db, bfromcstr("array"));
+ bstring integerstr = Object_to_string(integer);
+
+ mu_assert(bstrcmp(integerstr, bfromcstr("999")) == 0, "Wrong popped value.");
+
+ Object *array = DB_get(db, bfromcstr("array"));
+ mu_assert(DArray_count(array->value.as_array) == 2, "Value was not really popped.");
+ return NULL;
+}
+
char *test_destroy()
{
DB_destroy(db);
@@ -57,6 +76,8 @@ char *all_tests() {
mu_run_test(test_create);
mu_run_test(test_set);
mu_run_test(test_get);
+ mu_run_test(test_apush);
+ mu_run_test(test_apop);
mu_run_test(test_destroy);
return NULL;

0 comments on commit ae322e9

Please sign in to comment.