os/persistentdata/persistentstorage/sql/SRC/Server/Compact/SqlCompact.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.
sl@0
     1
// Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "SqlAssert.h"
sl@0
    17
#include "SqlCompact.h"
sl@0
    18
#include "SqlCompactEntry.h"
sl@0
    19
#include "SqlCompactTimer.h"
sl@0
    20
#include "SqlUtil.h"
sl@0
    21
#include "OstTraceDefinitions.h"
sl@0
    22
#ifdef OST_TRACE_COMPILER_IN_USE
sl@0
    23
#include "SqlCompactTraces.h"
sl@0
    24
#endif
sl@0
    25
#include "SqlTraceDef.h"
sl@0
    26
sl@0
    27
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    28
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    29
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    30
sl@0
    31
/**
sl@0
    32
Initializes the background compaction settings with their default values.
sl@0
    33
*/
sl@0
    34
TSqlCompactSettings::TSqlCompactSettings() :
sl@0
    35
	iStepLength(KSqlCompactStepLengthMs),
sl@0
    36
	iFreePageThresholdKb(KSqlCompactFreePageThresholdKb)
sl@0
    37
	{
sl@0
    38
	}
sl@0
    39
sl@0
    40
#ifdef _DEBUG
sl@0
    41
/**
sl@0
    42
CSqlCompactSettings invariant.
sl@0
    43
*/
sl@0
    44
void TSqlCompactSettings::Invariant() const
sl@0
    45
	{
sl@0
    46
	__ASSERT_DEBUG(iStepLength > 0, __SQLPANIC(ESqlPanicInternalError));
sl@0
    47
	__ASSERT_DEBUG(iFreePageThresholdKb >= 0, __SQLPANIC(ESqlPanicInternalError));
sl@0
    48
	}
sl@0
    49
#endif//_DEBUG
sl@0
    50
sl@0
    51
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    52
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    53
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    54
sl@0
    55
/**
sl@0
    56
The granularity of the container maintained by CSqlCompactor
sl@0
    57
sl@0
    58
@see CSqlCompactor
sl@0
    59
@internalComponent
sl@0
    60
*/
sl@0
    61
const TInt KEntriesGranularity = 16;
sl@0
    62
sl@0
    63
/**
sl@0
    64
Creates a new CSqlCompactor instance.
sl@0
    65
sl@0
    66
@param aConnFactoryL MSqlCompactConn factory function.
sl@0
    67
@param aCompactStepInterval The time interval (ms) between the background compaction steps.
sl@0
    68
sl@0
    69
@return A pointer to the created CSqlCompactor instance
sl@0
    70
sl@0
    71
@leave KErrNoMemory, an out of memory condition has occurred;
sl@0
    72
                     Note that the function may also leave with some other database specific 
sl@0
    73
                     errors categorised as ESqlDbError, and other system-wide error codes.
sl@0
    74
sl@0
    75
@panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
sl@0
    76
@panic SqlDb 4 In _DEBUG mode. Zero or negative aCompactStepInterval.
sl@0
    77
*/
sl@0
    78
CSqlCompactor* CSqlCompactor::NewL(TSqlCompactConnFactoryL aConnFactoryL, TInt aCompactStepInterval)
sl@0
    79
	{
sl@0
    80
	__ASSERT_DEBUG(aConnFactoryL != NULL, __SQLPANIC2(ESqlPanicBadArgument));
sl@0
    81
	__ASSERT_DEBUG(aCompactStepInterval > 0, __SQLPANIC2(ESqlPanicBadArgument));
sl@0
    82
	CSqlCompactor* self = new (ELeave) CSqlCompactor(aConnFactoryL);
sl@0
    83
	CleanupStack::PushL(self);
sl@0
    84
	self->ConstructL(aCompactStepInterval);
sl@0
    85
	CleanupStack::Pop(self);
sl@0
    86
	return self;
sl@0
    87
	}
sl@0
    88
sl@0
    89
/**
sl@0
    90
Destroys the CSqlCompactor instance. 
sl@0
    91
Any entries left in the container will be destroyed.
sl@0
    92
*/
sl@0
    93
