os/persistentdata/persistentstorage/sql/SRC/Server/Compact/SqlCompact.h
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
/**
sl@0
     2
* Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:
sl@0
    15
* The main header file of the background compaction framework.
sl@0
    16
* Includes the declarations of the following classes:
sl@0
    17
* The CSqlCompactor is the main class controlling the background compaction.
sl@0
    18
* Only one instance of that class should be created by the server.
sl@0
    19
* The CSqlCompactor single instance manages a set of reference counted per-database entries and a CTimer compaction object.
sl@0
    20
* The MSqlCompactConn interface acts as an abstraction layer between the compaction entries and the 
sl@0
    21
* real object with member functions that are used to perform the compaction.
sl@0
    22
* 
sl@0
    23
*
sl@0
    24
*/
sl@0
    25
sl@0
    26
sl@0
    27
sl@0
    28
/**
sl@0
    29
 @file
sl@0
    30
 @see MSqlCompactConn
sl@0
    31
 @see CSqlCompactor
sl@0
    32
*/
sl@0
    33
#ifndef SQLCOMPACT_H
sl@0
    34
#define SQLCOMPACT_H
sl@0
    35
sl@0
    36
#include <e32base.h>
sl@0
    37
sl@0
    38
//Forward declarations
sl@0
    39
class CSqlCompactEntry;
sl@0
    40
class CSqlCompactTimer;
sl@0
    41
struct TSqlFreePageCallback;
sl@0
    42
//A test class that has a direct access to all data members and functions of the background compaction framework classes
sl@0
    43
class CSqlCompactTestActive;
sl@0
    44
sl@0
    45
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    46
//////////////////////////////          TSqlCompactSettings structure declaration         //////////////////////////
sl@0
    47
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    48
sl@0
    49
#ifdef _DEBUG
sl@0
    50
#define SQLCOMPACTSETTINGS_INVARIANT() Invariant()
sl@0
    51
#else
sl@0
    52
#define SQLCOMPACTSETTINGS_INVARIANT() void(0)
sl@0
    53
#endif
sl@0
    54
sl@0
    55
/**
sl@0
    56
Per-database background compaction settings/thresholds.
sl@0
    57
They are:
sl@0
    58
@code
sl@0
    59
 - The compaction step length in milliseconds;
sl@0
    60
 - The free pages threshold - the background compaction should be kicked-off if the free space in the free pages is 
sl@0
    61
 							  above this threshold (in Kb);
sl@0
    62
@endcode
sl@0
    63
sl@0
    64
@internalComponent
sl@0
    65
*/
sl@0
    66
NONSHARABLE_STRUCT(TSqlCompactSettings)
sl@0
    67
	{
sl@0
    68
	TSqlCompactSettings();
sl@0
    69
	void Invariant() const;
sl@0
    70
	
sl@0
    71
	TInt 	iStepLength;
sl@0
    72
	TInt	iFreePageThresholdKb;
sl@0
    73
	};
sl@0
    74
sl@0
    75
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    76
//////////////////////////////          MSqlCompactConn interface declaration         //////////////////////////////
sl@0
    77
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    78
sl@0
    79
/**
sl@0
    80
The background compaction connection interface.
sl@0
    81
The interface separates the CSqlCompactor entries from the object which does the compaction.
sl@0
    82
The MSqlCompactConn interface exposes minimal set of methods needed to perform a compaction:
sl@0
    83
 - MSqlCompactConn::Compact(TInt aPageCount, TInt& aProcessedPageCount);
sl@0
    84
sl@0
    85
As the interface name suffix ("Conn") suggests, the interface creates and maintains a 
sl@0
    86
connection with the database to be compacted.
sl@0
    87
sl@0
    88
Interface creation:
sl@0
    89
 The TSqlCompactConnFactoryL() factory function should be used for that.
sl@0
    90
sl@0
    91
Interface destruction:
sl@0
    92
 The MSqlCompactConn offers a Release() function for that.
sl@0
    93
sl@0
    94
@see CSqlCompactor
sl@0
    95
@see TSqlCompactConnFactoryL
sl@0
    96
sl@0
    97
@internalComponent
sl@0
    98
*/
sl@0
    99
NONSHARABLE_CLASS(MSqlCompactConn)
sl@0
   100
	{
sl@0
   101
	friend class CSqlCompactTestActive;
sl@0
   102
	
sl@0
   103
public:
sl@0
   104
	virtual void Release() = 0;
sl@0
   105
	virtual TInt Compact(TInt aPageCount, TInt& aProcessedPageCount, TInt aLength) = 0;
sl@0
   106
	
sl@0
   107
	};
sl@0
   108
