Example #1
0
// Function for parsing of storage group of commands.
// Receives array of string tokens.
// Returns pointer to Ascii_protocol_enum with bound fields.
func parseStorageCommands(args []string /*, data_block string*/) *Ascii_protocol_enum {
	protocol := new(Ascii_protocol_enum)
	if len(args) < 5 || /*len(data_block) == 0 ||*/ tools.In("", args) {
		return &Ascii_protocol_enum{error: ERROR_TEMP}
	}
	var err error
	//protocol.data_string = []byte(data_block)
	protocol.command = args[0]
	protocol.key = []string{args[1]}
	protocol.flags, err = tools.StringToInt32(args[2])
	protocol.exptime, err = tools.StringToInt64(args[3])
	protocol.exptime = tools.ToTimeStampFromNow(protocol.exptime)
	protocol.bytes, err = tools.StringToInt32(args[4])
	if args[0] == "cas" {
		if len(args) < 6 {
			err = errors.New("invalid arguments number")
		}
		protocol.cas_unique, err = tools.StringToInt64(args[5])
		if len(args) == 7 {
			protocol.noreply = (args[6] == "noreply")
		}
	} else {
		if len(args) == 6 {
			protocol.noreply = (args[5] == "noreply")
		}
	}
	if err != nil {
		return &Ascii_protocol_enum{error: ERROR_TEMP}
	}
	return protocol
}
Example #2
0
// Function for parsing of other group of commands.
// Receives array of string tokens.
// Returns pointer to Ascii_protocol_enum with bound fields.
func parseOtherCommands(args []string) *Ascii_protocol_enum {
	protocol := new(Ascii_protocol_enum)
	var err error
	err = nil
	if tools.In("", args) {
		err = errors.New("invalid arguments")
	}
	protocol.command = args[0]
	protocol.noreply = (args[len(args)-1] == "noreply")
	switch args[0] {
	case "delete":
		if len(args) < 2 {
			err = errors.New("invalid arguments number")
		} else {
			protocol.key = []string{args[1]}
		}
	case "touch":
		if len(args) < 3 {
			err = errors.New("invalid arguments number")
		} else {
			protocol.key = []string{args[1]}
			protocol.exptime, err = tools.StringToInt64(args[2])
		}
	case "flush_all":
		if len(args) >= 2 {
			protocol.exptime, err = tools.StringToInt64(args[1])
		}
	case "incr", "decr":
		if len(args) < 3 {
			err = errors.New("invalid arguments number")
		} else {
			protocol.key = []string{args[1]}
			protocol.data_string = []byte(args[2])
		}
		protocol.noreply = (args[len(args)-1] == "noreply")
	case "stats":
		protocol.key = args[1:]
	case "lru_crawler":
		if len(args) < 2 {
			err = errors.New("invalid arguments number")
		} else {
			protocol.key = args[1:]
		}
	}

	protocol.exptime = tools.ToTimeStampFromNow(protocol.exptime)
	if err != nil {
		return &Ascii_protocol_enum{error: ERROR_TEMP}
	}
	return protocol
}
Example #3
0
// Utility method, for joining common parts of incr/decr methods.
// Receives additional param sign, which defines operation: -1 or 1
func (enum *Ascii_protocol_enum) fold(storage *cache.LRUCache, sign int) (string, error) {
	if item := storage.Get(enum.key[0]); item != nil && (sign == 1 || sign == -1) {
		existed_data := tools.ExtractStoredData(item.Cacheable)
		if existed_data != nil {
			evaluated_data_for_existed, err_for_existed := tools.StringToInt64(string(existed_data))
			evaluated_data_for_passed, err_for_passed := tools.StringToUInt64(string(enum.data_string))
			if err_for_existed == nil && err_for_passed == nil {
				var result string
				if sign > 0 {
					result = tools.IntToString(evaluated_data_for_existed + int64(evaluated_data_for_passed))
				} else {
					result = tools.IntToString(evaluated_data_for_existed - int64(evaluated_data_for_passed))
				}
				if storage.Set(tools.NewStoredData([]byte(result), enum.key[0]), item.Flags, item.Exptime, 0) {
					return result + "\r\n", nil
				}
				return strings.Replace(SERVER_ERROR_TEMP, "%s", "Not enough memory", 1), errors.New("SERVER_ERROR")
			}
			return ERROR_TEMP, nil
		}
	}
	return NOT_FOUND, nil
}