Skip to content

Commit

Permalink
Feat: establish keeper registry
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 9, 2024
1 parent f69af81 commit 41d3aa7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bitcask/reg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bitcask

import "git.tcp.direct/tcp.direct/database"

func init() {
database.Register("bitcask", func(path string) (database.Keeper, error) {
db := OpenDB(path)
err := db.init()
return db, err
})
}
11 changes: 11 additions & 0 deletions pogreb/reg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pogreb

import "git.tcp.direct/tcp.direct/database"

func init() {
database.Register("pogreb", func(path string) (database.Keeper, error) {
db := OpenDB(path)
err := db.init()
return db, err
})
}
30 changes: 30 additions & 0 deletions reg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package database

import (
"sync"
)

var (
keepers = make(map[string]KeeperCreator)
regMu = &sync.RWMutex{}
)

func Register(name string, keeper KeeperCreator) {
regMu.Lock()
keepers[name] = keeper
regMu.Unlock()
}

func Get(name string) KeeperCreator {
regMu.RLock()
k := keepers[name]
regMu.RUnlock()
return k
}

func All() map[string]KeeperCreator {
regMu.RLock()
ks := keepers
regMu.RUnlock()
return ks
}

0 comments on commit 41d3aa7

Please sign in to comment.