heya, I'm trying out badger as an alternative to leveldb, when I ported my code over I find that there is no data being synced to disk:
total 252K
drwxrwxr-x 2 peter peter 4.0K Jun 28 14:28 .
drwxrwxrwt 710 root root 244K Jun 28 14:28 ..
-rw-rw-r-- 1 peter peter 0 Jun 28 14:28 000000.vlog
-rw-rw-r-- 1 peter peter 0 Jun 28 14:28 clog
note that the log.Println("wrote", item.ID) line get's called for each item and no error is produced for log.Println("write error", err).
I have tested the encoding/decoding logic and it works fine with leveldb, so don't worry about that
any help would be appreciated :)
// open database connection
conn := &badger.Connection{}
conn.Open(leveldbPath)
defer conn.Close()
package badger
import "github.com/dgraph-io/badger/badger"
// Connection - Connection
type Connection struct {
DB *badger.KV
}
// Open - open connection and set up
func (c *Connection) Open(path string) {
opt := badger.DefaultOptions
opt.Dir = path
opt.ValueDir = opt.Dir
db, err := badger.NewKV(&opt)
if err != nil {
panic(err)
}
c.DB = db
}
// Close - close connection and clean up
func (c *Connection) Close() {
c.DB.Close()
}
package badger
import (
"encoding/binary"
"log"
"math"
"github.com/dgraph-io/badger/badger"
"github.com/missinglink/gosmparse"
)
// WriteCoord - encode and write lat/lon pair to db
func (c *Connection) WriteCoord(item gosmparse.Node) error {
// encode id
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(item.ID))
// encode lat
lat := make([]byte, 8)
binary.BigEndian.PutUint64(lat, math.Float64bits(item.Lat))
// encode lon
lon := make([]byte, 8)
binary.BigEndian.PutUint64(lon, math.Float64bits(item.Lon))
// value
value := append(lat, lon...)
// write to db
err := c.DB.Set(key, value)
log.Println("wrote", item.ID)
if err != nil {
log.Println("write error", err)
return err
}
return nil
}
// ReadCoord - read lat/lon pair from db and decode
func (c *Connection) ReadCoord(id int64) (*gosmparse.Node, error) {
// encode id
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(id))
// read from db
var item badger.KVItem
err := c.DB.Get(key, &item)
if err != nil {
return nil, err
}
data := item.Value()
// decode item
return &gosmparse.Node{
ID: id,
Lat: math.Float64frombits(binary.BigEndian.Uint64(data[:8])),
Lon: math.Float64frombits(binary.BigEndian.Uint64(data[8:])),
}, nil
}
$ go version
go version go1.8.3 linux/amd64
$ uname -a
Linux peterpro 4.6.0-040600-generic #201606100558 SMP Fri Jun 10 10:01:15 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/issue
Ubuntu 16.04.2 LTS \n \l
$ cd ${GOPATH}/src/github.com/dgraph-io/badger/
$ git log -1
commit 8809f1c3328a7f17a5bf89a1a14f4ee11124e4c6
Author: Jason Chu <1lann@users.noreply.github.com>
Date: Wed Jun 28 15:02:09 2017 +0800
heya, I'm trying out badger as an alternative to leveldb, when I ported my code over I find that there is no data being synced to disk:
total 252K drwxrwxr-x 2 peter peter 4.0K Jun 28 14:28 . drwxrwxrwt 710 root root 244K Jun 28 14:28 .. -rw-rw-r-- 1 peter peter 0 Jun 28 14:28 000000.vlog -rw-rw-r-- 1 peter peter 0 Jun 28 14:28 clognote that the
log.Println("wrote", item.ID)line get's called for each item and no error is produced forlog.Println("write error", err).I have tested the encoding/decoding logic and it works fine with leveldb, so don't worry about that
any help would be appreciated :)