|
1 /* |
|
2 * Copyright (c) 2006-2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Handles periodical database cleanup routines |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <ecom.h> |
|
20 #include <pspresetinterface.h> |
|
21 |
|
22 #include "psdatabase.h" |
|
23 #include "psdatabasecleanup.h" |
|
24 #include "psdebug.h" |
|
25 |
|
26 const TInt KPSCleanupInterval = 480000000; // Interval for periodical compaction attempts |
|
27 const TInt KPSRequestCleanupCheckDelay = 20000000; // Delay when compaction attempt is done after a request to check. |
|
28 const TInt KPSCleanupAmountOfDirtAllowed = 300*1024; // Amount of dirty data allowed in database (bytes). |
|
29 const TInt KPSCleanupCompactThresholdPercentage = 50; // Threshold percentage in database data efficiency when db compact process is triggered. |
|
30 |
|
31 // ======== LOCAL FUNCTIONS ======== |
|
32 |
|
33 namespace |
|
34 { |
|
35 |
|
36 // --------------------------------------------------------------------------- |
|
37 // ?description_if_needed |
|
38 // --------------------------------------------------------------------------- |
|
39 // |
|
40 void CleanupImplArray( TAny* aSelf ) |
|
41 { |
|
42 RImplInfoPtrArray& self = *static_cast<RImplInfoPtrArray*>( aSelf ); |
|
43 self.ResetAndDestroy(); |
|
44 self.Close(); |
|
45 } |
|
46 |
|
47 } |
|
48 |
|
49 // ======== MEMBER FUNCTIONS ======== |
|
50 |
|
51 // --------------------------------------------------------------------------- |
|
52 // ?description_if_needed |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 CPSDatabaseCleanup::CPSDatabaseCleanup( RPSDatabase& aDatabase ) |
|
56 : CTimer( CActive::EPriorityStandard ), iDatabase( aDatabase ) |
|
57 { |
|
58 CActiveScheduler::Add( this ); |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // ?description_if_needed |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 void CPSDatabaseCleanup::ConstructL() |
|
66 { |
|
67 CTimer::ConstructL(); |
|
68 |
|
69 RunL(); |
|
70 } |
|
71 |
|
72 // --------------------------------------------------------------------------- |
|
73 // ?description_if_needed |
|
74 // --------------------------------------------------------------------------- |
|
75 // |
|
76 CPSDatabaseCleanup* CPSDatabaseCleanup::NewL( RPSDatabase& aDatabase ) |
|
77 { |
|
78 CPSDatabaseCleanup* self = new ( ELeave ) CPSDatabaseCleanup( aDatabase ); |
|
79 CleanupStack::PushL( self ); |
|
80 self->ConstructL(); |
|
81 CleanupStack::Pop( self ); |
|
82 return self; |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // ?description_if_needed |
|
87 // --------------------------------------------------------------------------- |
|
88 // |
|
89 CPSDatabaseCleanup::~CPSDatabaseCleanup() |
|
90 { |
|
91 Cancel(); |
|
92 } |
|
93 |
|
94 // --------------------------------------------------------------------------- |
|
95 // From class ?base_class. |
|
96 // ?implementation_description |
|
97 // --------------------------------------------------------------------------- |
|
98 // |
|
99 void CPSDatabaseCleanup::RunL() |
|
100 { |
|
101 After( KPSCleanupInterval ); |
|
102 |
|
103 User::LeaveIfError( iDatabase.UpdateStats() ); // Update the statistics |
|
104 RDbDatabase::TSize size = iDatabase.Size(); |
|
105 |
|
106 PSDEBUG( "PSDatabaseCleanup::RunL()" ); |
|
107 PSDEBUG3( "\tCompacting the database: iSize = %d, iUsage = %d", size.iSize, size.iUsage ); |
|
108 |
|
109 // Perform database compaction only if efficient data in database in percents is below defined percentage or the amount of dirty |
|
110 // data in database file is at least a defined constant value of bytes. |
|
111 TInt dirt = size.iSize * (100 - size.iUsage) / 100; |
|
112 if (dirt > KPSCleanupAmountOfDirtAllowed || size.iUsage < KPSCleanupCompactThresholdPercentage) |
|
113 { |
|
114 User::LeaveIfError( iDatabase.Compact() ); |
|
115 PSDEBUG3( "\tDatabase compacted succesfully: iSize = %d, iUsage = %d", iDatabase.Size().iSize, iDatabase.Size().iUsage ); |
|
116 } |
|
117 |
|
118 // Perform orphan cleanup. |
|
119 DeleteOrphanedPresetsL(); |
|
120 } |
|
121 |
|
122 // --------------------------------------------------------------------------- |
|
123 // ?description_if_needed |
|
124 // --------------------------------------------------------------------------- |
|
125 // |
|
126 void CPSDatabaseCleanup::DeleteOrphanedPresetsL() |
|
127 { |
|
128 RArray<TInt> dataHandlers; |
|
129 CleanupClosePushL( dataHandlers ); |
|
130 RImplInfoPtrArray implementations; |
|
131 CleanupStack::PushL( TCleanupItem( CleanupImplArray, &implementations ) ); |
|
132 |
|
133 REComSession::ListImplementationsL( KPSPresetInterface, implementations ); |
|
134 |
|
135 for ( TInt i = 0; i < implementations.Count(); i++ ) |
|
136 { |
|
137 dataHandlers.AppendL( implementations[i]->ImplementationUid().iUid ); |
|
138 } |
|
139 |
|
140 CleanupStack::PopAndDestroy(); // implementations |
|
141 |
|
142 REComSession::FinalClose(); |
|
143 |
|
144 iDatabase.DeleteOrphanedPresetsL( dataHandlers ); |
|
145 |
|
146 CleanupStack::PopAndDestroy( &dataHandlers ); |
|
147 } |
|
148 |
|
149 // --------------------------------------------------------------------------- |
|
150 // ?description_if_needed |
|
151 // --------------------------------------------------------------------------- |
|
152 // |
|
153 void CPSDatabaseCleanup::RequestCleanupCheck() |
|
154 { |
|
155 Cancel(); |
|
156 After(KPSRequestCleanupCheckDelay); |
|
157 } |
|
158 // ======== GLOBAL FUNCTIONS ======== |