Skip to content

Commit

Permalink
all: use strings for message ids
Browse files Browse the repository at this point in the history
Fixes #46.
  • Loading branch information
zephyrtronium committed Aug 10, 2024
1 parent e427263 commit 9e31d26
Show file tree
Hide file tree
Showing 18 changed files with 425 additions and 514 deletions.
26 changes: 14 additions & 12 deletions brain/braintest/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"testing"
"time"

"github.com/google/uuid"

"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/userhash"
)

func randid() string {
return strconv.FormatUint(rand.Uint64(), 16) + "-" + strconv.FormatUint(rand.Uint64(), 16)
}

// BenchLearn runs benchmarks on the brain's speed with recording new tuples.
// The learner returned by new must be safe for concurrent use.
func BenchLearn(ctx context.Context, b *testing.B, new func(ctx context.Context, b *testing.B) brain.Learner, cleanup func(brain.Learner)) {
Expand All @@ -40,9 +42,9 @@ func BenchLearn(ctx context.Context, b *testing.B, new func(ctx context.Context,
for pb.Next() {
t++
toks[len(toks)-1] = strconv.FormatInt(t, 10)
id := uuid.New()
id := randid()
u := userhash.Hash(randbytes(make([]byte, len(userhash.Hash{}))))
err := brain.Learn(ctx, l, "bocchi", u, id, time.Unix(t, 0), toks)
err := brain.Learn(ctx, l, "bocchi", id, u, time.Unix(t, 0), toks)
if err != nil {
b.Errorf("error while learning: %v", err)
}
Expand Down Expand Up @@ -78,9 +80,9 @@ func BenchLearn(ctx context.Context, b *testing.B, new func(ctx context.Context,
for pb.Next() {
t++
rand.Shuffle(len(toks), func(i, j int) { toks[i], toks[j] = toks[j], toks[i] })
id := uuid.New()
id := randid()
u := userhash.Hash(randbytes(make([]byte, len(userhash.Hash{}))))
err := brain.Learn(ctx, l, "bocchi", u, id, time.Unix(t, 0), toks[:8])
err := brain.Learn(ctx, l, "bocchi", id, u, time.Unix(t, 0), toks[:8])
if err != nil {
b.Errorf("error while learning: %v", err)
}
Expand Down Expand Up @@ -112,9 +114,9 @@ func BenchSpeak(ctx context.Context, b *testing.B, new func(ctx context.Context,
}
for t := range size {
toks[len(toks)-1] = strconv.FormatInt(t, 10)
id := uuid.New()
id := randid()
u := userhash.Hash(randbytes(make([]byte, len(userhash.Hash{}))))
err := brain.Learn(ctx, br, "bocchi", u, id, time.Unix(t, 0), toks)
err := brain.Learn(ctx, br, "bocchi", id, u, time.Unix(t, 0), toks)
if err != nil {
b.Errorf("error while learning: %v", err)
}
Expand Down Expand Up @@ -157,9 +159,9 @@ func BenchSpeak(ctx context.Context, b *testing.B, new func(ctx context.Context,
}
for t := range size {
rand.Shuffle(len(toks), func(i, j int) { toks[i], toks[j] = toks[j], toks[i] })
id := uuid.New()
id := randid()
u := userhash.Hash(randbytes(make([]byte, len(userhash.Hash{}))))
err := brain.Learn(ctx, br, "bocchi", u, id, time.Unix(t, 0), toks)
err := brain.Learn(ctx, br, "bocchi", id, u, time.Unix(t, 0), toks)
if err != nil {
b.Errorf("error while learning: %v", err)
}
Expand Down Expand Up @@ -202,9 +204,9 @@ func BenchSpeak(ctx context.Context, b *testing.B, new func(ctx context.Context,
}
for t := range size {
rand.Shuffle(len(toks), func(i, j int) { toks[i], toks[j] = toks[j], toks[i] })
id := uuid.New()
id := randid()
u := userhash.Hash(randbytes(make([]byte, len(userhash.Hash{}))))
err := brain.Learn(ctx, br, "bocchi", u, id, time.Unix(t, 0), toks)
err := brain.Learn(ctx, br, "bocchi", id, u, time.Unix(t, 0), toks)
if err != nil {
b.Errorf("error while learning: %v", err)
}
Expand Down
28 changes: 13 additions & 15 deletions brain/braintest/braintest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"testing"
"time"

"github.com/google/uuid"

"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/userhash"
)
Expand All @@ -33,70 +31,70 @@ func these(s ...string) func() []string {
}

var messages = [...]struct {
ID uuid.UUID
ID string
User userhash.Hash
Tag string
Time time.Time
Tokens func() []string
}{
{
ID: uuid.UUID{1},
ID: "1",
User: userhash.Hash{2},
Tag: "kessoku",
Time: time.Unix(0, 0),
Tokens: these("member ", "bocchi "),
},
{
ID: uuid.UUID{2},
ID: "2",
User: userhash.Hash{2},
Tag: "kessoku",
Time: time.Unix(1, 0),
Tokens: these("member ", "ryou "),
},
{
ID: uuid.UUID{3},
ID: "3",
User: userhash.Hash{3},
Tag: "kessoku",
Time: time.Unix(2, 0),
Tokens: these("member ", "nijika "),
},
{
ID: uuid.UUID{4},
ID: "4",
User: userhash.Hash{3},
Tag: "kessoku",
Time: time.Unix(3, 0),
Tokens: these("member ", "kita "),
},
{
ID: uuid.UUID{5},
ID: "5",
User: userhash.Hash{2},
Tag: "sickhack",
Time: time.Unix(0, 0),
Tokens: these("member ", "bocchi "),
},
{
ID: uuid.UUID{6},
ID: "6",
User: userhash.Hash{2},
Tag: "sickhack",
Time: time.Unix(1, 0),
Tokens: these("member ", "ryou "),
},
{
ID: uuid.UUID{7},
ID: "7",
User: userhash.Hash{3},
Tag: "sickhack",
Time: time.Unix(2, 0),
Tokens: these("member ", "nijika "),
},
{
ID: uuid.UUID{8},
ID: "8",
User: userhash.Hash{3},
Tag: "sickhack",
Time: time.Unix(3, 0),
Tokens: these("member ", "kita "),
},
{
ID: uuid.UUID{9},
ID: "9",
User: userhash.Hash{4},
Tag: "sickhack",
Time: time.Unix(43, 0),
Expand All @@ -107,7 +105,7 @@ var messages = [...]struct {
func learn(ctx context.Context, t *testing.T, br brain.Learner) {
t.Helper()
for _, m := range messages {
if err := brain.Learn(ctx, br, m.Tag, m.User, m.ID, m.Time, m.Tokens()); err != nil {
if err := brain.Learn(ctx, br, m.Tag, m.ID, m.User, m.Time, m.Tokens()); err != nil {
t.Fatalf("couldn't learn message %v: %v", m.ID, err)
}
}
Expand Down Expand Up @@ -266,8 +264,8 @@ func testCombinatoric(ctx context.Context, br brain.Brain) func(t *testing.T) {
for _, toks[5] = range band {
toks := toks
for len(toks) > 1 {
id := uuid.New()
err := brain.Learn(ctx, br, "bocchi", u, id, time.Unix(0, 0), toks)
id := randid()
err := brain.Learn(ctx, br, "bocchi", id, u, time.Unix(0, 0), toks)
if err != nil {
t.Fatalf("couldn't learn init: %v", err)
}
Expand Down
22 changes: 10 additions & 12 deletions brain/braintest/braintest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"testing"
"time"

"github.com/google/uuid"

"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/brain/braintest"
"github.com/zephyrtronium/robot/userhash"
Expand All @@ -20,29 +18,29 @@ import (
// to verify that the integration tests test the correct things.
type membrain struct {
mu sync.Mutex
tups map[string]map[string][]string // map of tags to map of prefixes to suffixes
users map[userhash.Hash][][3]string // map of hashes to tag and prefix+suffix
ids map[string]map[uuid.UUID][][2]string // map of tags to map of ids to prefix+suffix
tms map[string]map[int64][][2]string // map of tags to map of timestamps to prefix+suffix
tups map[string]map[string][]string // map of tags to map of prefixes to suffixes
users map[userhash.Hash][][3]string // map of hashes to tag and prefix+suffix
ids map[string]map[string][][2]string // map of tags to map of ids to prefix+suffix
tms map[string]map[int64][][2]string // map of tags to map of timestamps to prefix+suffix
}

var _ brain.Brain = (*membrain)(nil)

func (m *membrain) Learn(ctx context.Context, tag string, user userhash.Hash, id uuid.UUID, t time.Time, tuples []brain.Tuple) error {
func (m *membrain) Learn(ctx context.Context, tag, id string, user userhash.Hash, t time.Time, tuples []brain.Tuple) error {
m.mu.Lock()
defer m.mu.Unlock()
if m.tups == nil {
m.tups = make(map[string]map[string][]string)
m.users = make(map[userhash.Hash][][3]string)
m.ids = make(map[string]map[uuid.UUID][][2]string)
m.ids = make(map[string]map[string][][2]string)
m.tms = make(map[string]map[int64][][2]string)
}
if m.tups[tag] == nil {
m.tups[tag] = make(map[string][]string)
}
r := m.tups[tag]
if m.ids[tag] == nil {
m.ids[tag] = make(map[uuid.UUID][][2]string)
m.ids[tag] = make(map[string][][2]string)
}
ids := m.ids[tag]
if m.tms[tag] == nil {
Expand Down Expand Up @@ -79,14 +77,14 @@ func (m *membrain) Forget(ctx context.Context, tag string, tuples []brain.Tuple)
return nil
}

func (m *membrain) ForgetMessage(ctx context.Context, tag string, msg uuid.UUID) error {
func (m *membrain) ForgetMessage(ctx context.Context, tag, id string) error {
m.mu.Lock()
defer m.mu.Unlock()
u := m.ids[tag][msg]
u := m.ids[tag][id]
for _, v := range u {
m.forgetLocked(tag, v[0], v[1])
}
delete(m.ids[tag], msg)
delete(m.ids[tag], id)
return nil
}

Expand Down
15 changes: 7 additions & 8 deletions brain/kvbrain/forget.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/dgraph-io/badger/v4"
"github.com/google/uuid"

"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/userhash"
Expand All @@ -22,13 +21,13 @@ type past struct {

k uint8
key [256][][]byte
id [256]uuid.UUID
id [256]string
user [256]userhash.Hash
time [256]int64 // unix nano
}

// record associates a message with a knowledge key.
func (p *past) record(id uuid.UUID, user userhash.Hash, nanotime int64, keys [][]byte) {
func (p *past) record(id string, user userhash.Hash, nanotime int64, keys [][]byte) {
p.mu.Lock()
p.key[p.k] = slices.Grow(p.key[p.k][:0], len(keys))[:len(keys)]
for i, key := range keys {
Expand All @@ -42,12 +41,12 @@ func (p *past) record(id uuid.UUID, user userhash.Hash, nanotime int64, keys [][
}

// findID finds all keys corresponding to the given UUID.
func (p *past) findID(msg uuid.UUID) [][]byte {
func (p *past) findID(id string) [][]byte {
r := make([][]byte, 0, 64)
p.mu.Lock()
defer p.mu.Unlock()
for k, v := range p.id {
if v == msg {
if v == id {
keys := p.key[k]
r = slices.Grow(r, len(keys))
for _, v := range keys {
Expand Down Expand Up @@ -149,12 +148,12 @@ func (br *Brain) Forget(ctx context.Context, tag string, tuples []brain.Tuple) e

// ForgetMessage forgets everything learned from a single given message.
// If nothing has been learned from the message, it should be ignored.
func (br *Brain) ForgetMessage(ctx context.Context, tag string, msg uuid.UUID) error {
func (br *Brain) ForgetMessage(ctx context.Context, tag, id string) error {
past, _ := br.past.Load(tag)
if past == nil {
return nil
}
keys := past.findID(msg)
keys := past.findID(id)
batch := br.knowledge.NewWriteBatch()
defer batch.Cancel()
for _, key := range keys {
Expand All @@ -165,7 +164,7 @@ func (br *Brain) ForgetMessage(ctx context.Context, tag string, msg uuid.UUID) e
}
err := batch.Flush()
if err != nil {
return fmt.Errorf("couldn't commit deleting message %v: %w", msg, err)
return fmt.Errorf("couldn't commit deleting message %v: %w", id, err)
}
return nil
}
Expand Down
Loading

0 comments on commit 9e31d26

Please sign in to comment.