Skip to content

Commit

Permalink
brain/*: change ForgetUserSince to ForgetUser
Browse files Browse the repository at this point in the history
Fixes #42.
  • Loading branch information
zephyrtronium committed Mar 26, 2024
1 parent 025e990 commit d66156a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 86 deletions.
15 changes: 7 additions & 8 deletions brain/kvbrain/forget.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func (p *past) findDuring(since, before int64) [][]byte {

// findUser finds all knowledge keys of messages recorded from a given user
// since a timestamp.
func (p *past) findUser(user userhash.Hash, since int64) [][]byte {
func (p *past) findUser(user userhash.Hash) [][]byte {
r := make([][]byte, 0, 64)
p.mu.Lock()
defer p.mu.Unlock()
for k, v := range p.time {
if since <= v && p.user[k] == user {
for k, v := range p.user {
if v == user {
keys := p.key[k]
r = slices.Grow(r, len(keys))
for _, v := range keys {
Expand Down Expand Up @@ -191,13 +191,12 @@ func (br *Brain) ForgetDuring(ctx context.Context, tag string, since, before tim
return nil
}

// ForgetUserSince forgets all messages learned from a user since a given
// time.
func (br *Brain) ForgetUserSince(ctx context.Context, user *userhash.Hash, since time.Time) error {
// ForgetUser forgets all messages associated with a userhash.
func (br *Brain) ForgetUser(ctx context.Context, user *userhash.Hash) error {
var rangeErr error
u := *user
br.past.Range(func(tag string, past *past) bool {
keys := past.findUser(u, since.UnixNano())
keys := past.findUser(u)
if len(keys) == 0 {
return true
}
Expand All @@ -212,7 +211,7 @@ func (br *Brain) ForgetUserSince(ctx context.Context, user *userhash.Hash, since
}
err := batch.Flush()
if err != nil {
rangeErr = fmt.Errorf("couldn't commit deleting messages from user since %v: %w", since, err)
rangeErr = fmt.Errorf("couldn't commit deleting messages by user: %w", err)
return false
}
return false
Expand Down
7 changes: 4 additions & 3 deletions brain/kvbrain/forget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ func TestPastFindUser(t *testing.T) {
},
}
want := [][]byte{
[]byte("bocchi"),
[]byte("ryou"),
}
got := p.findUser(userhash.Hash{8}, 5)
got := p.findUser(userhash.Hash{8})
if !slices.EqualFunc(got, want, bytes.Equal) {
t.Errorf("wrong result: want %q, got %q", want, got)
}
Expand Down Expand Up @@ -202,7 +203,7 @@ func BenchmarkPastFindUser(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := range b.N {
use(p.findUser(userhash.Hash{byte(i)}, 0))
use(p.findUser(userhash.Hash{byte(i)}))
}
}

Expand Down Expand Up @@ -750,7 +751,7 @@ func TestForgetUserSince(t *testing.T) {
t.Errorf("failed to learn: %v", err)
}
}
if err := br.ForgetUserSince(ctx, &c.user, time.Unix(0, 0)); err != nil {
if err := br.ForgetUser(ctx, &c.user); err != nil {
t.Errorf("failed to forget from user %02x: %v", c.user, err)
}
dbcheck(t, db, c.want)
Expand Down
5 changes: 2 additions & 3 deletions brain/learn.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ type Learner interface {
ForgetMessage(ctx context.Context, tag string, msg uuid.UUID) error
// ForgetDuring forgets all messages learned in the given time span.
ForgetDuring(ctx context.Context, tag string, since, before time.Time) error
// ForgetUserSince forgets all messages learned from a user since a given
// time.
ForgetUserSince(ctx context.Context, user *userhash.Hash, since time.Time) error
// ForgetUser forgets all messages associated with a userhash.
ForgetUser(ctx context.Context, user *userhash.Hash) error
}

// Learn records tokens into a Learner.
Expand Down
2 changes: 1 addition & 1 deletion brain/learn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (t *testLearner) ForgetDuring(ctx context.Context, tag string, since, befor
return nil
}

func (t *testLearner) ForgetUserSince(ctx context.Context, user *userhash.Hash, since time.Time) error {
func (t *testLearner) ForgetUser(ctx context.Context, user *userhash.Hash) error {
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions brain/sqlbrain/forget.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (br *Brain) ForgetDuring(ctx context.Context, tag string, since, before tim
return nil
}

// ForgetUserSince removes tuples learned from the given user hash since a
// given time. The delete reason is set to "CLEARCHAT".
func (br *Brain) ForgetUserSince(ctx context.Context, user *userhash.Hash, since time.Time) error {
_, err := br.db.Exec(ctx, `UPDATE Message SET deleted='CLEARCHAT' WHERE user = ? AND time >= ?`, user[:], since.UnixMilli())
// ForgetUser removes tuples learned from the given user hash.
// The delete reason is set to "CLEARCHAT".
func (br *Brain) ForgetUser(ctx context.Context, user *userhash.Hash) error {
_, err := br.db.Exec(ctx, `UPDATE Message SET deleted='CLEARCHAT' WHERE user = ?`, user[:])
if err != nil {
return fmt.Errorf("couldn't forget messages from %x since %v: %w", user, since, err)
return fmt.Errorf("couldn't forget messages from %x: %w", user, err)
}
return nil
}
Expand Down
77 changes: 11 additions & 66 deletions brain/sqlbrain/forget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,6 @@ func TestForgetUserSince(t *testing.T) {
order int
insert []insert
user userhash.Hash
since int64
left []remain
}{
{
Expand All @@ -1015,9 +1014,8 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 1},
since: 1,
left: nil,
user: userhash.Hash{0: 1},
left: nil,
},
{
name: "multiple-1",
Expand All @@ -1033,9 +1031,8 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 1},
since: 1,
left: nil,
user: userhash.Hash{0: 1},
left: nil,
},
{
name: "user-1",
Expand All @@ -1050,32 +1047,7 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 2},
since: 1,
left: []remain{
{
tag: "madoka",
tuples: []brain.Tuple{
{Prefix: []string{"a"}, Suffix: "b"},
},
},
},
},
{
name: "time-1",
order: 1,
insert: []insert{
{
tag: "madoka",
user: userhash.Hash{0: 1},
time: 2,
tuples: []brain.Tuple{
{Prefix: []string{"a"}, Suffix: "b"},
},
},
},
user: userhash.Hash{0: 1},
since: 3,
user: userhash.Hash{0: 2},
left: []remain{
{
tag: "madoka",
Expand All @@ -1098,9 +1070,8 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 1},
since: 1,
left: nil,
user: userhash.Hash{0: 1},
left: nil,
},
{
name: "multiple-2",
Expand All @@ -1116,9 +1087,8 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 1},
since: 1,
left: nil,
user: userhash.Hash{0: 1},
left: nil,
},
{
name: "user-2",
Expand All @@ -1133,32 +1103,7 @@ func TestForgetUserSince(t *testing.T) {
},
},
},
user: userhash.Hash{0: 2},
since: 1,
left: []remain{
{
tag: "madoka",
tuples: []brain.Tuple{
{Prefix: []string{"a", "b"}, Suffix: "c"},
},
},
},
},
{
name: "time-2",
order: 2,
insert: []insert{
{
tag: "madoka",
user: userhash.Hash{0: 1},
time: 2,
tuples: []brain.Tuple{
{Prefix: []string{"a", "b"}, Suffix: "c"},
},
},
},
user: userhash.Hash{0: 1},
since: 3,
user: userhash.Hash{0: 2},
left: []remain{
{
tag: "madoka",
Expand Down Expand Up @@ -1199,7 +1144,7 @@ func TestForgetUserSince(t *testing.T) {
t.Fatalf("wrong tuples added before test (+got/-want):\n%s", diff)
}
}
if err := br.ForgetUserSince(ctx, &c.user, time.UnixMilli(c.since)); err != nil {
if err := br.ForgetUser(ctx, &c.user); err != nil {
t.Errorf("couldn't forget: %v", err)
}
var wantTags []string
Expand Down

0 comments on commit d66156a

Please sign in to comment.