Esempio n. 1
0
int Client::SetExpiry(const ByteString &key, uint64_t expiryTime)
{
	int			status;
	Command*	cmd;
	ByteString	args[2];
	DynArray<32> numString;

	VALIDATE_CLIENT();
	VALIDATE_KEY_LEN(key);
	VALIDATE_SAFE();
	VALIDATE_WRITE();

	numString.Writef("%U", expiryTime);

	args[0] = key;
	args[1] = numString;
	
	cmd = CreateCommand(KEYSPACECLIENT_SET_EXPIRY, SIZE(args), args);
	safeCommands.Append(cmd);
	
	if (IS_BATCHED())
	{
		result->AppendCommand(cmd);
		return KEYSPACE_SUCCESS;
	}
	
	result->Close();
	result->AppendCommand(cmd);

	EventLoop();
	status = result->CommandStatus();
	
	return status;
}
Esempio n. 2
0
int Client::Add(const ByteString &key, int64_t num, int64_t &res)
{
	Command*	cmd;
	ByteString	args[2];
	DynArray<32> numString;
	unsigned	nread;
	int			status;
	ByteString	value;

	VALIDATE_CLIENT();
	VALIDATE_KEY_LEN(key);
	VALIDATE_SAFE();
	VALIDATE_WRITE();

	numString.Writef("%U", num);
	
	args[0] = key;
	args[1] = numString;
	
	cmd = CreateCommand(KEYSPACECLIENT_ADD, 2, args);
	safeCommands.Append(cmd);
	
	if (IS_BATCHED())
	{
		result->AppendCommand(cmd);
		return KEYSPACE_SUCCESS;
	}
	
	result->Close();
	result->AppendCommand(cmd);
	
	EventLoop();
	status = result->CommandStatus();
	
	if (status != KEYSPACE_SUCCESS)
		return status;
	
	status = result->Value(value);
	// TODO: check conversion
	if (status == KEYSPACE_SUCCESS)
		res = strntoint64(value.buffer, value.length, &nread);

	return status;
}
Esempio n. 3
0
bool HttpFileHandler::HandleRequest(HttpConn* conn, const HttpRequest& request)
{
	DynArray<128>	path;
	DynArray<128>	tmp;
	DynArray<128>	ha;
	char			buf[128 * 1024];
	FILE*			fp;
	size_t			nread;
	size_t			fsize;
	const char*		mimeType;
	
	if (strncmp(request.line.uri, prefix, strlen(prefix)))
		return false;
	
	path.Writef("%s%s", documentRoot, request.line.uri);
	path.Append("", 1);
	
	mimeType = MimeTypeFromExtension(strrchr(path.buffer, '.'));
	
	fp = fopen(path.buffer, "rb");
	if (!fp)
		return false;
	
	fseek(fp, 0L, SEEK_END);
	fsize = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	
	HttpHeaderAppend(ha, 
		HTTP_HEADER_CONTENT_TYPE, sizeof(HTTP_HEADER_CONTENT_TYPE) - 1,
		mimeType, strlen(mimeType));
	
	tmp.Writef("%u", fsize);
	HttpHeaderAppend(ha,
		HTTP_HEADER_CONTENT_LENGTH, sizeof(HTTP_HEADER_CONTENT_LENGTH) - 1,
		tmp.buffer, tmp.length);

	// zero-terminate
	ha.Append("", 1);
	
	conn->ResponseHeader(HTTP_STATUS_CODE_OK, ha.buffer);
	
	while (true)
	{
		nread = fread(buf, 1, sizeof(buf), fp);
		if (feof(fp))
		{
			fclose(fp);
			conn->Write(buf, nread, true);
			break;
		}
		if (ferror(fp))
		{
			fclose(fp);
			break;
		}
		
		conn->Write(buf, nread);
	}

	conn->Flush();
	return true;
}
Esempio n. 4
0
int Client::ListKeyValues(const ByteString &prefix,
const ByteString &startKey, uint64_t count,
bool next, bool forward, bool dirty, bool values)
{
	Command*		cmd;
	ByteString		args[5];
	DynArray<32>	countString;
	DynArray<10>	nextString;
	DynArray<10>	backString;
	ByteString		sk;

	VALIDATE_CLIENT();
	VALIDATE_NOT_BATCHED();
	VALIDATE_KEY_LEN(prefix);

	countString.Writef("%U", count);
	if (next)
		nextString.Append("1", 1);
	else
		nextString.Append("0", 1);

	if (forward)
		backString.Append("f", 1);
	else
		backString.Append("b", 1);

	if (prefix.length > 0 && startKey.length >= prefix.length)
	{
		if (memcmp(prefix.buffer, startKey.buffer,
			MIN(prefix.length, startKey.length)) == 0)
		{
			sk.buffer = startKey.buffer + prefix.length;
			sk.length = startKey.length - prefix.length;
			sk.size = startKey.size - prefix.length;
		}
	}
	else
		sk = startKey; // TODO: this seems inconsistent

	args[0] = prefix;
	args[1] = sk;
	args[2] = countString;
	args[3] = nextString;
	args[4] = backString;
	
	if (dirty)
	{
		VALIDATE_DIRTY();
		
		if (values)
			cmd = CreateCommand(KEYSPACECLIENT_DIRTY_LISTP, SIZE(args), args);
		else
			cmd = CreateCommand(KEYSPACECLIENT_DIRTY_LIST, SIZE(args), args);

		dirtyCommands.Append(cmd);
	}
	else
	{
		VALIDATE_SAFE();
		
		if (values)
			cmd = CreateCommand(KEYSPACECLIENT_LISTP, SIZE(args), args);
		else
			cmd = CreateCommand(KEYSPACECLIENT_LIST, SIZE(args), args);
		
		safeCommands.Append(cmd);
	}
	
	result->Close();
	result->AppendCommand(cmd);
	
	EventLoop();
	return result->CommandStatus();	

}
Esempio n. 5
0
int Client::Count(uint64_t &res, const ByteString &prefix,
const ByteString &startKey,
uint64_t count = 0, bool next = false, bool forward = false, bool dirty = false)
{
	Command*		cmd;
	ByteString		args[5];
	DynArray<32>	countString;
	DynArray<10>	nextString;
	DynArray<10>	backString;
	ByteString		sk;
	int				status;
	unsigned		nread;
	ByteString		value;

	VALIDATE_CLIENT();
	VALIDATE_NOT_BATCHED();
	VALIDATE_KEY_LEN(prefix);
	
	countString.Writef("%U", count);
	if (next)
		nextString.Append("1", 1);
	else
		nextString.Append("0", 1);

	if (forward)
		backString.Append("f", 1);
	else
		backString.Append("b", 1);

	if (prefix.length > 0 && startKey.length >= prefix.length)
	{
		if (memcmp(prefix.buffer, startKey.buffer,
			MIN(prefix.length, startKey.length)) == 0)
		{
			sk.buffer = startKey.buffer + prefix.length;
			sk.length = startKey.length - prefix.length;
			sk.size = startKey.size - prefix.length;
		}
	}
	else
		sk = startKey; // TODO: this seems inconsistent

	args[0] = prefix;
	args[1] = sk;
	args[2] = countString;
	args[3] = nextString;
	args[4] = backString;
	
	if (dirty)
	{
		VALIDATE_DIRTY();
		cmd = CreateCommand(KEYSPACECLIENT_DIRTY_COUNT,	SIZE(args), args);
		dirtyCommands.Append(cmd);
	}
	else
	{
		VALIDATE_SAFE();
		cmd = CreateCommand(KEYSPACECLIENT_COUNT, SIZE(args), args);
		safeCommands.Append(cmd);
	}
	
	result->Close();
	result->AppendCommand(cmd);
	
	EventLoop();
	status = result->CommandStatus();
	if (status != KEYSPACE_SUCCESS)
		return status;
	
	// TODO check conversion
	status = result->Value(value);
	if (status == KEYSPACE_SUCCESS)
		res = strntoint64(value.buffer, value.length, &nread);

	return status;
}