os/persistentdata/persistentstorage/sql/SRC/Server/Compact/SqlCompactTimer.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "SqlCompactTimer.h"
    17 #include "SqlAssert.h"
    18 #include "SqlCompactEntry.h"
    19 #include "OstTraceDefinitions.h"
    20 #ifdef OST_TRACE_COMPILER_IN_USE
    21 #include "SqlCompactTimerTraces.h"
    22 #endif
    23 #include "SqlTraceDef.h"
    24 
    25 /**
    26 Creates new CSqlCompactTimer instance.
    27 
    28 @param aIntervalMs The timer intrerval (ms).
    29 
    30 @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs.
    31 */
    32 CSqlCompactTimer* CSqlCompactTimer::NewL(TInt aIntervalMs)
    33 	{
    34 	SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_NEWL_ENTRY, "Entry;0;CSqlCompactTimer::NewL;aIntervalMs=%d", aIntervalMs));
    35 	__ASSERT_DEBUG(aIntervalMs > 0, __SQLPANIC2(ESqlPanicBadArgument));
    36 	CSqlCompactTimer* self = new (ELeave) CSqlCompactTimer(aIntervalMs);
    37 	CleanupStack::PushL(self);
    38 	self->ConstructL();
    39 	CleanupStack::Pop(self);
    40 	SQL_TRACE_COMPACT(OstTraceExt2(TRACE_INTERNALS, CSQLCOMPACTTIMER_NEWL_EXIT, "Exit;0x%X;CSqlCompactTimer::NewL;aIntervalMs=%d", (TUint)self, aIntervalMs));
    41 	return self;
    42 	}
    43 
    44 /**
    45 Destroys the object.
    46 */
    47 CSqlCompactTimer::~CSqlCompactTimer()
    48 	{
    49 	SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_CSQLCOMPACTTIMER2, "0x%X;CSqlCompactTimer::~CSqlCompactTimer", (TUint)this));
    50 	Cancel();
    51 	}
    52 
    53 /**
    54 If the queue is not empty, the timer will be restarted.
    55 */
    56 void CSqlCompactTimer::Restart()
    57 	{
    58 	SQLCOMPACTTIMER_INVARIANT();
    59 	if(!iQueue.IsEmpty())
    60 		{
    61 		Cancel();
    62 		After(iIntervalMicroSec);
    63 		}
    64 	SQLCOMPACTTIMER_INVARIANT();
    65 	}
    66 
    67 /**
    68 Adds a database entry, that requires a background compaction, to the queue.
    69 
    70 @param aEntry The database entry to be compacted.
    71 
    72 @see CSqlCompactEntry
    73 */
    74 void CSqlCompactTimer::Queue(CSqlCompactEntry& aEntry)
    75 	{
    76 	SQL_TRACE_COMPACT(OstTraceExt3(TRACE_INTERNALS, CSQLCOMPACTTIMER_QUEUE, "0x%X;CSqlCompactTimer::Queue;aEntry=0x%X;Name=%S", (TUint)this, (TUint)&aEntry,__SQLPRNSTR(aEntry.FullName())));
    77 	SQLCOMPACTTIMER_INVARIANT();
    78 	iQueue.AddFirst(aEntry);
    79 	if(!IsActive())
    80 		{
    81 		After(iIntervalMicroSec);
    82 		}
    83 	SQLCOMPACTTIMER_INVARIANT();
    84 	}
    85 
    86 /**
    87 Removes the specified database entry from the queue.
    88 
    89 @param aEntry The database entry to be removed from the queue.
    90 
    91 @see CSqlCompactEntry
    92 */
    93 void CSqlCompactTimer::DeQueue(CSqlCompactEntry& aEntry)
    94 	{
    95 	SQL_TRACE_COMPACT(OstTraceExt2(TRACE_INTERNALS, CSQLCOMPACTTIMER_DEQUEUE, "0x%X;CSqlCompactTimer::DeQueue;aEntry=0x%X", (TUint)this, (TUint)&aEntry));
    96 	SQLCOMPACTTIMER_INVARIANT();
    97 	iQueue.Remove(aEntry);
    98 	if(iQueue.IsEmpty())
    99 		{
   100 		Cancel();
   101 		}
   102 	SQLCOMPACTTIMER_INVARIANT();
   103 	}
   104 
   105 /**
   106 Initializes the CSqlCompactTimer instance.
   107 
   108 @param aIntervalMs The timer intrerval (ms).
   109 
   110 @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs.
   111 */
   112 CSqlCompactTimer::CSqlCompactTimer(TInt aIntervalMs) :
   113 	CTimer(CActive::EPriorityIdle),
   114 	iIntervalMicroSec(aIntervalMs * 1000),
   115 	iQueue(_FOFF(CSqlCompactEntry, iLink))
   116 	{
   117 	__ASSERT_DEBUG(aIntervalMs > 0, __SQLPANIC(ESqlPanicBadArgument));
   118 	}
   119 
   120 /**
   121 Initializes the created CSqlCompactTimer instance.
   122 */
   123 void CSqlCompactTimer::ConstructL()
   124 	{
   125 	CTimer::ConstructL();	
   126 	CActiveScheduler::Add(this);
   127 	SQLCOMPACTTIMER_INVARIANT();
   128 	}
   129 
   130 /**
   131 CActive::RunL() implementation.
   132 The RunL() implementation picks-up the last CSqlCompactEntry object from the queue and calls its Compact() method.
   133 At the end of the call, if the queue is not empty (CSqlCompactEntry::Compact() may remove the object from the queue if
   134 the compaction has been completed), the timer will be reactivated.
   135 
   136 @panic SqlDb 7 The queue is empty.
   137 @panic SqlDb 7 In _DEBUG mode. The last entry in the queue is NULL.
   138 */
   139 void CSqlCompactTimer::RunL()
   140 	{
   141 	SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTTIMER_RUNL, "0x%X;CSqlCompactTimer::RunL", (TUint)this));
   142 	SQLCOMPACTTIMER_INVARIANT();
   143 	__ASSERT_ALWAYS(!iQueue.IsEmpty(), __SQLPANIC(ESqlPanicInternalError));	
   144 	CSqlCompactEntry* entry = iQueue.Last();
   145 	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()));
   146 	__ASSERT_DEBUG(entry, __SQLPANIC(ESqlPanicInternalError));	
   147 	(void)entry->Compact();
   148 	if(!iQueue.IsEmpty())
   149 		{
   150 		After(iIntervalMicroSec);
   151 		}
   152 	SQLCOMPACTTIMER_INVARIANT();
   153 	}
   154 
   155 #ifdef _DEBUG
   156 /**
   157 CSqlCompactTimer invariant.
   158 */
   159 void CSqlCompactTimer::Invariant() const
   160 	{
   161 	__ASSERT_DEBUG(iIntervalMicroSec > 0, __SQLPANIC(ESqlPanicInternalError));
   162 	if(!iQueue.IsEmpty())
   163 		{
   164 		CSqlCompactEntry* entry = iQueue.Last();
   165 		__ASSERT_DEBUG(entry != NULL, __SQLPANIC(ESqlPanicInternalError));
   166 		}
   167 	}
   168 #endif//_DEBUG