本文整理匯總了Golang中github.com/cmu440/tribbler/util.FormatUserKey函數的典型用法代碼示例。如果您正苦於以下問題:Golang FormatUserKey函數的具體用法?Golang FormatUserKey怎麽用?Golang FormatUserKey使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了FormatUserKey函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddSubscription
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
}
示例2: AddSubscription
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
}
示例3: RemoveSubscription
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
}
示例4: AddSubscription
// 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
}
示例5: CreateUser
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
}
示例6: AddSubscription
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
}
示例7: CreateUser
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
}
示例8: DeleteTribble
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
}
示例9: DeleteTribble
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
}
示例10: GetTribbles
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
}
示例11: PostTribble
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
}
示例12: DeleteTribble
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
}
示例13: GetTribblesBySubscription
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
}
示例14: GetTribblesBySubscription
/*
-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
}
示例15: PostTribble
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
}