package mdb import ( "net" "os" "sync" "git.crumpington.com/private/mdb/kvstore" ) type Database struct { root string kv *kvstore.KV collections map[string]dbCollection } func NewPrimary(root string) *Database { must(os.MkdirAll(root, 0700)) db := &Database{ root: root, collections: map[string]dbCollection{}, } db.kv = kvstore.NewPrimary(root) return db } func NewSecondary(root string) *Database { must(os.MkdirAll(root, 0700)) db := &Database{ root: root, collections: map[string]dbCollection{}, } db.kv = kvstore.NewSecondary(root, db.onStore, db.onDelete) return db } func (db *Database) Start() { wg := sync.WaitGroup{} for _, c := range db.collections { wg.Add(1) go func(c dbCollection) { defer wg.Done() c.loadData() }(c) } wg.Wait() } func (db *Database) WALStatus() (ws WALStatus) { ws.MaxID = db.kv.WALMaxSeqNum() ws.MaxAppliedID = db.kv.MaxSeqNum() return } func (db *Database) Close() { db.kv.Close() } // ---------------------------------------------------------------------------- func (db *Database) onStore(collection string, id uint64, data []byte) { c, ok := db.collections[collection] if ok { c.onStore(collection, id, data) } } func (db *Database) onDelete(collection string, id uint64) { c, ok := db.collections[collection] if ok { c.onDelete(collection, id) } } // ---------------------------------------------------------------------------- func (db *Database) SyncSend(conn net.Conn) { db.kv.SyncSend(conn) } func (db *Database) SyncRecv(conn net.Conn) { db.kv.SyncRecv(conn) }