mirror of
https://github.com/restic/restic.git
synced 2024-12-22 15:57:07 +00:00
backend: Remove IDSet
This can easily be replaced by a map[{32}byte]struct{}, and this is much faster.
This commit is contained in:
parent
982f7e2379
commit
1c14be8993
2 changed files with 0 additions and 116 deletions
|
@ -1,75 +0,0 @@
|
|||
package backend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type IDSet struct {
|
||||
list IDs
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func NewIDSet() *IDSet {
|
||||
return &IDSet{
|
||||
list: make(IDs, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *IDSet) find(id ID) (int, error) {
|
||||
pos := sort.Search(len(s.list), func(i int) bool {
|
||||
return id.Compare(s.list[i]) >= 0
|
||||
})
|
||||
|
||||
if pos < len(s.list) {
|
||||
candID := s.list[pos]
|
||||
if id.Compare(candID) == 0 {
|
||||
return pos, nil
|
||||
}
|
||||
}
|
||||
|
||||
return pos, errors.New("ID not found")
|
||||
}
|
||||
|
||||
func (s *IDSet) insert(id ID) {
|
||||
pos, err := s.find(id)
|
||||
if err == nil {
|
||||
// already present
|
||||
return
|
||||
}
|
||||
|
||||
// insert blob
|
||||
// https://code.google.com/p/go-wiki/wiki/SliceTricks
|
||||
s.list = append(s.list, ID{})
|
||||
copy(s.list[pos+1:], s.list[pos:])
|
||||
s.list[pos] = id
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (s *IDSet) Insert(id ID) {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
|
||||
s.insert(id)
|
||||
}
|
||||
|
||||
func (s *IDSet) Find(id ID) error {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
|
||||
_, err := s.find(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *IDSet) Len() int {
|
||||
s.m.Lock()
|
||||
defer s.m.Unlock()
|
||||
|
||||
return len(s.list)
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package backend_test
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/backend"
|
||||
. "github.com/restic/restic/test"
|
||||
)
|
||||
|
||||
func randomID() []byte {
|
||||
buf := make([]byte, backend.IDSize)
|
||||
_, err := io.ReadFull(rand.Reader, buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
s := backend.NewIDSet()
|
||||
|
||||
testID := randomID()
|
||||
err := s.Find(testID)
|
||||
Assert(t, err != nil, "found test ID in IDSet before insertion")
|
||||
|
||||
for i := 0; i < 238; i++ {
|
||||
s.Insert(randomID())
|
||||
}
|
||||
|
||||
s.Insert(testID)
|
||||
OK(t, s.Find(testID))
|
||||
|
||||
for i := 0; i < 80; i++ {
|
||||
s.Insert(randomID())
|
||||
}
|
||||
|
||||
s.Insert(testID)
|
||||
OK(t, s.Find(testID))
|
||||
}
|
Loading…
Reference in a new issue