示例#1
0
func init() {

	store.RegisterStoreType(StringType)

	store.DefaultDBManager.AddFuncWithSideEffects("append", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		if ok {
			str, _ := elem.Value.(string)
			str += args[1]
			elem.Value = str
			return reply.IntReply(len(str))
		} else {
			db.StoreSet(args[0], &data.Entry{args[1], StringType})
			return reply.IntReply(len(args[1]))
		}
	})

	bitCount := []uint8{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}

	store.DefaultDBManager.AddFuncWithSideEffects("bitcount", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		if ok {
			byte_arr, _ := elem.Value.([]byte)
			c := 0
			for _, c := range byte_arr {
				c += bitCount[c]
			}
			return reply.IntReply(c)
		} else {
			return reply.IntReply(0)
		}
	})

	store.DefaultDBManager.AddFuncWithSideEffects("decr", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		var num int
		if ok {
			str, _ := elem.Value.(string)
			num, err := strconv.Atoi(str)
			if err != nil {
				panic(err)
			}
			num -= 1
			elem.Value = fmt.Sprintf("%d", num)
		} else {
			num = -1
			db.StoreSet(args[0], &data.Entry{"-1", StringType})
		}
		return reply.IntReply(num)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("decrby", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		var num int

		decrby, err := strconv.Atoi(args[1])
		if err != nil {
			panic(err)
		}

		if ok {
			str, _ := elem.Value.(string)

			num, err := strconv.Atoi(str)
			if err != nil {
				panic(err)
			}

			num -= decrby
			elem.Value = fmt.Sprintf("%d", num)
		} else {
			str := fmt.Sprintf("-%d", decrby)
			db.StoreSet(args[0], &data.Entry{str, StringType})
		}
		println(num)
		return reply.IntReply(num)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("incr", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		var num int
		if ok {
			str, _ := elem.Value.(string)
			num, err := strconv.Atoi(str)
			if err != nil {
				panic(err)
			}
			num += 1
			elem.Value = fmt.Sprintf("%d", num)
		} else {
			num = 1
			db.StoreSet(args[0], &data.Entry{"-1", StringType})
		}
		return reply.IntReply(num)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("incrby", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		var num int

		decrby, err := strconv.Atoi(args[1])
		if err != nil {
			panic(err)
		}

		if ok {
			str, _ := elem.Value.(string)

			num, err := strconv.Atoi(str)
			if err != nil {
				panic(err)
			}

			num += decrby
			elem.Value = fmt.Sprintf("%d", num)
		} else {
			str := fmt.Sprintf("%d", decrby)
			db.StoreSet(args[0], &data.Entry{str, StringType})
		}
		println(num)
		return reply.IntReply(num)
	})

	store.DefaultDBManager.AddFunc("get", func(db *store.DB, args []string) string {
		elem, ok, err := db.StoreGet(args[0], StringType)
		if err != nil {
			return reply.ErrorReply(err)
		}
		if ok {
			str := elem.GetString()
			return reply.BulkReply(str)
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFunc("getset", func(db *store.DB, args []string) string {
		elem, ok, err := db.StoreGet(args[0], StringType)
		if err != nil {
			return reply.ErrorReply(err)
		}
		if ok {
			str := elem.GetString()
			elem.Value = args[1]
			return reply.BulkReply(str)
		} else {
			db.StoreSet(args[0], &data.Entry{args[1], StringType})
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFuncWithSideEffects("psetex", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)

		millis, err := strconv.Atoi(args[1])
		if err != nil {
			panic(err)
		}

		db.ExpiryRecord[args[0]] = store.Milliseconds() + uint64(millis)

		if ok {
			elem.Value = args[2]
		} else {
			db.StoreSet(args[0], &data.Entry{args[2], StringType})
		}

		return reply.OKReply
	})

	store.DefaultDBManager.AddFuncWithSideEffects("setex", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)

		seconds, err := strconv.Atoi(args[1])
		if err != nil {
			panic(err)
		}

		db.ExpiryRecord[args[0]] = store.Milliseconds() + uint64(seconds)*1000

		if ok {
			elem.Value = args[2]
		} else {
			db.StoreSet(args[0], &data.Entry{args[2], StringType})
		}

		return reply.OKReply
	})

	store.DefaultDBManager.AddFuncWithSideEffects("set", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], StringType)
		if ok {
			elem.Value = args[1]
		} else {
			db.StoreSet(args[0], &data.Entry{args[1], StringType})
		}
		return reply.OKReply
	})

	store.DefaultDBManager.AddFuncWithSideEffects("setnx", func(db *store.DB, args []string) string {
		s := args[0]
		_, ok, _ := db.StoreGet(s, StringType)
		if ok {
			return reply.IntReply(0)
		}
		db.StoreSet(s, &data.Entry{args[1], StringType})
		return reply.IntReply(1)
	})

	store.DefaultDBManager.AddFunc("strlen", func(db *store.DB, args []string) string {
		elem, ok, err := db.StoreGet(args[0], StringType)
		if err != nil {
			return reply.ErrorReply(err)
		}
		if ok {
			str := elem.GetString()
			return reply.IntReply(len(str))
		} else {
			return reply.IntReply(0)
		}
	})

}
示例#2
0
文件: keys.go 项目: brianbrunner/omg
func init() {

	store.DefaultDBManager.AddFuncWithSideEffects("del", func(db *store.DB, args []string) string {
		del_count := 0
		for _, v := range args {
			ok := db.StoreDel(v)
			if ok {
				del_count += 1
			}
		}
		return reply.IntReply(del_count)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("expire", func(db *store.DB, args []string) string {
		_, ok, _ := db.StoreGet(args[0], data.Any)
		if ok {
			millis := store.Milliseconds()
			expires, err := strconv.Atoi(args[1])
			if err != nil {
				return reply.IntReply(0)
			} else {
				db.ExpiryRecord[args[0]] = millis + uint64(expires*1000)
				return reply.IntReply(1)
			}
		}
		return reply.IntReply(0)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("pexpire", func(db *store.DB, args []string) string {
		_, ok, _ := db.StoreGet(args[0], data.Any)
		if ok {
			millis := store.Milliseconds()
			expires, err := strconv.Atoi(args[1])
			if err != nil {
				return reply.IntReply(0)
			} else {
				db.ExpiryRecord[args[0]] = millis + uint64(expires)
				return reply.IntReply(1)
			}
		}
		return reply.IntReply(0)
	})

	store.DefaultDBManager.AddFunc("ttl", func(db *store.DB, args []string) string {
		_, ok, _ := db.StoreGet(args[0], data.Any)
		if !ok {
			return reply.IntReply(-2)
		}

		time, ok := db.ExpiryRecord[args[0]]
		millis := store.Milliseconds()

		if ok {
			if time == 0 {
				return reply.IntReply(-1)
			} else if time < millis {
				db.StoreDel(args[0])
				return reply.IntReply(-2)
			} else {
				return reply.IntReply(int((time - millis) / 1000))
			}
		} else {
			return reply.IntReply(-1)
		}
	})

	store.DefaultDBManager.AddFunc("pttl", func(db *store.DB, args []string) string {
		_, ok, _ := db.StoreGet(args[0], data.Any)
		if !ok {
			return reply.IntReply(-2)
		}

		time, ok := db.ExpiryRecord[args[0]]
		millis := store.Milliseconds()

		if ok {
			if time == 0 {
				return reply.IntReply(-1)
			} else if time < millis {
				db.StoreDel(args[0])
				return reply.IntReply(-2)
			} else {
				return reply.IntReply(int(time - millis))
			}
		} else {
			return reply.IntReply(-1)
		}
	})

	store.DefaultDBManager.AddFunc("exists", func(db *store.DB, args []string) string {
		_, ok, _ := db.StoreGet(args[0], data.Any)
		if ok {
			return reply.IntReply(1)
		}
		return reply.IntReply(0)
	})

	store.DefaultDBManager.AddFunc("dump", func(db *store.DB, args []string) string {
		bytes_rep, ok := db.StoreDump(args[0])
		fmt.Println(bytes_rep)
		str_rep := string(bytes_rep)
		if ok {
			return reply.BulkReply(str_rep)
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFuncWithSideEffects("restore", func(db *store.DB, args []string) string {
		unquoted_data, _ := strconv.Unquote(args[1])
		fmt.Println(unquoted_data)
		bytes_rep := []byte(unquoted_data)
		ok := db.StoreLoad(args[0], bytes_rep)
		if ok {
			return reply.OKReply
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFunc("type", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], data.Any)
		if ok {
			return reply.IntReply(int(elem.Type()))
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFunc("keys", func(db *store.DB, args []string) string {
		keys := db.StoreKeysMatch(args[0])
		var writer reply.MultiBulkWriter
		writer.WriteCount(len(keys))
		for _, key := range keys {
			writer.WriteString(key)
		}
		return writer.String()
	})

	store.DefaultDBManager.AddFunc("randomkey", func(db *store.DB, args []string) string {
		for key, _ := range db.Store {
			return reply.BulkReply(key)
		}
		return reply.NilReply
	})

	store.DefaultDBManager.AddFunc("ping", func(db *store.DB, args []string) string {
		return reply.BulkReply("PONG")
	})

}
示例#3
0
文件: user.go 项目: brianbrunner/omg
func init() {

	gob.Register(&User{})

	store.RegisterStoreType(UserType)

	store.DefaultDBManager.AddFuncWithSideEffects("useradd", func(db *store.DB, args []string) string {
		s := args[0]
		_, ok, _ := db.StoreGet(s, UserType)
		if ok {
			return reply.ErrorReply("Key already exists")
		} else {
			raw_hash, _ := scrypt.Key([]byte(args[1]), []byte(args[0]), 16384, 8, 1, 32)
			b64_hash := store.Base64Encode(raw_hash)
			final_hash := fmt.Sprintf("$%d$%d$%d$%s", 16384, 8, 1, string(b64_hash))
			user := &User{args[0], final_hash, "", 0}
			db.StoreSet(args[0], &data.Entry{user, UserType})
			return reply.OKReply
		}
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("userget", func(db *store.DB, args []string) string {
		s := args[0]
		e, ok, _ := db.StoreGet(s, UserType)
		if ok {
			if user, ok := e.Value.(*User); ok {
				score_str := fmt.Sprintf("%d", user.Score)
				var rep reply.MultiBulkWriter
				rep.WriteCount(2)
				rep.WriteString(user.Username)
				rep.WriteString(score_str)
				return rep.String()
			} else {
				return reply.ErrorReply("Type Mismatch")
			}
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFuncWithSideEffects("userauth", func(db *store.DB, args []string) string {
		username := args[0]
		e, ok, _ := db.StoreGet(username, UserType)
		if ok {
			if user, ok := e.Value.(*User); ok {
				password := args[1]
				raw_hash, _ := scrypt.Key([]byte(password), []byte(username), 16384, 8, 1, 32)
				b64_hash := store.Base64Encode(raw_hash)
				final_hash := fmt.Sprintf("$%d$%d$%d$%s", 16384, 8, 1, string(b64_hash))
				if final_hash == user.Hash {
					c := 64
					b := make([]byte, c)
					n, err := io.ReadFull(rand.Reader, b)
					if n != len(b) || err != nil {
						return reply.ErrorReply("Unable to generate a secure random token")
					}
					token := store.Base64Encode(b)
					token_str := string(token)
					db.StoreSet(token_str, &data.Entry{username, 0})
					if user.Token != "" {
						db.StoreDel(token_str)
					}
					user.Token = token_str
					return reply.BulkReply(token_str)
				} else {
					return reply.ErrorReply("Password is incorrect")
				}
			} else {
				return reply.ErrorReply("How did you manage to mess things up this good?")
			}
		} else {
			return reply.ErrorReply("No user exists with that username")
		}
	})

	store.DefaultDBManager.AddFunc("usertoken", func(db *store.DB, args []string) string {
		token := args[0]

		e, ok, _ := db.StoreGet(token, strings.StringType)
		if ok {
			str, _ := e.Value.(string)
			return reply.BulkReply(str)
		} else {
			return reply.ErrorReply("Invalid or expired token")
		}
	})

}
示例#4
0
func init() {

	store.DefaultDBManager.AddFunc("select", func(db *store.DB, args []string) string {
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("info", func(db *store.DB, args []string) string {
		return reply.BulkReply("# Server\r\nredis_version:2.6.7\r\n")
	})

	store.DefaultDBManager.AddFunc("meminuse", func(db *store.DB, args []string) string {
		return reply.IntReply(int(store.MemInUse()))
	})

	store.DefaultDBManager.AddFunc("objectsinuse", func(db *store.DB, args []string) string {
		var memProfiles []runtime.MemProfileRecord
		n, _ := runtime.MemProfile(memProfiles, false)
		memProfiles = make([]runtime.MemProfileRecord, n)
		n, _ = runtime.MemProfile(memProfiles, false)

		objects := int64(0)
		for _, prof := range memProfiles {
			objects += prof.InUseObjects()
		}

		return reply.IntReply(int(objects))
	})

	store.DefaultDBManager.AddFunc("gc", func(db *store.DB, args []string) string {
		runtime.GC()
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("freeosmemory", func(db *store.DB, args []string) string {
		debug.FreeOSMemory()
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("save", func(db *store.DB, args []string) string {
		err := db.SaveToDiskSync()
		if err != nil {
			return reply.ErrorReply(err)
		} else {
			return reply.OKReply
		}
	})

	store.DefaultDBManager.AddFunc("bgsave", func(db *store.DB, args []string) string {
		store.DefaultDBManager.SaveToDiskAsync(nil)
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("__end_save_mode__", func(db *store.DB, args []string) string {
		db.EndSaveMode()
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("dbsize", func(db *store.DB, args []string) string {
		return reply.IntReply(store.DefaultDBManager.DBSize())
	})

	store.DefaultDBManager.AddFunc("lastdump", func(db *store.DB, args []string) string {
		elem, ok, _ := db.StoreGet(args[0], data.Any)
		if ok {
			return reply.IntReply(int(elem.LastDump()))
		} else {
			return reply.NilReply
		}
	})

	store.DefaultDBManager.AddFunc("lastdumpdb", func(db *store.DB, args []string) string {
		return reply.IntReply(int(db.LastDump()))
	})

	store.DefaultDBManager.AddFunc("flush", func(db *store.DB, args []string) string {
		db.Flush()
		return reply.OKReply
	})

	store.DefaultDBManager.AddFunc("keysperdb", func(db *store.DB, args []string) string {
		var w reply.MultiBulkWriter
		dbs := store.DefaultDBManager.GetDBs()
		w.WriteCount(len(dbs))
		for _, db := range dbs {
			w.WriteString(fmt.Sprintf("%d", len(db.Store)))
		}
		return w.String()
	})

}
示例#5
0
文件: set.go 项目: brianbrunner/omg
func init() {

	gob.Register(make(map[string]bool))

	store.RegisterStoreType(SetType)

	store.DefaultDBManager.AddFuncWithSideEffects("sadd", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil {
			return reply.ErrorReply("Type Mismatch")
		}

		var setData map[string]bool
		if !ok {
			setData = make(map[string]bool)
			db.StoreSet(args[0], &data.Entry{setData, SetType})
		} else {
			setData, _ = e.Value.(map[string]bool)
		}

		l := len(args)
		sdata := args[1:l]
		count := 0
		for _, member := range sdata {
			_, ok = setData[member]
			if !ok {
				setData[member] = true
				count += 1
			}
		}
		return reply.IntReply(count)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("srem", func(db *store.DB, args []string) string {

		e, ok, err := db.StoreGet(args[0], SetType)
		if err != nil {
			return reply.ErrorReply("Type Mismatch")
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			setData = make(map[string]bool)
			db.StoreSet(args[0], &data.Entry{setData, SetType})
		}

		l := len(args)
		sdata := args[1:l]
		count := 0
		for _, member := range sdata {
			_, ok = setData[member]
			if ok {
				delete(setData, member)
				count += 1
			}
		}

		return reply.IntReply(count)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("srem", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)
		if err != nil {
			return reply.ErrorReply("Type Mismatch")
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			setData = make(map[string]bool)
		}
		l := len(args)
		sdata := args[1:l]
		count := 0
		for _, member := range sdata {
			_, ok = setData[member]
			if ok {
				delete(setData, member)
				count += 1
			}
		}
		return reply.IntReply(count)
	})

	store.DefaultDBManager.AddFunc("sismember", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil || !ok {
			return "-ERR Type Mismatch\r\n"
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			return reply.IntReply(0)
		}
		_, ok = setData[args[1]]
		if ok {
			return reply.IntReply(1)
		}
		return reply.IntReply(0)
	})

	store.DefaultDBManager.AddFunc("scard", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil || !ok {
			return reply.ErrorReply("Type Mismatch")
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if ok {
			return reply.IntReply(len(setData))
		}
		return reply.IntReply(0)
	})

	store.DefaultDBManager.AddFuncWithSideEffects("spop", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil || !ok {
			return "-ERR Type Mismatch\r\n"
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			return reply.NilReply
		}
		for k, _ := range setData {
			delete(setData, k)
			return reply.BulkReply(k)
		}
		return reply.NilReply
	})

	store.DefaultDBManager.AddFuncWithSideEffects("smembers", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil || !ok {
			return "-ERR Type Mismatch\r\n"
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			return reply.NilReply
		}

		var w reply.MultiBulkWriter
		w.WriteCount(len(setData))
		for k, _ := range setData {
			w.WriteString(k)
		}
		return w.String()
	})

	store.DefaultDBManager.AddFuncWithSideEffects("srandmember", func(db *store.DB, args []string) string {
		e, ok, err := db.StoreGet(args[0], SetType)

		if err != nil || !ok {
			return "-ERR Type Mismatch\r\n"
		}
		if !ok {
			return reply.NilReply
		}

		setData, ok := e.Value.(map[string]bool)
		if !ok {
			return reply.NilReply
		}

		if len(args) > 1 {

			count, err := strconv.Atoi(args[1])
			if err != nil {
				panic(err)
			}

			var w reply.MultiBulkWriter
			w.WriteCount(count)
			for k, _ := range setData {
				w.WriteString(k)
				count -= 1
				if count == 0 {
					break
				}
			}
			return w.String()

		} else {

			for k, _ := range setData {
				return reply.BulkReply(k)
			}

		}

		return reply.NilReply

	})

}