Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends
CADatabase Class Reference

#include <CADatabase.hpp>

Collaboration diagram for CADatabase:
[legend]

List of all members.

Public Member Functions

 CADatabase ()
 ~CADatabase ()
SINT32 insert (UINT8 key[16], UINT64 timestamp)
 Inserts this key in the replay DB.
SINT32 start ()
SINT32 stop ()
SINT32 test ()

Static Public Member Functions

static SINT32 measurePerformance (UINT8 *strLogFile, UINT32 lowerBoundEntries, UINT32 upperBoundEntries, UINT32 stepBy, UINT32 meassuresPerStep, UINT32 insertsPerMeasure)
 This mehtod can be used to measure the performance of the Replay database.

Private Member Functions

t_databaseInfocreateDBInfo ()
 Creates and initialises a dbinfo struct.
SINT32 clearDB (t_databaseInfo *pDB)
 clears the whole database pDB - but does not delete the hashtable pDB
SINT32 deleteDB (t_databaseInfo *&pDB)
 Deletes the whole database pDB.
SINT32 nextClock ()
SINT32 fill (UINT32 nrOfEntries)
 Pre fills the database with nrOfEntries random entries.
SINT32 simulateInsert (UINT8 key[16])
 This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database.

Private Attributes

UINT64 m_lastSwitch
t_databaseInfom_currDatabase
t_databaseInfom_nextDatabase
t_databaseInfom_prevDatabase
volatile bool m_bRun
volatile SINT32 m_currentClock
CAMutexm_pMutex
CAThreadm_pThread

Friends

THREAD_RETURN db_loopMaintenance (void *param)

Detailed Description

Definition at line 52 of file CADatabase.hpp.


Constructor & Destructor Documentation

Definition at line 34 of file CADatabase.cpp.

References createDBInfo(), m_currDatabase, m_currentClock, m_lastSwitch, m_nextDatabase, m_pMutex, m_prevDatabase, and m_pThread.

Referenced by measurePerformance().

Here is the call graph for this function:

Definition at line 52 of file CADatabase.cpp.

References deleteDB(), CAMutex::lock(), m_currDatabase, m_nextDatabase, m_pMutex, m_prevDatabase, stop(), and CAMutex::unlock().

Here is the call graph for this function:


Member Function Documentation

clears the whole database pDB - but does not delete the hashtable pDB

Parameters:
pDBdatabase to delete

Definition at line 64 of file CADatabase.cpp.

References E_SUCCESS, _t_database_info::m_pHashTable, _t_database_info::m_u32Size, and _t_database_entry::next.

Referenced by deleteDB(), and nextClock().

  {
    UINT16 tmp,tmp2;
    for(tmp=0;tmp<256;tmp++){
      for(tmp2=0;tmp2<256;tmp2++){
        while (pDBInfo->m_pHashTable[tmp][tmp2]!=NULL){
          t_databaseEntry* anker=pDBInfo->m_pHashTable[tmp][tmp2];
          pDBInfo->m_pHashTable[tmp][tmp2]=anker->next;
          delete anker;
          anker = NULL;
          }
        }
      }
    pDBInfo->m_u32Size=0;
    return E_SUCCESS;
  }

Creates and initialises a dbinfo struct.

Definition at line 45 of file CADatabase.cpp.

Referenced by CADatabase().

  {
    t_databaseInfo* pInfo=new t_databaseInfo;
    memset(pInfo,NULL,sizeof(t_databaseInfo));
    return pInfo;
  }

Deletes the whole database pDB.

Parameters:
pDBdatabase to delete

Definition at line 81 of file CADatabase.cpp.

References clearDB(), and E_SUCCESS.

