Use a hash instead of an array for indexing loggables.

This commit is contained in:
Jonathan Moore Liles 2009-01-11 17:48:21 -06:00
parent 15a579774c
commit 0032dce478
2 changed files with 31 additions and 62 deletions

View File

@ -48,8 +48,7 @@ int Loggable::_level = 0;
off_t Loggable::_undo_offset = 0; off_t Loggable::_undo_offset = 0;
size_t Loggable::_loggables_size = 0; std::map <unsigned int, Loggable *> Loggable::_loggables;
Loggable ** Loggable::_loggables;
std::map <unsigned int, Log_Entry *> Loggable::_loggables_unjournaled; std::map <unsigned int, Log_Entry *> Loggable::_loggables_unjournaled;
std::map <std::string, create_func*> Loggable::_class_map; std::map <std::string, create_func*> Loggable::_class_map;
@ -65,31 +64,12 @@ void *Loggable::_snapshot_callback_arg = NULL;
Loggable::~Loggable ( ) Loggable::~Loggable ( )
{ {
_loggables[ _id - 1 ] = NULL; _loggables[ _id ] = NULL;
} }
/** ensure that _loggables array is big enough for /n/ elements */
void
Loggable::ensure_size ( size_t n )
{
if ( n > _loggables_size )
{
size_t p = 0;
while ( ( (unsigned)1 << p ) < n ) ++p;
size_t os = _loggables_size;
_loggables_size = 1 << p ;
_loggables = (Loggable**) realloc( _loggables, sizeof( Loggable ** ) * _loggables_size );
for ( unsigned int i = os; i < _loggables_size; ++i )
_loggables[ i ] = 0;
}
}
void void
Loggable::block_start ( void ) Loggable::block_start ( void )
{ {
@ -110,10 +90,7 @@ Loggable::block_end ( void )
Loggable * Loggable *
Loggable::find ( unsigned int id ) Loggable::find ( unsigned int id )
{ {
if ( id > _log_id ) return _loggables[ id ];
return NULL;
return _loggables[ id - 1 ];
} }
@ -175,7 +152,7 @@ Loggable::load_unjournaled_state ( void )
{ {
Log_Entry *e = new Log_Entry( buf ); Log_Entry *e = new Log_Entry( buf );
_loggables_unjournaled[ id - 1 ] = e; _loggables_unjournaled[ id ] = e;
Loggable *l = Loggable::find( id ); Loggable *l = Loggable::find( id );
} }
@ -242,21 +219,18 @@ Loggable::close ( void )
if ( ! snapshot( "snapshot" ) ) if ( ! snapshot( "snapshot" ) )
WARNING( "Failed to create snapshot" ); WARNING( "Failed to create snapshot" );
for ( unsigned int i = 0; i < _log_id - 1; ++i )
{
Loggable ** l = &_loggables[ i ];
if ( *l ) for ( std::map <unsigned int, Loggable *>::iterator i = _loggables.begin();
i != _loggables.end(); ++i )
{ {
delete *l; if ( i->second )
*l = NULL; {
delete i->second;
} }
} }
save_unjournaled_state(); save_unjournaled_state();
_log_id = 0;
return true; return true;
} }
@ -271,15 +245,14 @@ Loggable::save_unjournaled_state ( void )
/* write out the unjournaled state of all currently active /* write out the unjournaled state of all currently active
* loggables */ * loggables */
for ( int i = 0; i < _log_id - 1; ++i ) for ( std::map <unsigned int, Log_Entry *>::iterator i = _loggables_unjournaled.begin();
i != _loggables_unjournaled.end(); ++i )
{ {
Log_Entry *e = _loggables_unjournaled[ i ]; if ( i->second )
if ( e )
{ {
char *s = e->print(); char *s = i->second->print();
fprintf( fp, "0x%X set %s\n", i + 1, s ); fprintf( fp, "0x%X set %s\n", i->first, s );
free( s ); free( s );
} }
@ -298,9 +271,9 @@ Loggable::update_id ( unsigned int id )
{ {
/* make sure we're the last one */ /* make sure we're the last one */
assert( _id == _log_id ); assert( _id == _log_id );
assert( _loggables[ _id - 1 ] == this ); assert( _loggables[ _id ] == this );
_loggables[ _id - 1 ] = NULL; _loggables[ _id ] = NULL;
_log_id = max( _log_id, id ); _log_id = max( _log_id, id );
@ -309,13 +282,10 @@ Loggable::update_id ( unsigned int id )
_id = id; _id = id;
/* make sure it'll fit */ if ( _loggables[ _id ] )
ensure_size( _id ); FATAL( "Attempt to create object with an ID (0x%X) that already exists. The existing object is of type \"%s\", the new one is \"%s\". Corrupt journal?", _id, _loggables[ _id ]->class_name(), class_name() );
if ( _loggables[ _id - 1 ] ) _loggables[ _id ] = this;
FATAL( "Attempt to create object with an ID (0x%X) that already exists. The existing object is of type \"%s\", the new one is \"%s\". Corrupt journal?", _id, _loggables[ _id - 1 ]->class_name(), class_name() );
_loggables[ _id - 1 ] = this;
} }
/** return a pointer to a static copy of /s/ with all special characters escaped */ /** return a pointer to a static copy of /s/ with all special characters escaped */
@ -415,8 +385,10 @@ Loggable::do_this ( const char *s, bool reverse )
/* we're now creating a loggable. Apply any unjournaled /* we're now creating a loggable. Apply any unjournaled
* state it may have had in the past under this log ID */ * state it may have had in the past under this log ID */
if ( _loggables_unjournaled[ id - 1 ] ) Log_Entry *e = _loggables_unjournaled[ id ];
l->set( *_loggables_unjournaled[ id - 1 ] );
if ( e )
l->set( *e );
} }
} }
@ -717,19 +689,19 @@ Loggable::record_unjournaled ( void ) const
get_unjournaled( *e ); get_unjournaled( *e );
Log_Entry **le = &_loggables_unjournaled[ _id - 1 ]; Log_Entry *le = _loggables_unjournaled[ _id ];
if ( *le ) if ( le )
delete *le; delete le;
if ( e->size() ) if ( e->size() )
{ {
*le = e; _loggables_unjournaled[ _id ] = e;
DMESSAGE( "logging %s", (*le)->print() ); DMESSAGE( "logging %s", e->print() );
} }
else else
/* don't waste space on loggables with no unjournaled properties */ /* don't waste space on loggables with no unjournaled properties */
*le = NULL; _loggables_unjournaled[ _id ] = NULL;
} }
/** Log object destruction. *Must* be called at the beginning of the /** Log object destruction. *Must* be called at the beginning of the

View File

@ -71,8 +71,7 @@ class Loggable
static off_t _undo_offset; static off_t _undo_offset;
static size_t _loggables_size; static std::map <unsigned int, Loggable *> _loggables;
static Loggable ** _loggables;
static std::map <unsigned int, Log_Entry *> _loggables_unjournaled; static std::map <unsigned int, Log_Entry *> _loggables_unjournaled;
static std::map <std::string, create_func*> _class_map; static std::map <std::string, create_func*> _class_map;
@ -115,9 +114,7 @@ private:
{ {
_id = ++_log_id; _id = ++_log_id;
ensure_size( _id ); _loggables[ _id ] = this;
_loggables[ _id - 1 ] = this;
} }
else else
_id = 0; _id = 0;