Enable construction of TruncIndex from id array.

Fixes #5166

Current graph.restore is essentially O(n^2 log n) due to how
suffixarray creation works.

Rather than create/append/create new this supports creation from a seed
array of ids.

Functional testing shows this eliminates the hang on Creating image
graph reported on list.

Docker-DCO-1.1-Signed-off-by: Paul Nasrat <pnasrat@gmail.com> (github: pnasrat)
This commit is contained in:
Paul Nasrat
2014-04-11 16:39:58 -04:00
parent 931f065560
commit 4f169c2db5
4 changed files with 15 additions and 7 deletions
+8 -3
View File
@@ -426,12 +426,17 @@ type TruncIndex struct {
bytes []byte
}
func NewTruncIndex() *TruncIndex {
return &TruncIndex{
index: suffixarray.New([]byte{' '}),
func NewTruncIndex(ids []string) (idx *TruncIndex) {
idx = &TruncIndex{
ids: make(map[string]bool),
bytes: []byte{' '},
}
for _, id := range ids {
idx.ids[id] = true
idx.bytes = append(idx.bytes, []byte(id+" ")...)
}
idx.index = suffixarray.New(idx.bytes)
return
}
func (idx *TruncIndex) Add(id string) error {
+2 -1
View File
@@ -138,7 +138,8 @@ func TestRaceWriteBroadcaster(t *testing.T) {
// Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
func TestTruncIndex(t *testing.T) {
index := NewTruncIndex()
ids := []string{}
index := NewTruncIndex(ids)
// Get on an empty index
if _, err := index.Get("foobar"); err == nil {
t.Fatal("Get on an empty index should return an error")