Skip to content

Commit

Permalink
Merge branch 'main' into hash-support3
Browse files Browse the repository at this point in the history
  • Loading branch information
moticless committed Aug 13, 2023
2 parents 61a8914 + 0a58bb5 commit 9526c13
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ jobs:
run: |
export LD_LIBRARY_PATH=/usr/local/lib/
export LIBRDB_REDIS_FOLDER=~/redis/src
make
make all valgrind
working-directory: ${{github.workspace}}
96 changes: 61 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
ifeq ($(BUILD_TLS),yes)
export BUILD_TLS
endif

# DESTDIR Specifies library installation folder
DESTDIR?=/usr/local/lib
PREFIX?=/usr/local
DESTDIR?=
INSTALL = /usr/bin/install -c
BINDIR=$(DESTDIR)$(PREFIX)/bin
LIBDIR=$(DESTDIR)$(PREFIX)/lib
INCDIR=$(DESTDIR)$(PREFIX)/include/librdb/

VERSION = $(shell grep -oP '(?<=LIBRDB_VERSION_STRING ")[0-9]+\.[0-9]+\.[0-9]+' ./src/lib/version.h)

# ------------------------- ALL --------------------------------------

all:
$(MAKE) -C deps -f Makefile all
$(MAKE) -C src/lib -f Makefile all
$(MAKE) -C src/ext -f Makefile all
$(MAKE) -C src/cli -f Makefile all
$(MAKE) -C examples -f Makefile all
$(MAKE) -C test -f Makefile all
./runtests -v

lib:
$(MAKE) -C deps -f Makefile all
$(MAKE) -C src/lib -f Makefile all
$(MAKE) -C src/ext -f Makefile all
$(MAKE) -C examples -f Makefile all

clean:
$(MAKE) -C deps -f Makefile clean
Expand All @@ -28,38 +25,67 @@ clean:
$(MAKE) -C examples -f Makefile clean
$(MAKE) -C test -f Makefile clean

distclean:
$(MAKE) -C deps -f Makefile clean
$(MAKE) -C src/lib -f Makefile clean
$(MAKE) -C src/ext -f Makefile clean
$(MAKE) -C src/cli -f Makefile clean
$(MAKE) -C examples -f Makefile clean
$(MAKE) -C test -f Makefile clean
distclean: clean

example:
example: all
cd examples && export LD_LIBRARY_PATH=../lib && ./example1

test:
# ------------------------- TEST --------------------------------------

build_test: all
$(MAKE) -C test -f Makefile all

test: build_test
./runtests

valgrind:
valgrind: build_test
./runtests -v

install: lib
cp lib/librdb.so $(DESTDIR)
cp lib/librdb-ext.so $(DESTDIR)
# ------------------------- INSTALL --------------------------------------
install: all
$(INSTALL) -d $(BINDIR)
$(INSTALL) -m 755 bin/rdb-cli $(BINDIR)/rdb-cli-$(VERSION)
ln -fs $(BINDIR)/rdb-cli-$(VERSION) $(BINDIR)/rdb-cli
$(INSTALL) -d $(LIBDIR)
$(INSTALL) -m 755 lib/librdb.so $(LIBDIR)/librdb.so.$(VERSION)
ln -fs $(LIBDIR)/librdb.so.$(VERSION) $(LIBDIR)/librdb.so
$(INSTALL) -m 755 lib/librdb-ext.so $(LIBDIR)/librdb-ext.so.$(VERSION)
ln -fs $(LIBDIR)/librdb-ext.so.$(VERSION) $(LIBDIR)/librdb-ext.so
$(INSTALL) -m 755 lib/librdb.a $(LIBDIR)/librdb.a.$(VERSION)
ln -fs $(LIBDIR)/librdb.a.$(VERSION) $(LIBDIR)/librdb.a
$(INSTALL) -m 755 lib/librdb-ext.a $(LIBDIR)/librdb-ext.a.$(VERSION)
ln -fs $(LIBDIR)/librdb-ext.a.$(VERSION) $(LIBDIR)/librdb-ext.a
$(INSTALL) -d $(INCDIR)
$(INSTALL) -m 644 api/librdb-api.h $(INCDIR)
$(INSTALL) -m 644 api/librdb-ext-api.h $(INCDIR)

uninstall:
rm -f $(BINDIR)/rdb-cli || true
rm -f $(BINDIR)/rdb-cli-$(VERSION)
rm -f $(LIBDIR)/librdb.so
rm -f $(LIBDIR)/librdb.so.$(VERSION)
rm -f $(LIBDIR)/librdb-ext.so
rm -f $(LIBDIR)/librdb-ext.so.$(VERSION)
rm -f $(LIBDIR)/librdb.a
rm -f $(LIBDIR)/librdb.a.$(VERSION)
rm -f $(LIBDIR)/librdb-ext.a
rm -f $(LIBDIR)/librdb-ext.a.$(VERSION)
rm -f $(INCDIR)/librdb-api.h
rm -f $(INCDIR)/librdb-ext-api.h

