-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.go
125 lines (105 loc) Β· 2.47 KB
/
dump.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
package main
import (
"log"
"os"
"path/filepath"
"github.com/syndtr/goleveldb/leveldb"
"encoding/json"
"time"
"fmt"
)
var disk *leveldb.DB
var batch *leveldb.Batch
var written bool
// Initialize the disk storage
func initializeDiskStorage() {
path := cwd()
disk, _ = leveldb.OpenFile(path, nil)
batch = new(leveldb.Batch)
written = true
go flushingActivity()
}
// This will dump the keys to the disk
func flush() {
if !written {
fmt.Println("Dumping to disk")
disk.Write(batch, nil)
}
}
// will load the data from the disk on start up
func iterate() {
if !persistence {
fmt.Println("Sever started without persistence")
fmt.Println("we're ready to go!")
return
}
fmt.Println("Server is loading your data")
fmt.Println("Please be patient")
iter := disk.NewIterator(nil, nil)
for iter.Next() {
k := iter.Key()
v := iter.Value()
key := string(k)
err, value := decodeBytes(v)
if !err {
data[key] = value
}
}
iter.Release()
_ = iter.Error()
fmt.Println("Server has been initialized. We're ready to go!")
}
// puts data on the batch
func diskPut(key string, value interface{}) {
// If persistence is set to false, don't take the pain
if !persistence {
return
}
err, bytes := GetBytes(value)
if !err {
batch.Put([]byte(key), bytes)
written = false
}
}
//delete from batch
func diskDel(key string) {
// If persistence is set to false, don't take the pain
if !persistence {
return
}
batch.Delete([]byte(key))
written = false
}
// get current working directory
func cwd() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
os.Mkdir(dir + "/data", os.FileMode(0777))
return dir + "/data"
}
// converts json to bytes
func GetBytes(value interface{}) (bool, []byte) {
b, err := json.Marshal(value)
if err == nil {
return false, b
}
return true, nil
}
// decodes the bytes to json
func decodeBytes(value []byte) (bool, values) {
var v values
err := json.Unmarshal(value, &v)
if err == nil {
return false, v
}
return true, values{}
}
// will flush to disk every 5 minutes(configurable value)
func flushingActivity() {
for _ = range time.Tick(diskWriteInterval*time.Millisecond) {
flush()
written = true
}
}