Skip to content

Commit fdb2b46

Browse files
committed
added Null engine and refactored some code
1 parent a18a706 commit fdb2b46

File tree

7 files changed

+111
-30
lines changed

7 files changed

+111
-30
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Features
1919
=========
2020
- Core data structure: `KV`, `List`, `Hashmap` with advanced implementations.
2121
- Advanced Publish/Subscribe using webhook and websocket!
22-
- Pluggable Storage Engine (`badgerdb`, `boltdb`, 'leveldb')
22+
- Pluggable Storage Engine (`badgerdb`, `boltdb`, `leveldb`)
2323
- Very compatible with any `redis client` including `redis-cli`
2424
- Standalone with no external dependencies
2525
- Helpers commands for `Time`, `Encode <hex|md5|sha1|sha256|sha512> <payload>`, `RANDINT`, `RANDSTR`

commands_strings.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ func setCommand(c Context) {
3030
ttlVal = 0
3131
}
3232

33+
// if *flagACK {
3334
if err := c.db.Set(k, v, ttlVal); err != nil {
3435
c.WriteError(err.Error())
3536
return
3637
}
38+
// } else {
39+
// kvjobs <- func() {
40+
// c.db.Set(k, v, ttlVal)
41+
// }
42+
// }
3743

3844
c.WriteString("OK")
3945
}

helpers_db.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ package main
55

66
import (
77
"errors"
8-
"io/ioutil"
9-
"log"
10-
"os"
118
"path/filepath"
129
"strings"
1310

14-
"github.com/alash3al/color"
11+
"github.com/alash3al/redix/kvstore/null"
12+
1513
"github.com/alash3al/redix/kvstore"
1614
"github.com/alash3al/redix/kvstore/badgerdb"
1715
"github.com/alash3al/redix/kvstore/boltdb"
@@ -46,27 +44,8 @@ func openDB(engine, dbpath string) (kvstore.DB, error) {
4644
return boltdb.OpenBolt(dbpath)
4745
case "leveldb":
4846
return leveldb.OpenLevelDB(dbpath)
49-
}
50-
}
51-
52-
// initDBs - initialize databases from the disk for faster access
53-
func initDBs() {
54-
os.MkdirAll(*flagStorageDir, 0644)
55-
56-
dirs, _ := ioutil.ReadDir(*flagStorageDir)
57-
58-
for _, f := range dirs {
59-
if !f.IsDir() {
60-
continue
61-
}
62-
63-
name := filepath.Base(f.Name())
64-
65-
_, err := selectDB(name)
66-
if err != nil {
67-
log.Println(color.RedString(err.Error()))
68-
continue
69-
}
47+
case "null":
48+
return null.OpenNull()
7049
}
7150
}
7251

init.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"flag"
88
"fmt"
99
"io/ioutil"
10+
"log"
1011
"net/url"
1112
"os"
1213
"path/filepath"
@@ -50,8 +51,6 @@ func init() {
5051
return &ret
5152
})()
5253

53-
initDBs()
54-
5554
snowflakenode, err := snowflake.NewNode(1)
5655
if err != nil {
5756
fmt.Println(color.RedString(err.Error()))
@@ -60,4 +59,27 @@ func init() {
6059
}
6160

6261
snowflakeGenerator = snowflakenode
62+
63+
initDBs()
64+
}
65+
66+
// initDBs - initialize databases from the disk for faster access
67+
func initDBs() {
68+
os.MkdirAll(*flagStorageDir, 0644)
69+
70+
dirs, _ := ioutil.ReadDir(*flagStorageDir)
71+
72+
for _, f := range dirs {
73+
if !f.IsDir() {
74+
continue
75+
}
76+
77+
name := filepath.Base(f.Name())
78+
79+
_, err := selectDB(name)
80+
if err != nil {
81+
log.Println(color.RedString(err.Error()))
82+
continue
83+
}
84+
}
6385
}

