-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiptSaver.go
86 lines (74 loc) · 1.77 KB
/
receiptSaver.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package receipts
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/DCNT-Hammer/dcnt/common/interfaces"
"github.com/DCNT-Hammer/dcnt/common/primitives"
)
var DataStorePath string = "./receipts"
func FileNotExists(name string) bool {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return true
}
return err != nil
}
func Save(receipt *Receipt) error {
data, err := receipt.JSONByte()
if err != nil {
return err
}
var out bytes.Buffer
json.Indent(&out, data, "", "\t")
data = out.Next(out.Len())
entryID := receipt.Entry.EntryHash
dir := DataStorePath // + entryID
if FileNotExists(dir) {
err := os.MkdirAll(dir, 0777)
if err == nil {
fmt.Println("Created directory " + dir)
} else {
return err
}
}
fmt.Printf("Saving %v\n", fmt.Sprintf(dir+"/storeJSON.%v.block", entryID))
err = ioutil.WriteFile(fmt.Sprintf(dir+"/storeJSON.%v.block", entryID), data, 0777)
if err != nil {
return err
}
return nil
}
func ExportEntryReceipt(entryID string, dbo interfaces.DBOverlaySimple) error {
h, err := primitives.NewShaHashFromStr(entryID)
if err != nil {
return err
}
receipt, err := CreateFullReceipt(dbo, h, false)
if err != nil {
return err
}
return Save(receipt)
}
func ExportAllEntryReceipts(dbo interfaces.DBOverlay) error {
entryIDs, err := dbo.FetchAllEntryIDs()
if err != nil {
return err
}
for i, entryID := range entryIDs {
err = ExportEntryReceipt(entryID.String(), dbo)
if err != nil {
if err.Error() != "dirBlockInfo not found" {
return err
} else {
fmt.Printf("dirBlockInfo not found for entry %v/%v - %v\n", i, len(entryIDs), entryID)
}
}
}
return nil
}