Browse Source

add UpdateSuffixarray and refactor TruncIndex

This commit refactors TruncIndex to make it possible to add container
ids to the TruncIndex without updating the Suffixarray.

This is useful during the Docker daemon's startup when we don't want to
update the Suffixarray for every container we add.

Add continues to function like before.

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
unclejack 11 năm trước cách đây
mục cha
commit
219b7ae8b5
1 tập tin đã thay đổi với 22 bổ sung3 xóa
  1. 22 3
      utils/utils.go

+ 22 - 3
utils/utils.go

@@ -493,9 +493,7 @@ func NewTruncIndex(ids []string) (idx *TruncIndex) {
 	return
 	return
 }
 }
 
 
-func (idx *TruncIndex) Add(id string) error {
-	idx.Lock()
-	defer idx.Unlock()
+func (idx *TruncIndex) addId(id string) error {
 	if strings.Contains(id, " ") {
 	if strings.Contains(id, " ") {
 		return fmt.Errorf("Illegal character: ' '")
 		return fmt.Errorf("Illegal character: ' '")
 	}
 	}
@@ -504,10 +502,31 @@ func (idx *TruncIndex) Add(id string) error {
 	}
 	}
 	idx.ids[id] = true
 	idx.ids[id] = true
 	idx.bytes = append(idx.bytes, []byte(id+" ")...)
 	idx.bytes = append(idx.bytes, []byte(id+" ")...)
+	return nil
+}
+
+func (idx *TruncIndex) Add(id string) error {
+	idx.Lock()
+	defer idx.Unlock()
+	if err := idx.addId(id); err != nil {
+		return err
+	}
 	idx.index = suffixarray.New(idx.bytes)
 	idx.index = suffixarray.New(idx.bytes)
 	return nil
 	return nil
 }
 }
 
 
+func (idx *TruncIndex) AddWithoutSuffixarrayUpdate(id string) error {
+	idx.Lock()
+	defer idx.Unlock()
+	return idx.addId(id)
+}
+
+func (idx *TruncIndex) UpdateSuffixarray() {
+	idx.Lock()
+	defer idx.Unlock()
+	idx.index = suffixarray.New(idx.bytes)
+}
+
 func (idx *TruncIndex) Delete(id string) error {
 func (idx *TruncIndex) Delete(id string) error {
 	idx.Lock()
 	idx.Lock()
 	defer idx.Unlock()
 	defer idx.Unlock()