/
db.go
40 lines (33 loc) · 822 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package db
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/wrfly/yasuser/config"
"github.com/wrfly/yasuser/types"
)
const (
BOLT = "bolt"
REDIS = "redis"
)
var skipKeyNums int64 = 99
// Database is a KV storage, there are two relationships
// hashSum -> short & short -> long
// hashSum is the URL's hashSum
type Database interface {
Close() error
Len() int64
Store(URL *types.URL) error
GetShort(hashSum string) (URL *types.URL, err error)
GetLong(short string) (URL *types.URL, err error)
}
// New DB storage
func New(conf config.StoreConfig) (Database, error) {
logrus.Infof("Creating DB with [ %v ]", conf.DBType)
switch conf.DBType {
case BOLT:
return newBoltDB(conf.DBPath)
case REDIS:
return newRedisDB(conf.Redis)
}
return nil, fmt.Errorf("Unknown DB Type: %s", conf.DBType)
}