Esempio n. 1
0
// Implements append method
func (enum *Ascii_protocol_enum) append(storage *cache.LRUCache) (string, error) {
	existed_item := storage.Get(enum.key[0])
	if existed_item == nil {
		return NOT_STORED, nil
	}
	existed_data := tools.ExtractStoredData(existed_item.Cacheable)
	if existed_data == nil {
		return NOT_STORED, nil
	}
	return enum.pending(storage, existed_item, append(existed_data, enum.data_string...)) // ...
}
Esempio n. 2
0
func TestCacheGetSuite1(t *testing.T) {
	cache := New(10)
	cache.Set(tools.NewStoredData([]byte("TEST"), "key"), 0, 0, 0)
	res := cache.Get("key")
	if res == nil {
		t.Fatalf("Unexpected value.", res)
	}
	extr := tools.ExtractStoredData(res.Cacheable)
	if string(extr) != "TEST" {
		t.Fatalf("Wrong returned value: %s", string(extr))
	}
}
Esempio n. 3
0
func TestHandlingSuitePrepend2(t *testing.T) {
	var storage = cache.New(42)
	var testEnum = Ascii_protocol_enum{"set", []string{"key"}, 1, 0, 4, 0, false, []byte("TEST"), ""}
	res, err := testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "STORED\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	testEnum = Ascii_protocol_enum{"prepend", []string{"key"}, 0, 0, 5, 0, false, []byte("TEST2"), ""}
	res, err = testEnum.HandleRequest(storage, nil)
	if err != nil || string(res) != "STORED\r\n" {
		t.Fatalf("Unexpected returned values of handling: ", err, res)
	}
	stored := tools.ExtractStoredData(storage.Get("key").Cacheable)
	if stored == nil || string(stored) != "TEST2TEST" {
		t.Fatalf("Stored value is invalid: ", err, stored)
	}
}
Esempio n. 4
0
// Implements get method
// Passed boolean param cas - defines of returning cas_unique
func (enum *Ascii_protocol_enum) get(storage *cache.LRUCache, cas bool) (string, error) {
	var result = ""
	for _, value := range enum.key {
		item := storage.Get(value)
		if item != nil {
			data := tools.ExtractStoredData(item.Cacheable)
			if data == nil {
				continue
			}
			result += "VALUE " + value + " " + tools.IntToString(int64(item.Flags)) + " " + tools.IntToString(int64(len(data)))
			if cas {
				cas_id := tools.GenerateCasId()
				storage.SetCas(value, cas_id)
				result += " " + tools.IntToString(cas_id)
			}
			result += "\r\n"
			result += string(data) + "\r\n"
		}
	}
	return result + "END\r\n", nil
}
Esempio n. 5
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
}