Browse Source

List commands (phase one)

v0.4.0

* add Redis_LPUSH as a macro (for now)
* add LPUSHX & LPOP
* add LINDEX & LINSERT
tags/v0.4.0
Ryan Joseph 10 months ago
parent
commit
e5b5947d51
5 changed files with 72 additions and 16 deletions
  1. +23
    -1
      src/commands.c
  2. +17
    -0
      src/commands.h
  3. +8
    -0
      src/types.h
  4. +1
    -1
      src/yarl.h
  5. +23
    -14
      test/test.c

+ 23
- 1
src/commands.c View File

@@ -4,7 +4,6 @@
#include "resp.h"
#include "log.h"
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
@@ -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
- 0
src/commands.h View File

@@ -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
@@ -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
- 0
src/types.h View File

@@ -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__

+ 1
- 1
src/yarl.h View File

@@ -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"


+ 23
- 14
test/test.c View File

@@ -127,11 +127,7 @@ 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!"));
@@ -139,9 +135,7 @@ int main(int argc, char **argv)
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
@@ -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");
}

Loading…
Cancel
Save