sl@0: // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "SqlCompactTimer.h" sl@0: #include "SqlAssert.h" sl@0: #include "SqlCompactEntry.h" sl@0: #include "OstTraceDefinitions.h" sl@0: #ifdef OST_TRACE_COMPILER_IN_USE sl@0: #include "SqlCompactTimerTraces.h" sl@0: #endif sl@0: #include "SqlTraceDef.h" sl@0: sl@0: /** sl@0: Creates new CSqlCompactTimer instance. sl@0: sl@0: @param aIntervalMs The timer intrerval (ms). sl@0: sl@0: @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs. sl@0: */ sl@0: CSqlCompactTimer* CSqlCompactTimer::NewL(TInt aIntervalMs) sl@0: { sl@0: SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_NEWL_ENTRY, "Entry;0;CSqlCompactTimer::NewL;aIntervalMs=%d", aIntervalMs)); sl@0: __ASSERT_DEBUG(aIntervalMs > 0, __SQLPANIC2(ESqlPanicBadArgument)); sl@0: CSqlCompactTimer* self = new (ELeave) CSqlCompactTimer(aIntervalMs); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: SQL_TRACE_COMPACT(OstTraceExt2(TRACE_INTERNALS, CSQLCOMPACTTIMER_NEWL_EXIT, "Exit;0x%X;CSqlCompactTimer::NewL;aIntervalMs=%d", (TUint)self, aIntervalMs)); sl@0: return self; sl@0: } sl@0: sl@0: /** sl@0: Destroys the object. sl@0: */ sl@0: CSqlCompactTimer::~CSqlCompactTimer() sl@0: { sl@0: SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_CSQLCOMPACTTIMER2, "0x%X;CSqlCompactTimer::~CSqlCompactTimer", (TUint)this)); sl@0: Cancel(); sl@0: } sl@0: sl@0: /** sl@0: If the queue is not empty, the timer will be restarted. sl@0: */ sl@0: void CSqlCompactTimer::Restart() sl@0: { sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: if(!iQueue.IsEmpty()) sl@0: { sl@0: Cancel(); sl@0: After(iIntervalMicroSec); sl@0: } sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: } sl@0: sl@0: /** sl@0: Adds a database entry, that requires a background compaction, to the queue. sl@0: sl@0: @param aEntry The database entry to be compacted. sl@0: sl@0: @see CSqlCompactEntry sl@0: */ sl@0: void CSqlCompactTimer::Queue(CSqlCompactEntry& aEntry) sl@0: { sl@0: SQL_TRACE_COMPACT(OstTraceExt3(TRACE_INTERNALS, CSQLCOMPACTTIMER_QUEUE, "0x%X;CSqlCompactTimer::Queue;aEntry=0x%X;Name=%S", (TUint)this, (TUint)&aEntry,__SQLPRNSTR(aEntry.FullName()))); sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: iQueue.AddFirst(aEntry); sl@0: if(!IsActive()) sl@0: { sl@0: After(iIntervalMicroSec); sl@0: } sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: } sl@0: sl@0: /** sl@0: Removes the specified database entry from the queue. sl@0: sl@0: @param aEntry The database entry to be removed from the queue. sl@0: sl@0: @see CSqlCompactEntry sl@0: */ sl@0: void CSqlCompactTimer::DeQueue(CSqlCompactEntry& aEntry) sl@0: { sl@0: SQL_TRACE_COMPACT(OstTraceExt2(TRACE_INTERNALS, CSQLCOMPACTTIMER_DEQUEUE, "0x%X;CSqlCompactTimer::DeQueue;aEntry=0x%X", (TUint)this, (TUint)&aEntry)); sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: iQueue.Remove(aEntry); sl@0: if(iQueue.IsEmpty()) sl@0: { sl@0: Cancel(); sl@0: } sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: } sl@0: sl@0: /** sl@0: Initializes the CSqlCompactTimer instance. sl@0: sl@0: @param aIntervalMs The timer intrerval (ms). sl@0: sl@0: @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs. sl@0: */ sl@0: CSqlCompactTimer::CSqlCompactTimer(TInt aIntervalMs) : sl@0: CTimer(CActive::EPriorityIdle), sl@0: iIntervalMicroSec(aIntervalMs * 1000), sl@0: iQueue(_FOFF(CSqlCompactEntry, iLink)) sl@0: { sl@0: __ASSERT_DEBUG(aIntervalMs > 0, __SQLPANIC(ESqlPanicBadArgument)); sl@0: } sl@0: sl@0: /** sl@0: Initializes the created CSqlCompactTimer instance. sl@0: */ sl@0: void CSqlCompactTimer::ConstructL() sl@0: { sl@0: CTimer::ConstructL(); sl@0: CActiveScheduler::Add(this); sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: } sl@0: sl@0: /** sl@0: CActive::RunL() implementation. sl@0: The RunL() implementation picks-up the last CSqlCompactEntry object from the queue and calls its Compact() method. sl@0: At the end of the call, if the queue is not empty (CSqlCompactEntry::Compact() may remove the object from the queue if sl@0: the compaction has been completed), the timer will be reactivated. sl@0: sl@0: @panic SqlDb 7 The queue is empty. sl@0: @panic SqlDb 7 In _DEBUG mode. The last entry in the queue is NULL. sl@0: */ sl@0: void CSqlCompactTimer::RunL() sl@0: { sl@0: SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_RUNL, "0x%X;CSqlCompactTimer::RunL", (TUint)this)); sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: __ASSERT_ALWAYS(!iQueue.IsEmpty(), __SQLPANIC(ESqlPanicInternalError)); sl@0: CSqlCompactEntry* entry = iQueue.Last(); sl@0: SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTTIMER_RUNL2, "0x%X;CSqlCompactTimer::RunL;Compact;entry=0x%X;Name=%S;iQueue.IsEmpty()=%d", (TUint)this, (TUint)entry,__SQLPRNSTR(entry->FullName()), (TInt)iQueue.IsEmpty())); sl@0: __ASSERT_DEBUG(entry, __SQLPANIC(ESqlPanicInternalError)); sl@0: (void)entry->Compact(); sl@0: if(!iQueue.IsEmpty()) sl@0: { sl@0: After(iIntervalMicroSec); sl@0: } sl@0: SQLCOMPACTTIMER_INVARIANT(); sl@0: } sl@0: sl@0: #ifdef _DEBUG sl@0: /** sl@0: CSqlCompactTimer invariant. sl@0: */ sl@0: void CSqlCompactTimer::Invariant() const sl@0: { sl@0: __ASSERT_DEBUG(iIntervalMicroSec > 0, __SQLPANIC(ESqlPanicInternalError)); sl@0: if(!iQueue.IsEmpty()) sl@0: { sl@0: CSqlCompactEntry* entry = iQueue.Last(); sl@0: __ASSERT_DEBUG(entry != NULL, __SQLPANIC(ESqlPanicInternalError)); sl@0: } sl@0: } sl@0: #endif//_DEBUG