Пример #1
0
func (self *FrontServer) Lock(lockName string, client trib.Storage, succ *bool) error {
	if e := client.Set(&trib.KeyValue{lockName, self.id}, succ); e != nil {
		return e
	} else {
		return nil
	}
}
Пример #2
0
// unlock return true means no other server modified data
func (self *FrontServer) Unlock(lockName string, client trib.Storage) (bool, error) {
	value := ""
	if e := client.Get(lockName, &value); e != nil {
		return false, e
	}
	if value == self.id {
		return true, nil
	} else {
		return false, nil
	}
}
Пример #3
0
func CheckStorage(t *testing.T, s trib.Storage) {
	var v string
	var b bool
	var l = new(trib.List)
	var n int

	ne := func(e error) {
		if e != nil {
			debug.PrintStack()
			t.Fatal(e)
		}
	}

	as := func(cond bool) {
		if !cond {
			debug.PrintStack()
			t.Fatal("assertion failed")
		}
	}

	kv := func(k, v string) *trib.KeyValue {
		return &trib.KeyValue{k, v}
	}

	pat := func(pre, suf string) *trib.Pattern {
		return &trib.Pattern{pre, suf}
	}

	v = "_"
	ne(s.Get("", &v))
	as(v == "")

	v = "_"
	ne(s.Get("hello", &v))
	as(v == "")

	ne(s.Set(kv("h8liu", "run"), &b))
	as(b)
	v = ""
	ne(s.Get("h8liu", &v))
	as(v == "run")

	ne(s.Set(kv("h8liu", "Run"), &b))
	as(b)
	v = ""
	ne(s.Get("h8liu", &v))
	as(v == "Run")

	ne(s.Set(kv("h8liu", ""), &b))
	as(b)
	v = "_"
	ne(s.Get("h8liu", &v))
	as(v == "")

	ne(s.Set(kv("h8liu", "k"), &b))
	as(b)
	v = "_"
	ne(s.Get("h8liu", &v))
	as(v == "k")

	ne(s.Set(kv("h8he", "something"), &b))
	as(b)
	v = "_"
	ne(s.Get("h8he", &v))
	as(v == "something")

	ne(s.Keys(pat("h8", ""), l))
	sort.Strings(l.L)
	as(len(l.L) == 2)
	as(l.L[0] == "h8he")
	as(l.L[1] == "h8liu")

	ne(s.ListGet("lst", l))
	fmt.Println("DEBUG in test: ", l.L)
	as(l.L != nil && len(l.L) == 0)

	ne(s.ListAppend(kv("lst", "a"), &b))
	as(b)

	ne(s.ListGet("lst", l))
	as(len(l.L) == 1)
	as(l.L[0] == "a")

	ne(s.ListAppend(kv("lst", "a"), &b))
	as(b)

	ne(s.ListGet("lst", l))
	as(len(l.L) == 2)
	as(l.L[0] == "a")
	as(l.L[1] == "a")

	ne(s.ListRemove(kv("lst", "a"), &n))
	as(n == 2)

	ne(s.ListGet("lst", l))
	as(l.L != nil && len(l.L) == 0)

	ne(s.ListAppend(kv("lst", "h8liu"), &b))
	as(b)
	ne(s.ListAppend(kv("lst", "h7liu"), &b))
	as(b)

	ne(s.ListGet("lst", l))
	as(len(l.L) == 2)
	as(l.L[0] == "h8liu")
	as(l.L[1] == "h7liu")

	ne(s.ListKeys(pat("ls", "st"), l))
	as(len(l.L) == 1)
	as(l.L[0] == "lst")

	ne(s.ListKeys(pat("z", ""), l))
	as(l.L != nil && len(l.L) == 0)

	ne(s.ListKeys(pat("", ""), l))
	as(len(l.L) == 1)
	as(l.L[0] == "lst")
}
Пример #4
0
func runCmd(s trib.Storage, args []string) bool {
	var v string
	var b bool
	var lst trib.List
	var n int
	var cret uint64

	cmd := args[0]

	switch cmd {
	case "get":
		logError(s.Get(single(args), &v))
		fmt.Println(v)
	case "set":
		logError(s.Set(kva(args), &b))
		fmt.Println(b)
	case "keys":
		logError(s.Keys(pata(args), &lst))
		printList(lst)
	case "list-get":
		logError(s.ListGet(single(args), &lst))
		printList(lst)
	case "list-append":
		logError(s.ListAppend(kva(args), &b))
		fmt.Println(b)
	case "list-remove":
		logError(s.ListRemove(kva(args), &n))
		fmt.Println(n)
	case "list-keys":
		logError(s.ListKeys(pata(args), &lst))
		printList(lst)
	case "clock":
		var c uint64
		var e error
		if len(args) >= 2 {
			c, e = strconv.ParseUint(args[1], 10, 64)
			logError(e)
		}
		logError(s.Clock(c, &cret))
		fmt.Println(cret)
	case "help":
		fmt.Println(cmdHelp)
	case "exit":
		return true
	default:
		logError(fmt.Errorf("bad command, try \"help\"."))
	}
	return false
}