Example #1
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) 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
	}

	tribValue := makeTribValue(args.Contents)

	tribHash := makeTribHash(tribValue)
	tribListKey := makeTribListKey(user)

	// insert hash value as tribble ID to user tribbles list
	err = ts.Libstore.AppendToList(tribListKey, tribHash)
	if err != nil {
		return err
	}

	// then insert mapping from unique Id in each user to tribble value
	tribId := makeTribId(user, tribHash)

	err = ts.Libstore.Put(tribId, tribValue)
	if err != nil {
		return err
	}

	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
}
Example #3
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	reply.Status = tribrpc.OK
	t := time.Now()
	tribble := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   t,
		Contents: args.Contents,
	}
	tribbleByte, err := json.Marshal(tribble)
	tribbleString := string(tribbleByte)
	tribbleID := t.UTC().UnixNano()
	reply.PostKey = util.FormatPostKey(args.UserID, tribbleID)
	if err != nil {
		return errors.New("Error Marshal")
	}
	err = ts.ls.Put(reply.PostKey, tribbleString)
	if err != nil {
		return errors.New("Error Put")
	}
	err = ts.ls.AppendToList(util.FormatTribListKey(args.UserID), reply.PostKey)
	if err != nil {
		return errors.New("Error AppendToList: " + err.Error())
	}
	return nil
}
Example #4
0
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
}
Example #5
0
/*A well-written implementation will not store a gigantic list of all
Tribbles for a user in a single key/value entry. Your system should be able to handle
users with thousands of Tribbles without excessive bloat or slowdown. We suggest
storing a list of Tribble IDs (which you will have to define) in some way, and then
storing each Tribble as a separate key/value item stored on the same partition as the
user ID.
Assuming you implement your storage server in this way, we recommend using the Get/Put methods
to store Tribbles and the AppendToList/RemoveFromList methods to store lists of IDs that reference the
actual Tribble objects.*/
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	LOGE.Printf("PostTribble:: args=%s\n", string(marshal(*args)))
	userIDKey := GetUserKey(args.UserID)
	_, err := ts.lib.Get(userIDKey)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	var trible tribrpc.Tribble
	trible.UserID = args.UserID
	trible.Posted = time.Now()
	trible.Contents = args.Contents

	tribleIDKey := GetTribelPutKey(args.UserID, strconv.Itoa(ts.index), ts.hostPort)
	tribleValue, err := json.Marshal(trible)

	err = ts.lib.Put(tribleIDKey, string(tribleValue))
	if err != nil {
		reply.Status = tribrpc.Exists
		return nil
	}
	userTribleListKey := GetsTribleListKey(args.UserID)

	err = ts.lib.AppendToList(userTribleListKey, tribleIDKey)
	if err != nil {
		reply.Status = tribrpc.Exists
		return nil
	}

	reply.Status = tribrpc.OK
	ts.index++
	LOGE.Printf("PostTribble:: reply=%s\n", string(marshal(reply)))
	return nil
}
Example #6
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	fmt.Println(args.Contents)
	UserKey := GenerateUserKey(args.UserID)

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

	TribListKey := GenerateTribListKey(args.UserID)

	//err = ts.lib.AppendToList(TribListKey, strconv.Itoa(ts.id))
	//if err != nil {
	//	reply.Status = tribrpc.Exists
	//	return nil
	//}

	var trib tribrpc.Tribble
	trib.UserID = args.UserID
	trib.Posted = time.Now()
	trib.Contents = args.Contents

	TribIDKey := GenerateTribIDKey(args.UserID, strconv.Itoa(ts.id), ts.hostport)
	val, _ := json.Marshal(trib)

	err = ts.lib.Put(TribIDKey, string(val))
	if err != nil {
		reply.Status = tribrpc.Exists
		fmt.Println("trib id already exist!")
		return nil
	}

	err = ts.lib.AppendToList(TribListKey, TribIDKey)
	if err != nil {
		reply.Status = tribrpc.Exists
		return nil
	}

	reply.Status = tribrpc.OK
	ts.id++
	return nil
}
Example #7
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	if args == nil {
		return errors.New("ts: Can't post nil")
	}
	if reply == nil {
		return errors.New("ts: Can't reply with nil in Post")
	}

	curTime := time.Now()
	_, err := ts.ls.Get(util.FormatUserKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.PostKey = ""
		return nil
	}

	newTribID := util.FormatPostKey(args.UserID, curTime.UnixNano())

	for ts.ls.AppendToList(util.FormatTribListKey(args.UserID), newTribID) != nil {
		newTribID = util.FormatPostKey(args.UserID, curTime.UnixNano())
	}

	trb := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   curTime,
		Contents: args.Contents,
	}

	marshaled, _ := json.Marshal(trb)
	if ts.ls.Put(newTribID, string(marshaled[:])) != nil {
		ts.ls.RemoveFromList(util.FormatTribListKey(args.UserID), newTribID)
		reply.Status = tribrpc.NoSuchUser
		reply.PostKey = ""
		return errors.New("Fatal, failed to add new post")
	}

	reply.Status = tribrpc.OK
	reply.PostKey = newTribID
	return nil
}
Example #8
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) 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
	}

	now := time.Now()
	tribble := &tribrpc.Tribble{
		UserID:   args.UserID,
		Posted:   now,
		Contents: args.Contents,
	}

	tribble_str, _ := json.Marshal(tribble)

	post_key := util.FormatPostKey(args.UserID, time.Now().UnixNano())
	err = ts.lib_store.Put(post_key, string(tribble_str))
	//if err != nil {
	//	reply.Status = tribrpc.NoSuchUser
	//	fmt.Println(1)
	//	return nil
	//}

	user_trib_list_key := util.FormatTribListKey(args.UserID)
	err = ts.lib_store.AppendToList(user_trib_list_key, post_key)
	//if err != nil {
	//	reply.Status = tribrpc.NoSuchUser
	//	fmt.Println(2)
	//	return nil
	//}

	reply.Status = tribrpc.OK
	reply.PostKey = post_key
	return nil
}
Example #9
0
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {
	userkey := util.FormatUserKey(args.UserID)

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

	postkey := util.FormatPostKey(args.UserID, time.Now().UnixNano())
	// make sure postkey is unique
	for {
		if _, err := ts.ls.Get(postkey); err == nil {
			postkey = util.FormatPostKey(args.UserID, time.Now().UnixNano())
			continue
		}
		break
	}

	// Create the Tribble
	tribble := tribrpc.Tribble{UserID: args.UserID, Posted: time.Now(),
		Contents: args.Contents}
	value, err := json.Marshal(tribble)
	if err != nil {
		return errors.New("Couldn't create Tribble")
	}

	// Hash the Tribble and add the postkey to the user's TribList
	if ep := ts.ls.Put(postkey, string(value)); ep != nil {
		return errors.New("Couldn't post Tribble")
	}
	if ea := ts.ls.AppendToList(util.FormatTribListKey(args.UserID), postkey); ea != nil {
		return errors.New("Couldn't add TribbleID to user list")
	}
	reply.PostKey = postkey
	reply.Status = tribrpc.OK
	return nil
}
/*
	-Check if user is present
	-Create the tribble
	-timestamp: go time
	-create a tribble
	-marshal it

	-Appendto list that post key to the usrID
	-Add to the map from post key -> marshalled tribble
*/
func (ts *tribServer) PostTribble(args *tribrpc.PostTribbleArgs, reply *tribrpc.PostTribbleReply) error {

	if DBG {
		fmt.Println("-----> PostTribble")
	}
	var thisUsrId string = args.UserID
	var content string = args.Contents

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

	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")
		}
		//user not found
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	var timeNow time.Time = time.Now()

	newTribble := tribrpc.Tribble{
		UserID:   thisUsrId,
		Posted:   timeNow,
		Contents: content,
	}

	marshalTrib, err := json.Marshal(&newTribble)

	if err != nil {
		fmt.Println("Error marshalling")
		//return err
	}

	var postKey string = util.FormatPostKey(thisUsrId, timeNow.UnixNano())

	//store the tribble itself
	err = ts.libStore.Put(postKey, string(marshalTrib))

	if err != nil {
		fmt.Println("Error putting tribble contents")
		//return err
	}

	//store the postkey
	err = ts.libStore.AppendToList(util.FormatTribListKey(thisUsrId), postKey)

	if err != nil {
		fmt.Println("Error putting postKey")
		//return err
	}

	reply.PostKey = postKey
	reply.Status = tribrpc.OK
	return nil
}