2021-10-14 14:14:55 +00:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
|
|
|
rendezvous "github.com/status-im/go-waku-rendezvous"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// RendezVousLevelDB is a struct used to hold a reference to a LevelDB database
|
2021-10-14 14:14:55 +00:00
|
|
|
type RendezVousLevelDB struct {
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// NewRendezVousLevelDB opens a LevelDB database to be used for rendezvous protocol
|
2021-10-14 14:14:55 +00:00
|
|
|
func NewRendezVousLevelDB(dBPath string) (*RendezVousLevelDB, error) {
|
|
|
|
db, err := leveldb.OpenFile(dBPath, &opt.Options{OpenFilesCacheCapacity: 3})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RendezVousLevelDB{db}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// Delete removes a key from the database
|
2021-10-14 14:14:55 +00:00
|
|
|
func (r *RendezVousLevelDB) Delete(key []byte) error {
|
|
|
|
return r.db.Delete(key, nil)
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// Put inserts or updates a key in the database
|
2021-10-14 14:14:55 +00:00
|
|
|
func (r *RendezVousLevelDB) Put(key []byte, value []byte) error {
|
|
|
|
return r.db.Put(key, value, nil)
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// NewIterator returns an interator that can be used to iterate over all
|
|
|
|
// the records contained in the DB
|
2021-10-14 14:14:55 +00:00
|
|
|
func (r *RendezVousLevelDB) NewIterator(prefix []byte) rendezvous.Iterator {
|
|
|
|
return r.db.NewIterator(util.BytesPrefix(prefix), nil)
|
|
|
|
}
|