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