func (ts *Tribserver) GetTribbles(args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { //check errors for any calls to libstore //check if user exists _, userErr := ts.lstore.Get(args.Userid) if userErr != nil { reply.Status = tribproto.ENOSUCHUSER reply.Tribbles = nil return nil } //else //getList(user:timestamps) => timestamps, listErr := ts.lstore.GetList(args.Userid + ":timestamps") //if there's an error in retrieving a list, it means there are no tribbles if listErr != nil { reply.Status = tribproto.OK reply.Tribbles = []tribproto.Tribble{} return nil } //for 100 tribbles (or up to 100 tribbles) at end of array (newest pushed to end) numTribs := int(math.Min(100, float64(len(timestamps)))) // get(tribble ID) and push onto tribbles array var tribbles []tribproto.Tribble var i int count := numTribs for i = len(timestamps) - 1; count > 0; i-- { jtrib, err2 := ts.lstore.Get(args.Userid + ":" + timestamps[i]) if err2 != nil { return err2 } var trib tribproto.Tribble jtribBytes := []byte(jtrib) jerr := json.Unmarshal(jtribBytes, &trib) if jerr != nil { return jerr } tribbles = append(tribbles, trib) count-- } //reply = OK, tribbles array reply.Status = tribproto.OK reply.Tribbles = tribbles //return nil return nil }
func (ts *Tribserver) GetTribblesBySubscription(args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { // check that user exists _, err := ts.ls.Get(args.Userid) if err != nil { reply.Status = tribproto.ENOSUCHUSER reply.Tribbles = nil return nil } // get the list of subscriptions subsid := fmt.Sprintf("%s:subs", args.Userid) var sublist []string sublist, err = ts.ls.GetList(subsid) if err != nil { return err } // for each target user, get his last 100 tribbles and append them to each other allTribbles := make([]string, 0, 100*len(sublist)) for i := range sublist { id := fmt.Sprintf("%s:posts", sublist[i]) tribids, _ := ts.ls.GetList(id) if reply.Status == tribproto.OK { for i := range tribids { allTribbles = append(allTribbles, tribids[i]) } } } // sort the list of all posts sort.Sort(TribbleList(allTribbles)) // take the 100 most recent posts var triblist []string length := len(allTribbles) if length <= 100 { reply.Tribbles = make([]tribproto.Tribble, length) triblist = allTribbles } else { reply.Tribbles = make([]tribproto.Tribble, 100) triblist = allTribbles[0:100] } // get the tribbles contents for i := range triblist { mval, e := ts.ls.Get(triblist[i]) if e != nil { return e } e = json.Unmarshal([]byte(mval), &reply.Tribbles[i]) if e != nil { return e } } reply.Status = tribproto.OK return nil }
/**@brief collect all tribbles from all users followed * @param CreateUserArgs * @param CreateUserReply * @return error */ func (ts *Tribserver) GetTribblesBySubscription( args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { var fllw_key string var fllw_ids []string var err error var getArgs tribproto.GetTribblesArgs var getReply tribproto.GetTribblesReply var tribs Tribs fllw_key = fmt.Sprintf("%s:F", args.Userid) fllw_ids, err = ts.Store.GetList(fllw_key) if lsplog.CheckReport(1, err) { reply.Status = tribproto.ENOSUCHUSER reply.Tribbles = nil return nil } fmt.Printf("complete geting list %d\n", len(fllw_ids)) reply.Status = tribproto.OK for i := 0; i < len(fllw_ids); i++ { getArgs.Userid = fllw_ids[i] err = ts.GetTribbles(&getArgs, &getReply) fmt.Printf("try geting subscription user %d\n", i+1) if lsplog.CheckReport(1, err) { reply.Status = tribproto.ENOSUCHTARGETUSER reply.Tribbles = nil return nil } reply.Tribbles = append(reply.Tribbles, getReply.Tribbles...) } fmt.Printf("complete getting all subscribed tribs\n") //to satisfy go compiler type check tribs = reply.Tribbles //sort in time order sort.Sort(tribs) if len(reply.Tribbles) > 100 { reply.Tribbles = reply.Tribbles[:100] } return nil }
func (ts *Tribserver) GetTribbles(args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { // check that user exists _, err := ts.ls.Get(args.Userid) if err != nil { reply.Status = tribproto.ENOSUCHUSER reply.Tribbles = nil return nil } // get the list of tribbles postsid := fmt.Sprintf("%s:posts", args.Userid) var postlist []string postlist, err = ts.ls.GetList(postsid) if err != nil { return err } // get the last 100 tribbles ids var triblist []string max := len(postlist) if max <= 100 { triblist = make([]string, max) for i := range postlist { triblist[max-i-1] = postlist[i] } reply.Tribbles = make([]tribproto.Tribble, max) } else { triblist = make([]string, 100) cutlist := postlist[max-100 : max] for i := range cutlist { triblist[99-i] = cutlist[i] } reply.Tribbles = make([]tribproto.Tribble, 100) } // get the tribbles contents for i := range triblist { mval, e := ts.ls.Get(triblist[i]) if e != nil { return e } e = json.Unmarshal([]byte(mval), &reply.Tribbles[i]) if e != nil { return e } } reply.Status = tribproto.OK return nil }
/**@brief get posted tribbles * @param GetTribblesArgs * @param GetTribblesReply * @return error */ func (ts *Tribserver) GetTribbles( args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { var trib_key string var trib_ids []string var trib_enc string var err error var length int trib_key = fmt.Sprintf("%s:T", args.Userid) trib_ids, err = ts.Store.GetList(trib_key) if lsplog.CheckReport(1, err) { reply.Status = tribproto.ENOSUCHUSER reply.Tribbles = nil return nil } reply.Status = tribproto.OK if len(trib_ids) > 100 { length = 100 } else { length = len(trib_ids) } reply.Tribbles = make([]tribproto.Tribble, length) for i := 0; i < length; i++ { trib_enc, err = ts.Store.Get(trib_ids[len(trib_ids)-1-i]) if lsplog.CheckReport(1, err) { return lsplog.MakeErr("Get Tribbles Message Error") } //fmt.Printf("unmarshal string %s\n", trib_enc) _ = json.Unmarshal([]byte(trib_enc), &(reply.Tribbles[i])) } return nil }
func (ts *Tribserver) GetTribblesBySubscription(args *tribproto.GetTribblesArgs, reply *tribproto.GetTribblesReply) error { //check errors for any calls to libstore //check if user exists _, userErr := ts.lstore.Get(args.Userid) if userErr != nil { reply.Status = tribproto.ENOSUCHUSER return nil } //Get subscriptions subs, suberr := ts.lstore.GetList(args.Userid + ":subscriptions") //err indicates there are no subscriptions if suberr != nil { reply.Status = tribproto.OK reply.Tribbles = []tribproto.Tribble{} return nil } //for all users usrTimestamps := make(map[string]([]string)) totalTribs := 0 for i := 0; i < len(subs); i++ { //getTribbles for each user timestamps, listErr := ts.lstore.GetList(subs[i] + ":timestamps") //if not empty if listErr == nil && len(timestamps) > 0 { usrTimestamps[subs[i]] = timestamps totalTribs += len(timestamps) } } //Go through all tribble lists, and create new list of most recent 100 tribbles numTribs := int(math.Min(100, float64(totalTribs))) replyTribs := []tribproto.Tribble{} for j := 0; j < numTribs; j++ { var id string latest := "" for userId, timestamps := range usrTimestamps { if len(timestamps) > 0 { curr := timestamps[len(timestamps)-1] nanoCurr, _ := strconv.ParseInt(curr, 16, 64) nanoLatest, _ := strconv.ParseInt(latest, 16, 64) if nanoCurr > nanoLatest { latest = curr id = userId } } } //error makes no sense because we have these timestamps from a previous get jtrib, ohgawderr := ts.lstore.Get(id + ":" + latest) if ohgawderr != nil { return ohgawderr } var trib tribproto.Tribble jtribBytes := []byte(jtrib) jerr := json.Unmarshal(jtribBytes, &trib) if jerr != nil { return jerr } replyTribs = append(replyTribs, trib) //new list without latest Trib timestamps := usrTimestamps[id] newArray := []string{} for k := 0; k < len(timestamps)-1; k++ { newArray = append(newArray, timestamps[k]) } usrTimestamps[id] = newArray } reply.Status = tribproto.OK reply.Tribbles = replyTribs return nil }