# ------------------------- HELP --------------------------------------

help:
@echo "Target rules:"
@echo " all - Build parser libraries, tests, and run tests."
@echo " lib - Build parser libraries."
@echo " test - Run tests with shared lib."
@echo " valgrind - Run tests with static lib and valgrind."
@echo " example - Run the example."
@echo "librdb (v$(VERSION)) target rules:"
@echo " all - Build parser libraries, tests, and run tests"
@echo " test - Run tests with shared lib"
@echo " valgrind - Run tests with static lib and valgrind"
@echo " example - Run the example"
@echo " clean - Clean without deps folders"
@echo " distclean - Clean including deps folders"
@echo " install - Build parser libraries and copy to DESTDIR."
@echo " help - Prints this message."
@echo " install - Build parser libraries and copy to (DESTDIR)\(PREFIX)"
@echo " uninstall - Remove libraries from (DESTDIR)\(PREFIX)"
@echo " help - Prints this message"


.PHONY: all clean test help valgrind lib
.PHONY: all test valgrind example clean distclean install uninstall build_test help version
53 changes: 36 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,60 @@ protocols, enabling consumption by various writers. Additionally, a command-line

## Current status
The project is currently in its early phase and is considered to be a draft. At present,
the parser is only capable of handling string and list data types. We are actively seeking
feedback on the design, API, and implementation to refine the project before proceeding
with further development. Community contributions are welcome, yet please note that
the codebase is still undergoing significant changes and may evolve in the future.
the parser is only capable of handling STRING, LIST, HASH and SET data types. We are
actively seeking feedback on the design, API, and implementation to refine the project
before proceeding with further development. Community contributions are welcome, yet
please note that the codebase is still undergoing significant changes and may evolve in
the future.

## Getting Started
If you just wish to get a basic understanding of the library's functionality, without
running tests (To see parser internal state printouts, execute the command
`export LIBRDB_DEBUG_DATA=1` beforehand):

% make lib example
% make all example

To build and run tests, you need to have cmocka unit testing framework installed and then:
To build and run tests, you need to have cmocka unit testing framework installed:

% make
% make test

Run CLI extension of this library and parse RDB file to json (might need
`export LD_LIBRARY_PATH=./lib` beforehand):
Install and run CLI extension of this library. Parse RDB file to json:

% make install
% rdb-cli multiple_lists_strings.rdb json

[{
"string2":"Hi there!",
"mylist1":["v1"],
"mylist3":["v3","v2","v1"],
"lzf_compressed":"cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
"string1":"blaa",
"mylist2":["v2","v1"]
}]

% ./bin/rdb-cli ./test/dumps/multiple_lists_strings.rdb json

Run CLI extension to generate RESP commands

% ./bin/rdb-cli ./test/dumps/multiple_lists_strings.rdb resp
% rdb-cli ./test/dumps/multiple_lists_strings.rdb resp
*3
$5
RPUSH
$6
$3
SET
$7
string2
...

Run against Redis server, say, on address 127.0.0.1:6379, and upload RDB file:
Run against live Redis server and upload RDB file (example assumes Redis is installed locally):

% ./bin/rdb-cli ./test/dumps/multiple_lists_strings.rdb redis -h 127.0.0.1 -p 6379
% redis-server --port 6379 &
% rdb-cli multiple_lists_strings.rdb redis -h 127.0.0.1 -p 6379
% redis-cli keys "*"

(rdb-cli usage available [here](#rdb-cli usage).)
1) "string2"
2) "mylist3"
3) "mylist2"
4) "mylist1"
5) "string1"
6) "lzf_compressed"

