mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +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) {
|
||||
var err error
|
||||
|
||||
c.mu.Lock()
|
||||
keyPrefix := Key(kvObject.KeyPrefix()...)
|
||||
kmap, ok := c.kmm[keyPrefix]
|
||||
@@ -52,8 +50,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
|
||||
}
|
||||
|
||||
dstO := kvObject.New()
|
||||
err = dstO.SetValue(kvPair.Value)
|
||||
if err != nil {
|
||||
if err := dstO.SetValue(kvPair.Value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -116,19 +113,18 @@ func (c *cache) del(kvObject KVObject, atomic bool) error {
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
// If atomic is true, cache needs to maintain its own index
|
||||
// for atomicity and del needs to be atomic.
|
||||
if atomic {
|
||||
if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
|
||||
if prev.Index() != kvObject.Index() {
|
||||
c.mu.Unlock()
|
||||
return ErrKeyModified
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete(kmap, Key(kvObject.Key()...))
|
||||
c.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -77,7 +77,6 @@ type dummyObject struct {
|
||||
ID string
|
||||
DBIndex uint64
|
||||
DBExists bool
|
||||
SkipSave bool
|
||||
ReturnValue bool
|
||||
}
|
||||
|
||||
@@ -119,27 +118,34 @@ func (n *dummyObject) Exists() 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) {
|
||||
return json.Marshal(map[string]any{
|
||||
"name": n.Name,
|
||||
"networkType": n.NetworkType,
|
||||
"enableIPv6": n.EnableIPv6,
|
||||
"generic": n.Generic,
|
||||
return json.Marshal(tmpStruct{
|
||||
Name: n.Name,
|
||||
NetworkType: n.NetworkType,
|
||||
EnableIPv6: n.EnableIPv6,
|
||||
Generic: n.Generic,
|
||||
})
|
||||
}
|
||||
|
||||
func (n *dummyObject) UnmarshalJSON(b []byte) error {
|
||||
var netMap map[string]any
|
||||
var netMap tmpStruct
|
||||
if err := json.Unmarshal(b, &netMap); err != nil {
|
||||
return err
|
||||
}
|
||||
n.Name = netMap["name"].(string)
|
||||
n.NetworkType = netMap["networkType"].(string)
|
||||
n.EnableIPv6 = netMap["enableIPv6"].(bool)
|
||||
n.Generic = netMap["generic"].(map[string]any)
|
||||
n.Name = netMap.Name
|
||||
n.NetworkType = netMap.NetworkType
|
||||
n.EnableIPv6 = netMap.EnableIPv6
|
||||
n.Generic = netMap.Generic
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -199,22 +205,23 @@ func (r *recStruct) Skip() bool {
|
||||
}
|
||||
|
||||
func dummyKVObject(id string, retValue bool) *dummyObject {
|
||||
cDict := map[string]string{
|
||||
"foo": "bar",
|
||||
"hello": "world",
|
||||
}
|
||||
return &dummyObject{
|
||||
Name: "testNw",
|
||||
NetworkType: "bridge",
|
||||
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,
|
||||
DBIndex: 0,
|
||||
ReturnValue: retValue,
|
||||
DBExists: false,
|
||||
SkipSave: false,
|
||||
Generic: map[string]any{
|
||||
"label1": &recStruct{Name: "value1", Field1: 1, Dict: cDict},
|
||||
Generic: options.Generic{
|
||||
"label1": &recStruct{Name: "value1", Field1: 1, Dict: map[string]string{
|
||||
"foo": "bar",
|
||||
"hello": "world",
|
||||
}},
|
||||
"label2": "subnet=10.1.1.0/16",
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user