Skip to content

Commit

Permalink
count limits on space delete + fix group unref
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Mar 7, 2024
1 parent ce25081 commit f5e88eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions index/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (ri *redisIndex) spaceDelete(ctx context.Context, key Key, entry groupSpace
return spaceId == key.SpaceId
})

// in case of isolated space - return limit to the group
if entry.space.Limit != 0 {
entry.group.Limit += entry.space.Limit
}

_, err = ri.cl.Pipelined(ctx, func(pipe redis.Pipeliner) error {
entry.group.Save(ctx, pipe)
pipe.Del(ctx, sk)
Expand Down
5 changes: 5 additions & 0 deletions index/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func TestRedisIndex_SpaceDelete(t *testing.T) {
assert.NotEmpty(t, groupInfo.BytesUsage)
assert.Contains(t, groupInfo.SpaceIds, key.SpaceId)

require.NoError(t, fx.SetGroupLimit(ctx, key.GroupId, 5000))
require.NoError(t, fx.SetSpaceLimit(ctx, key, 4000))

ok, err = fx.SpaceDelete(ctx, key)
require.NoError(t, err)
assert.True(t, ok)
Expand All @@ -53,6 +56,8 @@ func TestRedisIndex_SpaceDelete(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, groupInfo.BytesUsage)
assert.NotContains(t, groupInfo.SpaceIds, key.SpaceId)
assert.Equal(t, uint64(5000), groupInfo.AccountLimit)
assert.Equal(t, uint64(5000), groupInfo.Limit)

// second call
ok, err = fx.SpaceDelete(ctx, key)
Expand Down
10 changes: 7 additions & 3 deletions index/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package index

import (
"context"
"strconv"
"strings"

"github.com/anyproto/any-sync/commonfile/fileproto/fileprotoerr"
Expand Down Expand Up @@ -153,20 +154,23 @@ func (op *spaceLimitOp) isolateSpace(ctx context.Context, entry groupSpaceEntry)
sk := spaceKey(key)
gk := groupKey(key)

keys, err := op.cl.HKeys(ctx, sk).Result()
keys, err := op.cl.HGetAll(ctx, sk).Result()
if err != nil {
return
}

var cids []cid.Cid
var cidRefs []int64
// collect all cids
for _, k := range keys {
for k, val := range keys {
if strings.HasPrefix(k, "c:") {
c, cErr := cid.Decode(k[2:])
if cErr != nil {
log.WarnCtx(ctx, "can't decode cid", zap.String("cid", k[2:]), zap.Error(cErr))
} else {
ref, _ := strconv.ParseInt(val, 10, 64)
cids = append(cids, c)
cidRefs = append(cidRefs, ref)
}
}
}
Expand All @@ -183,7 +187,7 @@ func (op *spaceLimitOp) isolateSpace(ctx context.Context, entry groupSpaceEntry)
var groupDecrResults = make([]*redis.IntCmd, len(cids))
if _, err = op.cl.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
for i, c := range cids {
groupDecrResults[i] = pipe.HIncrBy(ctx, gk, cidKey(c), -1)
groupDecrResults[i] = pipe.HIncrBy(ctx, gk, cidKey(c), -cidRefs[i])
}
return nil
}); err != nil {
Expand Down

0 comments on commit f5e88eb

Please sign in to comment.