Skip to content

Commit

Permalink
DropDB and use CSV with fsnotify
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 6, 2024
1 parent 519602d commit 3ee6376
Show file tree
Hide file tree
Showing 42 changed files with 560 additions and 1,004 deletions.
67 changes: 3 additions & 64 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/node",
},
{
"name": "Launch allowAll",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/actor/",
"console": "integratedTerminal",
"args": [
"--p2p-discovery-allow-all"
],
"env": {
"GOLOG_FILE": "/tmp/debug.log",
"GOLOG_LOG_LEVEL": "debug",
"GOLOG_STDOUT": "false",
"GOLOG_OUTPUT": "file"
}
},
{
"name": "Launch actor as bahner",
Expand Down Expand Up @@ -67,20 +50,6 @@
"GOLOG_OUTPUT": "file"
}
},
{
"name": "Launch relay",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/relay/",
"console": "integratedTerminal",
"env": {
"GOLOG_FILE": "/tmp/debug.log",
"GOLOG_LOG_LEVEL": "debug",
"GOLOG_STDOUT": "false",
"GOLOG_OUTPUT": "file"
}
},
{
"name": "go-ma-actor generate",
"type": "go",
Expand All @@ -98,39 +67,9 @@
"--generate",
"--nick",
"FUBAR",
"--publish",
"--force",
]
},
{
"name": "Launch Foo",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/actor/",
"args": [
"-c",
"~/.ma/foo.yaml"
],
"console": "integratedTerminal"
},
{
"name": "show-config",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/actor/",
"console": "integratedTerminal",
"args": [
"--show-config"
],
"env": {
"GO_MA_ACTOR_ACTOR_NICK": "jonatan",
"GO_MA_ACTOR_LOG_FILE": "/dev/null",
"GOLOG_FILE": "debug.log",
"GOLOG_LOG_LEVEL": "debug",
"GOLOG_STDOUT": "false",
"GOLOG_OUTPUT": "file"
}
}

}
]
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ clean:
rm -f $(NAME)-*.tar
find -type f -name "*.log" -delete
rm -f actor.exe
rm go-ma-actor*
rm -f go-ma-actor*

distclean: clean
rm -rf releases
Expand Down
8 changes: 8 additions & 0 deletions cmd/actor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import (
"os"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/p2p/peer"

log "github.com/sirupsen/logrus"
)

func initConfig(defaultProfileName string) actor.ActorConfig {
Expand All @@ -24,5 +28,9 @@ func initConfig(defaultProfileName string) actor.ActorConfig {
os.Exit(0)
}

log.Info("Reading CSV files...")
go entity.WatchCSV()
go peer.WatchCSV()

return c
}
24 changes: 12 additions & 12 deletions cmd/node/entity.go → cmd/node/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import (
log "github.com/sirupsen/logrus"
)

var entities *entityCache
var actors *actorCache

