|
1 // Copyright (c) 2008-2009 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 "SqlPanic.h" |
|
18 #include "SqlCompactEntry.h" |
|
19 |
|
20 /** |
|
21 Creates new CSqlCompactTimer instance. |
|
22 |
|
23 @param aIntervalMs The timer intrerval (ms). |
|
24 |
|
25 @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs. |
|
26 */ |
|
27 CSqlCompactTimer* CSqlCompactTimer::NewL(TInt aIntervalMs) |
|
28 { |
|
29 __SQLASSERT(aIntervalMs > 0, ESqlPanicBadArgument); |
|
30 CSqlCompactTimer* self = new (ELeave) CSqlCompactTimer(aIntervalMs); |
|
31 CleanupStack::PushL(self); |
|
32 self->ConstructL(); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 /** |
|
38 Destroys the object. |
|
39 */ |
|
40 CSqlCompactTimer::~CSqlCompactTimer() |
|
41 { |
|
42 Cancel(); |
|
43 } |
|
44 |
|
45 /** |
|
46 If the queue is not empty, the timer will be restarted. |
|
47 */ |
|
48 void CSqlCompactTimer::Restart() |
|
49 { |
|
50 SQLCOMPACTTIMER_INVARIANT(); |
|
51 if(!iQueue.IsEmpty()) |
|
52 { |
|
53 Cancel(); |
|
54 After(iIntervalMicroSec); |
|
55 } |
|
56 SQLCOMPACTTIMER_INVARIANT(); |
|
57 } |
|
58 |
|
59 /** |
|
60 Adds a database entry, that requires a background compaction, to the queue. |
|
61 |
|
62 @param aEntry The database entry to be compacted. |
|
63 |
|
64 @see CSqlCompactEntry |
|
65 */ |
|
66 void CSqlCompactTimer::Queue(CSqlCompactEntry& aEntry) |
|
67 { |
|
68 SQLCOMPACTTIMER_INVARIANT(); |
|
69 iQueue.AddFirst(aEntry); |
|
70 if(!IsActive()) |
|
71 { |
|
72 After(iIntervalMicroSec); |
|
73 } |
|
74 SQLCOMPACTTIMER_INVARIANT(); |
|
75 } |
|
76 |
|
77 /** |
|
78 Removes the specified database entry from the queue. |
|
79 |
|
80 @param aEntry The database entry to be removed from the queue. |
|
81 |
|
82 @see CSqlCompactEntry |
|
83 */ |
|
84 void CSqlCompactTimer::DeQueue(CSqlCompactEntry& aEntry) |
|
85 { |
|
86 SQLCOMPACTTIMER_INVARIANT(); |
|
87 iQueue.Remove(aEntry); |
|
88 if(iQueue.IsEmpty()) |
|
89 { |
|
90 Cancel(); |
|
91 } |
|
92 SQLCOMPACTTIMER_INVARIANT(); |
|
93 } |
|
94 |
|
95 /** |
|
96 Initializes the CSqlCompactTimer instance. |
|
97 |
|
98 @param aIntervalMs The timer intrerval (ms). |
|
99 |
|
100 @panic SqlDb 4 In _DEBUG mode. Zero or negative aIntervalMs. |
|
101 */ |
|
102 CSqlCompactTimer::CSqlCompactTimer(TInt aIntervalMs) : |
|
103 CTimer(CActive::EPriorityIdle), |
|
104 iIntervalMicroSec(aIntervalMs * 1000), |
|
105 iQueue(_FOFF(CSqlCompactEntry, iLink)) |
|
106 { |
|
107 __SQLASSERT(aIntervalMs > 0, ESqlPanicBadArgument); |
|
108 } |
|
109 |
|
110 /** |
|
111 Initializes the created CSqlCompactTimer instance. |
|
112 */ |
|
113 void CSqlCompactTimer::ConstructL() |
|
114 { |
|
115 CTimer::ConstructL(); |
|
116 CActiveScheduler::Add(this); |
|
117 SQLCOMPACTTIMER_INVARIANT(); |
|
118 } |
|
119 |
|
120 /** |
|
121 CActive::RunL() implementation. |
|
122 The RunL() implementation picks-up the last CSqlCompactEntry object from the queue and calls its Compact() method. |
|
123 At the end of the call, if the queue is not empty (CSqlCompactEntry::Compact() may remove the object from the queue if |
|
124 the compaction has been completed), the timer will be reactivated. |
|
125 |
|
126 @panic SqlDb 7 The queue is empty. |
|
127 @panic SqlDb 7 In _DEBUG mode. The last entry in the queue is NULL. |
|
128 */ |
|
129 void CSqlCompactTimer::RunL() |
|
130 { |
|
131 SQLCOMPACTTIMER_INVARIANT(); |
|
132 __SQLASSERT_ALWAYS(!iQueue.IsEmpty(), ESqlPanicInternalError); |
|
133 CSqlCompactEntry* entry = iQueue.Last(); |
|
134 __SQLASSERT(entry, ESqlPanicInternalError); |
|
135 (void)entry->Compact(); |
|
136 if(!iQueue.IsEmpty()) |
|
137 { |
|
138 After(iIntervalMicroSec); |
|
139 } |
|
140 SQLCOMPACTTIMER_INVARIANT(); |
|
141 } |
|
142 |
|
143 #ifdef _DEBUG |
|
144 /** |
|
145 CSqlCompactTimer invariant. |
|
146 */ |
|
147 void CSqlCompactTimer::Invariant() const |
|
148 { |
|
149 __SQLASSERT(iIntervalMicroSec > 0, ESqlPanicInternalError); |
|
150 if(!iQueue.IsEmpty()) |
|
151 { |
|
152 CSqlCompactEntry* entry = iQueue.Last(); |
|
153 __SQLASSERT(entry != NULL, ESqlPanicInternalError); |
|
154 } |
|
155 } |
|
156 #endif//_DEBUG |