-
Notifications
You must be signed in to change notification settings - Fork 29
/
flashdb.go
162 lines (136 loc) · 3.18 KB
/
flashdb.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package flashdb
import (
"errors"
"sync"
"time"
"github.com/arriqaaq/aol"
"github.com/arriqaaq/hash"
)
var (
ErrInvalidKey = errors.New("invalid key")
ErrInvalidTTL = errors.New("invalid ttl")
ErrExpiredKey = errors.New("key has expired")
ErrTxClosed = errors.New("tx closed")
ErrDatabaseClosed = errors.New("database closed")
ErrTxNotWritable = errors.New("tx not writable")
)
type (
FlashDB struct {
mu sync.RWMutex
config *Config
exps *hash.Hash // hashmap of ttl keys
log *aol.Log
closed bool // set when the database has been closed
persist bool // do we write to disk
strStore *strStore
hashStore *hashStore
setStore *setStore
zsetStore *zsetStore
evictors []evictor // background manager to delete keys periodically
}
)
func New(config *Config) (*FlashDB, error) {
config.validate()
db := &FlashDB{
config: config,
strStore: newStrStore(),
setStore: newSetStore(),
hashStore: newHashStore(),
zsetStore: newZSetStore(),
exps: hash.New(),
}
evictionInterval := config.evictionInterval()
if evictionInterval > 0 {
db.evictors = []evictor{
newSweeperWithStore(db.strStore, evictionInterval),
newSweeperWithStore(db.setStore, evictionInterval),
newSweeperWithStore(db.hashStore, evictionInterval),
newSweeperWithStore(db.zsetStore, evictionInterval),
}
for _, evictor := range db.evictors {
go evictor.run(db.exps)
}
}
db.persist = config.Path != ""
if db.persist {
opts := aol.DefaultOptions
opts.NoSync = config.NoSync
l, err := aol.Open(config.Path, opts)
if err != nil {
return nil, err
}
db.log = l
// load data from append-only log
err = db.load()
if err != nil {
return nil, err
}
}
return db, nil
}
func (db *FlashDB) setTTL(dType DataType, key string, ttl int64) {
db.exps.HSet(dType, key, ttl)
}
func (db *FlashDB) getTTL(dType DataType, key string) interface{} {
return db.exps.HGet(dType, key)
}
func (db *FlashDB) hasExpired(key string, dType DataType) (expired bool) {
ttl := db.exps.HGet(dType, key)
if ttl == nil {
return
}
if time.Now().Unix() > ttl.(int64) {
expired = true
}
return
}
func (db *FlashDB) evict(key string, dType DataType) {
ttl := db.exps.HGet(dType, key)
if ttl == nil {
return
}
var r *record
if time.Now().Unix() > ttl.(int64) {
switch dType {
case String:
r = newRecord([]byte(key), nil, StringRecord, StringRem)
db.strStore.Delete([]byte(key))
case Hash:
r = newRecord([]byte(key), nil, HashRecord, HashHClear)
db.hashStore.HClear(key)
case Set:
r = newRecord([]byte(key), nil, SetRecord, SetSClear)
db.setStore.SClear(key)
case ZSet:
r = newRecord([]byte(key), nil, ZSetRecord, ZSetZClear)
db.zsetStore.ZClear(key)
}
if err := db.write(r); err != nil {
panic(err)
}
db.exps.HDel(dType, key)
}
}
func (db *FlashDB) Close() error {
db.closed = true
for _, evictor := range db.evictors {
evictor.stop()
}
if db.log != nil {
err := db.log.Close()
if err != nil {
return err
}
}
return nil
}
func (db *FlashDB) write(r *record) error {
if db.log == nil {
return nil
}
encVal, err := r.encode()
if err != nil {
return err
}
return db.log.Write(encVal)
}