2015-04-29 21:54:44 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-05-28 23:59:08 +00:00
|
|
|
appDir string
|
|
|
|
db *bolt.DB
|
2015-04-29 21:54:44 +00:00
|
|
|
|
|
|
|
bucketUsers = []byte("Users")
|
|
|
|
bucketServers = []byte("Servers")
|
|
|
|
bucketChannels = []byte("Channels")
|
|
|
|
bucketMessages = []byte("Messages")
|
|
|
|
)
|
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
func Initialize(dir string) {
|
2015-05-01 23:02:21 +00:00
|
|
|
var err error
|
2015-05-28 23:59:08 +00:00
|
|
|
appDir = dir
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
log.Println("Storing data at", dir)
|
2015-04-29 21:54:44 +00:00
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
db, err = bolt.Open(path.Join(dir, "data.db"), 0600, nil)
|
2015-04-29 21:54:44 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Could not open database file")
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2015-05-01 23:02:21 +00:00
|
|
|
|
2015-05-28 23:59:08 +00:00
|
|
|
func Clear(dir string) {
|
|
|
|
os.RemoveAll(path.Join(dir, "logs"))
|
|
|
|
os.Remove(path.Join(dir, "data.db"))
|
2015-05-01 23:02:21 +00:00
|
|
|
}
|