-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeta_db.go
74 lines (61 loc) · 1.48 KB
/
meta_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
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
package blackhole
import (
"bytes"
"io/ioutil"
"github.com/dgraph-io/badger"
shell "github.com/ipfs/go-ipfs-api"
)
// MetaDB is a db by reference, a way you can to create a very distrubuted DB
type MetaDB struct {
encryptKey []byte
principalNode string
publicKey string
privateKey []byte
localDB *badger.DB
remoteShell *shell.Shell
}
func (db *DB) snapshotMetaDB(publicKey string, privateKey []byte) (*MetaDB, error) {
data, err := encodeDBFile(db.options.LocalDBDir, privateKey)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(data)
cid, err := db.remoteShell.Add(buf)
if err != nil {
return nil, err
}
return &MetaDB{
encryptKey: db.encryptKey,
principalNode: db.principalNode,
publicKey: cid,
privateKey: privateKey,
localDB: db.localDB,
remoteShell: db.remoteShell,
}, nil
}
func (db *DB) readMetaDB(publicKey string, privateKey []byte) (*MetaDB, error) {
reader, err := db.remoteShell.Cat(publicKey)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
err = decodeDBFile(data, privateKey, db.options.LocalDBDir)
if err != nil {
return nil, err
}
ldb, err := badger.Open(badger.DefaultOptions(db.options.LocalDBDir))
if err != nil {
return nil, err
}
return &MetaDB{
encryptKey: db.encryptKey,
principalNode: db.principalNode,
publicKey: publicKey,
privateKey: privateKey,
localDB: ldb,
remoteShell: db.remoteShell,
}, nil
}