func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { userkey := util.FormatUserKey(args.UserID) targetkey := util.FormatUserKey(args.TargetUserID) usersubs := util.FormatSubListKey(args.UserID) target := args.TargetUserID // Make sure user exists if _, eu := ts.ls.Get(userkey); eu != nil { reply.Status = tribrpc.NoSuchUser return nil } // Make sure targetUser exists if _, et := ts.ls.Get(targetkey); et != nil { reply.Status = tribrpc.NoSuchTargetUser return nil } // Make sure user isn't already subscribed to target err := ts.ls.AppendToList(usersubs, target) if err == nil { reply.Status = tribrpc.OK } else { reply.Status = tribrpc.Exists } return nil }
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { user_id := util.FormatUserKey(args.UserID) target_user_id := util.FormatUserKey(args.TargetUserID) // If the user_id doesn't exist // Return error _, err := ts.lib_store.Get(user_id) if err != nil { reply.Status = tribrpc.NoSuchUser return nil } // If the target_user_id doesn't exist // Return error _, err = ts.lib_store.Get(target_user_id) if err != nil { reply.Status = tribrpc.NoSuchTargetUser return nil } user_sub := util.FormatSubListKey(args.UserID) err = ts.lib_store.AppendToList(user_sub, args.TargetUserID) if err != nil { reply.Status = tribrpc.Exists return nil } reply.Status = tribrpc.OK return nil }
func (ts *tribServer) RemoveSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { userkey := util.FormatUserKey(args.UserID) targetkey := util.FormatUserKey(args.TargetUserID) usersubs := util.FormatSubListKey(args.UserID) target := args.TargetUserID // Make sure user exists if _, eu := ts.ls.Get(userkey); eu != nil { reply.Status = tribrpc.NoSuchUser return nil } // Make sure target exists if _, et := ts.ls.Get(targetkey); et != nil { reply.Status = tribrpc.NoSuchTargetUser return nil } // Make sure user is subscribed to target if err := ts.ls.RemoveFromList(usersubs, target); err != nil { reply.Status = tribrpc.NoSuchTargetUser } else { reply.Status = tribrpc.OK } return nil }
// AddSubscription adds TargerUserID to UserID's list of subscriptions. // Replies with status NoSuchUser if the specified UserID does not exist, and NoSuchTargerUser // if the specified TargerUserID does not exist. func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { userId := args.UserID targetId := args.TargetUserID userIdKey := util.FormatUserKey(userId) targetIdKey := util.FormatUserKey(targetId) _, ok_user := ts.libstore.Get(userIdKey) _, ok_target := ts.libstore.Get(targetIdKey) if ok_user != nil { reply.Status = tribrpc.NoSuchUser } else if ok_target != nil { reply.Status = tribrpc.NoSuchTargetUser } else { subKey := util.FormatSubListKey(userId) err := ts.libstore.AppendToList(subKey, targetId) if err != nil { reply.Status = tribrpc.Exists } else { reply.Status = tribrpc.OK } } return nil }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { if DBG { fmt.Println("-----> CreateUser") } var usrID string = args.UserID _, err := ts.libStore.Get(util.FormatUserKey(usrID)) if err == nil { //user not found reply.Status = tribrpc.Exists return nil //return err } err = ts.libStore.Put(util.FormatUserKey(usrID), "exists") if err != nil { return nil } //populate reply struct reply.Status = tribrpc.OK return nil }
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { userid := args.UserID targetid := args.TargetUserID useridkey := util.FormatUserKey(userid) targetidkey := util.FormatUserKey(targetid) _, userExists := ts.ls.Get(useridkey) if userExists != nil { reply.Status = tribrpc.NoSuchUser return nil } _, targetExists := ts.ls.Get(targetidkey) if targetExists != nil { reply.Status = tribrpc.NoSuchTargetUser return nil } userSubListKey := util.FormatSubListKey(userid) appendErr := ts.ls.AppendToList(userSubListKey, targetid) if appendErr != nil { reply.Status = tribrpc.Exists return nil } reply.Status = tribrpc.OK return nil }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { if ts.userExist(args.UserID) == true { reply.Status = tribrpc.Exists return nil } reply.Status = tribrpc.OK ts.ls.Put(util.FormatUserKey(args.UserID), util.FormatUserKey(args.UserID)) return nil }
func (ts *tribServer) DeleteTribble(args *tribrpc.DeleteTribbleArgs, reply *tribrpc.DeleteTribbleReply) error { if args == nil { return errors.New("ts: Can't delete nil") } if reply == nil { return errors.New("ts: Can't reply with nil in Delete") } _, err := ts.ls.Get(util.FormatUserKey(args.UserID)) if err != nil { reply.Status = tribrpc.NoSuchUser return nil } err = ts.ls.RemoveFromList(util.FormatTribListKey(args.UserID), args.PostKey) if err != nil { reply.Status = tribrpc.NoSuchPost return nil } if ts.ls.Delete(args.PostKey) != nil { reply.Status = tribrpc.NoSuchPost return errors.New("Fatal, failed to delete a key") } reply.Status = tribrpc.OK return nil }
func (ts *tribServer) DeleteTribble(args *tribrpc.DeleteTribbleArgs, reply *tribrpc.DeleteTribbleReply) error { userkey := util.FormatUserKey(args.UserID) _, userErr := ts.libstore.Get(userkey) if userErr != nil { reply.Status = tribrpc.NoSuchUser } else { delErr := ts.libstore.Delete(args.PostKey) if delErr != nil { reply.Status = tribrpc.NoSuchPost return nil } tribkey := util.FormatTribListKey(args.UserID) delListErr := ts.libstore.RemoveFromList(tribkey, args.PostKey) if delListErr != nil { reply.Status = tribrpc.NoSuchPost return nil } reply.Status = tribrpc.OK } return nil }
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 }
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error { key := util.FormatUserKey(args.UserID) if ts.checkIfKnown(key) { newTrib := tribrpc.Tribble{args.UserID, time.Now(), args.Contents} tribStr, err := json.Marshal(newTrib) if err != nil { return err } id := newTrib.UserID time := newTrib.Posted.UnixNano() post_key := util.FormatPostKey(id, time) for ts.checkIfKnown(post_key) { post_key = util.FormatPostKey(id, time) } err = ts.storage.Put(post_key, string(tribStr)) if err != nil { return err } triblist_key := util.FormatTribListKey(args.UserID) err = ts.storage.AppendToList(triblist_key, post_key) if err != nil { return err } reply.PostKey = post_key reply.Status = tribrpc.OK } else { reply.Status = tribrpc.NoSuchUser } return nil }
func (ts *tribServer) DeleteTribble(args *tribrpc.DeleteTribbleArgs, reply *tribrpc.DeleteTribbleReply) error { key := util.FormatUserKey(args.UserID) post_key := args.PostKey if !ts.checkIfKnown(key) { reply.Status = tribrpc.NoSuchUser } else if !ts.checkIfKnown(post_key) { reply.Status = tribrpc.NoSuchPost } else { err := ts.storage.Delete(post_key) if err != nil { return err } triblist_key := util.FormatTribListKey(args.UserID) err = ts.storage.RemoveFromList(triblist_key, post_key) if err != nil { return err } reply.Status = tribrpc.OK } return nil }
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 }
/* -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 }
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error { userkey := util.FormatUserKey(args.UserID) _, userErr := ts.libstore.Get(userkey) if userErr != nil { reply.Status = tribrpc.NoSuchUser } else { postTime := time.Now() tribble := &tribrpc.Tribble{ UserID: args.UserID, Posted: postTime, Contents: args.Contents, } marshalTribble, _ := json.Marshal(tribble) unixTime := int64(postTime.UnixNano()) postKey := util.FormatPostKey(args.UserID, unixTime) putErr := ts.libstore.Put(postKey, string(marshalTribble)) if putErr != nil { return errors.New("Fail to put postkey and tribble") } tribListKey := util.FormatTribListKey(args.UserID) appendErr := ts.libstore.AppendToList(tribListKey, postKey) if appendErr != nil { return errors.New("Fail to append tribble to user") } reply.Status = tribrpc.OK reply.PostKey = postKey } return nil }
func (ts *tribServer) userExist(UserID string) bool { _, err := ts.ls.Get(util.FormatUserKey(UserID)) if err != nil { return false } return true }
func (ts *tribServer) DeleteTribble(args *tribrpc.DeleteTribbleArgs, reply *tribrpc.DeleteTribbleReply) 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 { reply.Status = tribrpc.NoSuchUser return nil } user_trib_list_key := util.FormatTribListKey(args.UserID) err = ts.lib_store.RemoveFromList(user_trib_list_key, args.PostKey) if err != nil { reply.Status = tribrpc.NoSuchPost fmt.Println(2) return nil } err = ts.lib_store.Delete(args.PostKey) if err != nil { reply.Status = tribrpc.NoSuchPost return nil } reply.Status = tribrpc.OK return nil }
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error { user_key := util.FormatUserKey(args.UserID) target_key := util.FormatUserKey(args.TargetUserID) if !ts.checkIfKnown(user_key) { reply.Status = tribrpc.NoSuchUser } else if !ts.checkIfKnown(target_key) { reply.Status = tribrpc.NoSuchTargetUser } else { sublist_key := util.FormatSubListKey(args.UserID) err := ts.storage.AppendToList(sublist_key, args.TargetUserID) if err != nil { reply.Status = tribrpc.Exists } else { reply.Status = tribrpc.OK } } return nil }
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 }
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 }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { key := util.FormatUserKey(args.UserID) if ts.checkIfKnown(key) { reply.Status = tribrpc.Exists } else { err := ts.storage.Put(key, args.UserID) if err != nil { return err } reply.Status = tribrpc.OK } return nil }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { user := util.FormatUserKey(args.UserID) // Check that this user doesn't already exist if _, err := ts.ls.Get(user); err != nil { ts.ls.Put(user, user) reply.Status = tribrpc.OK } else { reply.Status = tribrpc.Exists } return nil }
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error { subkey := util.FormatSubListKey(args.UserID) userkey := util.FormatUserKey(args.UserID) _, userErr := ts.libstore.Get(userkey) if userErr != nil { reply.Status = tribrpc.NoSuchUser } else { list, _ := ts.libstore.GetList(subkey) reply.Status = tribrpc.OK reply.UserIDs = list } 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 }
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error { key := util.FormatUserKey(args.UserID) if ts.checkIfKnown(key) { sublist_key := util.FormatSubListKey(args.UserID) sub_ids, err := ts.storage.GetList(sublist_key) if err != nil { sub_ids = []string{} } reply.UserIDs = sub_ids reply.Status = tribrpc.OK } else { reply.Status = tribrpc.NoSuchUser } return nil }
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 }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { key := util.FormatUserKey(args.UserID) _, err := ts.libstore.Get(key) if err == nil { reply.Status = tribrpc.Exists } else { err2 := ts.libstore.Put(key, key) if err2 != nil { return err2 } reply.Status = tribrpc.OK } return nil }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { user_id := util.FormatUserKey(args.UserID) _, err := ts.lib_store.Get(user_id) if err == nil { reply.Status = tribrpc.Exists return nil } err = ts.lib_store.Put(user_id, args.UserID) if err != nil { reply.Status = tribrpc.Exists return err } reply.Status = tribrpc.OK return nil }
func (ts *tribServer) CreateUser(args *tribrpc.CreateUserArgs, reply *tribrpc.CreateUserReply) error { userid := args.UserID useridkey := util.FormatUserKey(userid) _, err := ts.ls.Get(useridkey) if err == nil { reply.Status = tribrpc.Exists return nil } err2 := ts.ls.Put(useridkey, userid) if err2 != nil { reply.Status = tribrpc.Exists return err2 } reply.Status = tribrpc.OK return nil }
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 }