Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set the minimum value for a seqset if known. #4075

Merged
merged 3 commits into from Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions server/avl/seqset.go
Expand Up @@ -62,17 +62,34 @@ func (ss *SequenceSet) Exists(seq uint64) bool {
return false
}

// SetInitialMin should be used to set the initial minimum sequence when known.
// This will more effectively utilize space versus self selecting.
// The set should be empty.
func (ss *SequenceSet) SetInitialMin(min uint64) error {
if !ss.IsEmpty() {
return ErrSetNotEmpty
}
ss.root, ss.nodes = &node{base: min, h: 1}, 1
return nil
}

// Delete will remove the sequence from the set.
// Wil optionally remove nodes and rebalance.
func (ss *SequenceSet) Delete(seq uint64) {
// Will optionally remove nodes and rebalance.
// Returns where the sequence was set.
func (ss *SequenceSet) Delete(seq uint64) bool {
if ss == nil || ss.root == nil {
return
return false
}
ss.root = ss.root.delete(seq, &ss.changed, &ss.nodes)
if ss.changed {
ss.changed = false
ss.size--
if ss.size == 0 {
ss.Empty()
}
return true
}
return false
}

// Size returns the number of items in the set.
Expand Down Expand Up @@ -228,7 +245,10 @@ func (ss SequenceSet) Encode(buf []byte) ([]byte, error) {
}

// ErrBadEncoding is returned when we can not decode properly.
var ErrBadEncoding = errors.New("ss: bad encoding")
var (
ErrBadEncoding = errors.New("ss: bad encoding")
ErrSetNotEmpty = errors.New("ss: set not empty")
)

func Decode(buf []byte) (*SequenceSet, error) {
if len(buf) < minLen || buf[0] != magic || buf[1] != version {
Expand Down
17 changes: 17 additions & 0 deletions server/avl/seqset_test.go
Expand Up @@ -218,6 +218,23 @@ func TestSeqSetUnion(t *testing.T) {
}
}

func TestSeqSetFirst(t *testing.T) {
var ss SequenceSet

seqs := []uint64{22, 222, 2222, 222_222}
for _, seq := range seqs {
// Normal case where we pick first/base.
ss.Insert(seq)
require_True(t, ss.root.base == (seq/numEntries)*numEntries)
ss.Empty()
// Where we set the minimum start value.
ss.SetInitialMin(seq)
ss.Insert(seq)
require_True(t, ss.root.base == seq)
ss.Empty()
}
}

func require_NoError(t *testing.T, err error) {
t.Helper()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions server/filestore.go
Expand Up @@ -2788,6 +2788,10 @@ func (fs *fileStore) removeMsg(seq uint64, secure, viaLimits, needFSLock bool) (
}
}
} else if !isEmpty {
if mb.dmap.IsEmpty() {
// Mark initial base for delete set.
mb.dmap.SetInitialMin(mb.first.seq)
}
// Out of order delete.
mb.dmap.Insert(seq)
// Check if <25% utilization and minimum size met.
Expand Down