...
Code Block |
---|
func build(im typeutil.ImmutablemapString2string) *Builder { builder := &Builder{ reference: im, mutable: nil, } return builder } type Builder struct { reference typeutil.ImmutablemapString2string mutable map[string]string } func (bdr *Builder) maybeClone() { if !bdr.reference.IsEmpty() { bdr.mutable = make(map[string]string) for k, v := range bdr.reference.GetCopy() { bdr.mutable[k] = v } bdr.reference = typeutil.ImmutablemapString2string{} } else { bdr.mutable = make(map[string]string) } } func (bdr *Builder) getnamefromalias(key string) (string, bool) { bdr.maybeClone() rstr, rbool := bdr.mutable[key] return rstr, rbool } func (bdr *Builder) putalias2name(key string, val string) (string, bool) { bdr.maybeClone() var pre string v, ok := bdr.mutable[key] if ok { pre = v } else { pre = "" } bdr.mutable[key] = val return pre, ok } func (bdr *Builder) removealias2name(key string) (string, bool) { bdr.maybeClone() var pre string v, ok := bdr.mutable[key] if ok { pre = v delete(bdr.mutable, key) } else { pre = "" } return pre, ok } func (bdr *Builder) Build() typeutil.ImmutablemapString2string { if !bdr.reference.IsEmpty() { reference := bdr.reference bdr.reference = typeutil.ImmutablemapString2string{} return reference } mutable := bdr.mutable bdr.mutable = nil res := typeutil.NewImmutablemapString2string(mutable) return res } func (mt * metatable) addAlias(collectionAlias string, collectionName string) (error){ mt.ddLock.Lock() defer mt.ddLock.Unlock() ... ts = getTimestamp() tspre = mt.newestAliasTs Bdr = build(mt.ts2alias2name[tspre]) _,ok := Bdr.putalias2name(collectionAlias, collectionName) if ok{ return fmt.Errorf("alias already exist when add alias") } mt.ts2alias2name[ts] = Bdr.build() mt.newestAliasTs = ts ... return nil } func (mt * metatable) dropAlias(collectionAlias string) (error){ mt.ddLock.Lock() defer mt.ddLock.Unlock() ... ts = getTimestamp() tspre = mt.newestAliasTs Bdr = build(mt.ts2alias2name[tspre]) _, ok := Bdr.removealias2id(collectionAlias) if !ok{ return fmt.Errorf("alias not exist when drop alias") } mt.ts2alias2name[ts] = Bdr.build() mt.newestAliasTs = ts ... return nil } func (mt * metatable) alterAlias(collectionAlias string, collectionName string) (string, error){ mt.ddLock.Lock() defer mt.ddLock.Unlock() ... ts = getTimestamp() tspre = mt.newestAliasTs Bdr = build(mt.ts2alias2name[tspre]) pre_,ok := Bdr.putalias2name(collectionAlias, collectionName) if !ok{ return nil, fmt.Errorf("alias not exist when alter alias") } mt.ts2alias2name[ts] = Bdr.build() mt.newestAliasTs = ts ... return pre, nil } |
GetCollectionByName(), IsAlias(), ListAlias() ...
which is related to alias will add alias timestamp as parameter to select whichalias2name
map will be used ints2alias2name
.- Users can't drop the
collection
if thecollection
is referenced by analias
...