bool UserDatabase::putUserKeys(unsigned long uid,const std::string& pubKey,const std::string& privKey) { Dbt key,data; init_DBT(&key,&data); int ret; const int BUFSZ=512; char buf[BUFSZ]; RawData pubKeyData(pubKey.c_str(),pubKey.length()); RawData privKeyData(privKey.c_str(),privKey.length()); int offset=pubKeyData.serialize(buf,BUFSZ); offset+=privKeyData.serialize(buf+offset,BUFSZ-offset); key.set_data( &uid ); key.set_size( sizeof(uid) ); data.set_data( buf ); data.set_size( offset ); ret = m_pkeyDB->put(NULL, &key, &data, 0); if(ret != 0) { debug("putUserKeys error : "<<uid); } return true; }
int neo_bdb_search(void *signs, char *dat) { int ret; unsigned mod; DBT key, data; init_DBT(&key, &data); key.data = signs; key.size = sizeof(u_int64_t); mod = (*(unsigned *) signs) % MAX_BDB_NUM; pthread_mutex_lock(&bdb_mutex[mod]); ret = dbp[mod]->get(dbp[mod], NULL, &key, &data, 0); pthread_mutex_unlock(&bdb_mutex[mod]); switch (ret) { case 0: snprintf(dat, MAX_URL_LEN, "%s", (char *) data.data); return 0; default: //dbp[mod]->err(dbp[mod], ret, "DB->get"); return -1; } }
int UserDatabase::get_record(string email, USERDB *&rcrd) { if(m_psec_mazeuserDB == NULL) return -1; int ret; Dbt key, data; init_DBT(&key,&data); int emailLen = email.length() + 1; char *newEmail = new char[emailLen]; memcpy(newEmail, email.c_str(), emailLen - 1); newEmail[emailLen- 1] = '\0'; key.set_data(newEmail); key.set_size(emailLen); //key.set_data(const_cast<char*>(email.c_str()) ); //key.set_size(email.length() + 1); ret = m_psec_mazeuserDB->get(NULL, &key, &data, 0); if(ret != 0) { debug("get_record,No user : "<<email); } else { memcpy(rcrd, data.get_data(), sizeof(USERDB)); } delete []newEmail; return ret; }
bool UserDatabase::getUserKeys(unsigned long uid,std::string& pubKey,std::string& privKey) { int ret; Dbt key,data; init_DBT(&key,&data); key.set_data( &uid ); key.set_size( sizeof(uid) ); ret = m_pkeyDB->get(NULL,&key,&data,0); if(ret != 0) { pubKey=""; privKey=""; return false; } RawData pubKeyData,privKeyData; int offset=pubKeyData.unserialize((char *)data.get_data(),data.get_size()); offset+=privKeyData.unserialize((char *)data.get_data()+offset,data.get_size()-offset); assert(offset==data.get_size()); pubKey.assign(pubKeyData.data,pubKeyData.len); privKey.assign(privKeyData.data,privKeyData.len); return true; }
int UserDatabase::put_record(unsigned long uid, USERDB *rcrd) { if(m_pmazeuserDB == NULL) return -1; int ret; Dbt key, data; init_DBT(&key,&data); key.set_data(&uid); key.set_size( sizeof(unsigned long)); data.set_data (rcrd); data.set_size(sizeof(USERDB)); ret = m_pmazeuserDB->put(NULL, &key, &data, 0); if(ret != 0) { debug("put_record error : "<<uid); } return ret; /* //update by vvsong //check exist before put ret = m_pmazeuserDB->exists(NULL,&key,0); if (ret == 0){ cout<<"(test by vvsong),key already exists!"<<endl; debug("key already exists : "<<uid); return -1; } else { ret = m_pmazeuserDB->put(NULL, &key, &data, 0); if(ret != 0) { debug("put_record error : "<<uid); } else { //cout<<uid<<endl; } } //end update return ret; */ }
int neo_bdb_insert(void *signs, char *dat) { int ret; unsigned mod; DBT key, data; init_DBT(&key, &data); key.data = signs; key.size = sizeof(u_int64_t); data.data = dat; data.size = strlen(dat) + 1; mod = (*(unsigned *) signs) % MAX_BDB_NUM; pthread_mutex_lock(&bdb_mutex[mod]); #ifndef RADAR ret = dbp[mod]->put(dbp[mod], NULL, &key, &data, DB_NOOVERWRITE); #else ret = dbp[mod]->put(dbp[mod], NULL, &key, &data, 0); #endif pthread_mutex_unlock(&bdb_mutex[mod]); switch (ret) { case 0: pthread_mutex_lock(&bdb_mutex[mod]); cacheNum[mod]++; #ifndef RADAR if (cacheNum[mod] % 64 == 0) #else if (cacheNum[mod] % 128 == 0) #endif { dbp[mod]->sync(dbp[mod], 0); } pthread_mutex_unlock(&bdb_mutex[mod]); return 0; case DB_KEYEXIST: return 0; default: dbp[mod]->err(dbp[mod], ret, "DB->put"); return -1; } }
int UserDatabase::del_record(unsigned long uid) { if(m_pmazeuserDB == NULL) return -1; Dbt key; init_DBT(&key,&key); int ret; key.set_data(&uid); key.set_size(sizeof(uid)); ret = m_pmazeuserDB->del(NULL, &key, 0); if (ret != 0) { debug("del record error!"<<uid); } return ret; }
int UserDatabase::del_record(string email) { if(m_psec_mazeuserDB == NULL) return -1; int ret; Dbt key; init_DBT(&key,&key); key.set_data(const_cast<char*>(email.c_str()) ); key.set_size(email.length() + 1); ret = m_psec_mazeuserDB->del(NULL, &key, 0); if (ret != 0) { debug("Delete record Error!"<<email); } return ret; }
int UserDatabase::get_record(unsigned long uid, USERDB *&rcrd) { if(m_pmazeuserDB == NULL) return -1; int ret; Dbt key, data; init_DBT(&key,&data); key.set_data(&uid); key.set_size(sizeof(unsigned long)); ret = m_pmazeuserDB->get(NULL, &key, &data, 0); if(ret != 0) { debug("No user : "<<uid); } else { memcpy(rcrd, data.get_data(), sizeof(USERDB)); } return ret; }