Skip to content

Instantly share code, notes, and snippets.

@dvirsky
dvirsky / redis-bashcomplete
Last active Jul 20, 2017
Bash completion for redis server and cli
View redis-bashcomplete
# bash completion for redis-cli and redis-server
have redis-server &&
_redisserver()
{
local cur prev split=false
COMPREPLY=()
_get_comp_words_by_ref cur prev
@dvirsky
dvirsky / Extensions.md
Last active Mar 12, 2017
RediSearch Extensions
View Extensions.md

Extending RediSearch

RediSearch supports an extension mechanism, much like Redis supports modules. The API is very minimal at the moment, and it does not yet support dynamic loading of extensions in run-time. Instead, extensions must be written in C and compiled into the engine when building it.

There are two kinds of extension APIs at the moment:

  1. Query Expanders, whose role is to expand query tokens (i.e. stemmers).
  2. Scoring Funtions, whose role is to rank search results in query time.

Registering Extensions

@dvirsky
dvirsky / gendocs.py
Last active Sep 12, 2021
Generate Markdown documentation from a python package
View gendocs.py
# This script generates mkdocs friendly Markdown documentation from a python package.
# It is based on the the following blog post by Christian Medina
# https://medium.com/python-pandemonium/python-introspection-with-the-inspect-module-2c85d5aa5a48#.twcmlyack
import pydoc
import os, sys
module_header = "# Package {} Documentation\n"
class_header = "## Class {}"
function_header = "### {}"
View time.redis
not connected> IDX.CREATE users TYPE HASH SCHEMA tm TIME
OK
127.0.0.1:6379> IDX.INTO users HMSET user1 tm 1476119602
(integer) 1
127.0.0.1:6379> IDX.INTO users HMSET user2 tm 1476019602
(integer) 1
127.0.0.1:6379> IDX.FROM users WHERE "tm < NOW" HGETALL $
1) user2
2) 1) "tm"
@dvirsky
dvirsky / example.txt
Last active Sep 26, 2016
index example
View example.txt
# creating an index:
127.0.0.1:6379> IDX.CREATE users TYPE HASH SCHEMA name STRING age INT32
OK
# running HASH commands while indexing them:
127.0.0.1:6379> IDX.INTO users HMSET user1 name "alice" age 24
OK
127.0.0.1:6379> IDX.INTO users HMSET user2 name "bob" age 19
@dvirsky
dvirsky / tasks.json
Created Sep 20, 2016
tasks.json for building and testing generic go projects in vscode
View tasks.json
{
"version": "0.1.0",
"isShellCommand": true,
"showOutput": "always",
"command": "go",
"echoCommand": true ,
"options": {
"cwd": "${fileDirname}"
},
"tasks": [
@dvirsky
dvirsky / RQL.md
Last active Dec 20, 2017
Proposed Secondary Index API and Query Language
View RQL.md

Proposed Redis Secondary Index Module API

What's This?

Redis is a very sophisticated data structure server, that can be used as a powerful in-memory database. However, if you look at redis as database per-se, it only has primary keys. There is no native way to ask redis for something like "what are the names of users over 18 who have visited my website yesterday?".

If you're familiar with traditional relational databases, this is usually done by creating an index on the relevant columns in your table, allowing to efficiently add complex WHERE clauses to your query. Such an index is called a Secondary Index.

While it is possible (and done by many many people) to implement these indexes on top of redis manually, doing them right and in a performant way is hard.

View serialization.c
// A serializer prototype - you serialize by calling Write N times.
// the key type is determined on registration so not needed here
// the list size will be determined by redis when you exit, based on the number of times you called write()
int (*Serialize)(RedisModuleCtx *ctx, RedisModuleObjectWriter *out, RedisModuleKey *k)
// you deserialize by calling Read and reading one object at a time.
// numElements is passed so the user can preallocate stuff in advance
int (*Deserialize)(RedisModuleCtx *ctx, RedisModuleObjectReader *in, RedisModuleString *keyName, size_t numElements)
View module.md

Creating a redis Module in 15 lines of code!

A quick guide to write a very very simple "ECHO" style module to redis and load it. It's not really useful of course, but the idea is to illustrate how little boilerplate it takes.

Step 1: open your favorite editor and write/paste the following code in a file called module.c

#include "redismodule.h"
/* ECHO <string> - Echo back a string sent from the client */
int EchoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
View foo_test.go
package foo
import (
"testing"
)
type Foo struct {
a string
b int
c float32