sl@0: /** sl@0: * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * The main header file of the background compaction framework. sl@0: * Includes the declarations of the following classes: sl@0: * The CSqlCompactor is the main class controlling the background compaction. sl@0: * Only one instance of that class should be created by the server. sl@0: * The CSqlCompactor single instance manages a set of reference counted per-database entries and a CTimer compaction object. sl@0: * The MSqlCompactConn interface acts as an abstraction layer between the compaction entries and the sl@0: * real object with member functions that are used to perform the compaction. sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @see MSqlCompactConn sl@0: @see CSqlCompactor sl@0: */ sl@0: #ifndef SQLCOMPACT_H sl@0: #define SQLCOMPACT_H sl@0: sl@0: #include sl@0: sl@0: //Forward declarations sl@0: class CSqlCompactEntry; sl@0: class CSqlCompactTimer; sl@0: struct TSqlFreePageCallback; sl@0: //A test class that has a direct access to all data members and functions of the background compaction framework classes sl@0: class CSqlCompactTestActive; sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: ////////////////////////////// TSqlCompactSettings structure declaration ////////////////////////// sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: #ifdef _DEBUG sl@0: #define SQLCOMPACTSETTINGS_INVARIANT() Invariant() sl@0: #else sl@0: #define SQLCOMPACTSETTINGS_INVARIANT() void(0) sl@0: #endif sl@0: sl@0: /** sl@0: Per-database background compaction settings/thresholds. sl@0: They are: sl@0: @code sl@0: - The compaction step length in milliseconds; sl@0: - The free pages threshold - the background compaction should be kicked-off if the free space in the free pages is sl@0: above this threshold (in Kb); sl@0: @endcode sl@0: sl@0: @internalComponent sl@0: */ sl@0: NONSHARABLE_STRUCT(TSqlCompactSettings) sl@0: { sl@0: TSqlCompactSettings(); sl@0: void Invariant() const; sl@0: sl@0: TInt iStepLength; sl@0: TInt iFreePageThresholdKb; sl@0: }; sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: ////////////////////////////// MSqlCompactConn interface declaration ////////////////////////////// sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: /** sl@0: The background compaction connection interface. sl@0: The interface separates the CSqlCompactor entries from the object which does the compaction. sl@0: The MSqlCompactConn interface exposes minimal set of methods needed to perform a compaction: sl@0: - MSqlCompactConn::Compact(TInt aPageCount, TInt& aProcessedPageCount); sl@0: sl@0: As the interface name suffix ("Conn") suggests, the interface creates and maintains a sl@0: connection with the database to be compacted. sl@0: sl@0: Interface creation: sl@0: The TSqlCompactConnFactoryL() factory function should be used for that. sl@0: sl@0: Interface destruction: sl@0: The MSqlCompactConn offers a Release() function for that. sl@0: sl@0: @see CSqlCompactor sl@0: @see TSqlCompactConnFactoryL sl@0: sl@0: @internalComponent sl@0: */ sl@0: NONSHARABLE_CLASS(MSqlCompactConn) sl@0: { sl@0: friend class CSqlCompactTestActive; sl@0: sl@0: public: sl@0: virtual void Release() = 0; sl@0: virtual TInt Compact(TInt aPageCount, TInt& aProcessedPageCount, TInt aLength) = 0; sl@0: sl@0: }; sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: /////////////////// MSqlCompactConn factory function type declaration ///////////////////////////// sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: /** sl@0: MSqlCompactConn interface - factory function type definition. sl@0: sl@0: @param aFullName The full name of the database to be compacted (including the path). sl@0: @param aFreePageCallback Input/Output parameter. Object containing the free page callback parameters. sl@0: aFreePageCallback.iThreshold must be set to be in Kb. sl@0: If the function call completes successfully and the free pages space is above the threshold, sl@0: the aFreePageCallback.iThreshold will be set to contain the free pages count. sl@0: Otherwise aFreePageCallback.iThreshold will be initialized with zero. sl@0: sl@0: @return A pointer to the created MSqlCompactConn interface. sl@0: sl@0: @leave KErrNoMemory, an out of memory condition has occurred, sl@0: KErrArgument, invalid data in the aFreePageCallback object; sl@0: Note that the function may also leave with some other database specific sl@0: errors categorised as ESqlDbError, and other system-wide error codes. sl@0: sl@0: @see MSqlCompactConn sl@0: sl@0: @internalComponent sl@0: */ sl@0: typedef MSqlCompactConn* (*TSqlCompactConnFactoryL)(const TDesC& aFullName, TSqlFreePageCallback& aFreePageCallback); sl@0: sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: ////////////////////////////// CSqlCompactor class declaration //////////////////////////////////// sl@0: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: #ifdef _DEBUG sl@0: #define SQLCOMPACTOR_INVARIANT() Invariant() sl@0: #else sl@0: #define SQLCOMPACTOR_INVARIANT() void(0) sl@0: #endif sl@0: sl@0: /** sl@0: The main class of the background compaction framework, that acts as a container of reference counted sl@0: per-database entries. sl@0: Only one instance of this class should be created by the SQL server. sl@0: Using the CSqlCompactor instance: sl@0: - A new background compaction entry can be added to the container - the CSqlCompactor::AddEntryL() method. sl@0: If an entry with the same name does exist, no new entry is created, the reference counter of the existing one sl@0: is incremented; sl@0: - An existing background compaction entry can be removed from the container with CSqlCompactor::ReleaseEntry(). sl@0: The entry is reference-counted and when the reference counter reaches 0, the entry will be removed from the container; sl@0: sl@0: @see MSqlCompactConn sl@0: sl@0: @internalComponent sl@0: */ sl@0: NONSHARABLE_CLASS(CSqlCompactor) : public CBase sl@0: { sl@0: friend class CSqlCompactTestActive; sl@0: sl@0: public: sl@0: static CSqlCompactor* NewL(TSqlCompactConnFactoryL aConnFactoryL, TInt aCompactStepInterval); sl@0: virtual ~CSqlCompactor(); sl@0: void RestartTimer(); sl@0: void AddEntryL(const TDesC& aFullName, const TSqlCompactSettings& aSettings); sl@0: void ReleaseEntry(const TDesC& aFullName); sl@0: void Invariant() const; sl@0: sl@0: private: sl@0: CSqlCompactor(TSqlCompactConnFactoryL aConnFactoryL); sl@0: void ConstructL(TInt aCompactStepInterval); sl@0: static TInt Search(const TDesC* aFullName, const CSqlCompactEntry& aEntry); sl@0: static TInt Compare(const CSqlCompactEntry& aLeft, const CSqlCompactEntry& aRight); sl@0: sl@0: private: sl@0: TSqlCompactConnFactoryL iConnFactoryL; sl@0: CSqlCompactTimer* iTimer; sl@0: typedef RPointerArray RCompactEntryArray; sl@0: RCompactEntryArray iEntries; sl@0: sl@0: }; sl@0: sl@0: #endif//SQLCOMPACT_H