CSqlCompactor::~CSqlCompactor()
sl@0
    94
	{
sl@0
    95
	for(TInt idx=iEntries.Count()-1;idx>=0;--idx)
sl@0
    96
		{
sl@0
    97
		__ASSERT_DEBUG(iEntries[idx] != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
    98
		while(iEntries[idx]->Release() != 0)
sl@0
    99
			{
sl@0
   100
			}
sl@0
   101
		}
sl@0
   102
	iEntries.Close();
sl@0
   103
	delete iTimer;
sl@0
   104
	}
sl@0
   105
sl@0
   106
/**
sl@0
   107
Restarts the background compaction timer. 
sl@0
   108
This function should be called from the server side session object's ServiceL(). 
sl@0
   109
If ServiceL() is being executed at the moment, it is very likely that the SQL client(s) will issue another
sl@0
   110
request few ms later. In order to not delay the processing of the client(s) requests, ServiceL() should call
sl@0
   111
at the end RestartTimer(). If there are database entries scheduled for a background compaction, the compaction
sl@0
   112
will be delayed by the time interval used by the CSqlCompactTimer object (default value - KSqlCompactStepInterval).
sl@0
   113
sl@0
   114
@see CSqlCompactTimer
sl@0
   115
@see KSqlCompactStepInterval
sl@0
   116
*/
sl@0
   117
void CSqlCompactor::RestartTimer()
sl@0
   118
	{
sl@0
   119
	SQLCOMPACTOR_INVARIANT();
sl@0
   120
	iTimer->Restart();
sl@0
   121
	SQLCOMPACTOR_INVARIANT();
sl@0
   122
	}
sl@0
   123
sl@0
   124
/**
sl@0
   125
If an entry referring to a database with name aFullName does not exist in the container, a new entry will be created,
sl@0
   126
a connection with the database established.
sl@0
   127
If an entry with the specified name already exists, no new entry wil be created, the reference counter of the existing one 
sl@0
   128
will be incremented.
sl@0
   129
sl@0
   130
@param aFullName The full database name, including the path.
sl@0
   131
@param aSettings Per-database background compaction settings
sl@0
   132
sl@0
   133
@leave KErrNoMemory, an out of memory condition has occurred;
sl@0
   134
                     Note that the function may also leave with some other database specific 
sl@0
   135
                     errors categorised as ESqlDbError, and other system-wide error codes.
sl@0
   136
sl@0
   137
@panic SqlDb 4 In _DEBUG mode. Too short or too long database name (aFullName parameter)
sl@0
   138
@panic SqlDb 7 In _DEBUG mode. An entry with the specified name has been found but the entry is NULL.
sl@0
   139
*/
sl@0
   140
void CSqlCompactor::AddEntryL(const TDesC& aFullName, const TSqlCompactSettings& aSettings)
sl@0
   141
	{
sl@0
   142
	__ASSERT_DEBUG(aFullName.Length() > 0 && aFullName.Length() <= KMaxFileName, __SQLPANIC(ESqlPanicBadArgument));
sl@0
   143
	SQLCOMPACTOR_INVARIANT();
sl@0
   144
	CSqlCompactEntry* entry = NULL;
sl@0
   145
	TInt idx = iEntries.FindInOrder(aFullName, &CSqlCompactor::Search);
sl@0
   146
	if(idx == KErrNotFound)
sl@0
   147
		{
sl@0
   148
		SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTOR_ADDENTRYL1, "0x%X;CSqlCompactor::AddEntryL;New entry;aFullName=%S;iStepLength=%d;iFreePageThreashold=%d", (TUint)this, __SQLPRNSTR(aFullName), aSettings.iStepLength, aSettings.iFreePageThresholdKb));
sl@0
   149
		entry = CSqlCompactEntry::NewLC(aFullName, iConnFactoryL, aSettings, *iTimer);
sl@0
   150
		TLinearOrder<CSqlCompactEntry> order(&CSqlCompactor::Compare);
sl@0
   151
		__SQLLEAVE_IF_ERROR(iEntries.InsertInOrder(entry, order));
sl@0
   152
		CleanupStack::Pop(entry);
sl@0
   153
		}
sl@0
   154
	else
sl@0
   155
		{
sl@0
   156
		SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTOR_ADDENTRYL2, "0x%X;CSqlCompactor::AddEntryL;Reuse entry;aFullName=%S;iStepLength=%d;iFreePageThreashold=%d", (TUint)this, __SQLPRNSTR(aFullName), aSettings.iStepLength, aSettings.iFreePageThresholdKb));
sl@0
   157
		entry = iEntries[idx];
sl@0
   158
		__ASSERT_DEBUG(entry != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   159
		(void)entry->AddRef();
sl@0
   160
		}
sl@0
   161
	SQLCOMPACTOR_INVARIANT();
sl@0
   162
	}
sl@0
   163
sl@0
   164
/**
sl@0
   165
Decrements the reference counter of the specified entry.
sl@0
   166
If the counter reaches zero, the entry will be destroyed and removed from the container, 
sl@0
   167
the database connection - closed.
sl@0
   168
sl@0
   169
@param aFullName The full database name, including the path.
sl@0
   170
*/
sl@0
   171
