func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userkey := util.FormatUserKey(args.UserID)
	_, userErr := ts.libstore.Get(userkey)

	if userErr != nil {
		reply.Status = tribrpc.NoSuchUser
	} else {
		reply.Status = tribrpc.OK
		subskey := util.FormatSubListKey(args.UserID)
		subsList, _ := ts.libstore.GetList(subskey)

		totalTribList := make(Tribbles, 0)
		for i := 0; i < len(subsList); i++ {
			curUser := subsList[i]
			curtribList := ts.GetTribblesOneUser(curUser)

			for j := 0; j < len(curtribList); j++ {
				totalTribList = append(totalTribList, curtribList[j])
			}
			//totalTribList = append(totalTribList, curtribList)
		}

		sort.Sort(totalTribList)
		totalLen := len(totalTribList)
		if totalLen > 100 {
			totalLen = 100
		}
		reply.Tribbles = make(Tribbles, totalLen)
		for j := 0; j < totalLen; j++ {
			reply.Tribbles[j] = totalTribList[j]
		}
	}
	return nil

}
Example #2
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	user := args.UserID

	_, err := ts.Libstore.Get(user)
	switch err {
	case nil: // expected case, do nothing
	case libstore.ErrorKeyNotFound:
		reply.Status = tribrpc.NoSuchUser
		return nil
	default:
		return err
	}

	subscListKey := makeSubscListKey(user)
	subscList, err := ts.Libstore.GetList(subscListKey)
	switch err {
	case nil:
	case libstore.ErrorKeyNotFound:
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		reply.Status = tribrpc.OK
		return nil
	default:
		return err
	}

	tribbles, err := ts.getTribsFromSubs(subscList)
	if err != nil { // ignore error
		// return err
	}

	reply.Tribbles = tribbles
	reply.Status = tribrpc.OK
	return nil
}
Example #3
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	key := util.FormatUserKey(args.UserID)
	if ts.checkIfKnown(key) {
		triblist_key := util.FormatTribListKey(args.UserID)
		post_ids, err := ts.storage.GetList(triblist_key)
		if err != nil {
			post_ids = []string{}
		}
		sort.Sort(ByChrono(post_ids))
		bound := min(100, len(post_ids))
		tribs := make([]tribrpc.Tribble, bound)
		for i, v := range post_ids[0:bound] {
			postStr, err := ts.storage.Get(v)
			if err != nil {
				return err
			}
			trib := tribrpc.Tribble{}
			json.Unmarshal([]byte(postStr), &trib)
			tribs[i] = trib
		}
		reply.Tribbles = tribs
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.NoSuchUser
	}
	return nil
}
/*
-format sublist
*/
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {

	if DBG {
		fmt.Println("-----> GetTribblesBySubscription")
	}
	var usrID string = args.UserID

	//check if user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(usrID))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
	}

	//get list of subscribers
	subscrList, err := ts.libStore.GetList(util.FormatSubListKey(usrID))

	if err != nil {
		//return err
		fmt.Println("No subscribers, or error getting list of subscribers")
		reply.Status = tribrpc.OK
		return nil
	}

	//initialise empty slice of all postKeys from subscribers
	var allPostKeys []string = make([]string, 0)

	//populate allPostKeys
	for _, currUser := range subscrList {
		currPostKeys, err := ts.libStore.GetList(util.FormatTribListKey(currUser))

		if err == nil {
			allPostKeys = append(allPostKeys, currPostKeys...)

		} else {
			//fmt.Println("0 tribs for user detected")
		}
	}

	sort.Sort(PostByTime(allPostKeys))

	//choose most recent posts, and get tribbles
	var tribList []tribrpc.Tribble = ts.getTribbleList(allPostKeys)

	reply.Tribbles = tribList
	reply.Status = tribrpc.OK
	return nil

}
Example #5
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	// Getsublist by key
	// Get tribble list by user_ids in sublist
	// sort the tribble list by the timestamp
	// retrive the first 100 tribble (Be careful about the delete operation)
	user_id := util.FormatUserKey(args.UserID)

	// If the user_id doesn't exist
	// Reture NoSuchUser
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Get user's subscription_list
	user_id_list := util.FormatSubListKey(args.UserID)
	// list of IDs
	user_list, _ := ts.lib_store.GetList(user_id_list)

	if user_list == nil {
		reply.Status = tribrpc.OK
		return nil
	}

	// Get tribble_ids from the user_ids in the subscription list
	tribble_id_list := make([]string, 0)
	for _, usr_id := range user_list {
		t_id_list, err := ts.lib_store.GetList(util.FormatTribListKey(usr_id))
		if err == nil {
			tribble_id_list = append(tribble_id_list, t_id_list...)
		}
	}

	sort.Sort(SortTribbleByTimestamp(tribble_id_list))

	counter := 0
	tribble_list := make([]tribrpc.Tribble, 100)
	for _, tid := range tribble_id_list {
		tribble, err := ts.lib_store.Get(tid)
		if err == nil {
			json.Unmarshal([]byte(tribble), &tribble_list[counter])
			counter += 1
		}
		if counter >= 100 {
			break
		}
	}

	reply.Status = tribrpc.OK
	reply.Tribbles = tribble_list[:counter]
	return nil
}
/*
-getList, formatTribListKey to get all the post keys
-Slice it off at 100
-reverse it or wtv (if needed)
-Get() with that post key, and get the marshalled tribble
*/
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {

	if DBG {
		fmt.Println("-----> GetTribbles")
	}
	var usrID string = args.UserID

	//check if user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(usrID))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
	}

	postKeysList, err := ts.libStore.GetList(util.FormatTribListKey(usrID))

	if err != nil {
		//fmt.Println("Could not get list of postKeys for user")
		//return empty tribble list, as the list is not yet created (0 tribbles)

		switch err.Error() {
		case KEY_NOT_FOUND:
			reply.Status = tribrpc.OK
			reply.Tribbles = make([]tribrpc.Tribble, 0)
			return nil
		case WRONG_SERVER:
			fmt.Println("WRONG SERVER CONTACTED!")
			return nil
		default:
			fmt.Println("ERROR in tribserver: wrong error message received!")
			return nil
		}

	}

	reply.Status = tribrpc.OK
	reply.Tribbles = ts.getTribbleList(postKeysList)

	return nil
}
Example #7
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	tribbles, err := ts.GetAllTribblesByUser(args.UserID)
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = prepareTribbles(tribbles)
	return nil
}
Example #8
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userid := args.UserID
	useridkey := util.FormatUserKey(userid)

	_, userExists := ts.ls.Get(useridkey)

	if userExists != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	userTribListKey := util.FormatTribListKey(userid)
	tribbleIdList, err := ts.ls.GetList(userTribListKey)

	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = nil
		return nil
	}

	tmp := tribbleIDs(tribbleIdList)
	sort.Sort(tmp)
	tribbleIdsChrono := []string(tmp)

	// Calculate the length of the tribbles we want to get
	length := 100
	if len(tribbleIdsChrono) < 100 {
		length = len(tribbleIdsChrono)
	}
	allTribbles := make([]tribrpc.Tribble, length)
	var tribTmp tribrpc.Tribble
	i := 0
	for i < length {
		trib, err := ts.ls.Get(tribbleIdsChrono[i])
		if err == nil {
			json.Unmarshal([]byte(trib), &tribTmp)
			allTribbles[i] = tribTmp
			i += 1 // If error occurs, we do NOT increment i
		}
	}

	reply.Status = tribrpc.OK
	reply.Tribbles = allTribbles

	return nil

}
Example #9
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	subscriptions, err := ts.ls.GetList(util.FormatSubListKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}
	reply.Tribbles = make([]tribrpc.Tribble, 0)
	mychan := make(chan []tribrpc.Tribble, 1000)
	go func() {
		for i := 0; i < len(subscriptions); i++ {
			user := subscriptions[i]
			go func() {
				tribbles, _ := ts.GetAllTribblesByUser(user)
				mychan <- tribbles
			}()
		}
	}()
	for i := 0; i < len(subscriptions); i++ {
		tribbles := <-mychan
		reply.Tribbles = append(reply.Tribbles, tribbles...)
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = prepareTribbles(reply.Tribbles)
	return nil
}
Example #10
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	user := args.UserID

	_, err := ts.Libstore.Get(user)
	switch err {
	case nil: // expected case, do nothing
	case libstore.ErrorKeyNotFound:
		reply.Status = tribrpc.NoSuchUser
		return nil
	default:
		return err
	}

	tribListKey := makeTribListKey(user)

	totalHashIds, err := ts.Libstore.GetList(tribListKey)
	switch err {
	case nil:
	case libstore.ErrorKeyNotFound:
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		reply.Status = tribrpc.OK
		return nil
	default:
		return err
	}

	// reverse hash Id to achieve most recent tribbles first.
	length := Min(len(totalHashIds), maxGetTribbleNum)
	hashIds := make([]string, length)
	for i := range hashIds {
		hashIds[i] = totalHashIds[len(totalHashIds)-i-1]
	}

	tribValues, err := ts.getTribValuesFromHashIds(user, hashIds)
	if err != nil { // ignore error
		// return err
	}

	reply.Tribbles = makeTribbles(user, tribValues)
	reply.Status = tribrpc.OK
	return nil
}
Example #11
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	user_id := util.FormatUserKey(args.UserID)

	// If the user_id doesn't exist
	// Reture NoSuchUser
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		fmt.Println(1)
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Return a list of tribble_ids
	raw_tribble_list, _ := ts.lib_store.GetList(util.FormatTribListKey(args.UserID))

	if raw_tribble_list == nil {
		reply.Status = tribrpc.OK
		return nil
	}

	// Sort the tribble_ids by timestamp reversely
	sort.Sort(SortTribbleByTimestamp(raw_tribble_list))

	counter := 0
	tribble_list := make([]tribrpc.Tribble, 100)
	for _, tid := range raw_tribble_list {
		tribble, err := ts.lib_store.Get(tid)
		if err == nil {
			// var t tribrpc.Tribble
			json.Unmarshal([]byte(tribble), &tribble_list[counter])
			counter += 1
		}
		if counter >= 100 {
			break
		}
	}

	reply.Status = tribrpc.OK
	reply.Tribbles = tribble_list[:counter]
	return nil
}
Example #12
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	UserKey := GenerateUserKey(args.UserID)

	_, err := ts.lib.Get(UserKey)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = nil
		return nil
	}

	SubsKey := GenerateSubsKey(args.UserID)
	SubsList, err := ts.lib.GetList(SubsKey)
	if err != nil {
		//reply.Status = tribrpc.NoSuchUser
		reply.Status = tribrpc.OK
		reply.Tribbles = nil
		return nil
	}

	for i := 0; i < len(SubsList); i++ {
		var Subargs tribrpc.GetTribblesArgs
		var Subreply tribrpc.GetTribblesReply

		Subargs.UserID = SubsList[i]
		err = ts.GetTribbles(&Subargs, &Subreply)
		if err != nil {
			return err
		}

		reply.Tribbles = append(reply.Tribbles, Subreply.Tribbles...)
	}

	var tempTrib Tribs
	tempTrib = reply.Tribbles
	sort.Sort(tempTrib)
	if len(tempTrib) > 100 {
		reply.Status = tribrpc.OK
		reply.Tribbles = tempTrib[:100]
	} else {
		reply.Status = tribrpc.OK
		reply.Tribbles = tempTrib
	}

	return nil
}
Example #13
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userkey := util.FormatUserKey(args.UserID)

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Store list of marshalled Tribbles in lst.  Return empty list if user had no subscriptions.
	var lst []string
	var err error
	if lst, err = ts.ls.GetList(util.FormatTribListKey(args.UserID)); err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}

	// Find out how many posts we will return
	var recentPosts []string
	if len(lst) > 100 {
		recentPosts = lst[len(lst)-100:]
	} else {
		recentPosts = lst
	}

	// Loop through this users Tribbles in reverse order and grab up to 100
	tribbles := make([]tribrpc.Tribble, len(recentPosts))
	var mtribble string
	var tribble tribrpc.Tribble
	for i := 0; i < len(tribbles); i++ {
		mtribble, _ = ts.ls.Get(recentPosts[len(tribbles)-1-i])
		json.Unmarshal([]byte(mtribble), &tribble)
		tribbles[i] = tribble
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = tribbles
	return nil
}
Example #14
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	LOGE.Printf("GetTribblesBySubscription:: args=%s\n", string(marshal(*args)))
	userIDKey := GetUserKey(args.UserID)
	_, err := ts.lib.Get(userIDKey)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = nil
		return nil
	}

	userTribleListKey := GetsSubsKey(args.UserID)
	subsList, err := ts.lib.GetList(userTribleListKey)
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = nil
		return nil
	}

	for i := 0; i < len(subsList); i++ {
		var subArgs tribrpc.GetTribblesArgs
		var subReply tribrpc.GetTribblesReply

		subArgs.UserID = subsList[i]

		err = ts.GetTribbles(&subArgs, &subReply)
		if err != nil {
			return err
		}
		reply.Tribbles = append(reply.Tribbles, subReply.Tribbles...)
	}

	var tempTribbles Tribs
	tempTribbles = reply.Tribbles
	sort.Sort(tempTribbles)
	if len(tempTribbles) > 100 {
		reply.Status = tribrpc.OK
		reply.Tribbles = tempTribbles[:100]
	} else {
		reply.Status = tribrpc.OK
		reply.Tribbles = tempTribbles
	}
	LOGE.Printf("GetTribblesBySubscription:: reply=%s\n", string(marshal(reply)))
	return nil
}
Example #15
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	//fmt.Println("get tribble begin!")
	UserKey := GenerateUserKey(args.UserID)

	_, err := ts.lib.Get(UserKey)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = nil
		return nil
	}

	TribListKey := GenerateTribListKey(args.UserID)
	TribIDs, err := ts.lib.GetList(TribListKey)
	if err != nil {
		//reply.Status = tribrpc.NoSuchUser
		//return nil
		reply.Status = tribrpc.OK
		return nil
	}

	//for i := 0; i < len(TribIDs); i++ {
	//	fmt.Println(TribIDs[i])
	//}

	var length int
	length = len(TribIDs)
	//fmt.Println("actual length:",length)
	//log.Print(length)
	if length > 100 {
		length = 100
	}
	reply.Tribbles = make([]tribrpc.Tribble, length)

	for i := 0; i < length; i++ {
		//TribIDKey := GenerateTribIDKey(args.UserID, TribIDs[len(TribIDs)-1-i])
		TribIDKey := TribIDs[len(TribIDs)-1-i]
		val, err := ts.lib.Get(TribIDKey)
		if err != nil {
			//	continue
			reply.Status = tribrpc.NoSuchUser
			return errors.New("get invalid tribble")
		}
		if val == "" {
			return errors.New("empty string!!!!")
		}
		json.Unmarshal([]byte(val), &(reply.Tribbles[i]))
	}

	reply.Status = tribrpc.OK
	//for i := 0; i < len(reply.Tribbles); i++ {
	//	fmt.Println("result of getting tribbles")
	//	fmt.Print(reply.Tribbles[i].UserID+" ")
	//	//fmt.Print(reply.Tribbles[i].Posted)
	//	fmt.Print(reply.Tribbles[i].Contents)
	//	fmt.Println()
	//}
	return nil
}
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {

	userkey := util.FormatUserKey(args.UserID)
	_, userErr := ts.libstore.Get(userkey)
	if userErr != nil {
		reply.Status = tribrpc.NoSuchUser
	} else {
		reply.Status = tribrpc.OK
		tribList := ts.GetTribblesOneUser(args.UserID)

		if tribList != nil {
			length := len(tribList)

			reply.Tribbles = make([]tribrpc.Tribble, length)
			for i := 0; i < length; i++ {
				reply.Tribbles[i] = tribList[i]
			}

		}
	}

	return nil
}
Example #17
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	key := util.FormatUserKey(args.UserID)
	if ts.checkIfKnown(key) {
		sublist_key := util.FormatSubListKey(args.UserID)
		sublist_ids, err := ts.storage.GetList(sublist_key)
		if err != nil {
			sublist_ids = []string{}
		}

		post_ids := []string{}
		for _, userid := range sublist_ids {
			triblist_key := util.FormatTribListKey(userid)
			local_post_ids, err := ts.storage.GetList(triblist_key)
			if err != nil {
				sublist_ids = []string{}
			}
			post_ids = append(post_ids, local_post_ids...)
		}

		sort.Sort(ByChrono(post_ids))
		tribs := []tribrpc.Tribble{}
		for _, v := range post_ids[0:min(100, len(post_ids))] {
			postStr, err := ts.storage.Get(v)
			if err != nil {
				return err
			}
			trib := tribrpc.Tribble{}
			json.Unmarshal([]byte(postStr), &trib)
			tribs = append(tribs, trib)
		}
		reply.Tribbles = tribs
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.NoSuchUser
	}
	return nil
}
Example #18
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	if args == nil {
		return errors.New("ts: Can't getSubscription nil")
	}
	if reply == nil {
		return errors.New("ts: Can't reply with nil in getSubscription")
	}

	_, err := ts.ls.Get(util.FormatUserKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = make([]tribrpc.Tribble, 0, 0)
		return nil
	}

	users, err := ts.ls.GetList(util.FormatSubListKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0, 0)
		return nil
	}

	postIDs := make([]string, 0)
	for _, userID := range users {
		lst, err := ts.ls.GetList(util.FormatTribListKey(userID))
		if err == nil {
			postIDs = append(postIDs, lst...)
		}
	}

	tmp := tribbleIDs(postIDs)

	sort.Sort(tmp)
	tribbleIdsChrono := []string(tmp)

	length := 100
	if len(tribbleIdsChrono) < 100 {
		length = len(tribbleIdsChrono)
	}

	tribbles := make([]tribrpc.Tribble, length)
	var tribTmp tribrpc.Tribble
	i := 0
	for i < length {
		trib, err := ts.ls.Get(tribbleIdsChrono[i])
		if err == nil {
			json.Unmarshal([]byte(trib), &tribTmp)
			tribbles[i] = tribTmp
			i += 1 // If error occurs, we do NOT increment i
		}
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = tribbles
	return nil
}
Example #19
0
func (ts *tribServer) GetTribbles(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	LOGE.Printf("GetTribbles:: args=%s\n", string(marshal(*args)))

	userIDKey := GetUserKey(args.UserID)
	_, err := ts.lib.Get(userIDKey)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = nil
		return nil
	}
	userTribleListKey := GetsTribleListKey(args.UserID)
	tribles, err := ts.lib.GetList(userTribleListKey)

	if err != nil {
		reply.Status = tribrpc.OK
		return nil
	}

	length := len(tribles)
	if length > 100 {
		length = 100
	}

	reply.Tribbles = make([]tribrpc.Tribble, length)
	for i := 0; i < length; i++ {
		triblePutKey := tribles[len(tribles)-1-i]
		val, err := ts.lib.Get(triblePutKey)
		if err != nil {
			reply.Status = tribrpc.NoSuchUser
			return err
		}
		json.Unmarshal([]byte(val), &(reply.Tribbles[i]))
	}
	reply.Status = tribrpc.OK
	LOGE.Printf("GetTribbles:: reply=%s\n", string(marshal(reply)))
	return nil
}
Example #20
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userkey := util.FormatUserKey(args.UserID)

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Get list of subscriptions.  return empty list if user has no subscriptions.
	var subs []string
	var esubs error
	if subs, esubs = ts.ls.GetList(util.FormatSubListKey(args.UserID)); esubs != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}

	numTribbles := 0

	// Get the last 100 tribble IDs from each subscribed user
	tribIDs := make([][]string, len(subs))
	for i := 0; i < len(subs); i++ {
		if lst, err := ts.ls.GetList(util.FormatTribListKey(subs[i])); err == nil {
			if len(lst) > 100 {
				tribIDs[i] = lst[len(lst)-100:]
			} else {
				tribIDs[i] = lst
			}
			numTribbles += len(lst)
		}
	}

	// lastTribs is the most recent tribble seen from each subscribed user
	lastTribs := make([]tribrpc.Tribble, len(subs))
	// index is the list of indexes into tribIDs for each subscribed user
	index := make([]int, len(subs))

	if numTribbles > 100 {
		numTribbles = 100
	}

	finalTribbles := make([]tribrpc.Tribble, numTribbles)
	var mtribble string
	var tribble tribrpc.Tribble

	// valid[i] marks if an unused Tribble is located at lastTribs[i]
	valid := make([]bool, len(lastTribs))

	for j := 0; j < numTribbles; j++ {
		minIndex := -1
		for check := 0; check < len(lastTribs); check++ {
			// If this user has more tribbles left
			if index[check] < len(tribIDs[check]) {
				// If we need to get a new tribble for this user
				if valid[check] == false {
					mtribble, _ = ts.ls.Get(tribIDs[check][len(tribIDs[check])-1-index[check]])
					json.Unmarshal([]byte(mtribble), &tribble)
					lastTribs[check] = tribble
					valid[check] = true
				}
				// This tribble is more
				if minIndex == -1 || cmpLessTribble(lastTribs[check], lastTribs[minIndex]) {
					minIndex = check
				}
			}
		}
		copyTribble := lastTribs[minIndex]
		finalTribbles[j] = copyTribble
		valid[minIndex] = false
		index[minIndex] += 1

	}

	reply.Status = tribrpc.OK
	reply.Tribbles = finalTribbles
	return nil
}