kvstore/null/null.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2018 The Redix Authors. All rights reserved.
2+
// Use of this source code is governed by a Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
//
5+
// null is a db engine based on `/dev/null` style
6+
package null
7+
8+
import (
9+
"github.com/alash3al/redix/kvstore"
10+
)
11+
12+
// Null - represents a Null db implementation
13+
type Null struct{}
14+
15+
// OpenNull - Opens the specified path
16+
func OpenNull() (*Null, error) {
17+
return new(Null), nil
18+
}
19+
20+
// Size - returns the size of the database in bytes
21+
func (ldb *Null) Size() int64 {
22+
return 0
23+
}
24+
25+
// GC - runs the garbage collector
26+
func (ldb *Null) GC() error {
27+
return nil
28+
}
29+
30+
// Incr - increment the key by the specified value
31+
func (ldb *Null) Incr(k string, by int64) (int64, error) {
32+
return 1, nil
33+
}
34+
35+
// Set - sets a key with the specified value and optional ttl
36+
func (ldb *Null) Set(k, v string, ttl int) error {
37+
return nil
38+
}
39+
40+
// MSet - sets multiple key-value pairs
41+
func (ldb *Null) MSet(data map[string]string) error {
42+
return nil
43+
}
44+
45+
// Get - fetches the value of the specified k
46+
func (ldb *Null) Get(k string) (string, error) {
47+
return "", nil
48+
}
49+
50+
// MGet - fetch multiple values of the specified keys
51+
func (ldb *Null) MGet(keys []string) []string {
52+
return nil
53+
}
54+
55+
// TTL - returns the time to live of the specified key's value
56+
func (ldb *Null) TTL(key string) int64 {
57+
return -2
58+
}
59+
60+
// Del - removes key(s) from the store
61+
func (ldb *Null) Del(keys []string) error {
62+
return nil
63+
}
64+
65+
// Scan - iterate over the whole store using the handler function
66+
func (ldb *Null) Scan(scannerOpt kvstore.ScannerOptions) error {
67+
return nil
68+
}

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ package main
55

66
import (
77
"fmt"
8+
"strconv"
89

910
"github.com/alash3al/go-color"
1011
)
1112

1213
func main() {
14+
fmt.Println(color.MagentaString(redixBrand))
1315
fmt.Printf("⇨ redix server version: %s \n", color.GreenString(redixVersion))
16+
fmt.Printf("⇨ redix selected engine: %s \n", color.GreenString(*flagEngine))
17+
fmt.Printf("⇨ redix workers count: %s \n", color.GreenString(strconv.Itoa(*flagWorkers)))
1418
fmt.Printf("⇨ redix resp server available at: %s \n", color.GreenString(*flagRESPListenAddr))
1519
fmt.Printf("⇨ redix http server available at: %s \n", color.GreenString(*flagHTTPListenAddr))
1620

vars.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
flagEngineOpions = flag.String("engine-options", "", "options related to used engine in the url query format, i.e (opt1=val2&opt2=val2)")
2222
flagWorkers = flag.Int("workers", runtime.NumCPU(), "the default workers number")
2323
flagVerbose = flag.Bool("verbose", false, "verbose or not")
24+
flagACK = flag.Bool("ack", true, "acknowledge write or return immediately")
2425
)
2526

2627
var (
@@ -29,6 +30,7 @@ var (
2930
webhooks *sync.Map
3031
websockets *sync.Map
3132
snowflakeGenerator *snowflake.Node
33+
kvjobs chan func()
3234
)
3335

3436
var (
@@ -114,7 +116,7 @@ var (
114116
"badgerdb": true,
115117
"boltdb": true,
116118
"leveldb": true,
117-
"tikv": true,
119+
"null": true,
118120
}
119121
engineOptions = url.Values{}
120122
defaultPubSubAllTopic = "*"
@@ -124,7 +126,7 @@ const (
124126
redixVersion = "1.9"
125127
redixBrand = `
126128
127-
_______ _______ ______ _________
129+
_______ _______ ______ _________
128130
( ____ )( ____ \( __ \ \__ __/|\ /|
129131
| ( )|| ( \/| ( \ ) ) ( ( \ / )
130132
| (____)|| (__ | | ) | | | \ (_) /

0 commit comments

Comments
 (0)