- Added `Export` method in `database/database.go` to export events to an io.Writer - Implemented detailed logic for exporting all or specific pubkeys' events - Removed placeholder `Export` function with TODO comment from `database/database.go` - Updated error handling in `handleReq.go` and `publisher.go` by using `err != nil` instead of `chk.E(err)` - Added more detailed logging in privilege check conditions in both `publisher.go` and `handleReq.go` - Introduced new imports such as `"fmt"` in `connection.go` for improved error message formatting - Created a new file `export.go` under the `database` package with complete implementation of export functionality
121 lines
2.5 KiB
Go
121 lines
2.5 KiB
Go
package database
|
|
|
|
import (
|
|
"github.com/dgraph-io/badger/v4"
|
|
"io"
|
|
"orly.dev/pkg/encoders/eventidserial"
|
|
"orly.dev/pkg/utils/apputil"
|
|
"orly.dev/pkg/utils/chk"
|
|
"orly.dev/pkg/utils/context"
|
|
"orly.dev/pkg/utils/log"
|
|
"orly.dev/pkg/utils/lol"
|
|
"orly.dev/pkg/utils/units"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type D struct {
|
|
ctx context.T
|
|
cancel context.F
|
|
dataDir string
|
|
Logger *logger
|
|
*badger.DB
|
|
seq *badger.Sequence
|
|
}
|
|
|
|
func New(ctx context.T, cancel context.F, dataDir, logLevel string) (
|
|
d *D, err error,
|
|
) {
|
|
d = &D{
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
dataDir: dataDir,
|
|
Logger: NewLogger(lol.GetLogLevel(logLevel), dataDir),
|
|
DB: nil,
|
|
seq: nil,
|
|
}
|
|
|
|
// Ensure the data directory exists
|
|
if err = os.MkdirAll(dataDir, 0755); chk.E(err) {
|
|
return
|
|
}
|
|
|
|
// Also ensure the directory exists using apputil.EnsureDir for any potential subdirectories
|
|
dummyFile := filepath.Join(dataDir, "dummy.sst")
|
|
if err = apputil.EnsureDir(dummyFile); chk.E(err) {
|
|
return
|
|
}
|
|
|
|
opts := badger.DefaultOptions(d.dataDir)
|
|
opts.BlockCacheSize = int64(units.Gb)
|
|
opts.BlockSize = units.Gb
|
|
opts.CompactL0OnClose = true
|
|
opts.LmaxCompaction = true
|
|
if d.DB, err = badger.Open(opts); chk.E(err) {
|
|
return
|
|
}
|
|
log.T.Ln("getting event sequence lease", d.dataDir)
|
|
if d.seq, err = d.DB.GetSequence([]byte("EVENTS"), 1000); chk.E(err) {
|
|
return
|
|
}
|
|
go func() {
|
|
<-d.ctx.Done()
|
|
d.cancel()
|
|
d.seq.Release()
|
|
d.DB.Close()
|
|
}()
|
|
return
|
|
}
|
|
|
|
// Path returns the path where the database files are stored.
|
|
func (d *D) Path() string { return d.dataDir }
|
|
|
|
func (d *D) Wipe() (err error) {
|
|
// TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (d *D) Import(r io.Reader) {
|
|
// TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (d *D) SetLogLevel(level string) {
|
|
d.Logger.SetLogLevel(lol.GetLogLevel(level))
|
|
}
|
|
|
|
func (d *D) EventIdsBySerial(start uint64, count int) (
|
|
evs []eventidserial.E, err error,
|
|
) {
|
|
// TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
// Init initializes the database with the given path.
|
|
func (d *D) Init(path string) (err error) {
|
|
// The database is already initialized in the New function,
|
|
// so we just need to ensure the path is set correctly.
|
|
d.dataDir = path
|
|
return nil
|
|
}
|
|
|
|
// Sync flushes the database buffers to disk.
|
|
func (d *D) Sync() (err error) {
|
|
return d.DB.Sync()
|
|
}
|
|
|
|
// Close releases resources and closes the database.
|
|
func (d *D) Close() (err error) {
|
|
if d.seq != nil {
|
|
if err = d.seq.Release(); chk.E(err) {
|
|
return
|
|
}
|
|
}
|
|
if d.DB != nil {
|
|
if err = d.DB.Close(); chk.E(err) {
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|