## Motivation behind this project
There is a genuine need by the Redis community for a versatile RDB file parser that can
Expand Down
4 changes: 0 additions & 4 deletions api/librdb-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,6 @@ _LIBRDB_API void RDB_handleByLevel(RdbParser *p, RdbDataType t, RdbHandlersLevel
* Follows the semantic semver versioning convention
*****************************************************************/

#define RDB_MAJOR_VERSION 255
#define RDB_MINOR_VERSION 255
#define RDB_PATCH_VERSION 255

_LIBRDB_API const char* RDB_getLibVersion(int* major, int* minor, int* patch);

#ifdef __cplusplus
Expand Down
3 changes: 2 additions & 1 deletion src/cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ endif

######################################### RULES #######################################
all: $(TARGET_APP)
mv $(TARGET_APP) ../../bin
rm -f ../../bin/$(TARGET_APP)
cp $(TARGET_APP) ../../bin/
@echo "Done.";

$(TARGET_APP): %: %.c
Expand Down
7 changes: 5 additions & 2 deletions src/cli/rdb-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ static void loggerWrap(RdbLogLevel l, const char *msg, ...) {
}

static void printUsage() {
printf("[v%s] ", RDB_getLibVersion(NULL,NULL,NULL));
printf("Usage: rdb-cli /path/to/dump.rdb [OPTIONS] <FORMAT {json|resp|redis}> [FORMAT_OPTIONS]\n");
printf("OPTIONS:\n");
printf("\t-k, --filter-key <REGEX> Filter keys using regular expressions\n");
printf("\t-l, --log-file <PATH> Path to the log file (Default: './rdb-cli.log')\n\n");

printf("FORMAT_OPTIONS ('json'):\n");
printf("\t-w, --with-aux-values Include auxiliary values\n");
printf("\t-f, --flatten Print flatten json, without DBs Parenthesis\n");
printf("\t-o, --output <FILE> Specify the output file. If not specified, output goes to stdout\n\n");

printf("FORMAT_OPTIONS ('resp'):\n");
Expand All @@ -77,12 +79,13 @@ static void printUsage() {

static RdbRes formatJson(RdbParser *parser, char *input, int argc, char **argv) {
char *output = NULL;/*default:stdout*/
int withAuxValues = 0; /*without*/
int flatten=0, withAuxValues = 0; /*without*/

/* parse specific command options */
for (int at = 1; at < argc; ++at) {
char *opt = argv[at];
if (getOptArg(argc, argv, &at, "-o", "--output", opt, NULL, &output)) continue;
if (getOptArg(argc, argv, &at, "-f", "--flatten", opt, &flatten, NULL)) continue;
if (getOptArg(argc, argv, &at, "-w", "--with-aux-values", opt, &withAuxValues, NULL)) continue;

fprintf(stderr, "Invalid JSON [FORMAT_OPTIONS] argument: %s\n", opt);
Expand All @@ -94,7 +97,7 @@ static RdbRes formatJson(RdbParser *parser, char *input, int argc, char **argv)
.level = RDB_LEVEL_DATA,
.encoding = RDBX_CONV_JSON_ENC_PLAIN,
.skipAuxField = !(withAuxValues),
.flatten = 1,
.flatten = flatten,
};

if (RDBX_createReaderFile(parser, input) == NULL)
Expand Down
15 changes: 5 additions & 10 deletions src/lib/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "../../deps/redis/crc64.h"
#include "bulkAlloc.h"
#include "parser.h"
#include "version.h"
#include "defines.h"
#include "../../deps/redis/endianconv.h"
#include "../../deps/redis/util.h"
Expand Down Expand Up @@ -525,16 +526,10 @@ _LIBRDB_API void RDB_handleByLevel(RdbParser *p, RdbDataType type, RdbHandlersLe
}

_LIBRDB_API const char *RDB_getLibVersion(int *major, int *minor, int *patch) {
static int initialized = 0;
static char versionString[50];

if (major) *major = RDB_MAJOR_VERSION;
if (minor) *minor = RDB_MINOR_VERSION;
if (patch) *patch = RDB_PATCH_VERSION;
if (!initialized) sprintf(versionString, "%d.%d.%d", *major, *minor, *patch);
initialized = 1;

return versionString;
if (major) *major = LIBRDB_MAJOR_VERSION;
if (minor) *minor = LIBRDB_MINOR_VERSION;
if (patch) *patch = LIBRDB_PATCH_VERSION;
return LIBRDB_VERSION_STRING;
}

/*** various functions ***/
Expand Down
8 changes: 8 additions & 0 deletions src/lib/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#define LIBRDB_MAJOR_VERSION 0
#define LIBRDB_MINOR_VERSION 0
#define LIBRDB_PATCH_VERSION 1

/* Keep direct value for external readers */
#define LIBRDB_VERSION_STRING "0.0.1"


2 changes: 1 addition & 1 deletion test/test_rdb_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void test_rdb_cli_resp_common(const char *rdbfile) {

static void test_rdb_cli_json(void **state) {
UNUSED(state);
runSystemCmd("./bin/rdb-cli ./test/dumps/multiple_lists_strings.rdb json -w -o ./test/tmp/out.json > /dev/null ");
runSystemCmd("./bin/rdb-cli ./test/dumps/multiple_lists_strings.rdb json -f -w -o ./test/tmp/out.json > /dev/null ");
assert_json_equal(DUMP_FOLDER("multiple_lists_strings_data.json"), "./test/tmp/out.json", 0);
}

Expand Down

0 comments on commit 9526c13

Please sign in to comment.