Skip to content
This repository has been archived by the owner on Oct 24, 2018. It is now read-only.

Commit

Permalink
add DBPath Config
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Jun 20, 2018
1 parent 0bfb0f5 commit 831d8be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
15 changes: 11 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ type RvsPxy struct {
URL string `json:"url"`
Addr string `json:"addr"`
}

var DefaultDBPath = "boast.db"

type JsonConfig struct {
DebugAddr string `json:"debug_addr"`
List []RvsPxy `json:"list"`
// it'll persist to DB, if DBPath is not empty
DBPath string `json:"db_path"`
}

var Config JsonConfig
Expand All @@ -39,12 +44,14 @@ func CmdInit() {
}

func Init(s *httptest.Server, addr, debugAddr string) {
Config = JsonConfig{
DebugAddr: debugAddr,
List: []RvsPxy{
Config.DebugAddr = debugAddr
Config.List = []RvsPxy{
RvsPxy{
s.URL, addr,
},
},
}
}

func SetDBPath(path string) {
Config.DBPath = path
}
3 changes: 2 additions & 1 deletion transaction/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/google/uuid"
)

var TxHub = NewTxHub()
var TxHub *Hub

var transport = &Transport{http.DefaultTransport}

func Serve() {
TxHub = NewTxHub()
for _, rp := range config.Config.List {
target, err := url.Parse(rp.URL)
if err != nil {
Expand Down
40 changes: 23 additions & 17 deletions transaction/transactionHub.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/pkg/errors"
"log"
"github.com/dcb9/boast/config"
)

const MAX_TRANSACTIONS_LEN int = 8 * 1024
Expand All @@ -20,28 +21,30 @@ 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),
}
hub.Init()
if dbPath := config.Config.DBPath; dbPath != "" {
hub.initDB(dbPath)
}
return hub
}

var db *bolt.DB

func (h *Hub) Init() {
func (h *Hub) initDB(path string) {
var err error
db, err = bolt.Open("my.db", 0600, nil)
h.DB, err = bolt.Open(path, 0600, nil)
if err != nil {
log.Fatal(err)
}

err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("transactions"))
err = h.DB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(bucket)
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
Expand All @@ -51,8 +54,8 @@ func (h *Hub) Init() {
log.Fatal(err)
}

err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("transactions"))
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() {
Expand Down Expand Up @@ -87,14 +90,17 @@ func (h *Hub) Add(t Tx) error {

s.Lock()
h.Transactions[t.ID] = &t
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("transactions"))
bytes, err := json.Marshal(t)
if err != nil {
return err
}
return b.Put(t.ID[:], bytes)
})
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
Expand Down

0 comments on commit 831d8be

Please sign in to comment.