Skip to content

Commit fb199d0

Browse files
committed
filter module changes
1 parent db8728c commit fb199d0

39 files changed

+3386
-601
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ coverage
2121

2222
# Compiled binary addons (http://nodejs.org/api/addons.html)
2323
build/Release
24-
lib
24+
/lib
2525

2626
# Dependency directory
2727
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2828
node_modules
2929
test/dump
30+
31+
# So o files
32+
*.so
33+
*.o
34+
*.lo
35+
.vscode
36+
/csrc/lib/jansson/*

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
test
1+
test
2+
.vscode

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ BABEL := ./node_modules/.bin/babel
33
THIS_FILE := $(lastword $(MAKEFILE_LIST))
44

55
BUILD_DIR := ./lib
6-
SOURCES := index.js filtered-list-bust.lua sorted-filtered-list.lua groupped-list.lua
6+
SOURCES := index.js
77

88
$(BUILD_DIR)/%.js: %.js
99
$(BABEL) $*.js -d $@
1010

11-
$(BUILD_DIR)/%.lua: %.lua
12-
cp $*.lua $@
13-
1411
clean:
1512
rm -rf $(BUILD_DIR)
1613

1714
build: $(foreach src, $(SOURCES), $(BUILD_DIR)/$(src))
1815

1916
all: clean build
17+
18+
redis-module:
19+
make -C csrc/ build
20+

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
[![Build Status](https://travis-ci.org/makeomatic/redis-filtered-sort.svg)](https://travis-ci.org/makeomatic/redis-filtered-sort)
44

5-
Exports LUA script, which is able to perform multi filter operations, as well as sorts
5+
Wraps Redis `FilterSortModule` api, which is able to perform multi filter operations, as well as sorts
66

77
This basically replicates `http://redis.io/commands/sort` but with extra features and ability to run it in clustered mode with
88
hashed keys, which resolve to the same slot
99

10+
## Dependencies
11+
Redis must have `FilterSortModule` enabled.
12+
Please see [HOWTO](./csrc/doc/build.md)
13+
API provided [API](./csrc/doc/api.md)
14+
1015
## Installation
1116

1217
`npm i redis-filtered-sort -S`

csrc/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.o
2+
*.a
3+
*.so
4+
*.db
5+
.vscode
6+
lib/rmutil/test_vector
7+

csrc/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
include variables.include
2+
3+
all: clean build
4+
5+
docker-build:
6+
docker build . -f ./docker/Dockerfile -t $(DOCKER_IMAGE)
7+
docker-rebuild:
8+
docker build . --no-cache -f ./docker/Dockerfile -t $(DOCKER_IMAGE)
9+
10+
build:
11+
cd redis-filtered-sort && $(MAKE) all
12+
13+
clean:
14+
cd redis-filtered-sort && $(MAKE) $@
15+
16+
deps: jansson
17+
18+
19+
clean-deps:
20+
rm -rf $(JANSSON_LIBDIR)/*
21+
22+
jansson: jansson_src jansson_configure
23+
(cd $(JANSSON_LIBDIR) && make CFLAGS='$(CFLAGS)')
24+
25+
jansson_configure:
26+
(cd $(JANSSON_LIBDIR) && ./configure)
27+
28+
jansson_clean:
29+
(cd $(JANSSON_LIBDIR) && make clean)
30+
31+
jansson_src: $(JANSSON_LIBDIR)/src/jansson.h
32+
33+
$(JANSSON_LIBDIR)/src/jansson.h:
34+
wget -O- $(JANSSON_LINK) | tar xvz --directory=$(JANSSON_LIBDIR) --strip-components=1 jansson-$(JANSSON_VERSION)

csrc/docker/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM alpine AS build-mod
2+
RUN apk add --no-cache --virtual .build-deps \
3+
coreutils \
4+
gcc \
5+
linux-headers \
6+
make \
7+
musl-dev \
8+
&& apk add \
9+
bash
10+
RUN mkdir /src
11+
WORKDIR /src
12+
13+
ADD . /src/
14+
RUN make deps && make
15+
16+
FROM redis:5.0.5-alpine
17+
COPY --from=build-mod /src/redis-filtered-sort/filter_module.so /usr/local/lib/redis_filtered_sort.so
18+
ENTRYPOINT ["docker-entrypoint.sh"]
19+
CMD ["redis-server", "--loadmodule", "/usr/local/lib/redis_filtered_sort.so"]
20+
EXPOSE 6379

csrc/lib/jansson/.gitkeep

Whitespace-only changes.

csrc/redis-filtered-sort/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
include ../variables.include
2+
3+
#override path defined in variables
4+
JANSSON_LIBDIR=../lib/jansson
5+
6+
ifndef RM_INCLUDE_DIR
7+
RM_INCLUDE_DIR=../lib/
8+
endif
9+
10+
ifndef RMUTIL_LIBDIR
11+
RMUTIL_LIBDIR=../lib/rmutil
12+
endif
13+
14+
SOURCEDIR=$(shell pwd -P)
15+
CC_SOURCES = $(wildcard $(SOURCEDIR)/*.c)
16+
CC_OBJECTS = $(patsubst $(SOURCEDIR)/%.c, $(SOURCEDIR)/%.o, $(CC_SOURCES))
17+
18+
all: rmutil $(CC_OBJECTS) filter_module.so
19+
rmutil: FORCE
20+
$(MAKE) -C $(RMUTIL_LIBDIR)
21+
22+
filter_module.so: $(CC_OBJECTS)
23+
$(LD) -o $@ $(CC_OBJECTS) $(SHOBJ_LDFLAGS) $(LIBS) -L$(RMUTIL_LIBDIR) -L$(JANSSON_LIBDIR)/src/.libs -lrmutil -lpthread -lc -Bstatic -ljansson
24+
25+
clean:
26+
rm -rf *.xo *.so *.o
27+
cd $(RMUTIL_LIBDIR) && make clean
28+
29+
FORCE:
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "filter_module.h"
2+
#include "fsort.h"
3+
#include "fsort_utils.h"
4+
5+
int FSortBust_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
6+
RedisModule_AutoMemory(ctx);
7+
8+
if (argc <3 || argc > 4 ) {
9+
return RedisModule_WrongArity(ctx);
10+
}
11+
12+
pthread_t tid;
13+
RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0);
14+
15+
void **targ = RedisModule_Alloc(sizeof(void*)*3);
16+
targ[0] = bc;
17+
targ[1] = (void*)(unsigned long) argc;
18+
targ[2] = (void*)(RedisModuleString **)argv;
19+
20+
if (pthread_create(&tid,NULL,fsort_bust_thread,targ) != 0) {
21+
RedisModule_AbortBlock(bc);
22+
return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
23+
}
24+
25+
return REDISMODULE_OK;
26+
}
27+
28+
int FSortAggregate_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
29+
RedisModule_AutoMemory(ctx);
30+
31+
if (argc != 4) {
32+
return RedisModule_WrongArity(ctx);
33+
}
34+
35+
pthread_t tid;
36+
RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0);
37+
38+
void **targ = RedisModule_Alloc(sizeof(void*)*3);
39+
targ[0] = bc;
40+
targ[1] = (void*)(unsigned long) argc;
41+
targ[2] = (void*)(RedisModuleString **)argv;
42+
43+
if (pthread_create(&tid,NULL,fsort_aggregate_thread,targ) != 0) {
44+
RedisModule_AbortBlock(bc);
45+
return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
46+
}
47+
48+
return REDISMODULE_OK;
49+
}
50+
51+
int FSort_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
52+
RedisModule_AutoMemory(ctx);
53+
54+
if (argc < 7 ) {
55+
return RedisModule_WrongArity(ctx);
56+
}
57+
58+
pthread_t tid;
59+
RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0);
60+
61+
void **targ = RedisModule_Alloc(sizeof(void*)*3);
62+
targ[0] = bc;
63+
targ[1] = (void*)(unsigned long) argc;
64+
targ[2] = (void*)(RedisModuleString **)argv;
65+
66+
if (pthread_create(&tid,NULL,fsort_fsort_thread,targ) != 0) {
67+
RedisModule_AbortBlock(bc);
68+
return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
69+
}
70+
71+
return REDISMODULE_OK;
72+
}
73+
74+
int RedisModule_OnLoad(RedisModuleCtx *ctx) {
75+
76+
if (RedisModule_Init(ctx, "FilterSortModule", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
77+
return REDISMODULE_ERR;
78+
}
79+
80+
if (RedisModule_CreateCommand(ctx, "fsort", FSort_RedisCommand, "write", 1, 2, 1) == REDISMODULE_ERR) {
81+
return REDISMODULE_ERR;
82+
}
83+
84+
if (RedisModule_CreateCommand(ctx, "fsortBust", FSortBust_RedisCommand, "write", 1, 1, 1) == REDISMODULE_ERR) {
85+
return REDISMODULE_ERR;
86+
}
87+
88+
if (RedisModule_CreateCommand(ctx, "fsortaggregate", FSortAggregate_RedisCommand, "write", 1, 2, 1) == REDISMODULE_ERR) {
89+
return REDISMODULE_ERR;
90+
}
91+
92+
return REDISMODULE_OK;
93+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __FILTERMODULE_H
2+
#define __FILTERMODULE_H 1
3+
4+
#include <sys/types.h>
5+
6+
#include "fsort.h"
7+
8+
#include "pthread.h"
9+
10+
11+
#endif

0 commit comments

Comments
 (0)