Exemplo n.º 1
0
string HTWorker::append_shared(const ZPack &zpack) {

	string result;

	if (zpack.key().empty())
		return Const::ZSC_REC_EMPTYKEY; //-1

	string key = zpack.key();
	int ret = PMAP->append(key, zpack.SerializeAsString());

	if (ret != 0) {

		printf("thread[%lu] DB Error: fail to append, rcode = %d\n",
				pthread_self(), ret);
		fflush(stdout);

		result = Const::ZSC_REC_NONEXISTKEY; //-92
	} else {

		if (_instant_swap) {
			PMAP->writeFileFG();
		}

		result = Const::ZSC_REC_SUCC; //0, succeed.
	}

	return result;
}
Exemplo n.º 2
0
string ZHTClient::commonOpInternal(const string &opcode, const string &key,
		const string &val, const string &val2, string &result, int lease) {

	ZPack zpack;
	zpack.set_opcode(opcode); //"001": lookup, "002": remove, "003": insert, "004": append, "005", compare_swap
	zpack.set_replicanum(3);

	if (key.empty())
		return Const::ZSC_REC_EMPTYKEY; //-1, empty key not allowed.
	else
		zpack.set_key(key);

	if (val.empty()) {

		zpack.set_val("^"); //coup, to fix ridiculous bug of protobuf! //to debug
		zpack.set_valnull(true);
	} else {

		zpack.set_val(val);
		zpack.set_valnull(false);
	}

	if (val2.empty()) {

		zpack.set_newval("?"); //coup, to fix ridiculous bug of protobuf! //to debug
		zpack.set_newvalnull(true);
	} else {

		zpack.set_newval(val2);
		zpack.set_newvalnull(false);
	}

	zpack.set_lease(Const::toString(lease));

	string msg = zpack.SerializeAsString();

	/*ZPack tmp;
	 tmp.ParseFromString(msg);
	 printf("{%s}:{%s,%s}\n", tmp.key().c_str(), tmp.val().c_str(),
	 tmp.newval().c_str());*/

	char *buf = (char*) calloc(_msg_maxsize, sizeof(char));
	size_t msz = _msg_maxsize;

	/*send to and receive from*/
	_proxy->sendrecv(msg.c_str(), msg.size(), buf, msz);

	/*...parse status and result*/
	string sstatus;

	string srecv(buf);

	if (srecv.empty()) {

		sstatus = Const::ZSC_REC_SRVEXP;
	} else {

		result = srecv.substr(3); //the left, if any, is lookup result or second-try zpack
		sstatus = srecv.substr(0, 3); //status returned, the first three chars, like 001, -98...
	}

	free(buf);
	return sstatus;
}
Exemplo n.º 3
0
string ZHTClient::commonOpInternal(const string &opcode, const string &key,
		const string &val, const string &val2, string &result, int lease) {

	ZPack zpack;
	zpack.set_opcode(opcode); //"001": lookup, "002": remove, "003": insert, "004": append, "005", compare_swap
	zpack.set_replicanum(3);

        //cout<<"Op Code"<<opcode<<"Key"<<key<<endl;

	if (key.empty())
		return Const::ZSC_REC_EMPTYKEY; //-1, empty key not allowed.
	else
		zpack.set_key(key);

	if (val.empty()) {

		zpack.set_val("^"); //coup, to fix ridiculous bug of protobuf! //to debug
		zpack.set_valnull(true);
	} else {

		zpack.set_val(val);
		zpack.set_valnull(false);
	}

	if (val2.empty()) {

		zpack.set_newval("?"); //coup, to fix ridiculous bug of protobuf! //to debug
		zpack.set_newvalnull(true);
	} else {

		zpack.set_newval(val2);
		zpack.set_newvalnull(false);
	}

	zpack.set_lease(Const::toString(lease));
//cout<<"lease "<<lease<<endl;
	string msg = zpack.SerializeAsString();
//cout<<msg<<endl;
	/*ZPack tmp;
	 tmp.ParseFromString(msg);
	 printf("{%s}:{%s,%s}\n", tmp.key().c_str(), tmp.val().c_str(),
	 tmp.newval().c_str());*/
/*

	char *buf = (char*) calloc(_msg_maxsize, sizeof(char));
	size_t msz = _msg_maxsize;

*/
	/*send to and receive from*/
	// this is old making large msg problem
	//_proxy->sendrecv(msg.c_str(), msg.size(), buf, msz);

	TCPProxy tcp;
		ZHTUtil zu;
		HostEntity he = zu.getHostEntityByKey(msg);
		int sock = tcp.getSockCached(he.host, he.port);
		tcp.sendTo(sock, (void*) msg.c_str(), msg.size());
		string res;

		int recvcount = loopedrecv(sock, NULL, res);

	/*...parse status and result*/
	string sstatus;
	//  srecv is no longer used due to a large msg fix. now instead of sendrecv,
	//we are using sendto and loopedrecv methods
/*
	string srecv(buf);
cout<<"res value is "<<res<<endl;
	if (srecv.empty()) {
		cout<<"empty"<<endl;
		sstatus = Const::ZSC_REC_SRVEXP;
	} else {

		result = srecv.substr(3); //the left, if any, is lookup result or second-try zpack
		//cout<<"srecv string size="<<srecv.size()<<endl;
		if (srecv.size() == 36) {
			sstatus = srecv.substr(0, 36);
			//sstatus=Const::ZSC_REC_REMOVEMETADATAQUEUE;
		} else {
			sstatus = srecv.substr(0, 3); //status returned, the first three chars, like 001, -98...
		}

	}
*/
//cout<<"res value is "<<res<<endl;
	if (res.empty()) {
		//cout<<"empty"<<endl;
		sstatus = Const::ZSC_REC_SRVEXP;
	} else {

		result = res.substr(3); //the left, if any, is lookup result or second-try zpack
		//cout<<"srecv string size="<<srecv.size()<<endl;
		if (res.size() == 36) {
			sstatus = res.substr(0, 36);
			//sstatus=Const::ZSC_REC_REMOVEMETADATAQUEUE;
		} else {
			sstatus = res.substr(0, 3); //status returned, the first three chars, like 001, -98...
		}

	}


//	free(buf);
	return sstatus;
}