ledisdb/store/boltdb/db.go

162 lines
2.6 KiB
Go
Raw Normal View History

2014-08-02 11:16:16 +04:00
package boltdb
import (
"github.com/boltdb/bolt"
"github.com/siddontang/ledisdb/config"
2014-08-02 11:16:16 +04:00
"github.com/siddontang/ledisdb/store/driver"
"os"
"path"
)
var bucketName = []byte("ledisdb")
type Store struct {
2014-08-02 11:16:16 +04:00
}
func (s Store) String() string {
2014-08-15 20:08:01 +04:00
return DBName
2014-08-02 11:16:16 +04:00
}
func (s Store) Open(dbPath string, cfg *config.Config) (driver.IDB, error) {
os.MkdirAll(dbPath, 0755)
name := path.Join(dbPath, "ledis_bolt.db")
2014-08-02 11:16:16 +04:00
db := new(DB)
var err error
db.path = name
db.cfg = cfg
2014-08-02 11:16:16 +04:00
db.db, err = bolt.Open(name, 0600, nil)
if err != nil {
return nil, err
}
var tx *bolt.Tx
tx, err = db.db.Begin(true)
if err != nil {
return nil, err
}
_, err = tx.CreateBucketIfNotExists(bucketName)
if err != nil {
tx.Rollback()
return nil, err
}
if err = tx.Commit(); err != nil {
return nil, err
}
return db, nil
}
func (s Store) Repair(path string, cfg *config.Config) error {
2014-08-02 11:16:16 +04:00
return nil
}
type DB struct {
cfg *config.Config
db *bolt.DB
path string
}
2014-08-02 11:16:16 +04:00
func (db *DB) Close() error {
return db.db.Close()
}
func (db *DB) Get(key []byte) ([]byte, error) {
var value []byte
t, err := db.db.Begin(false)
if err != nil {
return nil, err
}
2014-08-25 10:18:23 +04:00
defer t.Rollback()
2014-08-02 11:16:16 +04:00
b := t.Bucket(bucketName)
value = b.Get(key)
if value == nil {
return nil, nil
} else {
return append([]byte{}, value...), nil
}
}
func (db *DB) Put(key []byte, value []byte) error {
err := db.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
return b.Put(key, value)
})
return err
}
func (db *DB) Delete(key []byte) error {
err := db.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
return b.Delete(key)
})
return err
}
func (db *DB) NewIterator() driver.IIterator {
tx, err := db.db.Begin(false)
if err != nil {
return &Iterator{}
}
b := tx.Bucket(bucketName)
return &Iterator{
tx: tx,
it: b.Cursor()}
}
func (db *DB) NewWriteBatch() driver.IWriteBatch {
return driver.NewWriteBatch(db)
}
func (db *DB) Begin() (driver.Tx, error) {
tx, err := db.db.Begin(true)
if err != nil {
return nil, err
}
return &Tx{
tx: tx,
b: tx.Bucket(bucketName),
}, nil
}
2014-08-25 10:18:23 +04:00
func (db *DB) NewSnapshot() (driver.ISnapshot, error) {
return newSnapshot(db)
}
2014-08-02 11:16:16 +04:00
func (db *DB) BatchPut(writes []driver.Write) error {
err := db.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketName)
var err error
for _, w := range writes {
if w.Value == nil {
err = b.Delete(w.Key)
} else {
err = b.Put(w.Key, w.Value)
}
if err != nil {
return err
}
}
return nil
})
return err
}
2014-08-15 20:08:01 +04:00
2014-09-12 11:06:36 +04:00
func (db *DB) Compact() error {
return nil
}
2014-08-15 20:08:01 +04:00
func init() {
driver.Register(Store{})
}