Exemple #1
0
func encode(s string) []byte {
	switch *enc {
	case stringFormat:
		return []byte(s)
	case floatFormat:
		result, err := strconv.ParseFloat(s, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeFloat64(result)
	case intFormat:
		result, err := strconv.ParseInt(s, 10, 64)
		if err != nil {
			panic(err)
		}
		return common.EncodeInt64(result)
	case bigFormat:
		result, ok := new(big.Int).SetString(s, 10)
		if !ok {
			panic(fmt.Errorf("Bad BigInt format: %v", s))
		}
		return common.EncodeBigInt(result)
	}
	panic(fmt.Errorf("Unknown encoding: %v", *enc))
}
Exemple #2
0
func TestFloatSum(t *testing.T) {
	found := floatSum([][]byte{common.EncodeFloat64(1)}, [][]byte{common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected := [][]byte{common.EncodeFloat64(10)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
	found = floatSum(nil, [][]byte{common.EncodeFloat64(1), common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected = [][]byte{common.EncodeFloat64(10)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
	found = floatSum([][]byte{common.EncodeFloat64(1)}, [][]byte{common.EncodeFloat64(2)}, 2)
	expected = [][]byte{common.EncodeFloat64(5)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", found, expected)
	}
}
Exemple #3
0
func TestFloatDiv(t *testing.T) {
	found := floatDiv([][]byte{common.EncodeFloat64(48)}, [][]byte{common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected := [][]byte{common.EncodeFloat64(2)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
	found = floatDiv(nil, [][]byte{common.EncodeFloat64(48), common.EncodeFloat64(2), common.EncodeFloat64(3), common.EncodeFloat64(4)}, 1)
	expected = [][]byte{common.EncodeFloat64(2)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
	found = floatDiv([][]byte{common.EncodeFloat64(48)}, [][]byte{common.EncodeFloat64(2)}, 2)
	expected = [][]byte{common.EncodeFloat64(12)}
	if !reflect.DeepEqual(found, expected) {
		t.Errorf("%v should be %v", fmt.Sprint(common.DecodeFloat64(found[0])), fmt.Sprint(common.DecodeFloat64(expected[0])))
	}
}
Exemple #4
0
func ExampleSetExpression() {
	conn := MustConn("127.0.0.1:9191")
	conn.Kill()
	conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
	conn.SubPut([]byte("yourfriends"), []byte("bob"), common.EncodeFloat64(6))
	conn.SubPut([]byte("yourfriends"), []byte("charlie"), common.EncodeFloat64(4))
	fmt.Printf("name score\n")
	for _, friend := range conn.SetExpression(setop.SetExpression{
		Code: "(U:FloatSum myfriends yourfriends)",
	}) {
		fmt.Printf("%v %v\n", string(friend.Key), common.MustDecodeFloat64(friend.Values[0]))
	}
	// Output:
	// name score
	// alice 10
	// bob 11
	// charlie 4
}
Exemple #5
0
func ExampleTreeMirror() {
	conn := MustConn("127.0.0.1:9191")
	conn.Kill()
	conn.SubAddConfiguration([]byte("myfriends"), "mirrored", "yes")
	conn.SubPut([]byte("myfriends"), []byte("alice"), common.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), common.EncodeFloat64(5))
	conn.SubPut([]byte("myfriends"), []byte("charlie"), common.EncodeFloat64(6))
	conn.SubPut([]byte("myfriends"), []byte("denise"), common.EncodeFloat64(4))
	fmt.Printf("name score\n")
	for _, friend := range conn.MirrorReverseSlice([]byte("myfriends"), nil, nil, true, true) {
		fmt.Printf("%v %v\n", common.MustDecodeFloat64(friend.Key), string(friend.Value))
	}
	// Output:
	// name score
	// 10 alice
	// 6 charlie
	// 5 bob
	// 4 denise
}
func handleUDP(ch chan []byte, c *client.Conn, queueSize *int32) {
	var err error
	var mess common.Message
	for bytes := range ch {
		err = json.Unmarshal(bytes, &mess)
		if err == nil {
			if mess.Type == common.View {
				// Create a byte encoded timestamp for now
				t := time.Now().UnixNano()
				encT := godCommon.EncodeInt64(t)
				// Make the object id active
				c.SubPut(activeObjectsKey, []byte(mess.Object), encT)
				// Create a key for the views of this user
				vKey := uViewsKey(mess.User)
				// Make sure the sub tree is mirrored
				c.SubAddConfiguration(vKey, "mirrored", "yes")
				// Log this view
				c.SubPut(vKey, []byte(mess.Object), encT)
				// Create an encoded timestamp for something older than timeout
				tooOld := time.Now().Add(-time.Hour * 24 * time.Duration((*timeout))).UnixNano()
				// Delete all viewed entries with timestamp older than that
				for _, item := range c.MirrorSlice(vKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(vKey, item.Value)
				}
				// Delete all active entries with timestamp older than that
				for _, item := range c.MirrorSlice(activeObjectsKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(activeObjectsKey, item.Value)
				}
			} else if mess.Type == common.Like {
				// Record the liked object under user
				c.SubPut(uLikesKey(mess.User), []byte(mess.Object), godCommon.EncodeFloat64(mess.Weight))
				// Record the liker under the liked object
				c.SubPut(oLikesKey(mess.Object), []byte(mess.User), nil)
				if !mess.DontActivate {
					// Make the object id active
					c.SubPut(activeObjectsKey, []byte(mess.Object), nil)
				}
				// Create an encoded timestamp for something older than timeout
				tooOld := time.Now().Add(-time.Hour * 24 * time.Duration((*timeout))).UnixNano()
				// Delete all active entries with timestamp older than that
				for _, item := range c.MirrorSlice(activeObjectsKey, nil, godCommon.EncodeInt64(tooOld), true, true) {
					c.SubDel(activeObjectsKey, item.Value)
				}
			} else if mess.Type == common.Deactivate {
				// Remote the object id from the active objects
				c.SubDel(activeObjectsKey, []byte(mess.Object))
			}
		} else {
			fmt.Printf("When parsing %v: %v\n", string(bytes), err)
		}
		atomic.AddInt32(queueSize, -1)
	}
}
Exemple #7
0
//
// Puts a lifter (stored in the all lifters tree) together with best competition score, in ranking trees that are relevant
//
func PutLifterInRankingTrees(lifterItem common.Item) {

	conn := client.MustConn("localhost:9191")

	var lifterCompetitionItems []common.Item = conn.SliceIndex(lifterItem.Key, nil, nil) // Get all competitions under given lifter

	var lifter Lifter

	if err := json.Unmarshal(lifterItem.Value, &lifter); err != nil {
		panic(err)
	}

	var sinclairScores SinclairScoreByCategory = sortCompetitionRankingsByCategory(lifter, lifterCompetitionItems)

	if lifter.Sex == "female" {
		if sinclairScores.Youth != 0.0 {
			conn.SubPut(youthWomenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Youth))
		}

		if sinclairScores.Junior != 0.0 {
			conn.SubPut(juniorWomenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Junior))
		}

		if sinclairScores.Senior != 0.0 {
			conn.SubPut(seniorWomenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Senior))
		}

		if sinclairScores.Veteran != 0.0 {
			conn.SubPut(veteranWomenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Veteran))
		}
	} else { // Mens categories
		if sinclairScores.Youth != 0.0 {
			conn.SubPut(youthMenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Youth))
		}

		if sinclairScores.Junior != 0.0 {
			conn.SubPut(juniorMenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Junior))
		}

		if sinclairScores.Senior != 0.0 {
			conn.SubPut(seniorMenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Senior))
		}

		if sinclairScores.Veteran != 0.0 {
			conn.SubPut(veteranMenRankingKey, lifterItem.Key, common.EncodeFloat64(sinclairScores.Veteran))
		}
	}
}