This commit is contained in:
@ -12,6 +12,7 @@ import (
|
||||
|
||||
"forge.redroom.link/yves/meowlib"
|
||||
"github.com/dgraph-io/badger"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type PeerStorage struct {
|
||||
@ -22,7 +23,12 @@ type PeerStorage struct {
|
||||
|
||||
// Open the badger database from struct PeerStorage
|
||||
func (ps *PeerStorage) open() error {
|
||||
|
||||
if ps.DbFile == "" {
|
||||
ps.DbFile = uuid.New().String()
|
||||
}
|
||||
if ps.cache == nil {
|
||||
ps.cache = make(map[string]*Peer)
|
||||
}
|
||||
opts := badger.DefaultOptions(filepath.Join(GetConfig().StoragePath, GetConfig().GetIdentity().Uuid, ps.DbFile))
|
||||
opts.Logger = nil
|
||||
var err error
|
||||
@ -55,6 +61,8 @@ func (ps *PeerStorage) StorePeer(peer *Peer) error {
|
||||
}
|
||||
shakey := sha256.Sum256([]byte(peer.Uid))
|
||||
key := shakey[:]
|
||||
// add it to cache
|
||||
ps.cache[peer.Uid] = peer
|
||||
// then store it in the database
|
||||
return ps.db.Update(func(txn *badger.Txn) error {
|
||||
return txn.Set(key, data)
|
||||
|
Reference in New Issue
Block a user