Skip to content

Commit

Permalink
brain/kvbrain: hash tag in keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Mar 9, 2024
1 parent c6925f3 commit 388f13d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 8 additions & 3 deletions brain/kvbrain/kvbrain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kvbrain

import (
"context"
"hash/fnv"
"io"
"time"

"github.com/dgraph-io/badger/v4"
Expand Down Expand Up @@ -48,9 +50,6 @@ func New(knowledge *badger.DB) *Brain {
}
}

// tagBytes is the number of bytes used to record tags in the KV database.
const tagBytes = 8 // TODO(zeph): we should just use a hash instead

// Order returns the number of elements in the prefix of a chain. It is
// called once at the beginning of learning. The returned value must always
// be at least 1.
Expand Down Expand Up @@ -83,3 +82,9 @@ func (br *Brain) ForgetDuring(ctx context.Context, tag string, since, before tim
func (br *Brain) ForgetUserSince(ctx context.Context, user *userhash.Hash, since time.Time) error {
panic("not implemented") // TODO: Implement
}

func hashTag(tag string) uint64 {
h := fnv.New64a()
io.WriteString(h, tag)
return h.Sum64()
}
5 changes: 3 additions & 2 deletions brain/kvbrain/learn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kvbrain
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"slices"
Expand Down Expand Up @@ -33,8 +34,8 @@ func (br *Brain) Learn(ctx context.Context, meta *brain.MessageMeta, tuples []br
for i, t := range tuples {
b.Reset()
// Write the tag.
u := make([]byte, tagBytes)
copy(u, meta.Tag)
u := make([]byte, 8)
binary.LittleEndian.PutUint64(u, hashTag(meta.Tag))
b.Write(u)
// Write prefixes.
k := slices.IndexFunc(t.Prefix, func(s string) bool { return s != "" })
Expand Down
5 changes: 3 additions & 2 deletions brain/kvbrain/learn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kvbrain

import (
"context"
"encoding/binary"
"testing"
"time"

Expand All @@ -14,8 +15,8 @@ import (

func TestLearn(t *testing.T) {
mkey := func(tag, toks string, id uuid.UUID) string {
b := make([]byte, tagBytes, tagBytes+len(toks)+len(id))
copy(b, tag)
b := make([]byte, 8, 8+len(toks)+len(id))
binary.LittleEndian.PutUint64(b, hashTag(tag))
b = append(b, toks...)
b = append(b, id[:]...)
return string(b)
Expand Down

0 comments on commit 388f13d

Please sign in to comment.