Reduce KIID size from 20 to 16 bytes.

Removes separate legacy timestamp value.
This commit is contained in:
Alex Shvartzkop 2024-06-20 13:59:15 +03:00
parent f5be388acd
commit b143ffb797
3 changed files with 42 additions and 30 deletions

View File

@ -73,8 +73,6 @@ KIID& NilUuid()
KIID::KIID()
{
m_cached_timestamp = 0;
#if BOOST_VERSION >= 106700
try
{
@ -102,16 +100,14 @@ KIID::KIID()
KIID::KIID( int null ) :
m_uuid( nilGenerator() ),
m_cached_timestamp( 0 )
m_uuid( nilGenerator() )
{
wxASSERT( null == 0 );
}
KIID::KIID( const std::string& aString ) :
m_uuid(),
m_cached_timestamp( 0 )
m_uuid()
{
if( aString.length() == 8
&& std::all_of( aString.begin(), aString.end(),
@ -128,17 +124,12 @@ KIID::KIID( const std::string& aString ) :
std::string octet = aString.substr( i * 2, 2 );
m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
}
m_cached_timestamp = strtol( aString.c_str(), nullptr, 16 );
}
else
{
try
{
m_uuid = stringGenerator( aString );
if( IsLegacyTimestamp() )
m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), nullptr, 16 );
}
catch( ... )
{
@ -206,18 +197,10 @@ bool KIID::SniffTest( const wxString& aCandidate )
KIID::KIID( timestamp_t aTimestamp )
{
m_cached_timestamp = aTimestamp;
// A legacy-timestamp-based UUID has only the last 4 octets filled in.
// Convert them individually to avoid stepping in the little-endian/big-endian
// doo-doo.
wxString str = AsLegacyTimestampString();
for( int i = 0; i < 4; ++i )
{
wxString octet = str.substr( i * 2, 2 );
m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
}
m_uuid.data[12] = static_cast<uint8_t>( aTimestamp >> 24 );
m_uuid.data[13] = static_cast<uint8_t>( aTimestamp >> 16 );
m_uuid.data[14] = static_cast<uint8_t>( aTimestamp >> 8 );
m_uuid.data[15] = static_cast<uint8_t>( aTimestamp );
}
@ -229,7 +212,14 @@ bool KIID::IsLegacyTimestamp() const
timestamp_t KIID::AsLegacyTimestamp() const
{
return m_cached_timestamp;
timestamp_t ret = 0;
ret |= m_uuid.data[12] << 24;
ret |= m_uuid.data[13] << 16;
ret |= m_uuid.data[14] << 8;
ret |= m_uuid.data[15];
return ret;
}
@ -249,8 +239,7 @@ size_t KIID::Hash() const
void KIID::Clone( const KIID& aUUID )
{
m_uuid = aUUID.m_uuid;
m_cached_timestamp = aUUID.m_cached_timestamp;
m_uuid = aUUID.m_uuid;
}
@ -277,8 +266,7 @@ void KIID::ConvertTimestampToUuid()
if( !IsLegacyTimestamp() )
return;
m_cached_timestamp = 0;
m_uuid = randomGenerator();
m_uuid = randomGenerator();
}

View File

@ -126,8 +126,6 @@ public:
private:
boost::uuids::uuid m_uuid;
timestamp_t m_cached_timestamp;
};

View File

@ -19,6 +19,7 @@
#include <boost/test/unit_test.hpp>
#include <kiid.h>
#include <wx/string.h>
BOOST_AUTO_TEST_SUITE( Kiid )
@ -70,4 +71,29 @@ BOOST_AUTO_TEST_CASE( KiidPathTest )
}
BOOST_AUTO_TEST_CASE( LegacyTimestamp )
{
timestamp_t ts_a = 0xAABBCCDD;
timestamp_t ts_b = 0x00000012;
wxString str_a( wxS( "AABBCCDD" ) );
wxString str_b( wxS( "00000012" ) );
KIID a( ts_a );
KIID b( ts_b );
BOOST_CHECK( a.AsLegacyTimestamp() == ts_a );
BOOST_CHECK( a.AsLegacyTimestampString() == str_a );
BOOST_CHECK( b.AsLegacyTimestamp() == ts_b );
BOOST_CHECK( b.AsLegacyTimestampString() == str_b );
BOOST_CHECK( KIID( str_a ).AsLegacyTimestamp() == ts_a );
BOOST_CHECK( KIID( str_b ).AsLegacyTimestamp() == ts_b );
BOOST_CHECK( KIID( str_a ).AsLegacyTimestampString() == str_a );
BOOST_CHECK( KIID( str_b ).AsLegacyTimestampString() == str_b );
}
BOOST_AUTO_TEST_SUITE_END()