Skip to content

Commit

Permalink
feat: dump connected components
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Mar 22, 2024
1 parent 91b6bc1 commit c0f294f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
10 changes: 5 additions & 5 deletions pkg/silo/default_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ package silo
import "github.com/cgi-fr/silo/pkg/multimap"

type BackendInMemory struct {
links multimap.Multimap[DataNode, DataNode]
links multimap.Multimap[string, string]
}

func NewBackendInMemory() *BackendInMemory {
return &BackendInMemory{
links: multimap.Multimap[DataNode, DataNode]{},
links: multimap.Multimap[string, string]{},
}
}

func (b *BackendInMemory) Store(key DataNode, value DataNode) error {
func (b *BackendInMemory) Store(key string, value string) error {
b.links.Add(key, value)

return nil
Expand All @@ -45,10 +45,10 @@ func (b *BackendInMemory) Close() error {
return nil
}

func (b *BackendInMemory) Next() (DataNode, bool) {
func (b *BackendInMemory) Next() (string, bool) {
return b.links.RandomKey()
}

func (b *BackendInMemory) PullAll(node DataNode) ([]DataNode, error) {
func (b *BackendInMemory) PullAll(node string) ([]string, error) {
return b.links.Delete(node), nil
}
4 changes: 2 additions & 2 deletions pkg/silo/default_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func NewDumpToStdout() *DumpToStdout {
return &DumpToStdout{}
}

func (d *DumpToStdout) Write(node DataNode, uuid string) error {
println(uuid, node.String())
func (d *DumpToStdout) Write(node string, uuid string) error {
println(uuid, node)

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/silo/driven.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type DataNodeReader interface {
}

type Backend interface {
Store(key DataNode, value DataNode) error
Store(key string, value string) error
Snapshot() Snapshot
Close() error
}

type Snapshot interface {
Next() (DataNode, bool)
PullAll(node DataNode) ([]DataNode, error)
Next() (string, bool)
PullAll(node string) ([]string, error)
Close() error
}

type DumpWriter interface {
Write(node DataNode, uuid string) error
Write(node string, uuid string) error
Close() error
}
30 changes: 23 additions & 7 deletions pkg/silo/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,37 @@ func (d *Driver) Dump() error {
snapshot := d.backend.Snapshot()

for count := 0; ; count++ {

entryNode, present := snapshot.Next()
if !present {
break
}

_ = d.writer.Write(entryNode, strconv.Itoa(count))

connectedNodes, err := snapshot.PullAll(entryNode)
if err != nil {
done := map[string]any{entryNode: nil}

if err := d.dump(snapshot, entryNode, done, count); err != nil {
return fmt.Errorf("%w", err)
}
}

return nil
}

func (d *Driver) dump(snapshot Snapshot, node string, done map[string]any, id int) error {
connectedNodes, err := snapshot.PullAll(node)
if err != nil {
return fmt.Errorf("%w", err)
}

for _, connectedNode := range connectedNodes {
_ = d.writer.Write(connectedNode, strconv.Itoa(count))
for _, connectedNode := range connectedNodes {
if _, ok := done[connectedNode]; !ok {
_ = d.writer.Write(connectedNode, strconv.Itoa(id))
done[connectedNode] = nil

if err := d.dump(snapshot, connectedNode, done, id); err != nil {
return fmt.Errorf("%w", err)
}
}
}

Expand All @@ -77,11 +93,11 @@ func (d *Driver) Scan(input DataRowReader) error {
links := Scan(datarow)

for _, link := range links {
if err := d.backend.Store(link.E1, link.E2); err != nil {
if err := d.backend.Store(link.E1.String(), link.E2.String()); err != nil {
return fmt.Errorf("%w: %w", ErrPersistingData, err)
}

if err := d.backend.Store(link.E2, link.E1); err != nil {
if err := d.backend.Store(link.E2.String(), link.E1.String()); err != nil {
return fmt.Errorf("%w: %w", ErrPersistingData, err)
}
}
Expand Down

0 comments on commit c0f294f

Please sign in to comment.