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