github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/manager/state/store/object.go (about) 1 package store 2 3 import ( 4 "github.com/docker/swarmkit/api" 5 memdb "github.com/hashicorp/go-memdb" 6 ) 7 8 // ObjectStoreConfig provides the necessary methods to store a particular object 9 // type inside MemoryStore. 10 type ObjectStoreConfig struct { 11 Table *memdb.TableSchema 12 Save func(ReadTx, *api.StoreSnapshot) error 13 Restore func(Tx, *api.StoreSnapshot) error 14 ApplyStoreAction func(Tx, api.StoreAction) error 15 } 16 17 // RestoreTable takes a list of new objects of a particular type (e.g. clusters, 18 // nodes, etc., which conform to the StoreObject interface) and replaces the 19 // existing objects in the store of that type with the new objects. 20 func RestoreTable(tx Tx, table string, newObjects []api.StoreObject) error { 21 checkType := func(by By) error { 22 return nil 23 } 24 var oldObjects []api.StoreObject 25 appendResult := func(o api.StoreObject) { 26 oldObjects = append(oldObjects, o) 27 } 28 29 err := tx.find(table, All, checkType, appendResult) 30 if err != nil { 31 return nil 32 } 33 34 updated := make(map[string]struct{}) 35 36 for _, o := range newObjects { 37 objectID := o.GetID() 38 if existing := tx.lookup(table, indexID, objectID); existing != nil { 39 if err := tx.update(table, o); err != nil { 40 return err 41 } 42 updated[objectID] = struct{}{} 43 } else { 44 if err := tx.create(table, o); err != nil { 45 return err 46 } 47 } 48 } 49 for _, o := range oldObjects { 50 objectID := o.GetID() 51 if _, ok := updated[objectID]; !ok { 52 if err := tx.delete(table, objectID); err != nil { 53 return err 54 } 55 } 56 } 57 return nil 58 }