Referenced by ~CADatabase().

  {
    clearDB(pDBInfo);
    delete pDBInfo;
    pDBInfo=NULL;
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CADatabase::fill ( UINT32  nrOfEntries) [private]

Pre fills the database with nrOfEntries random entries.

Parameters:
nrOfEntriesnumber of entries to put in the database

Definition at line 334 of file CADatabase.cpp.

References E_SUCCESS, getRandom(), and insert().

Referenced by measurePerformance(), and test().

  {
    UINT32 i=0;
    UINT8 key[16];
    while(i<nrOfEntries)
      {
        getRandom(key,16);
        if(insert(key,time(NULL))==E_SUCCESS)
          i++;
      }
    return E_SUCCESS;
  }

Here is the call graph for this function:

SINT32 CADatabase::insert ( UINT8  key[16],
UINT64  timestamp 
)

Inserts this key in the replay DB.

Definition at line 90 of file CADatabase.cpp.

References E_SUCCESS, E_UNKNOWN, FUTURE_TOLERANCE, _t_database_entry::key, CAMutex::lock(), m_currDatabase, m_lastSwitch, m_nextDatabase, _t_database_info::m_pHashTable, m_pMutex, m_prevDatabase, _t_database_info::m_u32Size, _t_database_entry::next, SECONDS_PER_INTERVALL, and CAMutex::unlock().

Referenced by fill(), CALastMixA::loop(), CAFirstMixA::loop(), CALastMixB::loop(), and mm_loopReadFromMixBefore().

  {
  // insert hash if timestamp valid in prevDB currDB nextDB
    m_pMutex->lock();


    // return E_UNKNOWN if the timestamp is too old or is too far in the future
    if ((timestamp<(m_lastSwitch-SECONDS_PER_INTERVALL))||(timestamp>(time(NULL)+FUTURE_TOLERANCE))) {
      // timestamp not valid!!
      m_pMutex->unlock();
      return E_UNKNOWN;
      }

    t_databaseInfo* aktDB=NULL;
    t_databaseInfo* prevDB=NULL;
    t_databaseInfo* nextDB=NULL;

    if(timestamp<m_lastSwitch){
      aktDB=m_prevDatabase;
      prevDB=NULL;
      nextDB=m_currDatabase;
      }
    else if(timestamp<(m_lastSwitch+SECONDS_PER_INTERVALL)){
      aktDB=m_currDatabase;
      prevDB=m_prevDatabase;
      nextDB=m_nextDatabase;
      }
    else {
      aktDB=m_nextDatabase;
      prevDB=m_currDatabase;
      nextDB=NULL;
      }

    UINT64 hashkey= (((UINT64)key[2])<<56)+
            (((UINT64)key[3])<<48)+
            (((UINT64)key[4])<<40)+
            (((UINT64)key[5])<<32)+
            (((UINT64)key[6])<<24)+
            (((UINT64)key[7])<<16)+
            (((UINT64)key[8])<<8)+
            ((UINT64)key[9]);

// insert
    if (prevDB!=NULL){
      t_databaseEntry* tmp=prevDB->m_pHashTable[key[0]][key[1]];

      while(tmp!=NULL){
        if (tmp->key!=hashkey) {
          tmp=tmp->next;
          }
        else {
          // duplicate found!!
          m_pMutex->unlock();
          return E_UNKNOWN;
          }
        }

      // inserting in DB
      tmp=new t_databaseEntry;
      tmp->next=prevDB->m_pHashTable[key[0]][key[1]];
      tmp->key=hashkey;
      prevDB->m_pHashTable[key[0]][key[1]]=tmp;
      prevDB->m_u32Size++;
      }

    t_databaseEntry* tmp=aktDB->m_pHashTable[key[0]][key[1]];

    while(tmp!=NULL){
      if (tmp->key!=hashkey) {
        tmp=tmp->next;
        }
      else {
        // duplicate found!!
        m_pMutex->unlock();
        return E_UNKNOWN;
        }
      }

    // inserting in DB
    tmp=new t_databaseEntry;
    tmp->next=aktDB->m_pHashTable[key[0]][key[1]];
    tmp->key=hashkey;
    aktDB->m_pHashTable[key[0]][key[1]]=tmp;
    aktDB->m_u32Size++;

    if (nextDB!=NULL){
      t_databaseEntry* tmp=nextDB->m_pHashTable[key[0]][key[1]];

      while(tmp!=NULL){
        if (tmp->key!=hashkey) {
          tmp=tmp->next;
          }
        else {
          // duplicate found!!
          m_pMutex->unlock();
          return E_UNKNOWN;
          }
        }

      // inserting in DB
      tmp=new t_databaseEntry;
      tmp->next=nextDB->m_pHashTable[key[0]][key[1]];
      tmp->key=hashkey;
      nextDB->m_pHashTable[key[0]][key[1]]=tmp;
      nextDB->m_u32Size++;
      }

    m_pMutex->unlock();
    return E_SUCCESS;

  }

Here is the call graph for this function:

SINT32 CADatabase::measurePerformance ( UINT8 strLogFile,
UINT32  lowerBoundEntries,
UINT32  upperBoundEntries,
UINT32  stepBy,
UINT32  meassuresPerStep,
UINT32  insertsPerMeasure 
) [static]

This mehtod can be used to measure the performance of the Replay database.

The results are stored in a file in csv format. Ths method will do several measures with different numbers of elements in the database. These number could be specified using owerBoundEntries,upperBoundEntries and stepBy.

Parameters:
strLogFilethe log file name
lowerBoundEntriesthe number of entries in the database (at beginn)
upperBoundEntriesthe number of entries in the database (at end)
stepByhow many entries should be added for each new measurement
meassuresPerStephow many measure values should be generate per step. That means that the experiement is repeated this many times.
insertsPerMeasureone measure value will be the time: (Total Insertion Time)/insertsPerMeasure

Definition at line 283 of file CADatabase.cpp.

References CADatabase(), diff64(), E_SUCCESS, fill(), getcurrentTimeMicros(), getRandom(), initRandom(), CAMsg::printMsg(), and simulateInsert().

  {
    initRandom();
    CADatabase* pDatabase=NULL;
    UINT32 aktNrOfEntries=lowerBoundEntries;
    UINT8* key=new UINT8[insertsPerMeasure*16];
    UINT8* aktKey;
    SINT32 file=open((char*)strLogFile,O_CREAT|O_WRONLY|O_LARGEFILE|O_TRUNC,S_IREAD|S_IWRITE);
    char buff[255];
    const char* atemplate="%u,%u,%u\n";
    const char* header="The format is as follows: Number of Entries in DB, Number of Inserts done, Total time for Inserts (in micro seconds)\n";
    write(file,header,strlen(header));
    while(aktNrOfEntries<=upperBoundEntries)
      {
        CAMsg::printMsg(LOG_DEBUG,"Starting measurement with %u entries in the replay database\n",aktNrOfEntries);
        for(UINT32 i=0;i<meassuresPerStep;i++)
          {
            pDatabase=new CADatabase();
            pDatabase->fill(aktNrOfEntries);
            UINT64 startTime,endTime;
            getRandom(key,insertsPerMeasure*16);
            aktKey=key;
            getcurrentTimeMicros(startTime);
            for(UINT32 j=0;j<insertsPerMeasure;j++)
              {
                pDatabase->simulateInsert(aktKey);
                aktKey+=16;
              }
            getcurrentTimeMicros(endTime);
            sprintf(buff,atemplate,aktNrOfEntries,insertsPerMeasure,diff64(endTime,startTime));
            write(file,buff,strlen(buff));
            printf("Start delete \n");
            getcurrentTimeMicros(startTime);
            delete pDatabase;
            pDatabase = NULL;
            getcurrentTimeMicros(endTime);
            printf("delete takes %u microsecs\n",diff64(endTime,startTime));
          }
        aktNrOfEntries+=stepBy;
      }
    delete[] key;
    key = NULL;
    return E_SUCCESS;
  }

Here is the call graph for this function:

Definition at line 242 of file CADatabase.cpp.

References clearDB(), E_SUCCESS, CAMutex::lock(), m_currDatabase, m_lastSwitch, m_nextDatabase, m_pMutex, m_prevDatabase, _t_database_info::m_u32Size, CAMsg::printMsg(), SECONDS_PER_INTERVALL, and CAMutex::unlock().

Referenced by db_loopMaintenance().

Here is the call graph for this function:

SINT32 CADatabase::simulateInsert ( UINT8  key[16]) [private]

This is a modified copy of insert() which simulates the insert() function as close as possible without actually changing the replay database.

Definition at line 348 of file CADatabase.cpp.

References E_SUCCESS, CAMutex::lock(), m_currDatabase, m_currentClock, m_pMutex, _t_database_info::m_u32Size, and CAMutex::unlock().

Referenced by measurePerformance().

  {
    m_pMutex->lock();
    UINT16 timestamp=(key[14]<<8)|key[15];
    if(timestamp<m_currentClock-1||timestamp>m_currentClock+1)
      {
        //m_pMutex->unlock();
        //return E_UNKNOWN;
      }
    t_databaseInfo* aktDB=m_currDatabase;
    if(timestamp>m_currentClock)
      {
        //aktDB=m_nextDatabase;
      }
    else if(timestamp<m_currentClock)
      {
        //aktDB=m_prevDatabase;
      }
//    UINT16 hashKey=(key[8]<<8)|key[9];
//    t_databaseEntry* hashList=aktDB->m_pHashTable[hashKey];
//    if(hashList==NULL)
//      {
//        LP_databaseEntry newEntry=getNewDBEntry(aktDB);
//        newEntry->left=NULL;
//        newEntry->right=NULL;
//        newEntry->key=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
        //aktDB->m_pHashTable[hashKey]=newEntry;
        aktDB->m_u32Size++;
        m_pMutex->unlock();
        return E_SUCCESS;
/*      }
    else
      {
//        UINT32 ret=key[0]<<24|key[1]<<16|key[2]<<8|key[3];
//        LP_databaseEntry before=NULL;
        do
          {
            //newEntry->keymemcmp(key,hashList->key,6);
            if(ret==hashList->key)
              {
                m_pMutex->unlock();
                return E_UNKNOWN;
              }
            before=hashList;
            if(hashList->key<ret)
              {
                hashList=hashList->right;
              }
            else
              {
                hashList=hashList->left;
              }
          } while(hashList!=NULL);
//        LP_databaseEntry newEntry=getNewDBEntry(aktDB);
//        newEntry->left=newEntry->right=NULL;
        //memcpy(newEntry->key,key,6);
//        newEntry->key=ret;
//        if(before->key<ret)
//          {
            //before->right=newEntry;
//          }
//        else
//          {
            //before->left=newEntry;
//          }
      }
    aktDB->m_u32Size++;
    m_pMutex->unlock();
    return E_SUCCESS; */
  }

Here is the call graph for this function:

Definition at line 202 of file CADatabase.cpp.

References db_loopMaintenance, m_bRun, m_pThread, CAThread::setMainLoop(), and CAThread::start().

Referenced by CAMiddleMix::init(), CALastMix::init(), and CAFirstMix::init().

Here is the call graph for this function:

Definition at line 210 of file CADatabase.cpp.

References E_SUCCESS, CAThread::join(), m_bRun, and m_pThread.

Referenced by ~CADatabase().

  {
    m_bRun=false;
    SINT32 ret=E_SUCCESS;
    if(m_pThread!=NULL)
      {
        ret=m_pThread->join();
        delete m_pThread;
        m_pThread=NULL;
      }
    return ret;
  }

Here is the call graph for this function:

Definition at line 257 of file CADatabase.cpp.

References E_SUCCESS, and fill().

  {
/*    CADatabase oDatabase;
    oDatabase.start();
    UINT8 key[16];
    memset(key,0,16);
    UINT32 i;
    for(i=0;i<20;i++)
      {
        getRandom(key,4);
        oDatabase.insert(key,time(NULL));///TODO WRONG - fixme
      }
    for(i=0;i<200000;i++)
      {
        getRandom(key,16);
        oDatabase.insert(key,time(NULL));//TODO WRONG - Fixme
      }
    oDatabase.stop();
*/
    UINT32 entries=10000;
    fill(entries);
    return E_SUCCESS;
//    return CADatabase::fill(entries);
  }

Here is the call graph for this function:


Friends And Related Function Documentation

THREAD_RETURN db_loopMaintenance ( void *  param) [friend]

Definition at line 223 of file CADatabase.cpp.

Referenced by start().

  {
    INIT_STACK;
    BEGIN_STACK("CADatabase::db_loopMaintenance");

    CADatabase* pDatabase=(CADatabase*)param;
    while(pDatabase->m_bRun)
      {
        sSleep(10);
        if (pDatabase->m_lastSwitch+SECONDS_PER_INTERVALL<=time(NULL)) {
          pDatabase->nextClock();
          }
      }

    FINISH_STACK("CADatabase::db_loopMaintenance");

    THREAD_RETURN_SUCCESS;
  }

Member Data Documentation

volatile bool CADatabase::m_bRun [private]

Definition at line 106 of file CADatabase.hpp.

Referenced by db_loopMaintenance(), start(), and stop().

Definition at line 103 of file CADatabase.hpp.

Referenced by CADatabase(), insert(), nextClock(), simulateInsert(), and ~CADatabase().

volatile SINT32 CADatabase::m_currentClock [private]

Definition at line 108 of file CADatabase.hpp.

Referenced by CADatabase(), and simulateInsert().

Definition at line 102 of file CADatabase.hpp.

Referenced by CADatabase(), db_loopMaintenance(), insert(), and nextClock().

Definition at line 104 of file CADatabase.hpp.

Referenced by CADatabase(), insert(), nextClock(), and ~CADatabase().

Definition at line 109 of file CADatabase.hpp.

Referenced by CADatabase(), insert(), nextClock(), simulateInsert(), and ~CADatabase().

Definition at line 105 of file CADatabase.hpp.

Referenced by CADatabase(), insert(), nextClock(), and ~CADatabase().

Definition at line 110 of file CADatabase.hpp.

Referenced by CADatabase(), start(), and stop().


The documentation for this class was generated from the following files: