wip
parent
6e8a6e491c
commit
3e6902cfcd
|
@ -4,12 +4,7 @@ An in-process, in-memory database for Go.
|
||||||
|
|
||||||
## TO DO
|
## TO DO
|
||||||
|
|
||||||
* [ ] wal: delete old entries
|
|
||||||
* [ ] wal: shipping_test: secondary too far behind
|
|
||||||
* [ ] wal: writer_test: call RecvWAL twice
|
|
||||||
* [ ] wal: writer_test double start / stop
|
|
||||||
* [ ] kvstore: tests
|
* [ ] kvstore: tests
|
||||||
* [ ] wal: writer lock via flock?
|
|
||||||
|
|
||||||
## Structure
|
## Structure
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,10 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.crumpington.com/private/mdb/wal"
|
"git.crumpington.com/private/mdb/kvstore/wal"
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
@ -20,10 +18,9 @@ type KV struct {
|
||||||
dataPath string
|
dataPath string
|
||||||
walPath string
|
walPath string
|
||||||
|
|
||||||
w *wal.Writer
|
w *wal.Writer
|
||||||
f *wal.Follower
|
f *wal.Follower
|
||||||
lock *os.File
|
db *sql.DB
|
||||||
db *sql.DB
|
|
||||||
|
|
||||||
stop chan struct{}
|
stop chan struct{}
|
||||||
done sync.WaitGroup
|
done sync.WaitGroup
|
||||||
|
@ -36,13 +33,6 @@ type KV struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kv *KV) init() {
|
func (kv *KV) init() {
|
||||||
// Acquire the lock.
|
|
||||||
lock, err := os.OpenFile(kv.lockPath, os.O_RDWR|os.O_CREATE, 0600)
|
|
||||||
must(err)
|
|
||||||
must(unix.Flock(int(lock.Fd()), unix.LOCK_EX))
|
|
||||||
|
|
||||||
kv.lock = lock
|
|
||||||
|
|
||||||
opts := `?_journal=WAL`
|
opts := `?_journal=WAL`
|
||||||
db, err := sql.Open("sqlite3", kv.dataPath+opts)
|
db, err := sql.Open("sqlite3", kv.dataPath+opts)
|
||||||
must(err)
|
must(err)
|
||||||
|
@ -71,7 +61,6 @@ func (kv *KV) start() {
|
||||||
func NewPrimary(dir string) *KV {
|
func NewPrimary(dir string) *KV {
|
||||||
kv := &KV{
|
kv := &KV{
|
||||||
primary: true,
|
primary: true,
|
||||||
lockPath: lockPath(dir),
|
|
||||||
dataPath: dataPath(dir),
|
dataPath: dataPath(dir),
|
||||||
walPath: walPath(dir),
|
walPath: walPath(dir),
|
||||||
}
|
}
|
||||||
|
@ -87,7 +76,6 @@ func NewSecondary(
|
||||||
) *KV {
|
) *KV {
|
||||||
kv := &KV{
|
kv := &KV{
|
||||||
primary: false,
|
primary: false,
|
||||||
lockPath: lockPath(dir),
|
|
||||||
dataPath: dataPath(dir),
|
dataPath: dataPath(dir),
|
||||||
walPath: walPath(dir),
|
walPath: walPath(dir),
|
||||||
}
|
}
|
||||||
|
@ -145,12 +133,10 @@ func (kv *KV) Close() {
|
||||||
kv.w.Close()
|
kv.w.Close()
|
||||||
kv.f.Close()
|
kv.f.Close()
|
||||||
kv.db.Close()
|
kv.db.Close()
|
||||||
kv.lock.Close()
|
|
||||||
|
|
||||||
kv.w = nil
|
kv.w = nil
|
||||||
kv.f = nil
|
kv.f = nil
|
||||||
kv.db = nil
|
kv.db = nil
|
||||||
kv.lock = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kv *KV) Store(collection string, id uint64, data []byte) {
|
func (kv *KV) Store(collection string, id uint64, data []byte) {
|
||||||
|
|
|
@ -24,7 +24,3 @@ func dataPath(dir string) string {
|
||||||
func walPath(dir string) string {
|
func walPath(dir string) string {
|
||||||
return filepath.Join(dir, "wal")
|
return filepath.Join(dir, "wal")
|
||||||
}
|
}
|
||||||
|
|
||||||
func lockPath(dir string) string {
|
|
||||||
return filepath.Join(dir, "lock")
|
|
||||||
}
|
|
||||||
|
|
Reference in New Issue