Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement AAT and expose ACOUNT to the server

  • Loading branch information...
commit 421e125d8291021061aa23f14ab3a130a4fb7f8d 1 parent a78b675
@txus authored
View
1  README.md
@@ -37,6 +37,7 @@ Example session:
* APUSH [key] [value]: Pushes value to the array located at key.
* APOP [key]: Pops the array located at key and returns the popped value.
+* AAT [key] [index]: Gets the array element at a certain index.
* ACOUNT [key]: Returns the number of elements in the array.
### Hash instructions
View
1  src/shitdb/darray.h
@@ -22,6 +22,7 @@ int DArray_push(DArray *array, void *el);
void *DArray_pop(DArray *array);
void DArray_clear_destroy(DArray *array);
+#define DArray_at(A,I) ((A)->contents[(I)])
#define DArray_last(A) ((A)->contents[(A)->end - 1])
#define DArray_first(A) ((A)->contents[0])
#define DArray_end(A) ((A)->end)
View
7 src/shitdb/db.c
@@ -47,6 +47,13 @@ DB_apop(DB *db, bstring key)
}
Object*
+DB_aat(DB *db, bstring key, int index)
+{
+ Object *array = (Object*)Hashmap_get(db->map, key);
+ return (Object*)DArray_at(array->value.as_array, index);
+}
+
+Object*
DB_acount(DB *db, bstring key)
{
Object *array = (Object*)Hashmap_get(db->map, key);
View
1  src/shitdb/db.h
@@ -17,6 +17,7 @@ Object* DB_get(DB *db, bstring key);
// Array
void DB_apush(DB *db, bstring key, Object *value);
Object* DB_apop(DB *db, bstring key);
+Object* DB_aat(DB *db, bstring key, int index);
Object* DB_acount(DB *db, bstring key);
void DB_hset(DB *db, bstring key, bstring hkey, Object *value);
Object* DB_hget(DB *db, bstring key, bstring hkey);
View
16 src/shitdb/server.c
@@ -19,6 +19,10 @@ int Command_arity(bstring cmd)
return 2;
} else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
return 1;
+ } else if (bstrcmp(cmd, bfromcstr("AAT")) == 0) {
+ return 2;
+ } else if (bstrcmp(cmd, bfromcstr("ACOUNT")) == 0) {
+ return 1;
} else if (bstrcmp(cmd, bfromcstr("HSET")) == 0) {
return 3;
} else if (bstrcmp(cmd, bfromcstr("HGET")) == 0) {
@@ -61,6 +65,18 @@ int Server_execute(DB *db, bstring command, Object *result)
check(value, "Invalid value to APUSH.");
DB_apush(db, key, value);
+ } else if (bstrcmp(cmd, bfromcstr("AAT")) == 0) {
+ bstring key = *ptr;
+ ptr++;
+ int index = atoi(bdata(*ptr));
+
+ Object *ret = DB_aat(db, key, index);
+
+ memcpy(result, ret, sizeof(Object));
+ } else if (bstrcmp(cmd, bfromcstr("ACOUNT")) == 0) {
+ bstring key = *ptr;
+ Object *ret = DB_acount(db, key);
+ memcpy(result, ret, sizeof(Object));
} else if (bstrcmp(cmd, bfromcstr("APOP")) == 0) {
bstring key = *ptr;
ptr++;
View
14 tests/db_tests.c
@@ -46,8 +46,8 @@ char *test_get()
char *test_apush()
{
- Object *number = Object_create_integer(999);
- DB_apush(db, bfromcstr("array"), number);
+ Object *integer = Object_create_integer(999);
+ DB_apush(db, bfromcstr("array"), integer);
return NULL;
}
@@ -60,6 +60,15 @@ char *test_apop()
return NULL;
}
+char *test_aat()
+{
+ Object *integer = DB_aat(db, bfromcstr("array"), 0);
+ bstring integerstr = Object_to_string(integer);
+
+ mu_assert(bstrcmp(integerstr, bfromcstr("42")) == 0, "Wrong value at index.");
+ return NULL;
+}
+
char *test_acount()
{
Object *count = DB_acount(db, bfromcstr("array"));
@@ -107,6 +116,7 @@ char *all_tests() {
mu_run_test(test_get);
mu_run_test(test_apush);
mu_run_test(test_apop);
+ mu_run_test(test_aat);
mu_run_test(test_acount);
mu_run_test(test_hset);
mu_run_test(test_hget);
Please sign in to comment.
Something went wrong with that request. Please try again.