Leavitt/pkg/core/db.go

59 lines
1.3 KiB
Go

/*
Copyright © 2022 Sameer Rahmani <lxsameer@gnu.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package core
import (
"github.com/cockroachdb/pebble"
log "github.com/sirupsen/logrus"
)
type DB struct {
connection *pebble.DB
}
func CreateDB(path string) (*DB, error) {
log.Debugf("Opening up the state db at '%s'", path)
db, err := pebble.Open(path, nil)
if err != nil {
return nil, err
}
return &DB{
connection: db,
}, nil
}
func (db *DB) Set(k []byte, v []byte) error {
return db.connection.Set(k, v, pebble.Sync)
}
func (db *DB) Get(k []byte) ([]byte, error) {
v, closer, err := db.connection.Get(k)
defer closer.Close()
if err != nil {
return nil, err
}
return v, nil
}
func (db *DB) Close() error {
return db.connection.Close()
}