sl@0
   109
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   110
///////////////////          MSqlCompactConn factory function type declaration         /////////////////////////////
sl@0
   111
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   112
sl@0
   113
/**
sl@0
   114
MSqlCompactConn interface - factory function type definition.
sl@0
   115
sl@0
   116
@param aFullName The full name of the database to be compacted (including the path).
sl@0
   117
@param aFreePageCallback Input/Output parameter. Object containing the free page callback parameters.
sl@0
   118
						 aFreePageCallback.iThreshold must be set to be in Kb. 	
sl@0
   119
						 If the function call completes successfully and the free pages space is above the threshold,
sl@0
   120
						 the aFreePageCallback.iThreshold will be set to contain the free pages count.
sl@0
   121
						 Otherwise aFreePageCallback.iThreshold will be initialized with zero.
sl@0
   122
sl@0
   123
@return A pointer to the created MSqlCompactConn interface.
sl@0
   124
sl@0
   125
@leave KErrNoMemory, an out of memory condition has occurred,
sl@0
   126
	   KErrArgument, invalid data in the aFreePageCallback object;
sl@0
   127
                     Note that the function may also leave with some other database specific 
sl@0
   128
                     errors categorised as ESqlDbError, and other system-wide error codes.
sl@0
   129
sl@0
   130
@see MSqlCompactConn
sl@0
   131
sl@0
   132
@internalComponent
sl@0
   133
*/
sl@0
   134
typedef MSqlCompactConn* (*TSqlCompactConnFactoryL)(const TDesC& aFullName, TSqlFreePageCallback& aFreePageCallback);
sl@0
   135
sl@0
   136
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   137
//////////////////////////////          CSqlCompactor class declaration         ////////////////////////////////////
sl@0
   138
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   139
sl@0
   140
#ifdef _DEBUG
sl@0
   141
#define SQLCOMPACTOR_INVARIANT() Invariant()
sl@0
   142
#else
sl@0
   143
#define SQLCOMPACTOR_INVARIANT() void(0)
sl@0
   144
#endif
sl@0
   145
sl@0
   146
/**
sl@0
   147
The main class of the background compaction framework, that acts as a container of reference counted 
sl@0
   148
per-database entries.
sl@0
   149
Only one instance of this class should be created by the SQL server.
sl@0
   150
Using the CSqlCompactor instance:
sl@0
   151
 - A new background compaction entry can be added to the container - the CSqlCompactor::AddEntryL() method.
sl@0
   152
   If an entry with the same name does exist, no new entry is created, the reference counter of the existing one 
sl@0
   153
   is incremented;
sl@0
   154
 - An existing background compaction entry can be removed from the container with CSqlCompactor::ReleaseEntry().
sl@0
   155
   The entry is reference-counted and when the reference counter reaches 0, the entry will be removed from the container;
sl@0
   156
sl@0
   157
@see MSqlCompactConn
sl@0
   158
sl@0
   159
@internalComponent
sl@0
   160
*/
sl@0
   161
NONSHARABLE_CLASS(CSqlCompactor) : public CBase
sl@0
   162
	{
sl@0
   163
	friend class CSqlCompactTestActive;
sl@0
   164
	
sl@0
   165
public:
sl@0
   166
	static CSqlCompactor* NewL(TSqlCompactConnFactoryL aConnFactoryL, TInt aCompactStepInterval);
sl@0
   167
	virtual ~CSqlCompactor();
sl@0
   168
	void RestartTimer();
sl@0
   169
	void AddEntryL(const TDesC& aFullName, const TSqlCompactSettings& aSettings);
sl@0
   170
	void ReleaseEntry(const TDesC& aFullName);
sl@0
   171
	void Invariant() const;
sl@0
   172
sl@0
   173
private:
sl@0
   174
	CSqlCompactor(TSqlCompactConnFactoryL aConnFactoryL);
sl@0
   175
	void ConstructL(TInt aCompactStepInterval);
sl@0
   176
	static TInt Search(const TDesC* aFullName, const CSqlCompactEntry& aEntry);
sl@0
   177
	static TInt Compare(const CSqlCompactEntry& aLeft, const CSqlCompactEntry& aRight);
sl@0
   178
sl@0
   179
private:
sl@0
   180
	TSqlCompactConnFactoryL iConnFactoryL;
sl@0
   181
	CSqlCompactTimer*		iTimer;
sl@0
   182
	typedef RPointerArray<CSqlCompactEntry> RCompactEntryArray;
sl@0
   183
	RCompactEntryArray		iEntries;
sl@0
   184
sl@0
   185
	};
sl@0
   186
	
sl@0
   187
#endif//SQLCOMPACT_H