This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ttl.go
157 lines (134 loc) · 3.17 KB
/
ttl.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
package gotinydb
import (
"context"
"encoding/json"
"time"
"github.com/alexandrestein/gotinydb/transaction"
"github.com/dgraph-io/badger"
)
type (
// TTL defines a type to permit to delete documents or files after a certain duration
ttl struct {
CleanTime time.Time
File bool
DocumentCollectionName string
DocumentID string
}
)
func (t *ttl) timeAsKey() []byte {
prefix := []byte{prefixTTL}
ret, _ := t.CleanTime.MarshalBinary()
return append(prefix, ret...)
}
func (t *ttl) exportAsBytes() []byte {
ret, _ := json.Marshal(t)
return ret
}
func parseTTL(input []byte) (*ttl, error) {
obj := new(ttl)
err := json.Unmarshal(input, obj)
return obj, err
}
func newTTL(colID, docOrFileID string, file bool, ttlDur time.Duration) *ttl {
if ttlDur <= 0 {
return nil
}
return &ttl{
CleanTime: time.Now().Add(ttlDur),
File: file,
DocumentCollectionName: colID,
DocumentID: docOrFileID,
}
}
func (d *DB) goWatchForTTLToClean() {
start:
var nextRun time.Duration
d.lock.RLock()
badgerStore := d.badger
localCtx := d.ctx
d.lock.RUnlock()
ctx, cancel := context.WithTimeout(localCtx, time.Second*10)
defer cancel()
tr := transaction.New(ctx)
badgerStore.View(func(txn *badger.Txn) (err error) {
iterOp := badger.DefaultIteratorOptions
iterOp.AllVersions = true
iter := txn.NewIterator(badger.DefaultIteratorOptions)
defer iter.Close()
prefix := []byte{prefixTTL}
for iter.Seek(prefix); iter.ValidForPrefix(prefix); iter.Next() {
item := iter.Item()
var key []byte
key = item.KeyCopy(key)
encryptedData := []byte{}
encryptedData, err = item.ValueCopy(encryptedData)
if err != nil {
return err
}
var clearData []byte
clearData, err = d.decryptData(key, encryptedData)
if err != nil {
return err
}
var ttl *ttl
ttl, err = parseTTL(clearData)
if err != nil {
return err
}
// If the time is passed
if ttl.CleanTime.Before(time.Now()) {
// If this is not a file
if !ttl.File {
// Get the related collection
col, _ := d.Use(ttl.DocumentCollectionName)
// Pass if any error
if col == nil {
continue
}
// Tries to delete the document
err = col.Delete(ttl.DocumentID)
} else {
err = d.GetFileStore().DeleteFile(ttl.DocumentID)
}
// If any error the TTL record is not remove to run the task again
if err == nil {
cleanTTLRecordOp := transaction.NewOperation("", "", ttl.timeAsKey(), nil, true, false)
tr.AddOperation(cleanTTLRecordOp)
}
} else {
// Setup the next run
nextRun = ttl.CleanTime.Sub(time.Now())
if nextRun > time.Second {
nextRun = time.Second
}
return
}
}
return nil
})
// DB closed
if closed := localCtx.Err(); closed != nil {
return
}
// Do the writing:
select {
case d.writeChan <- tr:
case <-localCtx.Done():
return
}
select {
case <-tr.ResponseChan:
case <-tr.Ctx.Done():
}
if nextRun == 0 {
nextRun = time.Second
}
// Found but in some time.
// Wait for it before stating the loop again.
select {
case <-time.After(nextRun):
goto start
case <-localCtx.Done():
return
}
}