int AdConHash::Add(void* pData) { struct node_s* pNode; char buff[AD_BYTE32]; snprintf(buff, AD_BYTE32, "bidding_%02d", (int)m_mNodes.size()); pthread_rwlock_wrlock(&m_pLock); if (m_pHandle == NULL) { m_pHandle = conhash_init(NULL); if (m_pHandle == NULL) { pthread_rwlock_unlock(&m_pLock); return AD_SUCCESS; } } pNode = (struct node_s *) malloc (sizeof(struct node_s)); if (pNode != NULL) { conhash_set_node(pNode, buff, HASH_REPLICA, pData); conhash_add_node(m_pHandle, pNode); m_mNodes[pData] = pNode; } pthread_rwlock_unlock(&m_pLock); return AD_SUCCESS; }
bool AdConHashs::Add(string & sKey, void* pData) { struct node_s* pNode; if(m_bFirst == false) { if (m_pHandleA == NULL) { m_pHandleA = conhash_init(NULL); if (m_pHandleA == NULL) return false; } pNode = (struct node_s *) malloc (sizeof(struct node_s)); if (pNode != NULL) { conhash_set_node(pNode, sKey.c_str(), HASH_REPLICA, pData); conhash_add_node(m_pHandleA, pNode); m_vNodesA.push_back(pNode); } else { return false; } } else { if (m_pHandleB == NULL) { m_pHandleB = conhash_init(NULL); if (m_pHandleB == NULL) return false; } pNode = (struct node_s *) malloc (sizeof(struct node_s)); if (pNode != NULL) { conhash_set_node(pNode, sKey.c_str(), HASH_REPLICA, pData); conhash_add_node(m_pHandleB, pNode); m_vNodesB.push_back(pNode); } else { return false; } } return true; }
int main(int argc, char* argv[]) { struct conhash_s* conhashp = conhash_init(NULL); int index = 0; for(index=0; index<6; index++) { struct node_s* nodep = (struct node_s*)malloc(sizeof(struct node_s)); memset(nodep, 0, sizeof(struct node_s)); nodep->index = 0; nodep->replicas = 500; nodep->flag = NODE_FLAG_INIT; snprintf(nodep->iden, 64, "10.1.15.1%d", index); nodep->iden[63] = '\0'; conhash_add_node(conhashp, nodep); } #if 0 char* uri = "/videos/v/20110926/205763500/205763500/1/f3a89defde4e580e4058149e9059d1d4.ts"; int len = strlen(uri); struct node_s* findp = conhash_lookup(conhashp, uri, len); fprintf(stdout, "find [%d][%s] by %s\n", findp->index, findp->iden, uri); uri = "/videos/v/20110926/205763500/205763500/2/c59965f441f474c6b0dfbe36232bd614.ts"; len = strlen(uri); findp = conhash_lookup(conhashp, uri, len); fprintf(stdout, "find [%d][%s] by %s\n", findp->index, findp->iden, uri); uri = "abcdefsdfgdsaklj"; len = strlen(uri); findp = conhash_lookup(conhashp, uri, len); fprintf(stdout, "find [%d][%s] by %s\n", findp->index, findp->iden, uri); #endif char buf[MAX_LINE]; FILE *fp; int len; if((fp = fopen("./ts.list","r")) == NULL) { perror("fail to read"); exit (1) ; } while(fgets(buf,MAX_LINE,fp) != NULL) { len = strlen(buf); buf[len-1] = '\0'; struct node_s* findp = conhash_lookup(conhashp, buf, len-1); fprintf(stdout, "%s,%s\n", buf, findp->iden); } fclose(fp); return 0; }
cluster* initialcluster(char *name) { cluster *_cluster; if (!name) return NULL; _cluster = (cluster *) malloc(sizeof(cluster)); strcpy(_cluster->clustername, name); _cluster->conhash = conhash_init(NULL); _cluster->nodelisthead = NULL; return _cluster; }
int main() { int i; const struct node_s *node; char str[128]; long hashes[512]; /* init conhash instance */ struct conhash_s *conhash = conhash_init(NULL); if(conhash) { /* set nodes */ conhash_set_node(&g_nodes[0], "memcache_1", 32); conhash_set_node(&g_nodes[1], "memcache_2", 24); conhash_set_node(&g_nodes[2], "memcache_3", 25); conhash_set_node(&g_nodes[3], "memcache_4", 10); conhash_set_node(&g_nodes[4], "memcache_5", 48); /* add nodes */ conhash_add_node(conhash, &g_nodes[0]); conhash_add_node(conhash, &g_nodes[1]); conhash_add_node(conhash, &g_nodes[2]); conhash_add_node(conhash, &g_nodes[3]); conhash_add_node(conhash, &g_nodes[4]); printf("virtual nodes number %d\n", conhash_get_vnodes_num(conhash)); printf("the hashing results--------------------------------------:\n"); /* try object */ for(i = 0; i < 20; i++) { sprintf(str, "James.km%03d", i); node = conhash_lookup(conhash, str); if(node) printf("[%16s] is in node: [%16s]\n", str, node->iden); } conhash_get_vnodes(conhash, hashes, sizeof(hashes)/sizeof(hashes[0])); conhash_del_node(conhash, &g_nodes[2]); printf("remove node[%s], virtual nodes number %d\n", g_nodes[2].iden, conhash_get_vnodes_num(conhash)); printf("the hashing results--------------------------------------:\n"); for(i = 0; i < 20; i++) { sprintf(str, "James.km%03d", i); node = conhash_lookup(conhash, str); if(node) printf("[%16s] is in node: [%16s]\n", str, node->iden); } } conhash_fini(conhash); return 0; }
void addnodechild(cluster *_cluster, char *target, char *childnodenamelist) { if (_cluster == NULL) return; char a[128] = ""; char *ch = childnodenamelist; int i = 0; while (*ch != '\0') { a[i++] = *ch; ch++; } char *nodename = strtok(a, "."); node_s_inlist *parent = NULL; node_s_inlist *cur = _cluster->nodelisthead; int flag = 0; while (nodename != NULL) { flag = 0; while (cur != NULL) { if (strcmp(cur->node->iden, nodename) == 0) { flag = 1; parent = cur; cur = cur->childern; break; } else { cur = cur->next; } } if (flag == 0) return; nodename = strtok(NULL, "."); } if (parent == NULL || flag == 0) return; if (parent->conhash == NULL) parent->conhash = conhash_init(NULL); node_s_inlist *newnode = (node_s_inlist*) malloc(sizeof(node_s_inlist)); newnode->childern = NULL; newnode->conhash = NULL; newnode->next = NULL; newnode->node = (struct node_s*) malloc(sizeof(struct node_s)); newnode->node->pointer = newnode; newnode->next = parent->childern; parent->childern = newnode; conhash_set_node(newnode->node, target, 1000); conhash_add_node(parent->conhash, newnode->node); }
int main() { int i; const struct node_s *node; char str[128]; long hashes[512]; /* init conhash instance */ struct conhash_s *conhash = conhash_init(NULL); if(conhash) { /* set nodes */ conhash_set_node(&g_nodes[0], "titanic", 32); conhash_set_node(&g_nodes[1], "terminator2018", 24); conhash_set_node(&g_nodes[2], "Xenomorph", 25); conhash_set_node(&g_nodes[3], "True Lies", 10); conhash_set_node(&g_nodes[4], "avantar", 48); /* add nodes */ conhash_add_node(conhash, &g_nodes[0]); conhash_add_node(conhash, &g_nodes[1]); conhash_add_node(conhash, &g_nodes[2]); conhash_add_node(conhash, &g_nodes[3]); conhash_add_node(conhash, &g_nodes[4]); printf("virtual nodes number %d\n", conhash_get_vnodes_num(conhash)); printf("origin: the hashing results--------------------------------------:\n"); /* try object */ for(i = 0; i < 20; i++) { sprintf(str, "James.km%03d", i); node = conhash_lookup(conhash, str); if(node) printf("[%16s] is in node: [%16s]\n", str, node->iden); } conhash_get_vnodes(conhash, hashes, sizeof(hashes)/sizeof(hashes[0])); /******************delete node********************/ conhash_del_node(conhash, &g_nodes[2]); printf("remove node[%s], virtual nodes number %d\n", g_nodes[2].iden, conhash_get_vnodes_num(conhash)); printf("remove: the hashing results--------------------------------------:\n"); for(i = 0; i < 20; i++) { sprintf(str, "James.km%03d", i); node = conhash_lookup(conhash, str); if(node) printf("[%16s] is in node: [%16s]\n", str, node->iden); } /*******************add node***********************/ conhash_add_node(conhash, &g_nodes[2]); printf("add node[%s], virtual nodes number %d\n", g_nodes[2].iden, conhash_get_vnodes_num(conhash)); printf("add: the hashing results--------------------------------------:\n"); for(i = 0; i < 20; i++) { sprintf(str, "James.km%03d", i); node = conhash_lookup(conhash, str); if(node) printf("[%16s] is in node: [%16s]\n", str, node->iden); } } conhash_fini(conhash); return 0; }
ZEND_METHOD(YConsistent,consistent_init){ zval *object = getThis(); conhash *con = conhash_init(); int res_id = ZEND_REGISTER_RESOURCE(NULL,con,le_conhash); add_property_resource(object,"conhash",res_id); }