type entityCache struct {
type actorCache struct {
store sync.Map
}

func init() {
entities = new(entityCache)
actors = new(actorCache)
}

// GetOrCreateEntity returns an entity from the cache or creates a new one
// The id is just the uniqgue name of the calling entity, not the full DID
func getOrCreateEntity(id string) (*actor.Actor, error) {
func getOrCreateActor(id string) (*actor.Actor, error) {

// Attempt to retrieve the entity from cache.
// This is runtime, so entities will be generated at least once.
if cachedEntity, ok := entities.Get(id); ok {
if cachedEntity, ok := actors.Get(id); ok {
if entity, ok := cachedEntity.(*actor.Actor); ok {
log.Debugf("found topic: %s in entities cache.", id)
return entity, nil // Successfully type-asserted and returned
Expand All @@ -40,31 +40,31 @@ func getOrCreateEntity(id string) (*actor.Actor, error) {
}

// Assuming entity.NewFromKeyset returns *actor.Actor
e, err := actor.NewFromKeyset(k)
a, err := actor.NewFromKeyset(k)
if err != nil {
return nil, fmt.Errorf("failed to create entity: %w", err)
}

e.Entity.Doc, err = e.CreateDocument(e.Entity.DID.Id)
a.Entity.Doc, err = a.CreateEntityDocument(a.Entity.DID.Id)
if err != nil {
return nil, fmt.Errorf("failed to create DID Document: %w", err)
}

// Force publication of document.
o := doc.DefaultPublishOptions()
o.Force = true
e.Entity.Doc.Publish(o)
a.Entity.Doc.Publish(o)

// Cache the newly created entity for future retrievals
entities.Set(id, e)
actors.Set(id, a)

return e, nil
return a, nil
}

func (e *entityCache) Set(key string, value interface{}) {
func (e *actorCache) Set(key string, value interface{}) {
e.store.Store(key, value)
}

func (e *entityCache) Get(key string) (interface{}, bool) {
func (e *actorCache) Get(key string) (interface{}, bool) {
return e.store.Load(key)
}
2 changes: 1 addition & 1 deletion cmd/node/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func New(id string) gen.ServerBehavior {

log.Debugf("Creating new genServer: %s", id)

a, err := getOrCreateEntity(id)
a, err := getOrCreateActor(id)
if err != nil {
log.Errorf("Error getting or creating entity: %s", err)
return nil
Expand Down
6 changes: 6 additions & 0 deletions cmd/pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/ui/web"

"github.com/bahner/go-ma-actor/p2p"
)
Expand Down Expand Up @@ -48,6 +49,11 @@ func main() {
actor.HelloWorld(ctx, a)
fmt.Println("Sent hello world.")

// WEB
fmt.Println("Initialising web UI...")
wh := web.NewEntityHandler(p, a.Entity)
go web.Start(wh)

fmt.Printf("Running in pong mode as %s@%s\n", a.Entity.DID.Id, p.Host.ID())
fmt.Println("Press Ctrl-C to stop.")

Expand Down
4 changes: 2 additions & 2 deletions cmd/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRobot() (i *RobotStruct, err error) {
go i.Robot.Entity.HandleIncomingMessages(context.Background(), messages)

// Subscribe to message at location
i.Location, err = entity.GetOrCreate(config.ActorLocation(), true)
i.Location, err = entity.GetOrCreate(config.ActorLocation())
if err != nil {
return nil, fmt.Errorf("failed to get or create actor location: %w", errors.Cause(err))
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (i *RobotStruct) handleMessage(ctx context.Context, m *entity.Message) erro
// Switch sender and receiver. Reply back to from :-)
// Broadcast are sent to the topic, and the topic is the DID of the recipient
replyFrom := i.Robot.Entity.DID.Id
replyTo, err := entity.New(m.Message.From)
replyTo, err := entity.GetOrCreate(m.Message.From)
if err != nil {
return fmt.Errorf("failed to create new entity: %w", errors.Cause(err))
}
Expand Down
6 changes: 3 additions & 3 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ func initActor() {
// This function fails fatally, so no return value
initActorKeyset(keyset_string)

if PublishFlag() && keyset_string != "" {
// If publish then publish, unless we are to generate a new keyset.
if PublishFlag() && !GenerateFlag() {
fmt.Println("Publishing identity to IPFS...")
err := PublishIdentityFromKeyset(keyset)
if err != nil {
Expand Down Expand Up @@ -146,7 +147,6 @@ func initActorKeyset(keyset_string string) {
log.Errorf("config.initActor: %v", err)
os.Exit(70) // EX_SOFTWARE
}

}

func generateActorIdentity(nick string) (string, error) {
Expand All @@ -164,7 +164,7 @@ func generateActorIdentity(nick string) (string, error) {

if err != nil {
if errors.Is(err, doc.ErrAlreadyPublished) {
log.Warnf("Actor document already published: %v", err)
log.Debugf("Actor document already published: %v", err)
} else {
return "", fmt.Errorf("failed to publish actor identity: %w", err)
}
Expand Down
35 changes: 25 additions & 10 deletions config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,42 @@ import (
"github.com/spf13/viper"
)

var DefaultDbPath = internal.NormalisePath(dataHome + "/." + Profile())
const CSVMode = 0664

var (
defaultPeersPath = internal.NormalisePath(dataHome + "/peers.csv")
defaultEntitiesPath = internal.NormalisePath(dataHome + "/entities.csv")
)

func init() {
pflag.String("db-path", DefaultDbPath, "Directory to use for database.")
viper.BindPFlag("db.path", pflag.Lookup("db-path"))
viper.SetDefault("db.path", DefaultDbPath)
pflag.String("peers", defaultPeersPath, "Filename for CSV peers file.")
pflag.String("entities", defaultEntitiesPath, "Filename for CSV entities file.")

viper.BindPFlag("db.peers", pflag.Lookup("peers"))
viper.BindPFlag("db.entities", pflag.Lookup("entities"))

viper.SetDefault("db.peers", defaultPeersPath)
viper.SetDefault("db.entities", defaultEntitiesPath)
}

type DBConfig struct {
Path string `yaml:"path"`
Peers string `yaml:"peers"`
Entities string `yaml:"entities"`
}

func DB() DBConfig {

viper.SetDefault("db.path", DefaultDbPath)

return DBConfig{
Path: DBPath(),
Peers: DBPeers(),
Entities: DBEntities(),
}

}

func DBPeers() string {
return viper.GetString("db.peers")
}

func DBPath() string {
return viper.GetString("db.path")
func DBEntities() string {
return viper.GetString("db.entities")
}
78 changes: 0 additions & 78 deletions db/badger/db.go

This file was deleted.

Loading

0 comments on commit 3ee6376

Please sign in to comment.