Skip to content

Re/cppver #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
jssrc
doc
lib
cmake-build-debug
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ coverage

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
lib
/lib

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
test/dump

# So o files
*.so
*.o
*.lo
.vscode
*.o
*.a
*.db
4 changes: 4 additions & 0 deletions .mdeprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"nycCoverage": false,
"services": []
}
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
test
test
.vscode
csrc/lib/jansson
18 changes: 18 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"branches": [
"master",
{ "name": "develop", "prerelease": true }
],
"analyzeCommits": {
"preset": "angular",
"releaseRules": [
{ "type": "docs", "release": "patch" },
{ "type": "refactor", "release": "patch" },
{ "type": "style", "release": "patch" },
{ "type": "minor", "release": "minor" },
{ "type": "patch", "release": "patch" },
{ "type": "major", "release": "major" },
{ "type": "breaking", "release": "major" }
]
}
}
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
DOCKER_COMPOSE_VERSION: 1.8.1
DOCKER_COMPOSE_VERSION: 1.25.4

sudo: required

Expand All @@ -10,9 +10,9 @@ services:

before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- curl -L https://github.com/docker/compose/releases/download//${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin

node_js:
- "7"
- "12"
25 changes: 25 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.10)
project(redis_filtered_sort)
set(LANGUAGE CXX)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_VERBOSE_MAKEFILE on )
set(Boost_USE_STATIC_LIBS Off)
set(Boost_USE_MULTITHREADED On)

find_package(Boost REQUIRED COMPONENTS thread)

include_directories(${Boost_INCLUDE_DIRS})

include_directories(src/)
include_directories(src/command)
include_directories(src/redis)
include_directories(src/util)
include_directories(src/util/filter)

