Skip to content

Commit

Permalink
List commands (phase one)
Browse files Browse the repository at this point in the history
v0.4.0

* add Redis_LPUSH as a macro (for now)
* add LPUSHX & LPOP
* add LINDEX & LINSERT
  • Loading branch information
rpj committed Aug 22, 2019
1 parent bf23cc2 commit e5b5947
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
24 changes: 23 additions & 1 deletion src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "resp.h"
#include "log.h"

#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
Expand Down Expand Up @@ -157,3 +156,26 @@ void Redis_PSUBSCRIBE(RedisConnection_t conn, const char *pattern)
{
_RedisCommand_issue(conn, "PSUBSCRIBE", 1, 0, pattern);
}

int Redis_LPUSHX(RedisConnection_t conn, const char *key, const char* value)
{
return _RedisCommandReturn_extractInt(_RedisCommand_issue(conn, "LPUSHX", 2, 0, key, value));
}

char* Redis_LPOP(RedisConnection_t conn, const char* key)
{
REDIS_CMD__GENERIC(RedisObjectType_BulkString, conn, "LPOP", 1, 0, (char *)cmdRet.obj, NULL, false, key);
}

char *Redis_LINDEX(RedisConnection_t conn, const char* key, int index)
{
REDIS_CMD__GENERIC(RedisObjectType_BulkString, conn, "LINDEX", 2, 0x2,
(char *)cmdRet.obj, NULL, false, key, _RedisObject_RESP__intAsStringWithLength(index, NULL));
}

int Redis_LINSERT(RedisConnection_t conn, const char* key, RedisLINSERTPivot_t pivotPoint, const char* pivot, const char* value)
{
assert(pivotPoint > PivotInvalid && pivotPoint < PivotLastSentinel);
const char* pivotStr = pivotPoint == PivotBefore ? "BEFORE" : "AFTER";
return _RedisCommandReturn_extractInt(_RedisCommand_issue(conn, "LINSERT", 4, 0, key, pivotStr, pivot, value));
}
17 changes: 17 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "constants.h"

#include <stdbool.h>
#include <stdint.h>

// any non-scalar return values are OWNED BY THE CALLER and must be free()ed

Expand All @@ -28,8 +29,24 @@ bool Redis_EXPIRE(RedisConnection_t conn, const char *key, int seconds);

bool Redis_EXPIREAT(RedisConnection_t conn, const char *key, int timestamp);

// int Redis_LPUSH(RedisConnection_t conn, const char *key, uint32_t argCount, ...)
#define Redis_LPUSH(conn, key, argCount, ...) \
_RedisCommandReturn_extractInt(_RedisCommand_issue((conn), "LPUSH", (argCount) + 1, 0x0, (key), __VA_ARGS__))

int Redis_LPUSHX(RedisConnection_t conn, const char *key, const char* value);

char* Redis_LPOP(RedisConnection_t conn, const char* key);

char* Redis_LINDEX(RedisConnection_t conn, const char* key, int index);

int Redis_LINSERT(RedisConnection_t conn, const char* key, RedisLINSERTPivot_t pivotPoint, const char* pivot, const char* value);

// use RedisConnection_getNextObject() to get messages
void Redis_SUBSCRIBE(RedisConnection_t conn, const char *channel);
void Redis_PSUBSCRIBE(RedisConnection_t conn, const char *pattern);

// for Redis_LPUSH as currently (macro) defined
extern RedisObject_t _RedisCommand_issue(RedisConnection_t, const char *, uint32_t, uint32_t, ...);
extern int _RedisCommandReturn_extractInt(RedisObject_t);

#endif // __YARL_COMMANDS__H__
8 changes: 8 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ typedef struct RedisArray_t
RedisObject_t *objects;
} RedisArray_t;

typedef enum
{
PivotInvalid = -1,
PivotBefore,
PivotAfter,
PivotLastSentinel
} RedisLINSERTPivot_t;

#endif // __YARL_TYPES__H__
2 changes: 1 addition & 1 deletion src/yarl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern "C" {
#endif

#define YARL_VERSION "0.3.1"
#define YARL_VERSION "0.4.0"

#include "commands.h"
#include "constants.h"
Expand Down
37 changes: 23 additions & 14 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,15 @@ int main(int argc, char **argv)
printf("Appended %d bytes, now have '%s'\n", appended, getVal);
free(getVal);

if (!Redis_DEL(rConn, TEST_KEY_NAME))
{
fprintf(stderr, "DEL failed\n");
exit(-2);
}
assert(Redis_DEL(rConn, TEST_KEY_NAME));

printf("Publish result: %d\n", Redis_PUBLISH(rConn, TEST_KEY_NAME, "Pub/sub is the bee's knees!"));

RedisArray_t *allKeys = Redis_KEYS(rConn, "*");

if (allKeys)
{
printf("Found %lu keys:\n", allKeys->count);
for (int i = 0; i < allKeys->count; i++)
printf("\t%s\n", (char *)allKeys->objects[i].obj);
printf("Found %lu keys\n", allKeys->count);
RedisArray_dealloc(allKeys);
}
else
Expand All @@ -150,10 +144,25 @@ int main(int argc, char **argv)
exit(-1);
}

if (!Redis_SET(rConn, TEST_KEY_NAME "ButWillExpireInSixty", "60...") ||
!Redis_EXPIRE(rConn, TEST_KEY_NAME "ButWillExpireInSixty", 60))
{
fprintf(stderr, "EXPIRE failed\n");
exit(-2);
}
assert(Redis_SET(rConn, TEST_KEY_NAME "ButWillExpireInSixty", "60...") &&
Redis_EXPIRE(rConn, TEST_KEY_NAME "ButWillExpireInSixty", 60));

char* lName = TEST_KEY_NAME "Listicle";
assert(Redis_LPUSH(rConn, lName, 3, "foo", "bar", "baz") == 3);
assert(Redis_LPUSHX(rConn, lName, "LPUSHX") == 4);

char* popped = Redis_LPOP(rConn, lName);
assert(!strncmp(popped, "LPUSHX", strlen("LPUSHX")));
free(popped);

assert(!Redis_LPUSHX(rConn, TEST_KEY_NAME "DNE", "LPUSHX"));

char* lidx = Redis_LINDEX(rConn, lName, 2);
assert(!strncmp(lidx, "foo", strlen("foo")));

assert(Redis_LINSERT(rConn, lName, PivotBefore, "bar", "before_bar") == 4 &&
Redis_LINSERT(rConn, lName, PivotAfter, "bar", "after_bar") == 5);

Redis_DEL(rConn, lName);
printf("List tests successful.\n");
}

0 comments on commit e5b5947

Please sign in to comment.