mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
libnetwork/datastore: small cleanups
- Use an intermediate struct for (un)marshaling dummyObject - Remove dummyObject.SkipSave as it would always be set to "false" (i.e., persisted to disk). - Minor cleanups in handling locks and some unused vars Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -21,8 +21,6 @@ func newCache(ds store.Store) *cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
||||||
var err error
|
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
keyPrefix := Key(kvObject.KeyPrefix()...)
|
keyPrefix := Key(kvObject.KeyPrefix()...)
|
||||||
kmap, ok := c.kmm[keyPrefix]
|
kmap, ok := c.kmm[keyPrefix]
|
||||||
@@ -52,8 +50,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dstO := kvObject.New()
|
dstO := kvObject.New()
|
||||||
err = dstO.SetValue(kvPair.Value)
|
if err := dstO.SetValue(kvPair.Value); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,19 +113,18 @@ func (c *cache) del(kvObject KVObject, atomic bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
// If atomic is true, cache needs to maintain its own index
|
// If atomic is true, cache needs to maintain its own index
|
||||||
// for atomicity and del needs to be atomic.
|
// for atomicity and del needs to be atomic.
|
||||||
if atomic {
|
if atomic {
|
||||||
if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
|
if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
|
||||||
if prev.Index() != kvObject.Index() {
|
if prev.Index() != kvObject.Index() {
|
||||||
c.mu.Unlock()
|
|
||||||
return ErrKeyModified
|
return ErrKeyModified
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(kmap, Key(kvObject.Key()...))
|
delete(kmap, Key(kvObject.Key()...))
|
||||||
c.mu.Unlock()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ type dummyObject struct {
|
|||||||
ID string
|
ID string
|
||||||
DBIndex uint64
|
DBIndex uint64
|
||||||
DBExists bool
|
DBExists bool
|
||||||
SkipSave bool
|
|
||||||
ReturnValue bool
|
ReturnValue bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,27 +118,34 @@ func (n *dummyObject) Exists() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *dummyObject) Skip() bool {
|
func (n *dummyObject) Skip() bool {
|
||||||
return n.SkipSave
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type tmpStruct struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
NetworkType string `json:"networkType"`
|
||||||
|
EnableIPv6 bool `json:"enableIPv6"`
|
||||||
|
Generic options.Generic `json:"generic"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *dummyObject) MarshalJSON() ([]byte, error) {
|
func (n *dummyObject) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(tmpStruct{
|
||||||
"name": n.Name,
|
Name: n.Name,
|
||||||
"networkType": n.NetworkType,
|
NetworkType: n.NetworkType,
|
||||||
"enableIPv6": n.EnableIPv6,
|
EnableIPv6: n.EnableIPv6,
|
||||||
"generic": n.Generic,
|
Generic: n.Generic,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *dummyObject) UnmarshalJSON(b []byte) error {
|
func (n *dummyObject) UnmarshalJSON(b []byte) error {
|
||||||
var netMap map[string]any
|
var netMap tmpStruct
|
||||||
if err := json.Unmarshal(b, &netMap); err != nil {
|
if err := json.Unmarshal(b, &netMap); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n.Name = netMap["name"].(string)
|
n.Name = netMap.Name
|
||||||
n.NetworkType = netMap["networkType"].(string)
|
n.NetworkType = netMap.NetworkType
|
||||||
n.EnableIPv6 = netMap["enableIPv6"].(bool)
|
n.EnableIPv6 = netMap.EnableIPv6
|
||||||
n.Generic = netMap["generic"].(map[string]any)
|
n.Generic = netMap.Generic
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,22 +205,23 @@ func (r *recStruct) Skip() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dummyKVObject(id string, retValue bool) *dummyObject {
|
func dummyKVObject(id string, retValue bool) *dummyObject {
|
||||||
cDict := map[string]string{
|
|
||||||
"foo": "bar",
|
|
||||||
"hello": "world",
|
|
||||||
}
|
|
||||||
return &dummyObject{
|
return &dummyObject{
|
||||||
Name: "testNw",
|
Name: "testNw",
|
||||||
NetworkType: "bridge",
|
NetworkType: "bridge",
|
||||||
EnableIPv6: true,
|
EnableIPv6: true,
|
||||||
Rec: &recStruct{Name: "gen", Field1: 5, Dict: cDict},
|
Rec: &recStruct{Name: "gen", Field1: 5, Dict: map[string]string{
|
||||||
|
"foo": "bar",
|
||||||
|
"hello": "world",
|
||||||
|
}},
|
||||||
ID: id,
|
ID: id,
|
||||||
DBIndex: 0,
|
DBIndex: 0,
|
||||||
ReturnValue: retValue,
|
ReturnValue: retValue,
|
||||||
DBExists: false,
|
DBExists: false,
|
||||||
SkipSave: false,
|
Generic: options.Generic{
|
||||||
Generic: map[string]any{
|
"label1": &recStruct{Name: "value1", Field1: 1, Dict: map[string]string{
|
||||||
"label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
|
"foo": "bar",
|
||||||
|
"hello": "world",
|
||||||
|
}},
|
||||||
"label2": "subnet=10.1.1.0/16",
|
"label2": "subnet=10.1.1.0/16",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user