func (p *P) UnmarshalBinary(data []byte) error { b := bytes.NewBuffer(data) _, err := fmt.Fscanln(b, &p.X, &p.Y, &p.Z) _, err = fmt.Fscanln(b, &p.Name) _, err = fmt.Fscanln(b, &p.q.X, &p.q.Y, &p.q.Name) var len int = 0 _, err = fmt.Fscanln(b, &len) p.mymap = make(map[int]Q) p.myset = treeset.NewWithStringComparator() for i := 0; i < len; i++ { var key int var val Q _, err = fmt.Fscanln(b, &key, &val.X, &val.Y, &val.Name) p.mymap[key] = val } var setlen int = 0 _, err = fmt.Fscanln(b, &setlen) p.myset = treeset.NewWithStringComparator() for j := 0; j < setlen; j++ { var val string _, err = fmt.Fscanln(b, &val) p.myset.Add(val) } /* Skipping UnMarshalling of lock - not needed */ return err }
// IteratorWithIndexExample to demonstrate basic usage of IteratorWithIndex func IteratorWithIndexExample() { set := treeset.NewWithStringComparator() set.Add("a", "b", "c") it := set.Iterator() fmt.Print("\nForward iteration\n") for it.Next() { index, value := it.Index(), it.Value() fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c] } fmt.Print("\nForward iteration (again)\n") for it.Begin(); it.Next(); { index, value := it.Index(), it.Value() fmt.Print("[", index, ":", value, "]") // [0:a][1:b][2:c] } fmt.Print("\nBackward iteration\n") for it.Prev() { index, value := it.Index(), it.Value() fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a] } fmt.Print("\nBackward iteration (again)\n") for it.End(); it.Prev(); { index, value := it.Index(), it.Value() fmt.Print("[", index, ":", value, "]") // [2:c][1:b][0:a] } if it.First() { fmt.Print("\nFirst index: ", it.Index()) // First index: 0 fmt.Print("\nFirst value: ", it.Value()) // First value: a } if it.Last() { fmt.Print("\nLast index: ", it.Index()) // Last index: 3 fmt.Print("\nLast value: ", it.Value()) // Last value: c } }
/* You may assume single [score member] inserts. */ func (store *db) ZADD(key string, zaddMap *map[string]int) (int, error) { var memberAdded int = 0 if store == nil { fmt.Println("ZADD : store is nil") return 0, errors.New(fmt.Sprint("ZADD : store is nil")) } if zaddMap == nil { fmt.Println("ZADD : argument zaddMap is nil") return 0, errors.New(fmt.Sprint("ZADD : Internal error")) } var entry *setmapData var ok bool store.setmapDBLock.RLock() /* Check if db has the key entry */ entry, ok = store.setmapEntry[key] /* If entry not present */ if ok == false { /* Take DB lock before creating entry for this key, to ensure only one entry gets created */ store.setmapDBLock.RUnlock() store.setmapDBLock.Lock() /* Again Check if db has the key entry, between above if & db lock it is possible other routine has created this entry */ var okrecheck bool entry, okrecheck = store.setmapEntry[key] if okrecheck == false { entry = &setmapData{ setEntry: make(map[int]*treeset.Set), lock: &sync.RWMutex{}, } /* ToDo : Handle allocation failure of struct, now sure how todo in Go */ } } /* Take DB entry lock before set, this is lock per key entry */ entry.lock.Lock() for member, score := range *zaddMap { /* Remove if same member exist at any score */ for _, value := range entry.setEntry { if true == value.Contains(member) { memberAdded = memberAdded - 1 value.Remove(member) } } /* Insert the score member pair */ if entry.setEntry[score] == nil { entry.setEntry[score] = treeset.NewWithStringComparator() } entry.setEntry[score].Add(member) memberAdded = memberAdded + 1 } store.setmapEntry[key] = entry entry.lock.Unlock() /* Both locks got released in LIFO order, entry lock followed by db lock */ if ok == false { store.setmapDBLock.Unlock() } else { store.setmapDBLock.RUnlock() } return memberAdded, nil }
func (store *db) UnmarshalBinary(data []byte) error { if store == nil { return errors.New(fmt.Sprintf("UnmarshalBinary : store nil")) } if store.mapEntry == nil { return errors.New(fmt.Sprintf("UnmarshalBinary : store.mapEntry nil")) } if store.setmapEntry == nil { return errors.New(fmt.Sprintf("UnmarshalBinary : store.setmapEntry nil")) } b := bytes.NewBuffer(data) //UnMarshal mapEntry var len int = 0 _, err := fmt.Fscanln(b, &len) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : mapEntry len nil")) } for i := 0; i < len; i++ { var key string var value string var e int64 _, err = fmt.Fscanln(b, &key) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : mapEntry key nil")) } _, err = fmt.Fscanln(b, &value) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : mapEntry val nil")) } _, err = fmt.Fscanln(b, &e) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : mapEntry expiration nil")) } mapEntry := &mapData{ val: value, Expiration: e, lock: &sync.RWMutex{}, } //UnMarshal mapData.lock skipped - not required store.mapEntry[key] = mapEntry } //UnMarshal setmapEntry len = 0 _, err = fmt.Fscanln(b, &len) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry len nil")) } for i := 0; i < len; i++ { var key string var len2 int var key2 int _, err = fmt.Fscanln(b, &key) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry key nil")) } _, err = fmt.Fscanln(b, &len2) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry key : %v setEntry len2 nil", key)) } setmapEntry := &setmapData{ setEntry: make(map[int]*treeset.Set), lock: &sync.RWMutex{}, } for k := 0; k < len2; k++ { _, err = fmt.Fscanln(b, &key2) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry key : %v setEntry len2 : %v key nil", key, len2)) } setmapEntry.setEntry[key2] = treeset.NewWithStringComparator() var setlen int = 0 _, err = fmt.Fscanln(b, &setlen) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry key : %v setEntry len nil", key)) } for j := 0; j < setlen; j++ { var val string _, err = fmt.Fscanln(b, &val) if err != nil { return errors.New(fmt.Sprintf("UnmarshalBinary : setmapEntry key : %v setEntry len : %v key2 : %v value nil for cursetIndex : %v ", key, setlen, key2, j)) } setmapEntry.setEntry[key2].Add(val) } } //UnMarshal setmapData.lock skipped - not required store.setmapEntry[key] = setmapEntry } //UnMarshal setmapDBLock skipped - not required return err }
func main() { var filename string = "integerdata.gob" p := P{X: 3, Y: 4, Z: 5, Name: "Pythagoras", mymap: make(map[int]Q), q: Q{10, 11, "Trad"}, myset: treeset.NewWithStringComparator()} p.lock.Lock() p.mymap[1] = Q{6, 7, "Quad"} p.mymap[2] = Q{8, 9, "R"} p.myset.Add("A") p.myset.Add("B") save(filename, &p) var q P load(filename, &q) fmt.Println(" save : ", p) fmt.Println(" save treeset : ", p.myset.Values()) fmt.Println(" load : ", q) fmt.Println(" load treeset : ", q.myset.Values()) }