This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtransactionHub.go
122 lines (104 loc) · 2.15 KB
/
transactionHub.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
package transaction
import (
"sync"
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
"github.com/google/uuid"
"github.com/pkg/errors"
"log"
"github.com/dcb9/boast/config"
)
const MAX_TRANSACTIONS_LEN int = 8 * 1024
var s sync.Mutex
var AddChannel chan *Tx = make(chan *Tx)
type Hub struct {
Transactions map[uuid.UUID]*Tx
SortID []uuid.UUID
DB *bolt.DB
}
var bucket = []byte("transactions")
func NewTxHub() *Hub {
hub := &Hub{
Transactions: make(map[uuid.UUID]*Tx),
SortID: make([]uuid.UUID, 0, 32*1024),
}
if dbPath := config.Config.DBPath; dbPath != "" {
hub.initDB(dbPath)
}
return hub
}
func (h *Hub) initDB(path string) {
var err error
h.DB, err = bolt.Open(path, 0600, nil)
if err != nil {
log.Fatal(err)
}
err = h.DB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
if err != nil {
log.Fatal(err)
}
err = h.DB.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var t Tx
err = json.Unmarshal(v, &t)
if err != nil {
log.Println("json.Unmarshal err ", err)
continue
}
h.Transactions[t.ID] = &t
h.SortID = append(h.SortID, t.ID)
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
func (h *Hub) Add(t Tx) error {
if t.ID == uuid.Nil {
return errors.New("Transcation id MUST BE set.")
}
if len(h.SortID) >= MAX_TRANSACTIONS_LEN {
deleteID := h.SortID[0]
h.SortID = h.SortID[1:]
delete(h.Transactions, deleteID)
}
s.Lock()
h.Transactions[t.ID] = &t
var err error
if h.DB != nil {
err = h.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucket)
bytes, err := json.Marshal(t)
if err != nil {
return err
}
return b.Put(t.ID[:], bytes)
})
}
s.Unlock()
if err != nil {
return err
}
h.SortID = append(h.SortID, t.ID)
AddChannel <- &t
return nil
}
func (h *Hub) List() []*Tx {
length := len(h.SortID)
list := make([]*Tx, 0, len(h.SortID))
for i := 0; i < length; i++ {
transaction := h.Transactions[h.SortID[i]]
list = append(list, transaction)
}
return list
}