persistentstorage/sql/SRC/Server/Compact/SqlCompactEntry.cpp
branchRCL_3
changeset 24 cc28652e0254
parent 23 26645d81f48d
equal deleted inserted replaced
23:26645d81f48d 24:cc28652e0254
    14 //
    14 //
    15 
    15 
    16 #include <e32debug.h>
    16 #include <e32debug.h>
    17 #include <hal.h>
    17 #include <hal.h>
    18 #include <sqldb.h>
    18 #include <sqldb.h>
    19 #include "SqlAssert.h"
    19 #include "SqlPanic.h"
    20 #include "SqlCompactEntry.h"
    20 #include "SqlCompactEntry.h"
    21 #include "SqlCompactTimer.h"
    21 #include "SqlCompactTimer.h"
    22 #include "SqliteSymbian.h"		//TSqlFreePageCallback
    22 #include "SqliteSymbian.h"		//TSqlFreePageCallback
    23 #include "OstTraceDefinitions.h"
       
    24 #ifdef OST_TRACE_COMPILER_IN_USE
       
    25 #include "SqlCompactEntryTraces.h"
       
    26 #endif
       
    27 #include "SqlTraceDef.h"
       
    28 
    23 
    29 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    24 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    30 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    25 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    31 
    26 
    32 /**
    27 /**
    50 @panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
    45 @panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
    51 */
    46 */
    52 CSqlCompactEntry* CSqlCompactEntry::NewLC(const TDesC& aFullName, TSqlCompactConnFactoryL aConnFactoryL, 
    47 CSqlCompactEntry* CSqlCompactEntry::NewLC(const TDesC& aFullName, TSqlCompactConnFactoryL aConnFactoryL, 
    53 										  const TSqlCompactSettings& aSettings, CSqlCompactTimer& aTimer)
    48 										  const TSqlCompactSettings& aSettings, CSqlCompactTimer& aTimer)
    54 	{
    49 	{
    55 	SQL_TRACE_COMPACT(OstTraceExt1(TRACE_INTERNALS, CSQLCOMPACTENTRY_NEWLC_ENTRY, "Entry;0;CSqlCompactEntry::NewLC;aFullName=%S", __SQLPRNSTR(aFullName)));
    50 	__SQLASSERT(aFullName.Length() > 0 && aFullName.Length() <= KMaxFileName, ESqlPanicBadArgument); 
    56 	__ASSERT_DEBUG(aFullName.Length() > 0 && aFullName.Length() <= KMaxFileName, __SQLPANIC2(ESqlPanicBadArgument)); 
    51 	__SQLASSERT(aConnFactoryL != NULL, ESqlPanicBadArgument);
    57 	__ASSERT_DEBUG(aConnFactoryL != NULL, __SQLPANIC2(ESqlPanicBadArgument));
       
    58 	CSqlCompactEntry* self = new (ELeave) CSqlCompactEntry(aSettings, aTimer);
    52 	CSqlCompactEntry* self = new (ELeave) CSqlCompactEntry(aSettings, aTimer);
    59 	CleanupStack::PushL(self);
    53 	CleanupStack::PushL(self);
    60 	self->ConstructL(aFullName, aConnFactoryL);
    54 	self->ConstructL(aFullName, aConnFactoryL);
    61 	SQL_TRACE_COMPACT(OstTrace1(TRACE_INTERNALS, CSQLCOMPACTENTRY_NEWLC_EXIT, "Exit;0x%X;CSqlCompactEntry::NewLC", (TUint)self));
       
    62 	return self;
    55 	return self;
    63 	}
    56 	}
    64 
    57 
    65 /**
    58 /**
    66 Destroys the CSqlCompactEntry instance. The database connection will be closed.
    59 Destroys the CSqlCompactEntry instance. The database connection will be closed.
    67 */
    60 */
    68 CSqlCompactEntry::~CSqlCompactEntry()
    61 CSqlCompactEntry::~CSqlCompactEntry()
    69 	{
    62 	{
    70 	SQL_TRACE_COMPACT(OstTraceExt3(TRACE_INTERNALS, CSQLCOMPACTENTRY_CSQLCOMPACTENTRY2, "0x%X;CSqlCompactEntry::~CSqlCompactEntry;iState=%d;iPageCount=%d", (TUint)this, (TInt)iState, iPageCount));
       
    71 	if(iState == CSqlCompactEntry::EInProgress)
    63 	if(iState == CSqlCompactEntry::EInProgress)
    72 		{
    64 		{
    73 		iTimer.DeQueue(*this);
    65 		iTimer.DeQueue(*this);
    74 		}
    66 		}
    75 	if(iConnection)
    67 	if(iConnection)
    84 
    76 
    85 @return The new reference counter value.
    77 @return The new reference counter value.
    86 */
    78 */
    87 TInt CSqlCompactEntry::AddRef()
    79 TInt CSqlCompactEntry::AddRef()
    88 	{
    80 	{
    89 	SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTENTRY_ADDREF, "0x%X;CSqlCompactEntry::AddRef;iState=%d;iPageCount=%d;iRefCounter=%d", (TUint)this, (TInt)iState, iPageCount, iRefCounter));
       
    90 	SQLCOMPACTENTRY_INVARIANT();
    81 	SQLCOMPACTENTRY_INVARIANT();
    91 	return ++iRefCounter;
    82 	return ++iRefCounter;
    92 	}
    83 	}
    93 
    84 
    94 /**
    85 /**
    97 
    88 
    98 @return The new reference counter value.
    89 @return The new reference counter value.
    99 */
    90 */
   100 TInt CSqlCompactEntry::Release()
    91 TInt CSqlCompactEntry::Release()
   101 	{
    92 	{
   102 	SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTENTRY_RELEASE, "0x%X;CSqlCompactEntry::Release;iState=%d;iPageCount=%d;iRefCounter=%d", (TUint)this, (TInt)iState, iPageCount, iRefCounter));
       
   103 	SQLCOMPACTENTRY_INVARIANT();
    93 	SQLCOMPACTENTRY_INVARIANT();
   104 	TInt rc = --iRefCounter;
    94 	TInt rc = --iRefCounter;
   105 	if(rc == 0)
    95 	if(rc == 0)
   106 		{
    96 		{
   107 		delete this;	
    97 		delete this;	
   122 @panic SqlDb 4 In _DEBUG mode. NULL aThis parameter.
   112 @panic SqlDb 4 In _DEBUG mode. NULL aThis parameter.
   123 @panic SqlDb 4 In _DEBUG mode. aFreePageCount is negative or zero.
   113 @panic SqlDb 4 In _DEBUG mode. aFreePageCount is negative or zero.
   124 */
   114 */
   125 /* static */ void CSqlCompactEntry::FreePageCallback(void* aThis, TInt aFreePageCount)
   115 /* static */ void CSqlCompactEntry::FreePageCallback(void* aThis, TInt aFreePageCount)
   126 	{
   116 	{
   127 	__ASSERT_DEBUG(aThis != NULL, __SQLPANIC2(ESqlPanicBadArgument)); 
   117 	__SQLASSERT(aThis != NULL, ESqlPanicBadArgument); 
   128 	__ASSERT_DEBUG(aFreePageCount > 0, __SQLPANIC2(ESqlPanicBadArgument)); 
   118 	__SQLASSERT(aFreePageCount > 0, ESqlPanicBadArgument); 
   129 	
   119 	
   130 	CSqlCompactEntry& entry = *(static_cast <CSqlCompactEntry*> (aThis));
   120 	CSqlCompactEntry& entry = *(static_cast <CSqlCompactEntry*> (aThis));
   131 	SQL_TRACE_COMPACT(OstTraceExt3(TRACE_INTERNALS, CSQLCOMPACTENTRY_FREEPAGECALLBACK, "0x%X;CSqlCompactEntry::FreePageCallback;aFreePageCount=%d;iState=%d", (TUint)aThis, aFreePageCount, (TInt)entry.iState));
       
   132 	if(entry.iFreePageCallbackDisabled)
   121 	if(entry.iFreePageCallbackDisabled)
   133 		{//The callback is disabled during the background compaction step.
   122 		{//The callback is disabled during the background compaction step.
   134 		 //The server is single-threaded, so no other client can activate the callback.
   123 		 //The server is single-threaded, so no other client can activate the callback.
   135 		 //During the background compaction step the callback can be activated only by the completion of the background 
   124 		 //During the background compaction step the callback can be activated only by the completion of the background 
   136 		 //compaction in which case if "entry.iPageCount" is bigger than the threshold, the page counter will be set from here
   125 		 //compaction in which case if "entry.iPageCount" is bigger than the threshold, the page counter will be set from here
   170 @panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
   159 @panic SqlDb 4 In _DEBUG mode. NULL aConnFactoryL.
   171 @panic SqlDb 7 In _DEBUG mode. The CSqlCompactEntry instance has been initialized already.
   160 @panic SqlDb 7 In _DEBUG mode. The CSqlCompactEntry instance has been initialized already.
   172 */
   161 */
   173 void CSqlCompactEntry::ConstructL(const TDesC& aFullName, TSqlCompactConnFactoryL aConnFactoryL)
   162 void CSqlCompactEntry::ConstructL(const TDesC& aFullName, TSqlCompactConnFactoryL aConnFactoryL)
   174 	{
   163 	{
   175 	__ASSERT_DEBUG(aFullName.Length() > 0 && aFullName.Length() <= KMaxFileName, __SQLPANIC(ESqlPanicBadArgument)); 
   164 	__SQLASSERT(aFullName.Length() > 0 && aFullName.Length() <= KMaxFileName, ESqlPanicBadArgument); 
   176 	__ASSERT_DEBUG(aConnFactoryL != NULL, __SQLPANIC(ESqlPanicBadArgument));
   165 	__SQLASSERT(aConnFactoryL != NULL, ESqlPanicBadArgument);
   177 	__ASSERT_DEBUG(!iConnection, __SQLPANIC(ESqlPanicInternalError));
   166 	__SQLASSERT(!iConnection, ESqlPanicInternalError);
   178 	
   167 	
   179 	__SQLLEAVE_IF_ERROR(iFullName.Create(aFullName));
   168 	__SQLLEAVE_IF_ERROR(iFullName.Create(aFullName));
   180 
   169 
   181 	//The second parameter of TSqlFreePageCallback's constructor is expected the be threshold in pages.
   170 	//The second parameter of TSqlFreePageCallback's constructor is expected the be threshold in pages.
   182 	//But the connection is not established yet and the page size is not known. Hence the threshold parameter
   171 	//But the connection is not established yet and the page size is not known. Hence the threshold parameter
   183 	//value is initialized with the threshold in Kbs. The connection construction is expected to convert
   172 	//value is initialized with the threshold in Kbs. The connection construction is expected to convert
   184 	//the threshold from Kbs to pages when the connection with the database is established.
   173 	//the threshold from Kbs to pages when the connection with the database is established.
   185 	TSqlFreePageCallback callback(this, iSettings.iFreePageThresholdKb, &CSqlCompactEntry::FreePageCallback);
   174 	TSqlFreePageCallback callback(this, iSettings.iFreePageThresholdKb, &CSqlCompactEntry::FreePageCallback);
   186 	iConnection = (*aConnFactoryL)(aFullName, callback);
   175 	iConnection = (*aConnFactoryL)(aFullName, callback);
   187 	__ASSERT_DEBUG(iConnection != NULL, __SQLPANIC(ESqlPanicInternalError));
   176 	__SQLASSERT(iConnection != NULL, ESqlPanicInternalError);
   188 	
   177 	
   189 	//"callback.iThreshold > 0" is an indication that the background compaction should be kicked-off
   178 	//"callback.iThreshold > 0" is an indication that the background compaction should be kicked-off
   190 	if(callback.iThreshold > 0) 
   179 	if(callback.iThreshold > 0) 
   191 		{
   180 		{
   192 		//Kick-off the compaction timer, if the number of the free pages is above the threshold.
   181 		//Kick-off the compaction timer, if the number of the free pages is above the threshold.
   210 @panic SqlDb 7 In _DEBUG mode. iPageCount <= 0 - no free pages to be processed.
   199 @panic SqlDb 7 In _DEBUG mode. iPageCount <= 0 - no free pages to be processed.
   211 @panic SqlDb 7 In _DEBUG mode. iState != CSqlCompactEntry::EInProgress.
   200 @panic SqlDb 7 In _DEBUG mode. iState != CSqlCompactEntry::EInProgress.
   212 */
   201 */
   213 TInt CSqlCompactEntry::Compact()
   202 TInt CSqlCompactEntry::Compact()
   214 	{
   203 	{
   215 	SQL_TRACE_COMPACT(OstTraceExt3(TRACE_INTERNALS, CSQLCOMPACTENTRY_COMPACT_ENTRY, "Entry;0x%X;CSqlCompactEntry::Compact;aFreePageCount=%d;iState=%d", (TUint)this, iPageCount, (TInt)iState));
   204 	//RDebug::Print(_L("++ CSqlCompactEntry::Compact() ++\r\n"));
   216 	SQLCOMPACTENTRY_INVARIANT();
   205 	SQLCOMPACTENTRY_INVARIANT();
   217 	__ASSERT_DEBUG(iPageCount > 0, __SQLPANIC(ESqlPanicInternalError));
   206 	__SQLASSERT(iPageCount > 0, ESqlPanicInternalError);
   218 	__ASSERT_DEBUG(iState == CSqlCompactEntry::EInProgress, __SQLPANIC(ESqlPanicInternalError));
   207 	__SQLASSERT(iState == CSqlCompactEntry::EInProgress, ESqlPanicInternalError);
   219 	TInt processedPageCount = 0;
   208 	TInt processedPageCount = 0;
   220 	iFreePageCallbackDisabled = ETrue;
   209 	iFreePageCallbackDisabled = ETrue;
   221 	TInt err = Connection().Compact(iPageCount, processedPageCount, iSettings.iStepLength);
   210 	TInt err = Connection().Compact(iPageCount, processedPageCount, iSettings.iStepLength);
   222 	iFreePageCallbackDisabled = EFalse;
   211 	iFreePageCallbackDisabled = EFalse;
   223 	__ASSERT_DEBUG(processedPageCount >= 0, __SQLPANIC(ESqlPanicInternalError));
   212 	__SQLASSERT(processedPageCount >= 0, ESqlPanicInternalError);
   224 	if(err == KErrNone)
   213 	if(err == KErrNone)
   225 		{
   214 		{
   226 		iPageCount -= processedPageCount;
   215 		iPageCount -= processedPageCount;
   227 		__ASSERT_DEBUG(iPageCount >= 0, __SQLPANIC(ESqlPanicInternalError));
   216 		__SQLASSERT(iPageCount >= 0, ESqlPanicInternalError);
   228 		}
   217 		}
   229 	TBool stopCompaction = err == KSqlErrCorrupt || err == KSqlErrNotDb || err == KErrCorrupt || err == KErrDisMounted;
   218 	TBool stopCompaction = err == KSqlErrCorrupt || err == KSqlErrNotDb || err == KErrCorrupt || err == KErrDisMounted;
   230 	if(iPageCount <= 0 || stopCompaction)
   219 	if(iPageCount <= 0 || stopCompaction)
   231 		{//No more pages to compact or the file is corrupted . Stop the compacting, move to EInactive state, remove from the timer queue.
   220 		{//No more pages to compact or the file is corrupted . Stop the compacting, move to EInactive state, remove from the timer queue.
   232 		ResetState();
   221 		ResetState();
   233 		iTimer.DeQueue(*this);
   222 		iTimer.DeQueue(*this);
   234 		}
   223 		}
   235 	SQL_TRACE_COMPACT(OstTraceExt4(TRACE_INTERNALS, CSQLCOMPACTENTRY_COMPACT_EXIT, "Exit;0x%X;CSqlCompactEntry::Compact;iPageCount=%d;iState=%d;err=%d", (TUint)this, iPageCount, (TInt)iState, err));
       
   236 	SQLCOMPACTENTRY_INVARIANT();
   224 	SQLCOMPACTENTRY_INVARIANT();
   237 	return err;
   225 	return err;
   238 	}
   226 	}
   239 
   227 
   240 /**
   228 /**
   267 @panic SqlDb 7 NULL MSqlCompactConn interface.
   255 @panic SqlDb 7 NULL MSqlCompactConn interface.
   268 */
   256 */
   269 MSqlCompactConn& CSqlCompactEntry::Connection()
   257 MSqlCompactConn& CSqlCompactEntry::Connection()
   270 	{
   258 	{
   271 	SQLCOMPACTENTRY_INVARIANT();
   259 	SQLCOMPACTENTRY_INVARIANT();
   272 	__ASSERT_ALWAYS(iConnection != NULL, __SQLPANIC(ESqlPanicInternalError));
   260 	__SQLASSERT_ALWAYS(iConnection != NULL, ESqlPanicInternalError);
   273 	return *iConnection;
   261 	return *iConnection;
   274 	}
   262 	}
   275 
   263 
   276 #ifdef _DEBUG
   264 #ifdef _DEBUG
   277 /**
   265 /**
   278 CSqlCompactEntry invariant.
   266 CSqlCompactEntry invariant.
   279 */
   267 */
   280 void CSqlCompactEntry::Invariant() const
   268 void CSqlCompactEntry::Invariant() const
   281 	{
   269 	{
   282 	__ASSERT_DEBUG(iFullName.Length() > 0 && iFullName.Length() <= KMaxFileName, __SQLPANIC(ESqlPanicInternalError)); 
   270 	__SQLASSERT(iFullName.Length() > 0 && iFullName.Length() <= KMaxFileName, ESqlPanicInternalError); 
   283 	__ASSERT_DEBUG(iConnection != NULL, __SQLPANIC(ESqlPanicInternalError));
   271 	__SQLASSERT(iConnection != NULL, ESqlPanicInternalError);
   284 	__ASSERT_DEBUG(iRefCounter > 0, __SQLPANIC(ESqlPanicInternalError));
   272 	__SQLASSERT(iRefCounter > 0, ESqlPanicInternalError);
   285 	__ASSERT_DEBUG(iState == CSqlCompactEntry::EInactive || iState == CSqlCompactEntry::EInProgress, __SQLPANIC(ESqlPanicInternalError));
   273 	__SQLASSERT(iState == CSqlCompactEntry::EInactive || iState == CSqlCompactEntry::EInProgress, ESqlPanicInternalError);
   286 	__ASSERT_DEBUG(iPageCount >= 0, __SQLPANIC(ESqlPanicInternalError));
   274 	__SQLASSERT(iPageCount >= 0, ESqlPanicInternalError);
   287 	iSettings.Invariant();
   275 	iSettings.Invariant();
   288 	}
   276 	}
   289 #endif//_DEBUG
   277 #endif//_DEBUG