UID optimizations addressing BIT-1016.

Max UID bit-length is now 128, but can be increased w/ trivial source
code change of BRO_UID_LEN.
This commit is contained in:
Jon Siwek 2013-08-28 15:35:18 -05:00
parent 0592b528ef
commit 5c119561ad
6 changed files with 60 additions and 42 deletions

View file

@ -4,10 +4,12 @@
#define BRO_UID_H
#include <string>
#include <vector>
#include "Reporter.h"
#include "util.h"
#define BRO_UID_LEN 2
namespace Bro {
/**
@ -18,53 +20,54 @@ class UID {
public:
/**
* Default ctor. The UID is uninitialized and in string format is
* represented by an empty string.
* Default ctor. The UID is uninitialized.
*/
UID() {}
UID() : initialized(false) {}
/**
* Construct a UID of a given bit-length, optionally from given values.
* @see UID::Set
*/
UID(bro_uint_t bits, const std::vector<uint64>& v = std::vector<uint64>())
{ Set(bits, v); }
UID(bro_uint_t bits, const uint64* v = 0, size_t n = 0)
{ Set(bits, v, n); }
/**
* Copy constructor.
*/
UID(const UID& other) { uid = other.uid; }
UID(const UID& other);
/**
* Inititialize a UID of a given bit-length, optionally from given values.
* @param bits The desired length in bits of the UID.
* @param v A vector of values with which to initialize the UID.
* If empty or doesn't contain enough values to satisfy \a bits,
* then values are automatically generated using
* @param bits The desired length in bits of the UID, up to a max of
* BRO_UID_LEN * 64.
* @param v A pointer to an array of values with which to initialize the
* UID. If empty or doesn't contain enough values to satisfy
* \a bits, then values are automatically generated using
* calculate_unique_id(). If \a bits isn't evenly divisible by
* 64, then a value is truncated to bit in desired bit-length.
* @param n number of 64-bit elements in array pointed to by \a v.
*/
void Set(bro_uint_t bits,
const std::vector<uint64>& v = std::vector<uint64>());
void Set(bro_uint_t bits, const uint64* v = 0, size_t n = 0);
/**
* Returns a base62 (characters 0-9, A-Z, a-z) representation of the UID.
* @param prefix An optional string prefix.
* @return a base62 string representing the UID.
*/
std::string Base62(const std::string& prefix = "") const;
std::string Base62(std::string prefix = "") const;
/**
* @return false if the UID instance was created via the default ctor
* and not yet initialized w/ Set().
* TODO: this would be better as an "explicit" conversion operator (C++11)
*/
operator bool() const { return ( ! uid.empty() ); }
operator bool() const
{ return initialized; }
/**
* Assignment operator.
*/
UID& operator=(const UID& other) { uid = other.uid; return *this; }
UID& operator=(const UID& other);
/**
* UID equality operator.
@ -78,11 +81,37 @@ public:
{ return ! ( u1 == u2 ); }
private:
std::vector<uint64> uid;
uint64 uid[BRO_UID_LEN];
bool initialized; // Since technically uid == 0 is a legit UID
};
bool operator==(const UID& u1, const UID& u2);
inline UID::UID(const UID& other)
{
for ( size_t i = 0; i < BRO_UID_LEN; ++i )
uid[i] = other.uid[i];
initialized = other.initialized;
}
inline UID& UID::operator=(const UID& other)
{
for ( size_t i = 0; i < BRO_UID_LEN; ++i )
uid[i] = other.uid[i];
initialized = other.initialized;
return *this;
}
inline std::string UID::Base62(std::string prefix) const
{
if ( ! initialized )
reporter->InternalError("use of uninitialized UID");
char tmp[64]; // technically, this should dynamically scale w/ BRO_UID_LEN
for ( size_t i = 0; i < BRO_UID_LEN; ++i )
prefix.append(uitoa_n(uid[i], tmp, sizeof(tmp), 62));
return prefix;
}
} // namespace Bro
#endif