Example #1
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 #2
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
}