//--------------------------------------------------------------------
//--------------------------------------------------------------------
//
void CPosLmNameIndex::SaveL( TChar aDrive )
    {
    RDbTable table;
    TInt err = table.Open( iDatabase, KPosLmIndexTable, RDbRowSet::EUpdatable );
    if ( err == KErrNotFound )
        {
        PosLmDatabaseManager::CreateIndexTableL( iDatabase );
        err = table.Open( iDatabase, KPosLmIndexTable, RDbRowSet::EUpdatable );
        }
    User::LeaveIfError( err );
    CleanupClosePushL( table );

    TInt currentSize = 0;
    table.FirstL();
    if ( table.AtEnd() )
        {
        table.InsertL();
        }
    else
        {
        table.GetL();
        currentSize = table.ColSize( EPosLmIncIndexDataCol ); 
        table.UpdateL();
        }
    
    if ( currentSize < DataSize() )
        {
        // check disk size
        CPosLmDiskUtilities* diskUtilities = CPosLmDiskUtilities::NewL();
        CleanupStack::PushL( diskUtilities );
    
        TInt bytesToWrite = DataSize() - currentSize;
        diskUtilities->DiskSpaceBelowCriticalLevelL( bytesToWrite, aDrive );
    
        CleanupStack::PopAndDestroy( diskUtilities );
        }
    
    // current language
    table.SetColL( EPosLmIncLanguageCol, User::Language() );
    
    // index data
    RDbColWriteStream writeStream;
    writeStream.OpenL( table, EPosLmIncIndexDataCol );
    CleanupClosePushL( writeStream );
    ExternalizeL( writeStream );
    CleanupStack::PopAndDestroy( &writeStream );
    
    // index timestamp
    TTime now;
    now.UniversalTime();
    table.SetColL( EPosLmIncTimestampCol, now );

#ifdef _DEBUG  
    TBuf<64> mtime;
    now.FormatL( mtime, KPosLmTimeFormat );
    LOG1( "NameIndex: Saving index timestamp %S", &mtime); 
#endif    

    table.PutL();
    CleanupStack::PopAndDestroy ( &table );
    
    iTimeStamp = now;
    }
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//
void CPosLmNameIndex::LoadL()
    {
    // Skip index loading for empty databases
    TInt numLandmarks = PosLmServerUtility::TotalLandmarkCountL( iDbAccess );
    if ( numLandmarks == 0 )
        {
        iTimeStamp.UniversalTime();
        iStatus = KErrNone;
        return;
        }
    
    RDbTable table;
    TInt err = table.Open( iDatabase, KPosLmIndexTable, RDbRowSet::EReadOnly );
    if ( err )
        {
        LOG("NameIndex::LoadL: index table not found"); 
        User::Leave( err );
        }
    CleanupClosePushL( table );

    table.FirstL();
    if ( table.AtEnd() )
        {
        LOG("NameIndex::LoadL: index not found"); 
        User::Leave( KErrNotFound  );
        }

    table.GetL();

    // verify that index is valid for current language
    if ( !table.IsColNull( EPosLmIncLanguageCol ) )
        {
        TLanguage lang = (TLanguage) table.ColInt32( EPosLmIncLanguageCol );
        if ( User::Language() != lang ) 
            {
            LOG2("NameIndex::LoadL: index not valid, lang %d, current lang %d", 
                lang, User::Language() );
            User::Leave( KErrCorrupt );
            }
        }
    else
        {
        LOG("NameIndex::LoadL: index lang not found"); 
        User::Leave( KErrCorrupt );
        }

    // read the index
    if ( !table.IsColNull( EPosLmIncIndexDataCol ) )
        {
        RDbColReadStream readStream;
        readStream.OpenL( table, EPosLmIncIndexDataCol );
        CleanupClosePushL( readStream );
        InternalizeL( readStream );
        CleanupStack::PopAndDestroy( &readStream );

        // basic check for the index
        if ( Count() != numLandmarks )
            {
            LOG2("NameIndex::LoadL: index not valid, count %d, landmarks in db %d", 
                Count(), numLandmarks );
            User::Leave( KErrCorrupt );
            }
        }
    else
        {
        LOG("NameIndex::LoadL: index data not found"); 
        User::Leave( KErrCorrupt );
        }

    // read the time stamp
    iTimeStamp.UniversalTime();
    if ( !table.IsColNull( EPosLmIncTimestampCol ) )
        {
        iTimeStamp = table.ColTime( EPosLmIncTimestampCol );
        }
    else
        {
        LOG("NameIndex::LoadL: index timestamp not found"); 
        User::Leave( KErrCorrupt );
        }

    CleanupStack::PopAndDestroy ( &table );

    // index is valid
    iStatus = KErrNone;
    }