Skip to content

Commit d281fa9

Browse files
committed
storage: extract id or name conflict check into function
Make it reusable for other callers, see next commit. Also while at it remove the dedupeStrings() call, as pointed out by Miloslav the work it is doing is more expensive than just checking the same name several times as it does a O(1) map lookup. Also most callers won't pass duplicated names to begin with. Signed-off-by: Paul Holzinger <[email protected]>
1 parent c46c3ae commit d281fa9

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

storage/layers.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,23 @@ func (r *layerStore) pickStoreLocation(volatile, writeable bool) layerLocations
14221422
}
14231423
}
14241424

1425+
// checkIdOrNameConfict checks if the id or names are already in use and returns an
1426+
// error in that case. As Special case if the layer already exists it returns it as
1427+
// well together with the error.
1428+
//
1429+
// Requires startWriting.
1430+
func (r *layerStore) checkIdOrNameConfict(id string, names []string) (*Layer, error) {
1431+
if duplicateLayer, idInUse := r.byid[id]; idInUse {
1432+
return duplicateLayer, ErrDuplicateID
1433+
}
1434+
for _, name := range names {
1435+
if _, nameInUse := r.byname[name]; nameInUse {
1436+
return nil, ErrDuplicateName
1437+
}
1438+
}
1439+
return nil, nil
1440+
}
1441+
14251442
// Requires startWriting.
14261443
func (r *layerStore) create(id string, parentLayer *Layer, names []string, mountLabel string, options map[string]string, moreOptions *LayerOptions, writeable bool, slo *stagedLayerOptions) (layer *Layer, size int64, err error) {
14271444
if moreOptions == nil {
@@ -1444,14 +1461,8 @@ func (r *layerStore) create(id string, parentLayer *Layer, names []string, mount
14441461
_, idInUse = r.byid[id]
14451462
}
14461463
}
1447-
if duplicateLayer, idInUse := r.byid[id]; idInUse {
1448-
return duplicateLayer, -1, ErrDuplicateID
1449-
}
1450-
names = dedupeStrings(names)
1451-
for _, name := range names {
1452-
if _, nameInUse := r.byname[name]; nameInUse {
1453-
return nil, -1, ErrDuplicateName
1454-
}
1464+
if layer, err := r.checkIdOrNameConfict(id, names); err != nil {
1465+
return layer, -1, err
14551466
}
14561467
parent := ""
14571468
if parentLayer != nil {

0 commit comments

Comments
 (0)