dispatch/storage/storage.go

49 lines
798 B
Go
Raw Normal View History

package storage
import (
"log"
"os"
2015-12-11 03:35:48 +00:00
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/boltdb/bolt"
)
var (
2016-01-07 22:59:38 +00:00
Path directory
2016-01-04 18:33:28 +00:00
db *bolt.DB
bucketUsers = []byte("Users")
bucketServers = []byte("Servers")
bucketChannels = []byte("Channels")
bucketMessages = []byte("Messages")
)
2016-01-07 22:59:38 +00:00
func Initialize(dir string) {
Path = directory(dir)
2016-01-07 22:59:38 +00:00
err := os.MkdirAll(Path.Logs(), 0700)
if err != nil {
log.Fatal(err)
}
}
func Open() {
2016-01-04 18:26:32 +00:00
var err error
db, err = bolt.Open(Path.Database(), 0600, nil)
if err != nil {
2016-01-04 18:26:32 +00:00
log.Fatal("Could not open database:", err)
}
db.Update(func(tx *bolt.Tx) error {
tx.CreateBucketIfNotExists(bucketUsers)
tx.CreateBucketIfNotExists(bucketServers)
tx.CreateBucketIfNotExists(bucketChannels)
return nil
})
}
func Close() {
db.Close()
}