-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (125 loc) · 3.08 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/dgraph-io/badger"
)
const invalidSembastFileMessage = "invalid sembast file"
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func getBytes(data interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
err := enc.Encode(data)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func main() {
args := os.Args
var sembastFile = ""
var badgerDir = ""
for argIndex, arg := range args {
if len(args) >= argIndex {
if arg == "-s" {
sembastFile = args[argIndex+1]
}
if arg == "-b" {
badgerDir = args[argIndex+1]
}
}
}
if sembastFile != "" && badgerDir != "" {
badgerDirExists, err := exists(badgerDir)
if err != nil {
log.Fatal(err)
}
if badgerDirExists {
log.Fatal(errors.New("badger directory already exists"))
}
// Open our jsonFile
jsonFile, err := os.Open(sembastFile)
// if we os.Open returns an error then handle it
if err != nil {
log.Fatal(err)
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
err = json.Unmarshal([]byte(byteValue), &result)
if err != nil {
log.Fatal(err)
}
if stores, ok := result["stores"]; ok {
//fmt.Println(stores)
storesSlice := stores.([]interface{})
// TODO: allow multiple stores.
if len(storesSlice) == 1 {
//fmt.Println(storesSlice[0])
if keys, ok := storesSlice[0].(map[string]interface{})["keys"]; ok {
//fmt.Println(keys)
if values, ok := storesSlice[0].(map[string]interface{})["values"]; ok {
//fmt.Println(values)
keysSlice := keys.([]interface{})
valuesSlice := values.([]interface{})
if len(keysSlice) == len(valuesSlice) {
opts := badger.DefaultOptions
opts.Dir = badgerDir
opts.ValueDir = badgerDir
db, err := badger.Open(opts)
if err != nil {
log.Fatal(err)
}
defer db.Close()
for keyIndex, key := range keysSlice {
valueBytes, err := getBytes(valuesSlice[keyIndex])
if err != nil {
log.Fatal(err)
}
err = db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key.(string)), valueBytes)
})
if err != nil {
log.Fatal(err)
}
}
fmt.Println("sembast-to-badger completed")
} else {
fmt.Println(invalidSembastFileMessage)
}
} else {
fmt.Println(invalidSembastFileMessage)
}
} else {
fmt.Println(invalidSembastFileMessage)
}
} else {
fmt.Println(invalidSembastFileMessage)
}
} else {
fmt.Println(invalidSembastFileMessage)
}
} else {
fmt.Println("Usage:")
fmt.Println("sembast-to-badger -s \"sembast export filename\" -b \"badger directory\"")
}
}