FILE(GLOB_RECURSE SOURCE_FILES src/*/*.cpp)
set (CMAKE_CXX_FLAGS "-g -Wall")

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
target_sources(${PROJECT_NAME} PUBLIC ${SOURCE_FILES} src/filter-module.cpp)
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Alpine image should be used same as the base image of the `redis:${ver}-alpine`
FROM alpine:${ALPINE_VERSION} AS build-base
RUN apk --no-cache add \
coreutils \
g++ \
make \
cmake \
boost-dev \
&& apk add \
bash
RUN mkdir /src
WORKDIR /src

FROM redis:${REDIS_VERSION}-alpine as base-redis
RUN apk --no-cache add \
libgcc \
libstdc++ \
boost

FROM build-base as build-fsort
ADD . /src/
RUN mkdir /src/build && cmake -B./build -S. && cd build && make -j 4

FROM base-redis
LABEL module_version=${MODULE_VERSION}
COPY --from=build-fsort /src/build/libredis_filtered_sort.so /usr/local/lib/libredis_filtered_sort.so

ENTRYPOINT ["docker-entrypoint.sh", "--loadmodule", "/usr/local/lib/libredis_filtered_sort.so"]

EXPOSE 6379
26 changes: 10 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
SHELL := /bin/sh
BABEL := ./node_modules/.bin/babel
THIS_FILE := $(lastword $(MAKEFILE_LIST))
DOCKER_IMAGE="makeomatic/redis_filter_mod"

BUILD_DIR := ./lib
SOURCES := index.js filtered-list-bust.lua sorted-filtered-list.lua groupped-list.lua
ifdef IMAGE_NAME
DOCKER_IMAGE = $(IMAGE_NAME)
endif

$(BUILD_DIR)/%.js: %.js
$(BABEL) $*.js -d $@
docker-push:
docker push $(DOCKER_IMAGE)
docker-build:
docker build . -f ./Dockerfile -t $(DOCKER_IMAGE)
docker-rebuild:
docker build . --no-cache -f ./Dockerfile -t $(DOCKER_IMAGE)

$(BUILD_DIR)/%.lua: %.lua
cp $*.lua $@

clean:
rm -rf $(BUILD_DIR)

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

all: clean build
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

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

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

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

## Dependencies
Redis must have `FilterSortModule` enabled.
Please see [HOWTO](./doc/build.md) build module.
API provided by module in [API](./doc/api.md).

## Installation

`npm i redis-filtered-sort -S`
Expand Down
10 changes: 10 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RedisFilterModule

Module basically replicates `http://redis.io/commands/sort` but with extra features and ability to run it in clustered mode with
hashed keys, which resolve to the same slot. Works in separate threads, so Redis db stays unblocked.

* See [HOWTO](./build.md) build module.
* API provided by module [API](./api.md)

## Load options
Module use 4 threads in it's own processing pool. Thread pool size can be changed in `redis.conf`, eg `loadmodule filter_module.so {POOL_SIZE}` or in Redis args `redis-server --loadmodule {filter_module.so} {POOL_SIZE}`
48 changes: 48 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# FilterSortModule redis commands
Module exports this Redis commands

**Please use `json Objects` instead of `json Arrays`**

## Redis Cluster NOTE
In cluster environment all keys used by this command must have same *fix with `{}` in key names

## fsort
Sorts, filters and paginates provided SET `idSetKey` using data from `metaKeyPattern`.`hashKey` using `sortOrder`,`filter` and stores processed data for `expire` seconds

**Usage**:
```console
127.0.0.1:6379:> cfsort idSetKey metaKeyPattern hashKey sortOrder filter(jsonFormattedString) curTime [offset:int] [limit:int] [expire] [keyOnly]
```
- **idSetKey**: Redis SET containing id values
- **metaKeyPattern**: Pattern to find HASHes of record, "*" is replaced by id from `idSetKey`, eg: _*-metakey_ produce _id-231-metakey_
- **hashKey**: field name from HASHkey formed from `metakeyPattern`
- **filter**: Json string with filters
- **curTime**: Current time in milliseconds
- **expire** : TTL for generated cacheds in milliseconds(default 30 sec)
- **keyOnly**: Forces command to return Redis `key`, containing result data. Usable for [`fsortBust`](#sortbust) method


## fsortBust :
Checking caches for specified `key` that was created in result of [fsort](#fsort) method. Allows forcing cache invalidation.

**Method usage**:
```
fsortbust key time [expire]
```
- **key**: Redis LIST containing cached sorted or filtered values. You can obtain it by passing `keyOnly` parameter into [fsort](#fsort) command
- **time**: Current or desired time in milliseconds
- **expire** : TTL in milliseconds(default 30 sec)


## fsortAggregate:
Groups elements by `aggregateParam` from set `idSet` using meta available under `metaKeyPattern` and reports count of each type of criteria.

Currently supports only `sum`;

* **Method usage**:
```
cfsortAggregate idSetKey metaKeyPattern aggregateParam`
```
- **idSetKey**: Redis SET containing id values
- **metaKeyPattern**: Pattern to find HASHes of record, "*" is replaced by id from `idSetKey`, eg: _*-metakey_ produce _id-231-metakey_
- **aggregateParam** : JsonString containing aggregate criteria.
42 changes: 42 additions & 0 deletions doc/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Build Redis FilterSortModule
Module works with `redis` v4.0 or higher.
## General Requirements
Cloned module [redis-filtered-sort](https://github.com/makeomatic/redis-filtered-sort) repository
```console
$~: git clone https://github.com/makeomatic/redis-filtered-sort.git
```

### Local build dependencies
* For local build you will need general C compiler and linker.
Generally on Ubuntu or other Debian like distros, all you have to do is install `build-essential` meta-package
```console
$~: sudo apt-get install build-essential
```

### Docker based build
For docker based build, You will need `docker` and `docker-compose` installed. This variant able generate special docker image with module included, or compile module under `alpine` linux.

## Local build
Enter `csrc` folder, located inside cloned repository.
Call:
```console
$~: make deps && make
```
Compiled module will be located `csrc/redis-filtered-sort/filter_module.so`. Copy this file into desired location and add `loadmodule {pathto filter_module.so}` option into your `redis.conf`.

## Docker build
Enter `csrc` folder, located inside cloned repository.
### Compile module
Set desired image name in `csrc\Makefile` and run
```
$~: make docker-build
```
If everything goes ok, You'll see **$DOCKER_IMAGE** image in your local *docker images*

If you want different name for image use `make docker-build IMAGE_NAME=foo/name:tag`

Push your image to docker with command

```
$~: make docker-push
```
Loading