int general_replica(Package package, struct HostEntity &destination) {
	package.set_replicano(3);
	string str = package.SerializeAsString();
//      cout << "socket_replica--------1" << endl;
//      cout << "socket_replica--------before makeConnForReplica sock = "<< destination.sock << endl;
	int sock = makeConnForReplica(destination); //reusable sockets creation
//      cout << "socket_replica--------after makeConnForReplica sock = "<< destination.sock << endl;
//      int sock = makeClientSocket("localhost", 50009, true);

//      cout << "socket_replica--------2,  sock = " << sock << endl;

	//      generalSend(destination.host, destination.port, sock, str.c_str(), 1);
//      cout << "socket_replica--------2, sock = " << sock << endl;
//        generalSendTCP(sock, str.c_str());
	generalSendTo(destination.host.c_str(), destination.port, sock, str.c_str(),
			str.size(), TCP);
//      cout << "socket_replica--------3" << endl;
	void *buff_return = (void*) malloc(sizeof(int32_t));
	//      int r = d3_svr_recv(sock, buff_return, sizeof(int32_t), 0, &recv_addr);
	// int r = generalReveiveTCP(sock, buff_return, sizeof buff_return, 0);
	struct sockaddr_in recvAddr;
//		int r =generalReceive(sock, buff_return, sizeof(int32_t), recvAddr, 0, TCP);
	int r = 0;
//      cout << "socket_replica--------4" << endl;
	//connect (int socket, struct sockaddr *addr, size_t length)
	if (r < 0) {
		cerr << "general_replica: got bad news from relica: " << r << endl;
	}
}
int c_zht_compare_and_swap_std(ZHTClient_c zhtClient, const char *key,
		const char *seen_value, const char *new_value, char **value_queried) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;
	string sKey(key);
	string sSeenValue(seen_value);
	string sNewValue(new_value);

	Package package;
	package.set_virtualpath(sKey);
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(5);  // 5 for comapre and swap
	if (!sSeenValue.empty()) {
		package.set_realfullpath(sSeenValue);
	}
	if (!sNewValue.empty()) {
		package.set_newfullpath(sNewValue);
	}

	string return_str;
	int rc = zhtcppClient->compare_and_swap(package.SerializeAsString(),
			return_str);

	Package ret_pack;
	ret_pack.ParseFromString(return_str);

	strcpy(*value_queried, ret_pack.realfullpath().c_str());

	return rc;
}
示例#3
0
/*
 * to be removed.
 */
