Skip to content

Commit

Permalink
WIP: use badgerdb key values store
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 22, 2024
1 parent ebee0f9 commit 2e85c1c
Show file tree
Hide file tree
Showing 30 changed files with 414 additions and 694 deletions.
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ NODE = $(NAME)-node
ROBOT = $(NAME)-robot
KEYSET = $(NAME)-create-keyset
FETCH = $(NAME)-fetch-document
DEBUG = $(NAME)-debug
# DEBUG = $(NAME)-debug
CMDS = $(ACTOR) $(RELAY) $(NODE) $(ROBOT) $(PONG)
ALL = $(FETCH) $(KEYSET) $(CMDS) $(DEBUG)

Expand Down Expand Up @@ -51,9 +51,8 @@ $(BINDIR):
install: $(BINDIR) $(CMDS)
sudo install -m755 $(CMDS) $(DESTDIR)$(BINDIR)/

$(DEBUG): BUILDFLAGS = -tags=debug
$(DEBUG): tidy
$(GO) build -o $(DEBUG) $(BUILDFLAGS) ./cmd/actor
debug: BUILDFLAGS = $(BUILDFAGS) -tags=debug
debug: install

$(ACTOR): tidy
$(GO) build -o $(ACTOR) $(BUILDFLAGS) ./cmd/actor
Expand Down
2 changes: 1 addition & 1 deletion cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/config/db"
"github.com/bahner/go-ma-actor/db"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
Expand Down
5 changes: 5 additions & 0 deletions cmd/node/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (

func (s *Subscription) handleEnvelopesLoop(ctx context.Context) {

if !s.actor.Keyset.IsValid() {
log.Errorf("handleEnvelopesLoop: No valid keyset for entity: %s", s.actor.Entity.DID.Id)
return
}

t := s.actor.Entity.Topic.String()

log.Debugf("Starting subscription envelope handling loop for topic: %s", t)
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func configTemplate(identity string, node string) map[string]interface{} {
"nick": config.ActorNick(),
},
"db": map[string]interface{}{
"file": config.DefaultDbFile,
"dir": config.DefaultDbPath,
},
// Use default log settings, so as not to pick up debug log settings
"log": map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion cmd/pong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func configTemplate(identity string, node string) map[string]interface{} {
"nick": pong,
},
"db": map[string]interface{}{
"file": config.DefaultDbFile,
"dir": config.DefaultDbPath,
},
"log": map[string]interface{}{
"level": config.LogLevel(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func configTemplate(node string) map[string]interface{} {
// so we manually recreate the structure based on the config we have set.
return map[string]interface{}{
"db": map[string]interface{}{
"file": config.DefaultDbFile,
"dir": config.DefaultDbPath,
},
"log": map[string]interface{}{
"level": config.LogLevel(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/robot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func configTemplate(identity string, node string) map[string]interface{} {
"nick": name,
},
"db": map[string]interface{}{
"file": config.DefaultDbFile,
"dir": config.DefaultDbPath,
},
"log": map[string]interface{}{
"level": config.LogLevel(),
Expand Down
21 changes: 11 additions & 10 deletions cmd/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,59 +52,60 @@ func NewRobot() (i *RobotStruct, err error) {

go i.Location.HandleIncomingMessages(context.Background(), messages)

go i.handleMessageEvents()
go i.handleEntityMessageEvents()

return i, err
}

func (i *RobotStruct) handleMessageEvents() {
func (i *RobotStruct) handleEntityMessageEvents() {
ctx := context.Background()
me := i.Robot.Entity.DID.Id
myMessages := i.Robot.Entity.Messages
errPrefix := fmt.Sprintf("handleEntityMessageEvents (%s): ", me)

log.Debugf("Starting handleMessageEvents for %s", me)

for {
select {
case <-ctx.Done(): // Check for cancellation signal
log.Info("handleMessageEvents: context cancelled, exiting...")
log.Info(errPrefix + "context cancelled, exiting...")
return

case m, ok := <-myMessages: // Attempt to receive a message
if !ok {
log.Debugf("messageEvents: channel closed, exiting...")
log.Debugf(errPrefix + "channel closed, exiting...")
return
}

if m == nil {
log.Debugf("messageEvents: received nil message, ignoring...")
log.Debugf(errPrefix + "received nil message, ignoring...")
continue
}

if m.Message.Verify() != nil {
log.Debugf("messageEvents: failed to verify message: %v", m)
log.Debugf(errPrefix+"failed to verify message: %v", m)
continue
}

content := string(m.Message.Content)
from := m.Message.From
to := m.Message.To

log.Debugf("Handling message: %v from %s to %s", content, from, to)
log.Debugf(errPrefix+"Handling message: %v from %s to %s", content, from, to)

if from == me {
log.Debugf("Received message from self, ignoring...")
log.Debugf(errPrefix + "Received message from self, ignoring...")
continue
}

if m.Message.Type == ma.MESSAGE_TYPE {
i.messageReply(ctx, m)
i.handleMessage(ctx, m)
}
}
}
}

func (i *RobotStruct) messageReply(ctx context.Context, m *entity.Message) error {
func (i *RobotStruct) handleMessage(ctx context.Context, m *entity.Message) error {

// Switch sender and receiver. Reply back to from :-)
// Broadcast are sent to the topic, and the topic is the DID of the recipient
Expand Down
10 changes: 8 additions & 2 deletions config/db.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package config

const defaultDBFilename = "ma.db"
import "github.com/spf13/viper"

var DefaultDbFile = NormalisePath(dataHome + defaultDBFilename)
const defaultDBDirname = ".madb"

var DefaultDbPath = NormalisePath(dataHome + defaultDBDirname)

func DBPath() string {
return viper.GetString("db.path")
}
114 changes: 0 additions & 114 deletions config/db/db.go

This file was deleted.

76 changes: 76 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package db

import (
"fmt"
"sync"

"github.com/bahner/go-ma-actor/config"
badger "github.com/dgraph-io/badger/v3"
"github.com/mitchellh/go-homedir"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func init() {

pflag.String("db", config.DefaultDbPath, "File for sqlite database.")

viper.BindPFlag("db.path", pflag.Lookup("db"))

}

var (
_db *badger.DB
once sync.Once
)

// InitDB initializes the Badger database and sets the global `db` variable.
// It uses `sync.Once` to ensure that the database is opened only once.
func Init() (db *badger.DB, err error) {

once.Do(func() {

var dbPath string

dbPath, err = dbDir()
if err != nil {
return
}

db, err = badger.Open(badger.DefaultOptions(dbPath))
if err != nil {
return
}
})

return db, nil
}

// CloseDB closes the Badger database. This should be called when your application exits.
func Close() (err error) {
if _db != nil {
err = _db.Close()
if err != nil {
return fmt.Errorf("failed to close BadgerDB: %v", err)
}
}
return nil
}

func DB() *badger.DB {
return _db
}

// Returns expanded path to the dbDir file
// If the expansion fails it returns an empty string
func dbDir() (string, error) {

p := viper.GetString("db.path")
p, err := homedir.Expand(p)
if err != nil {
return "", err
}

return config.NormalisePath(p), nil

}
Loading

0 comments on commit 2e85c1c

Please sign in to comment.