Public Member Functions | Static Public Member Functions

Dedupe::Hash::Hash< T > Class Template Reference

Public Member Functions

 Hash ()
 Initalise all with default, no data hashed.
 Hash (void *data, uint64__ length)
void GenerateHash (void *data, uint64__ length)
void HashFileInfo (Dedupe::FileInfo &info)
void HashMultiplyFileInfo (Dedupe::FileStream &stream)
void AddToHash (void *data, uint64__ length)
GetHash ()
bool operator== (Hash< T > &h1) const
bool operator!= (Hash< T > &h1)
void Display ()

Static Public Member Functions

static T GetHash (void *data, uint64__ length, Hash &object)

Detailed Description

template<typename T>
class Dedupe::Hash::Hash< T >

Definition at line 28 of file hash.h.


Constructor & Destructor Documentation

template<typename T >
Dedupe::Hash::Hash< T >::Hash ( void *  data,
uint64__  length 
) [inline]

Initialise the class and hashes the given data

Parameters:
data The data which to hash
length The length of the datafield

Definition at line 47 of file hash.h.

        :hash ( 0 ),
         offset( (sizeof(T) > 4)?(14695981039346656037ULL):(2166136261UL) ),
         magic( (sizeof(T) > 4)?(1099511628211ULL):(16777619UL) )
{
        hash = offset;
        uint64__ i = 0;
        char *data2 = static_cast<char*>(data);
        T *longdata = static_cast<T*>(data);
        if((length % sizeof(T)) != 0) //If the length isn't dividable by the size of the hashvar, hash the single bytes until it is easy hashable
        {
                for(; i < (length % sizeof(T)); i++)
                {
                        hash = (hash ^ *(data2++)) * magic;    //hashen
                }
        }
        longdata += (length % sizeof(T));
        for(; i < length; i+=sizeof(T))
        {
                hash = (hash ^ *(longdata++)) * magic;  //Hash the rest
        }
}


Member Function Documentation

template<typename T >
void Dedupe::Hash::Hash< T >::AddToHash ( void *  data,
uint64__  length 
) [inline]

Adds data to the hash WITH using the already hashed data

Parameters:
data The data to hash
length The length of data

Definition at line 158 of file hash.h.

{
        if(hash == 0)
        {
                return;
        }
        uint64__ i = 0;
        char *data2 = static_cast<char*>(data);
        T *longdata = static_cast<T*>(data);
        if((length % sizeof(T)) != 0)
        {
                for(; i < (length % sizeof(T)); i++)
                {
                        hash = (hash ^ *data2++) * magic;
                }
        }
        longdata += (length % sizeof(T));
        for(; i < length; i += sizeof(T))
        {
                hash = (hash ^ *longdata++) * magic;
        }
}

template<typename T >
void Dedupe::Hash::Hash< T >::Display (  ) 

Displays the Hash in the console.

template<typename T >
void Dedupe::Hash::Hash< T >::GenerateHash ( void *  data,
uint64__  length 
) [inline]

Attention: Resets everything to start. DO NOT USE if you want to hash ADDITIONAL data Generates a new hash

Parameters:
data The data to hash
length The length of the datafield

Definition at line 74 of file hash.h.

{
        hash = offset;
        uint64__ i = 0;
        char *data2 = static_cast<char*>(data);
        T *longdata = static_cast<T>(data);
        if((length % sizeof(T)) != 0)
        {
                for(; i < (length % sizeof(T)); i++)
                {
                        hash = (hash ^ *data2++) * magic;
                }
        }
        longdata += (length % sizeof(T));
        for(; i < length; i += sizeof(T))
        {
                hash = (hash ^ *longdata++) * magic;
        }
}

template<typename T >
static T Dedupe::Hash::Hash< T >::GetHash ( void *  data,
uint64__  length,
Hash< T > &  object 
) [inline, static]

Static function to get a hash out of the given data

Parameters:
data The data to hash
length The length of the data

Definition at line 97 of file hash.h.

{
        T nHash = object.offset;
        uint64__ i = 0;
        char *data2 = static_cast<char*>(data);
        T *longdata = static_cast<T*>(data);
        if((length % sizeof(T)) != 0)
        {
                for(; i < (length % sizeof(T)); i++)
                {
                        nHash = (nHash ^ *data2++) * object.magic;
                }
        }
        longdata += (length % sizeof(T));
        for(; i < length; i += sizeof(T))
        {
                nHash = (nHash ^ *longdata++) * object.magic;
        }
        return nHash;
}

template<typename T >
T Dedupe::Hash::Hash< T >::GetHash (  )  [inline]

Returns the hash

Returns:
The hash as the spezified datatype

Definition at line 183 of file hash.h.

        {
                return hash;
        }

template<typename T >
void Dedupe::Hash::Hash< T >::HashFileInfo ( Dedupe::FileInfo info  )  [inline]

Generats the hash for a FileInfo.

Parameters:
inf A reference to the FileInfo

Definition at line 120 of file hash.h.

References Dedupe::FileInfo::GetPath(), and Dedupe::FileInfo::SetHash().

Referenced by Dedupe::Core::Kernel::AutoUpdateDatabase(), and Dedupe::Core::Kernel::FindHashDuplicatesExtern().

{
  boost::filesystem::ifstream *file = new boost::filesystem::ifstream();
        char *data = new char[FILE_PACKET_SIZE];
        unsigned long long int length = 0;
        file->open(info.GetPath(), ifstream::in);
        file->seekg(0, ios::end);
        length = file->tellg();
        file->seekg(0, ios::beg);

        file->read(data, (FILE_PACKET_SIZE > length)?(length):(FILE_PACKET_SIZE));
        Hash<T> hash(data, (FILE_PACKET_SIZE > length)?(length):(FILE_PACKET_SIZE));
        length -= (104857600 > length)?(length):(FILE_PACKET_SIZE);
        while(file->good() && length > 0)
        {
          file->read(data, (FILE_PACKET_SIZE > length)?(length):(FILE_PACKET_SIZE));
                hash.AddToHash(data, (FILE_PACKET_SIZE > length)?(length):(FILE_PACKET_SIZE));
                length -= FILE_PACKET_SIZE;
        }
        file->close();
        info.SetHash( hash.GetHash() );


        delete file;
        delete data;
}

template<typename T >
void Dedupe::Hash::Hash< T >::HashMultiplyFileInfo ( Dedupe::FileStream &  stream  )  [inline]

Generats for all given FileInfo objects the hash

Parameters:
stream A reference to the filestream

Definition at line 150 of file hash.h.

{
        std::for_each( stream.begin(), stream.end(), HashFileInfo );
}


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