void CSqlCompactor::ReleaseEntry(const TDesC& aFullName)
sl@0
   172
	{
sl@0
   173
	SQL_TRACE_COMPACT(OstTraceExt2(TRACE_INTERNALS, CSQLCOMPACTOR_RELEASEENTRY1, "0x%X;CSqlCompactor::ReleaseEntry;aFullName=%S", (TUint)this, __SQLPRNSTR(aFullName)));
sl@0
   174
	SQLCOMPACTOR_INVARIANT();
sl@0
   175
	TInt idx = iEntries.FindInOrder(aFullName, &CSqlCompactor::Search);
sl@0
   176
	__ASSERT_DEBUG(idx >= 0, __SQLPANIC(ESqlPanicInternalError));
sl@0
   177
	if(idx >= 0)
sl@0
   178
		{
sl@0
   179
		CSqlCompactEntry* entry = iEntries[idx];
sl@0
   180
		__ASSERT_DEBUG(entry != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   181
		if(entry)
sl@0
   182
			{
sl@0
   183
			if(entry->Release() == 0)
sl@0
   184
				{
sl@0
   185
				iEntries.Remove(idx);			
sl@0
   186
#ifdef _DEBUG
sl@0
   187
//This is used prevent the failure of the resource allocation checking for debug mode. 
sl@0
   188
				if(iEntries.Count() == 0)
sl@0
   189
				    {
sl@0
   190
                    iEntries.Reset();
sl@0
   191
				    }
sl@0
   192
#endif  
sl@0
   193
				}
sl@0
   194
			}
sl@0
   195
		}
sl@0
   196
	SQLCOMPACTOR_INVARIANT();
sl@0
   197
	}
sl@0
   198
sl@0
   199
/**
sl@0
   200
Initializes the CSqlCompactor data members with their default values.
sl@0
   201
sl@0
   202
@param aConnFactoryL MSqlCompactConn factory function.
sl@0
   203
sl@0
   204
@panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
sl@0
   205
*/
sl@0
   206
CSqlCompactor::CSqlCompactor(TSqlCompactConnFactoryL aConnFactoryL) :
sl@0
   207
	iConnFactoryL(aConnFactoryL),
sl@0
   208
	iEntries(KEntriesGranularity)
sl@0
   209
	{
sl@0
   210
	__ASSERT_DEBUG(aConnFactoryL != NULL, __SQLPANIC(ESqlPanicBadArgument));
sl@0
   211
	}
sl@0
   212
sl@0
   213
/**
sl@0
   214
Initializes the created CSqlCompactor instance.
sl@0
   215
sl@0
   216
@param aCompactStepInterval The time interval between the background compaction steps.
sl@0
   217
sl@0
   218
@panic SqlDb 4 In _DEBUG mode. Zero or negative aCompactStepInterval.
sl@0
   219
*/
sl@0
   220
void CSqlCompactor::ConstructL(TInt aCompactStepInterval)
sl@0
   221
	{
sl@0
   222
	__ASSERT_DEBUG(aCompactStepInterval > 0, __SQLPANIC(ESqlPanicBadArgument));
sl@0
   223
	iTimer = CSqlCompactTimer::NewL(aCompactStepInterval);
sl@0
   224
	}
sl@0
   225
sl@0
   226
/**
sl@0
   227
Static method used internally for performing a search in the container using database name as a key.
sl@0
   228
sl@0
   229
@param aFullName The full database name, including the path.
sl@0
   230
@param aEntry CSqlCompactor reference.
sl@0
   231
*/
sl@0
   232
/* static */TInt CSqlCompactor::Search(const TDesC* aFullName, const CSqlCompactEntry& aEntry)
sl@0
   233
	{
sl@0
   234
	__ASSERT_DEBUG(&aEntry != NULL, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   235
	__ASSERT_DEBUG(aFullName != NULL, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   236
	const TDesC& fullName = *aFullName;
sl@0
   237
	__ASSERT_DEBUG(fullName.Length() > 0 && fullName.Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   238
	__ASSERT_DEBUG(aEntry.FullName().Length() > 0 && aEntry.FullName().Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   239
	return fullName.CompareF(aEntry.FullName());
sl@0
   240
	}
sl@0
   241
sl@0
   242
/**
sl@0
   243
Static method used internally for performing a search in the container using a CSqlCompactEntry reference as a key.
sl@0
   244
*/
sl@0
   245
/* static */TInt CSqlCompactor::Compare(const CSqlCompactEntry& aLeft, const CSqlCompactEntry& aRight)
sl@0
   246
	{
sl@0
   247
	__ASSERT_DEBUG(&aLeft != NULL, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   248
	__ASSERT_DEBUG(&aRight != NULL, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   249
	__ASSERT_DEBUG(aLeft.FullName().Length() > 0 && aLeft.FullName().Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   250
	__ASSERT_DEBUG(aRight.FullName().Length() > 0 && aRight.FullName().Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicInternalError));
sl@0
   251
	return aLeft.FullName().CompareF(aRight.FullName());
sl@0
   252
	}
sl@0
   253
sl@0
   254
#ifdef _DEBUG
sl@0
   255
/**
sl@0
   256
CSqlCompactor invariant.
sl@0
   257
*/
sl@0
   258
void CSqlCompactor::Invariant() const
sl@0
   259
	{
sl@0
   260
	__ASSERT_DEBUG(iConnFactoryL != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   261
	__ASSERT_DEBUG(iTimer != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   262
	iTimer->Invariant();
sl@0
   263
	for(TInt idx=iEntries.Count()-1;idx>=0;--idx)
sl@0
   264
		{
sl@0
   265
		__ASSERT_DEBUG(iEntries[idx] != NULL, __SQLPANIC(ESqlPanicInternalError));
sl@0
   266
		iEntries[idx]->Invariant();
sl@0
   267
		}
sl@0
   268
	}
sl@0
   269
#endif//_DEBUG