Skip to content

Commit

Permalink
Reimplement script commands against hive script
Browse files Browse the repository at this point in the history
Signed-off-by: Jussi Maki <jussi.maki@isovalent.com>
  • Loading branch information
joamaki committed Oct 1, 2024
1 parent da44c74 commit 1724458
Show file tree
Hide file tree
Showing 7 changed files with 691 additions and 1 deletion.
7 changes: 6 additions & 1 deletion any_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ type AnyTable struct {
}

func (t AnyTable) All(txn ReadTxn) iter.Seq2[any, Revision] {
all, _ := t.AllWatch(txn)
return all
}

func (t AnyTable) AllWatch(txn ReadTxn) (iter.Seq2[any, Revision], <-chan struct{}) {
indexTxn := txn.getTxn().mustIndexReadTxn(t.Meta, PrimaryIndexPos)
return anySeq(indexTxn.Iterator())
return anySeq(indexTxn.Iterator()), indexTxn.RootWatch()
}

func (t AnyTable) UnmarshalYAML(data []byte) (any, error) {
Expand Down
1 change: 1 addition & 0 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var Cell = cell.Module(

cell.Provide(
newHiveDB,
ScriptCommands,
),
)

Expand Down
93 changes: 93 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"bufio"
"context"
"fmt"
"log/slog"
"maps"
"os"

"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/script"
"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
)

type ExObj struct {
Name string
Foo string
}

func (ExObj) TableHeader() []string {
return []string{
"Name",
"Foo",
}
}

func (o ExObj) TableRow() []string {
return []string{
o.Name,
o.Foo,
}
}

var idIndex = statedb.Index[ExObj, string]{
Name: "id",
FromObject: func(o ExObj) index.KeySet {
return index.NewKeySet(index.String(o.Name))
},
FromKey: index.String,
Unique: true,
}

func newExTable(db *statedb.DB) (statedb.RWTable[ExObj], error) {
table, err := statedb.NewTable(
"ex",
idIndex,
)
if err != nil {
return nil, err
}
err = db.RegisterTable(table)
txn := db.WriteTxn(table)
table.Insert(txn, ExObj{"one", "hello"})
table.Insert(txn, ExObj{"two", "world"})
txn.Commit()
return table, err

}

func main() {
h := hive.New(
statedb.Cell,
cell.Invoke(newExTable),
)
cmds := h.ScriptCommands()
maps.Insert(cmds, maps.All(script.DefaultCmds()))

log := slog.Default()
h.Start(log, context.TODO())
defer h.Stop(log, context.TODO())

e := script.Engine{
Cmds: cmds,
Conds: nil,
Interactive: true,
}
s, err := script.NewState(context.TODO(), "/tmp/foo", nil)
if err != nil {
panic(err)
}
bio := bufio.NewReader(os.Stdin)
for {
err := e.Execute(s, "", bio, os.Stdout)
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module github.com/cilium/statedb

go 1.23

replace (
github.com/cilium/hive => ../hive
)

require (
github.com/cilium/hive v0.0.0-20240209163124-bd6ebb4ec11d
github.com/cilium/stream v0.0.0-20240209152734-a0792b51812d
Expand Down
Loading

0 comments on commit 1724458

Please sign in to comment.