Example #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()
	if *dir == address {
		*dir = fmt.Sprintf("%v:%v", *ip, *port)
	}
	s := dhash.NewNodeDir(fmt.Sprintf("%v:%v", *ip, *port), *dir)
	if *verbose {
		s.AddChangeListener(func(ring *common.Ring) bool {
			fmt.Println(s.Describe())
			return true
		})
		s.AddMigrateListener(func(dhash *dhash.Node, source, destination []byte) bool {
			fmt.Printf("Migrated from %v to %v\n", common.HexEncode(source), common.HexEncode(destination))
			return true
		})
		s.AddSyncListener(func(source, dest common.Remote, pulled, pushed int) bool {
			fmt.Printf("%v pulled %v and pushed %v keys synchronizing with %v\n", source.Addr, pulled, pushed, dest.Addr)
			return true
		})
		s.AddCleanListener(func(source, dest common.Remote, cleaned, pushed int) bool {
			fmt.Printf("%v cleaned %v and pushed %v keys to %v\n", source.Addr, cleaned, pushed, dest.Addr)
			return true
		})
	}
	s.MustStart()
	if *joinIp != "" {
		s.MustJoin(fmt.Sprintf("%v:%v", *joinIp, *joinPort))
	}

	for {
		time.Sleep(time.Second * 10)
	}
}
Example #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()
	if *dir == address {
		*dir = fmt.Sprintf("%v:%v", *broadcastIp, *port)
	}
	s := dhash.NewNodeDir(fmt.Sprintf("%v:%v", *listenIp, *port), fmt.Sprintf("%v:%v", *broadcastIp, *port), *dir)
	s.MustStart()
	if *joinIp != "" {
		s.MustJoin(fmt.Sprintf("%v:%v", *joinIp, *joinPort))
	}

	c := client.MustConn(s.GetListenAddr())
	c.Start()

	setupUDPService(c)
	setupJSONService(c)
}
Example #3
0
func ExampleTreeMirror() {
	server := dhash.NewNodeDir("127.0.0.1:3030", "127.0.0.1:3030", "").MustStart()
	defer server.Stop()
	conn := client.MustConn("127.0.0.1:3030")
	conn.SubAddConfiguration([]byte("myfriends"), "mirrored", "yes")
	conn.SubPut([]byte("myfriends"), []byte("alice"), setop.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), setop.EncodeFloat64(5))
	conn.SubPut([]byte("myfriends"), []byte("charlie"), setop.EncodeFloat64(6))
	conn.SubPut([]byte("myfriends"), []byte("denise"), setop.EncodeFloat64(4))
	fmt.Printf("name score\n")
	for _, friend := range conn.MirrorReverseSlice([]byte("myfriends"), nil, nil, true, true) {
		fmt.Printf("%v %v\n", fmt.Sprint(setop.DecodeFloat64(friend.Key)), string(friend.Value))
	}
	// Output:
	// name score
	// 10 <nil> alice
	// 6 <nil> charlie
	// 5 <nil> bob
	// 4 <nil> denise
}
Example #4
0
func ExampleSetExpression() {
	server := dhash.NewNodeDir("127.0.0.1:2020", "127.0.0.1:2020", "").MustStart()
	defer server.Stop()
	conn := client.MustConn("127.0.0.1:2020")
	conn.SubPut([]byte("myfriends"), []byte("alice"), setop.EncodeFloat64(10))
	conn.SubPut([]byte("myfriends"), []byte("bob"), setop.EncodeFloat64(5))
	conn.SubPut([]byte("yourfriends"), []byte("bob"), setop.EncodeFloat64(6))
	conn.SubPut([]byte("yourfriends"), []byte("charlie"), setop.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), fmt.Sprint(setop.DecodeFloat64(friend.Values[0])))
	}
	// Output:
	// name score
	// alice 10 <nil>
	// bob 11 <nil>
	// charlie 4 <nil>
}