void test_proto_buffer(const char *key, const char *value) {
	Package package;
	package.set_virtualpath(key); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	package.set_realfullpath(value);

	fprintf(stderr, "package virtualpath is: %s\n",
			package.virtualpath().c_str());

	const char * realfullpath = package.realfullpath().c_str();
	fprintf(stderr, "package realfullpath is: %s\n", realfullpath);

	string str = package.SerializeAsString();
	int len = strlen(str.c_str());

	Package package2;
	package2.ParseFromString(str);
	string str2 = package2.SerializeAsString();

	fprintf(stderr, "package2 virtualpath is: %s\n",
			package2.virtualpath().c_str());

	const char *realfullpath2 = package2.realfullpath().c_str();
	fprintf(stderr, "package2 realfullpath is: %s\n", realfullpath2);

}
示例#4
0
int Worker::zht_remove(string key) {

	/*Package package;
	 package.set_virtualpath(key);
	 package.set_operation(2);
	 string str = package.SerializeAsString();

	 int index;
	 svrclient.str2Host(str, index);*/

	int index = myhash(key.c_str(), svrclient.memberList.size());

	if (index != selfIndex) {
		Package package;
		package.set_virtualpath(key);
		package.set_operation(2);
		string str = package.SerializeAsString();
		pthread_mutex_lock(&msg_lock);
		int ret = svrclient.remove(str);
		pthread_mutex_unlock(&msg_lock);
		return ret;
	} else {
		int ret = pmap->remove(key);
		if (ret != 0) {
			cerr << "DB Error: fail to remove :ret= " << ret << endl;
			return -2;
		} else
			return 0; //succeed.
	}

}
int32_t HB_insert(NoVoHT *map, Package &package) {
	//int opt = package.operation();//opt not be used?
	string value = package.SerializeAsString();

	//int ret = db.set(package.virtualpath(), package_str); //virtualpath as key
//	cout << "Insert to pmap...value = " << value << endl;
	string key = package.virtualpath();

//      cout<<"key:"<<key<<endl;

//      cout<<"value:"<<value<<endl;
//      cout<<"Insert: k-v ready. put..."<<endl;
	int ret = map->put(key, value);
//      cout << "end inserting, ret = " << ret << endl;

	if (ret != 0) {
		cerr << "insert error: ret = " << ret << endl;
		return -3;
	}
	/*
	 cout << "String insted: " << package_str << endl;
	 */
	else
		return 0;
}
示例#6
0
int ZHTClient::removeMeta(string path) {

	Package package;
	package.set_operation(2); // 3 for insert, 1 for look up, 2 for remove
	package.set_replicano(3); //5: original, 3 not original
	package.set_virtualpath(path);
	string str = package.SerializeAsString();

	int sock = this->str2SockLRU(str, TCP);
	reuseSock(sock);
	// cout<<"sock = "<<sock<<endl;
	struct HostEntity dest = this->str2Host(str);
	sockaddr_in recvAddr;
	int sentSize = generalSendTo(dest.host.data() ,dest.port, sock, str.c_str(), TCP);
	// cout<<"remove sentSize "<< sentSize <<endl;
	int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));

	// generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0);
	generalReceive(sock, (void*)ret_buf, sizeof(int32_t),recvAddr,0, TCP);

	// generalSendTCP(sock, str.c_str());

	// int32_t* ret = (int32_t*) malloc(sizeof(int32_t));

	// generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0);
	int ret_1 = *(int32_t*) ret_buf;
	// cout<<"remove got: "<< ret_1 <<endl;
	// cout <<"Returned status: "<< *(int32_t*) ret<<endl;
	// d3_closeConnection(sock);
	free(ret_buf);

	return ret_1;
}
int32_t HB_insert_cstr_(map<char*, char*> &chmap, Package &package) {

	string package_str = package.SerializeAsString();

	char* value = (char*) malloc(package_str.length() * sizeof(char));
	strcpy(value, package_str.c_str());

	char* key = (char*) malloc(package.virtualpath().length() * sizeof(char));
//	cout << "package.virtualpath().c_str()="<<package.virtualpath().c_str()<<endl;
	strcpy(key, package.virtualpath().c_str());

//	cout <<"after scrcpy, key = "<<key<<", key length = "<< strlen(key) <<endl;

//      pair<map<char*, char*>::iterator, bool> ret;
//      ret = chmap.insert(pair<char*, char*>(key, value));
//      free(key);
//      free(value);
	if (chmap.insert(pair<char*, char*>(key, value)).second == false) {
		cout
				<< "HB_insert_cstr: insert failed, return -3, element exists, key = "
				<< key << ", value = " << value << endl;
		free(key);
		free(value);
		return -3;
	} else {
		cout << " HB_insert_cstr: insert succeeded. key = " << key
				<< ", value = " << chmap.find(key)->second << endl;
		free(key);
		free(value);
		return 0;
	}

}
示例#8
0
int Worker::zht_insert(string str) {
	Package package;
	package.ParseFromString(str);
	package.set_operation(3);
	str = package.SerializeAsString();

	int index;
	svrclient.str2Host(str, index);

	if (index != selfIndex) { //cout << "NOOOOO..index = " << index << endl;
		pthread_mutex_lock(&msg_lock);
		int ret = svrclient.insert(str);
		pthread_mutex_unlock(&msg_lock);
		return ret;
	} else {
		string key = package.virtualpath(); //cout << "key = " << key << endl;
		//pthread_mutex_lock(&zht_lock);
		int ret = pmap->put(key, str);
		//pthread_mutex_unlock(&zht_lock);
		if (ret != 0) {
			cerr << "insert error: key = " << key << " ret = " << ret << endl;
			return -3;
		} else
			return 0;
	}
}
示例#9
0
int zht_insert_meta(ZHTClient_c zhtClient, struct metadata * meta){

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	Package package;
	string keyStr(meta->key);
	
	//1. General infos on the file/storage
	package.set_virtualpath(keyStr); //as key
	package.set_isdir(false);
	package.set_replicano(1);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	package.set_realfullpath(keyStr);
	
	//2. General infos on the encoding
	package.set_k(meta->k);
	package.set_m(meta->m);
	package.set_encodinglib(meta->encodingLib);
	package.set_filesize(meta->fileSize);
	package.set_bufsize(meta->bufsize);
	
	//3. Chunks/Replicas locations
	struct comTransfer * current = meta->loc->transfers;
	Package_Location*  location;

	while(current != NULL){
		location = package.add_location();
		
		location->set_hostname(string(current->hostName));
		location->set_port(current->port);
		location->set_distantchunkname(string(current->distantChunkName));
		
		current=current->next;
	}
	//4. Insertion
	std::string serialized = package.SerializeAsString();
	
	dbgprintf("Package Length:%i\n",serialized.size());
	
	//TEST
	Package package4;
	package4.ParseFromString(serialized);
	dbgprintf("Serialized: %s \n",serialized.c_str());
	dbgprintf("key: %s \n",(package4.virtualpath()).c_str());
	dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package4.k(),package4.m(),package4.encodinglib(),package4.filesize());
	
	for (int j = 0; j < package4.location_size(); j++) {
		const Package_Location& location = package4.location(j);
		
		dbgprintf("chunk:%s, port:%i\n",location.distantchunkname().c_str(), location.port());
	}	
	//END TEST
	
	
	int res = zhtcppClient->insert(serialized);
	
	return res;
}
示例#10
0
void submittaskszht(ZHTClient &clientRet, string client_id)
{
	int ret = 0;
	Package package;
	package.set_virtualpath(client_id);
	package.set_operation(21);
	cout << "Ok, what is the hell!\n" << endl;

	for (map<uint32_t, NodeList>::iterator map_it = update_map_zht.begin(); map_it != update_map_zht.end(); ++map_it)
	{
		uint32_t index = map_it->first;
		NodeList &update_list = map_it->second;
		while (!update_list.empty())
		{
			int num_vector_count, per_vector_count;
			string alltasks;
			vector< vector<string> > tokenize_string = tokenize_1(update_list.front(), '$', '#', num_vector_count, per_vector_count);
			//cout << " num_vector_count = " << num_vector_count << " per_vector_count = " << per_vector_count << endl;
			for(int i = 0; i < per_vector_count; i++)
			{
				Package tmpPackage;
				tmpPackage.ParseFromString(tokenize_string.at(0).at(i));
				string key = tmpPackage.virtualpath();
				alltasks.append(key); alltasks.append("\'\"");
			} //cout << "Serve
			package.set_realfullpath(alltasks);
			string str = package.SerializeAsString();
			ret += clientRet.svrtosvr(str, str.length(), index);
			update_list.pop_front();
		}
	}

//	Package package; string alltasks;
//			package.set_virtualpath(client_id); // Here key is just the client ID
//			package.set_operation(21);
//			//package.set_operation(22);
//			num_packages++;
//
//			int num_tasks_this_package = max_tasks_per_package;
//			int num_tasks_left = num_tasks - total_submitted1;
//			if (num_tasks_left < max_tasks_per_package) {
//				num_tasks_this_package = num_tasks_left;
//			}
//			for(int j = 0; j < num_tasks_this_package; j++) {
//				int task_id = it->first; ++it;
//
//	        	        stringstream task_id_ss;
//	                	task_id_ss << task_id << client_id; // Task ID + Client ID
//				alltasks.append(task_id_ss.str()); alltasks.append("\'\""); // Task ID
//			}
//			total_submitted1 = total_submitted1 + num_tasks_this_package;
//			package.set_realfullpath(alltasks);
//			string str = package.SerializeAsString();
//			//cout << "String size = " << str.size() << " str length = " << strlen(str.c_str());
//			int32_t ret = clientRet.svrtosvr(str, str.length(), toserver);
}
示例#11
0
int ZHTClient::remove(string str) {

    Package package;
    package.ParseFromString(str);

    char *c_str;
    int size = str.length();
    c_str = (char*) malloc((size + 5 + 1) * sizeof(char));
    if (c_str == NULL) {
        cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl;
        exit(1);
    }
    int len = copystring(c_str, str);

    if (package.virtualpath().empty()) //empty key not allowed.
        return -1;
    /*if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf!
     package.set_realfullpath(" ");*/

    package.set_operation(2); //1 for look up, 2 for remove, 3 for insert
    package.set_replicano(3); //5: original, 3 not original
    str = package.SerializeAsString();

    int sock = this->str2SockLRU(str, TCP);
    reuseSock(sock);
//	cout<<"sock = "<<sock<<endl;
    struct HostEntity dest = this->str2Host(str);
    sockaddr_in recvAddr;
    //pthread_mutex_lock(&msg_lock);
    int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len,
                                 TCP);
    //pthread_mutex_unlock(&msg_lock);
    //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP);
