Skip to content

Commit 8eca13c

Browse files
committed
initial commit
1 parent 3982765 commit 8eca13c

File tree

9 files changed

+439
-0
lines changed

9 files changed

+439
-0
lines changed

.circleci/config.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Golang CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-go/ for more details
4+
version: 2
5+
jobs:
6+
build:
7+
docker:
8+
# specify the version
9+
- image: circleci/golang:1.12
10+
11+
- image: redislabs/rebloom:edge
12+
port: 6379:6379
13+
14+
working_directory: /go/src/github.com/RedisBloom/redisbloom-go
15+
steps:
16+
- checkout
17+
- run: go get -v -t -d ./...
18+
19+
#run tests with coverage
20+
- run: go test -v -race -coverprofile=coverage.txt -covermode=atomic
21+
- run: go tool cover -func=coverage.txt
22+
- run: bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
23+
24+
workflows:
25+
version: 2
26+
commit:
27+
jobs:
28+
- build
29+
nightly:
30+
triggers:
31+
- schedule:
32+
cron: "0 0 * * *"
33+
filters:
34+
branches:
35+
only:
36+
- master
37+
jobs:
38+
- build

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
.project

Gopkg.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/gomodule/redigo"
30+
version = "2.0.0"
31+
32+
[[constraint]]
33+
name = "github.com/stretchr/testify"
34+
version = "1.3.0"
35+
36+
[prune]
37+
go-tests = true
38+
unused-packages = true
39+

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![license](https://img.shields.io/github/license/RedisBloom/redisbloom-go.svg)](https://github.com/RedisBloom/redisbloom-go)
2+
[![CircleCI](https://circleci.com/gh/RedisBloom/redisbloom-go.svg?style=svg)](https://circleci.com/gh/RedisBloom/redisbloom-go)
3+
[![GitHub issues](https://img.shields.io/github/release/RedisBloom/redisbloom-go.svg)](https://github.com/RedisBloom/redisbloom-go/releases/latest)
4+
[![Codecov](https://codecov.io/gh/RedisBloom/redisbloom-go/branch/master/graph/badge.svg)](https://codecov.io/gh/RedisBloom/redisbloom-go)
5+
[![GoDoc](https://godoc.org/github.com/RedisBloom/redisbloom-go?status.svg)](https://godoc.org/github.com/RedisBloom/redisbloom-go)
6+
7+
8+
# redisbloom-go
9+
10+
Go client for RedisBloom (https://github.com/RedisBloom/redisbloom), based on redigo.
11+
12+
## Installing
13+
14+
```sh
15+
$ go get github.com/RedisBloom/redisbloom-go
16+
```
17+
18+
## Running tests
19+
20+
A simple test suite is provided, and can be run with:
21+
22+
```sh
23+
$ RedisBloom_TEST_PASSWORD="" go test
24+
```
25+
26+
The tests expect a Redis server with the RedisBloom module loaded to be available at localhost:6379
27+
28+
## Example Code
29+
30+
```go
31+
package main
32+
33+
import (
34+
"fmt"
35+
RedisBloom "github.com/RedisBloom/redisbloom-go"
36+
)
37+
38+
func main() {
39+
// Connect to localhost with no password
40+
var client = RedisBloom.NewClient("localhost:6379", "nohelp", nil)
41+
var keyname = "mytest"
42+
_, haveit := client.Info(keyname)
43+
if haveit != nil {
44+
client.CreateKeyWithOptions(keyname, RedisBloom.DefaultCreateOptions)
45+
client.CreateKeyWithOptions(keyname+"_avg", RedisBloom.DefaultCreateOptions)
46+
client.CreateRule(keyname, RedisBloom.AvgAggregation, 60, keyname+"_avg")
47+
}
48+
// Add sample with timestamp from server time and value 100
49+
// TS.ADD mytest * 100
50+
_, err := client.AddAutoTs(keyname, 100)
51+
if err != nil {
52+
fmt.Println("Error:", err)
53+
}
54+
}
55+
```
56+
57+
## Supported RedisBloom Commands
58+
59+
| Command | Recommended API and godoc |
60+
| :--- | ----: |
61+
| [TS.CREATE](https://oss.redislabs.com/RedisBloom/commands/#tscreate) | [CreateKeyWithOptions](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.CreateKeyWithOptions) |
62+
| [TS.ALTER](https://oss.redislabs.com/RedisBloom/commands/#tsalter) | N/A |
63+
| [TS.ADD](https://oss.redislabs.com/RedisBloom/commands/#tsadd) | <ul><li>[Add](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Add)</li><li>[AddAutoTs](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.AddAutoTs)</li><li>[AddWithOptions](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.AddWithOptions)</li><li>[AddAutoTsWithOptions](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.AddWithOptions)</li> </ul> |
64+
| [TS.MADD](https://oss.redislabs.com/RedisBloom/commands/#tsmadd) | N/A |
65+
| [TS.INCRBY/TS.DECRBY](https://oss.redislabs.com/RedisBloom/commands/#tsincrbytsdecrby) | N/A |
66+
| [TS.CREATERULE](https://oss.redislabs.com/RedisBloom/commands/#tscreaterule) | [CreateRule](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.CreateRule) |
67+
| [TS.DELETERULE](https://oss.redislabs.com/RedisBloom/commands/#tsdeleterule) | [DeleteRule](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.DeleteRule) |
68+
| [TS.RANGE](https://oss.redislabs.com/RedisBloom/commands/#tsrange) | [RangeWithOptions](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.RangeWithOptions) |
69+
| [TS.MRANGE](https://oss.redislabs.com/RedisBloom/commands/#tsmrange) | [MultiRangeWithOptions](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.MultiRangeWithOptions) |
70+
| [TS.GET](https://oss.redislabs.com/RedisBloom/commands/#tsget) | [Get](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Get) |
71+
| [TS.MGET](https://oss.redislabs.com/RedisBloom/commands/#tsmget) | [MultiGet](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.MultiGet) |
72+
| [TS.INFO](https://oss.redislabs.com/RedisBloom/commands/#tsinfo) | [Info](https://godoc.org/github.com/RedisBloom/redisbloom-go#Client.Info) |
73+
| [TS.QUERYINDEX](https://oss.redislabs.com/RedisBloom/commands/#tsqueryindex) | N/A |
74+
75+
76+
## License
77+
78+
redisbloom-go is distributed under the Apache-2 license - see [LICENSE](LICENSE)

client.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package redis_timeseries_go
2+
3+
import (
4+
"strings"
5+
"github.com/gomodule/redigo/redis"
6+
)
7+
8+
// TODO: refactor this hard limit and revise client locking
9+
// Client Max Connections
10+
var maxConns = 500
11+
12+
// Client is an interface to time series redis commands
13+
type Client struct {
14+
Pool ConnPool
15+
Name string
16+
}
17+
18+
// NewClient creates a new client connecting to the redis host, and using the given name as key prefix.
19+
// Addr can be a single host:port pair, or a comma separated list of host:port,host:port...
20+
// In the case of multiple hosts we create a multi-pool and select connections at random
21+
func NewClient(addr, name string, authPass *string) *Client {
22+
addrs := strings.Split(addr, ",")
23+
var pool ConnPool
24+
if len(addrs) == 1 {
25+
pool = NewSingleHostPool(addrs[0], authPass)
26+
} else {
27+
pool = NewMultiHostPool(addrs, authPass)
28+
}
29+
ret := &Client{
30+
Pool: pool,
31+
Name: name,
32+
}
33+
return ret
34+
}
35+
36+
// Add - Add (or create and add) a new value to the filter
37+
// args:
38+
// key - the name of the filter
39+
// item - the item to add
40+
func (client *Client) Add(key string, item string) (exists bool, err error) {
41+
conn := client.Pool.Get()
42+
defer conn.Close()
43+
return redis.Bool(conn.Do("BF.ADD", key, item))
44+
}
45+
46+
// Exists - Determines whether an item may exist in the Bloom Filter or not.
47+
// args:
48+
// key - the name of the filter
49+
// item - the item to check for
50+
func (client *Client) Exists(key string, item string) (exists bool, err error) {
51+
conn := client.Pool.Get()
52+
defer conn.Close()
53+
return redis.Bool(conn.Do("BF.EXISTS", key, item))
54+
}

client_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package redis_timeseries_go
2+
3+
import (
4+
"os"
5+
// "reflect"
6+
"testing"
7+
"time"
8+
9+
// "github.com/gomodule/redigo/redis"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func createClient() *Client {
14+
valueh, exists := os.LookupEnv("REDISTIMESERIES_TEST_HOST")
15+
host := "localhost:6379"
16+
if exists && valueh != "" {
17+
host = valueh
18+
}
19+
valuep, exists := os.LookupEnv("REDISTIMESERIES_TEST_PASSWORD")
20+
password := "SUPERSECRET"
21+
var ptr *string = nil
22+
if exists {
23+
password = valuep
24+
}
25+
if len(password) > 0 {
26+
ptr = &password
27+
}
28+
return NewClient(host, "test_client", ptr)
29+
}
30+
31+
var client = createClient()
32+
var _ = client.FlushAll()
33+
34+
var defaultDuration, _ = time.ParseDuration("1h")
35+
var tooShortDuration, _ = time.ParseDuration("10ms")
36+
37+
func (client *Client) FlushAll() (err error) {
38+
conn := client.Pool.Get()
39+
defer conn.Close()
40+
_, err = conn.Do("FLUSHALL")
41+
return err
42+
}
43+
44+
func TestAdd(t *testing.T) {
45+
client.FlushAll()
46+
key := "test_ADD"
47+
value := "test_ADD_value";
48+
exists, err := client.Add(key, value)
49+
assert.Nil(t, err)
50+
assert.True(t, exists)
51+
52+
exists, err = client.Add(key, value)
53+
assert.Nil(t, err)
54+
assert.False(t, exists)
55+
}
56+
57+
func TestExists(t *testing.T) {
58+
client.FlushAll()
59+
client.Add("test_ADD", "test_EXISTS")
60+
61+
exists, err := client.Exists("test_ADD", "test_EXISTS")
62+
assert.Nil(t, err)
63+
assert.True(t, exists)
64+
65+
exists, err = client.Exists("test_ADD", "test_EXISTS1")
66+
assert.Nil(t, err)
67+
assert.False(t, exists)
68+
}

0 commit comments

Comments
 (0)