2015-04-29 21:54:44 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2016-01-15 01:27:30 +00:00
|
|
|
"encoding/binary"
|
2015-04-29 21:54:44 +00:00
|
|
|
"log"
|
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2015-04-29 21:54:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-01-07 22:59:38 +00:00
|
|
|
Path directory
|
|
|
|
|
2016-01-04 18:33:28 +00:00
|
|
|
db *bolt.DB
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Open() {
|
2016-01-04 18:26:32 +00:00
|
|
|
var err error
|
|
|
|
db, err = bolt.Open(Path.Database(), 0600, nil)
|
2015-04-29 21:54:44 +00:00
|
|
|
if err != nil {
|
2016-01-04 18:26:32 +00:00
|
|
|
log.Fatal("Could not open database:", err)
|
2015-04-29 21:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.Update(func(tx *bolt.Tx) error {
|
|
|
|
tx.CreateBucketIfNotExists(bucketUsers)
|
|
|
|
tx.CreateBucketIfNotExists(bucketServers)
|
|
|
|
tx.CreateBucketIfNotExists(bucketChannels)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-01 23:02:21 +00:00
|
|
|
func Close() {
|
2015-04-29 21:54:44 +00:00
|
|
|
db.Close()
|
|
|
|
}
|
2016-01-15 01:27:30 +00:00
|
|
|
|
|
|
|
func idToBytes(i uint64) []byte {
|
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(b, i)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func idFromBytes(b []byte) uint64 {
|
|
|
|
return binary.BigEndian.Uint64(b)
|
|
|
|
}
|