//		cout<<"remove sentSize "<< sentSize <<endl;
    int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));

    //	generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0);
    //pthread_mutex_lock(&msg_lock);
    generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP);
    //pthread_mutex_unlock(&msg_lock);

//	generalSendTCP(sock, str.c_str());

//	int32_t* ret = (int32_t*) malloc(sizeof(int32_t));

//	generalReveiveTCP(sock, (void*) ret, sizeof(int32_t), 0);
    int ret_1 = *(int32_t*) ret_buf;
//	cout<<"remove got: "<< ret_1 <<endl;
//cout <<"Returned status: "<< *(int32_t*) ret<<endl;
//	d3_closeConnection(sock);
    free(ret_buf);

    return ret_1;
}
示例#12
0
文件: hash.cpp 项目: Hellblazer/ZHT
int HB_insert(DB &db, Package package) {
	//int opt = package.operation();//opt not be used?
	string package_str = package.SerializeAsString();
	int ret = db.set(package.virtualpath(), package_str); //virtualpath as key
	if (ret == 0) {
		return -3; //insert fail.
	}
	/*
	 cout << "String insted: " << package_str << endl;
	 */
	return 0;
}
int c_zht_remove2_std(ZHTClient_c zhtClient, const char *key) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);

	Package package;
	package.set_virtualpath(sKey);
	package.set_operation(2); //1 for look up, 2 for remove, 3 for insert, 4 append

	return zhtcppClient->remove(package.SerializeAsString());
}
示例#14
0
// general server to server send (for load information and task stealing)
int32_t ZHTClient::svrtosvr(string str, int size, int index) {

    Package package;
    package.ParseFromString(str);

    if (package.virtualpath().empty()) 	//empty key not allowed.
    {
        return -1;
    }
    str = package.SerializeAsString();

    char *c_str;
    c_str = (char*) malloc((size + 5 + 1) * sizeof(char));
    if (c_str == NULL) {
        cout << "ZHTClient::svrtosvr: " << strerror(errno) << endl;
        exit(1);
    }
    int len = copystring(c_str, str);

    if (package.virtualpath().empty()) //empty key not allowed.
    {
        return -1;
    }
    //cout << "C++ string len = " << str.length() << endl; cout << "C++ string: \n" << str << endl;
    /*cout << "C string: " << endl;
     for(int i = 0; i < len; i++) cout << c_str[i]; cout << endl;
     return 1;*/

    int sock = this->index2SockLRU(index, TCP);
    reuseSock(sock);

    struct HostEntity dest = this->index2Host(index);
    sockaddr_in recvAddr; //cout << "going to acquire lock" << endl;
    //pthread_mutex_lock(&msg_lock); //cout << "lock acquired" << endl;
    //int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(), str.size(), TCP);
    //cout << "what is the f**k!" << dest.host.data() << endl;
    int sentSize = generalSendTo(dest.host.data(), dest.port, sock, c_str, len,
                                 TCP); //cout << "svrsvr sent" << endl;
    //pthread_mutex_unlock(&msg_lock);
    int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));
    //pthread_mutex_lock(&msg_lock);
    generalReceive(sock, (void*) ret_buf, sizeof(int32_t), recvAddr, 0, TCP); //cout << "svrsvr recv" << endl;
    //pthread_mutex_unlock(&msg_lock); //cout << "lock released" << endl;
    int32_t ret_1 = *(int32_t*) ret_buf;
    free(ret_buf);

    //if(package.operation() == 14) {
    //	cout << "ZHTClient::svrtosvr num_task = " << ret_1 << endl;
    //}

    return ret_1;
}
示例#15
0
int c_zht_remove2(const char *key) {

	string keyStr(key);

	if (keyStr.empty()) //empty key not allowed.
		return -1;

	Package package;
	package.set_virtualpath(keyStr);
	package.set_operation(2); //1 for look up, 2 for remove, 3 for insert

	return zhtClient.remove(package.SerializeAsString());
}
示例#16
0
//send a plain string to destination, receive return state.
int ZHTClient::insert(string str) {

	/*	int sock = -1;

	 struct HostEntity dest = this->str2Host(str);

	 //	cout<<"Client: will send to "<<dest.host<<endl;
	 //int ret = simpleSend(str, dest, sock);

	 sock = makeClientSocket(dest.host.c_str(), dest.port, 1);
	 //	cout<<"client sock = "<< sock<<endl;
	 reuseSock(sock);
	 generalSendTCP(sock, str.c_str());
	 */
	Package package;
	package.ParseFromString(str);

	if (package.virtualpath().empty()) //empty key not allowed.
		return -1;
	if (package.realfullpath().empty()) //coup, to fix ridiculous bug of protobuf!
		package.set_realfullpath(" ");

	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	package.set_replicano(5); //5: original, 3 not original
	str = package.SerializeAsString();

	int sock = this->str2SockLRU(str, TCP);
	reuseSock(sock);
//	cout<<"sock = "<<sock<<endl;
//	int sentSize = generalSendTCP(sock, str.c_str());
	struct HostEntity dest = this->str2Host(str);
	sockaddr_in recvAddr;
	int sentSize = generalSendTo(dest.host.data(), dest.port, sock, str.c_str(),
			TCP);
//	cout <<"Client inseret sent: "<<sentSize<<endl;
	int32_t* ret_buf = (int32_t*) malloc(sizeof(int32_t));

//	generalReveiveTCP(sock, (void*) ret_buf, sizeof(int32_t), 0);
	generalReceive(sock, (void*) ret_buf, 4, recvAddr, 0, TCP);
	int ret = *(int32_t*) ret_buf;
	if (ret < 0) {
//		cerr << "zht_util.h: Failed to insert." << endl;
	}
//cout <<"Returned status: "<< *(int32_t*) ret<<endl;
//	d3_closeConnection(sock);
	free(ret_buf);

	//return ret_1;
//	cout<<"insert got: "<< *ret <<endl;
	return ret;
}
示例#17
0
int Worker::zht_update(map<uint32_t, NodeList> &update_map, string field,
		uint32_t toid = 0) {

	Package package;
	package.set_operation(25);
	package.set_virtualpath(field);
	if (!field.compare("nodehistory")) {
		package.set_currnode(toid);
	} //cout << "deque size = ";
	for (map<uint32_t, NodeList>::iterator map_it = update_map.begin();
			map_it != update_map.end(); ++map_it) {
		uint32_t index = map_it->first;
		NodeList &update_list = map_it->second;
		//cout << update_list.size() << " ";
		while (!update_list.empty()) {
			package.set_realfullpath(update_list.front());
			update_list.pop_front();
			if (index == selfIndex) {
				string *str;
				str = new string(package.SerializeAsString());
				pthread_mutex_lock(&notq_lock);
				notifyq.push(str);
				pthread_mutex_unlock(&notq_lock);
				//int ret = update(package);
			}
			//else if (index != toid){
			else {
				string update_str = package.SerializeAsString();
				pthread_mutex_lock(&msg_lock);
				int ret = svrclient.svrtosvr(update_str, update_str.size(),
						index);
				pthread_mutex_unlock(&msg_lock);
			}
		}
	} //cout << endl;
}
int32_t HB_append(NoVoHT *map, Package &package){
        string value = package.SerializeAsString();
//      cout << "Insert to pmap...value = " << value << endl;
        string key = package.virtualpath();
//      cout<<"key:"<<key<<endl;
//      cout<<"value:"<<value<<endl;
        int ret = map->append(key, value);
//      cout << "end inserting, ret = " << ret << endl;
        if (ret != 0) {
                cerr << "Append error: ret = " << ret << endl;
                return -4;
        }
        else
                return 0;
}
int32_t HB_insert_cstr(MY_MAP &chmap, Package &package) {

	string package_str = package.SerializeAsString();

	char* value = (char*) calloc(package_str.length(), sizeof(char));
	strcpy(value, package_str.c_str());

	char* key = (char*) calloc(package.virtualpath().length(), sizeof(char));
	strcpy(key, package.virtualpath().c_str());

//	cout <<"after scrcpy, key = "<<key<<", key length = "<< strlen(key) <<endl;

//      pair<map<char*, char*>::iterator, bool> ret;
//      ret = chmap.insert(pair<char*, char*>(key, value));
//      free(key);
//      free(value);

	pair<MY_MAP::iterator, bool> ret;
	ret = chmap.insert(MY_PAIR(key, value));

	MY_MAP::iterator it;
	cout << "mymap.size() is " << (int) chmap.size() << endl;
	cout << "mymap contains:\n";
	for (it = chmap.begin(); it != chmap.end(); it++)
		cout << (*it).first << " => " << (*it).second << endl;
	cout << "########" << endl;

	if (ret.second == false) {
		cout
				<< "HB_insert_cstr: insert failed, return -3, element exists, key = "
				<< key << ", value = " << value << endl;

		cout << "######## done ########" << endl;
		free(key);
		free(value);
		return -3;
	} else {
		cout << " HB_insert_cstr: insert succeeded. key = " << key
				<< ", value = " << chmap.find(key)->second << endl;

		cout << "######## done ########" << endl;
		free(key);
		free(value);
		return 0;
	}

}
示例#20
0
int benchmarkAppend() {

    vector<string> pkgList_append;

    vector<string>::iterator it;
    for (it = pkgList.begin(); it != pkgList.end(); it++) {

        Package package;
        package.ParseFromString((*it));

        package.add_listitem("item-----6-append");

        pkgList_append.push_back(package.SerializeAsString());
    }

    double start = 0;
    double end = 0;
    start = TimeUtil::getTime_msec();
    int errCount = 0;

    int c = 0;
    for (it = pkgList_append.begin(); it != pkgList_append.end(); it++) {

        c++;

        string pkg_str = *it;
        Package pkg;
        pkg.ParseFromString(pkg_str);

        int ret = zc.append(pkg.virtualpath(), pkg_str);

        if (ret < 0) {
            errCount++;
        }
    }

    end = TimeUtil::getTime_msec();

    char buf[200];
    sprintf(buf, "Appended packages, %d, %d, cost(ms), %f", numOfOps - errCount,
            numOfOps, end - start);
    cout << buf << endl;

    return 0;
}
int c_zht_insert2_std(ZHTClient_c zhtClient, const char *key,
		const char *value) {

	ZHTClient * zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);
	string sValue(value);

	Package package;
	package.set_virtualpath(sKey); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert, 4 append
	if (!sValue.empty())
		package.set_realfullpath(sValue);

	return zhtcppClient->insert(package.SerializeAsString());
}
示例#22
0
void init_packages() {

    for (int i = 0; i < numOfOps; i++) {

        Package package;
        package.set_virtualpath(HashUtil::randomString(lenString)); //as key
        package.set_isdir(true);
        package.set_replicanum(5); //orginal--Note: never let it be nagative!!!
        package.set_realfullpath(
            "Some-Real-longer-longer-and-longer-Paths--------");
        package.add_listitem("item-----1");
        package.add_listitem("item-----2");
        package.add_listitem("item-----3");
        package.add_listitem("item-----4");
        package.add_listitem("item-----5");
        pkgList.push_back(package.SerializeAsString());
    }
}
int c_zht_lookup2_std(ZHTClient_c zhtClient, const char *key, char *result,
		size_t *n) {

	ZHTClient *zhtcppClient = (ZHTClient *) zhtClient;

	string sKey(key);

	Package package;
	package.set_virtualpath(sKey); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(1); //1 for look up, 2 for remove, 3 for insert, 4 append

	string resultStr;
	int ret = zhtcppClient->lookup(package.SerializeAsString(), resultStr);

	/*
	 * hello,zht:hello,zht ==> hello,zht:zht
	 * */
	string store;
	char * pch, *sp;
	pch = strtok_r((char*) resultStr.c_str(), ":", &sp);
	Package package2;

	while (pch != NULL) {

		package2.ParseFromString(pch);
		string strRealfullpath = package2.realfullpath();

		store.append(strRealfullpath);
		store.append(":");

		pch = strtok_r(NULL, ":", &sp);
	}

	size_t found = store.find_last_of(":");
	store = store.substr(0, found);

	strncpy(result, store.c_str(), strlen(store.c_str()));
	*n = store.size();

	return ret;
}
示例#24
0
int c_zht_insert2(const char *key, const char *value) {

	string keyStr(key);
	string valueStr(value);

	if (keyStr.empty()) //empty key not allowed.
		return -1;

	Package package;
	package.set_virtualpath(keyStr); //as key
	package.set_isdir(true);
	package.set_replicano(5);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	if (!valueStr.empty())
		package.set_realfullpath(valueStr);

	return zhtClient.insert(package.SerializeAsString());

}
示例#25
0
string Worker::zht_lookup(string key) {
	string task_str;

	/*Package package;
	 package.set_virtualpath(key);
	 package.set_operation(1);
	 string lookup_str = package.SerializeAsString();
	 int index;
	 svrclient.str2Host(lookup_str, index);*/

	int index = myhash(key.c_str(), svrclient.memberList.size());

	if (index != selfIndex) {
		Package package;
		package.set_virtualpath(key);
		package.set_operation(1);
		string lookup_str = package.SerializeAsString();

		pthread_mutex_lock(&msg_lock);
		int ret = svrclient.lookup(lookup_str, task_str);
		pthread_mutex_unlock(&msg_lock);

		Package task_pkg;
		task_pkg.ParseFromString(task_str);
		//cout << "remote lookup: string = " << task_str << " str size = " << task_str.size() << endl;
		//cout << "remote lookup: task = " << task_pkg.virtualpath() << " nodehistory = " << task_pkg.nodehistory() << endl;
	} else {
		string *result = pmap->get(key);
		if (result == NULL) {
			//cout << "lookup find nothing. key = " << key << endl;
			string nullString = "Empty";
			return nullString;
		}
		task_str = *result;
		//Package task_pkg;
		//task_pkg.ParseFromString(task_str);
		//cout << "local lookup: task = " << task_pkg.virtualpath() << " nodehistory = " << task_pkg.nodehistory() << endl;
	}
	return task_str;
	/*Package task_pkg;
	 task_pkg.ParseFromString(task_str);
	 return task_pkg.realfullpath();*/
}
int32_t HB_insert(map<string, string> &hmap, Package &package) {
	//int opt = package.operation();//opt not be used?
	string package_str = package.SerializeAsString();
	//int ret = db.set(package.virtualpath(), package_str); //virtualpath as key
	//      cout<<"Insert to pmap..."<<endl;
	string key = package.virtualpath();
	//      cout<<"key:"<<key<<endl;
	string value = package_str;
	//      cout<<"value:"<<value<<endl;
	//      cout<<"Insert: k-v ready. put..."<<endl;

	//pair<map<string,string>::iterator,bool> ret;
	pair<map<string, string>::iterator, bool> ret;
	ret = hmap.insert(pair<string, string>(key, value));

	if (ret.second == false) {
		return -3;
	} else
		return 0;
}
示例#27
0
void submittasks(ZHTClient &clientRet) {
        /*uint32_t count = task_str_list.size();
        for(uint32_t i = 0; i < count; i++) {
                int32_t ret = clientRet.insert(task_str_list[i]); //cout << "task " << i << " sent" << endl;
        }*/
        int ret = 0;
        Package package;
        package.set_operation(20);
        package.set_virtualpath("zht_insert");
        for(map<uint32_t, NodeList>::iterator map_it = update_map.begin(); map_it != update_map.end(); ++map_it) {
                uint32_t index = map_it->first;
                NodeList &update_list = map_it->second;
                while(!update_list.empty()) { //cout << "str = " << update_list.front() << endl;
                        package.set_realfullpath(update_list.front());
                        update_list.pop_front();
                        string update_str = package.SerializeAsString();
                        ret += clientRet.svrtosvr(update_str, update_str.size(), index);
                }
        }
        cout << " no. tasks inserted = " << ret << endl;
}
示例#28
0
//DEPRECATED
const char* zht_parse_meta(struct metadata* meta) {
	Package package;
	package.set_virtualpath(meta->key); //as key
	package.set_isdir(false);
	package.set_replicano(1);
	package.set_operation(3); //1 for look up, 2 for remove, 3 for insert
	package.set_realfullpath(meta->physicalPath);
	
	// IDA metadata
	package.set_k(meta->k);
	package.set_m(meta->m);
	package.set_encodinglib(meta->encodingLib);
	package.set_filesize(meta->fileSize);
	package.set_bufsize(meta->bufsize);
	
	std::string serialized = package.SerializeAsString();
	
	cout << serialized << endl;
	dbgprintf("Serialized:%s \n",serialized.c_str());
	dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package.k(),package.m(),package.encodinglib(),package.filesize());
	
	const char * resString = serialized.c_str();
	
	Package package2;
	package2.ParseFromString(string(resString));
	dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package2.k(),package2.m(),package2.encodinglib(),package2.filesize());


	std::string reborn = string(resString);

	Package package3;
	package3.ParseFromString(reborn);
	dbgprintf("k:%i,m:%i,encodinglib:%i,filesize:%i \n",package3.k(),package3.m(),package3.encodinglib(),package3.filesize());

	
	
	
	return resString;
}
示例#29
0
float benchmarkRemove(vector<string> strList, ZHTClient &client) {
    vector<string>::iterator it;

    for (it = strList.begin(); it != strList.end(); it++) {
        Package package;
        package.ParseFromString((*it));
        package.set_operation(2); // 3 for insert, 1 for look up, 2 for remove
        package.set_replicano(5); //5: original, 3 not original

        strList.erase(it);
        string newStr = package.SerializeAsString();
        strList.push_back(newStr);
    }

    double start = 0;
    double end = 0;
    start = getTime_msec();
    int errCount = 0;
    int c=0;
    for (it = strList.begin(); it != strList.end(); it++) {
        string result;
        c++;
        //cout <<"Remove count "<< c << endl;

        //cout <<"Remove: "<< (*it).c_str() << endl;
        if (client.remove((*it)) < 0) {
            errCount++;
        }

    }

    end = getTime_msec();

    cout << "Remove " << strList.size() - errCount << " packages out of "
         << strList.size() << ", cost " << end - start << " ms" << endl;
    return 0;

    return 0;
}
示例#30
0
int Worker::zht_append(string str) {
	Package package;
	package.ParseFromString(str);
	package.set_operation(4);
	str = package.SerializeAsString();

	int index;
	svrclient.str2Host(str, index);

	if (index != selfIndex) {
		pthread_mutex_lock(&msg_lock);
		int ret = svrclient.append(str);
		pthread_mutex_unlock(&msg_lock);
		return ret;
	} else {
		string key = package.virtualpath();
		int ret = pmap->append(key, str);
		if (ret != 0) {
			cerr << "Append error: ret = " << ret << endl;
			return -4;
		} else
			return 0;
	}
}