persistentstorage/sql/SRC/Server/Compact/SqlCompactConn.cpp
branchRCL_3
changeset 9 667e88a979d7
parent 0 08ec8eefde2f
child 10 31a8f755b7fe
equal deleted inserted replaced
8:fa9941cf3867 9:667e88a979d7
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    25 /**
    25 /**
    26 Creates a new CSqlCompactConn instance.
    26 Creates a new CSqlCompactConn instance.
    27 
    27 
    28 @param aFullName The full database name, including the path.
    28 @param aFullName The full database name, including the path.
    29 @param aFreePageCallback A reference to an object containing the free pages threshold and the callback
    29 @param aFreePageCallback A reference to an object containing the free pages threshold and the callback
    30 					     that needs to be called when the free page count reaches ot is above the threshold.
    30 					     that needs to be called when the free page count reaches or is above the threshold.
    31 						 aFreePageCallback.iThreshold must be set to be in Kb. 	
    31 						 aFreePageCallback.iThreshold must be set to be in Kb. 	
    32 						 If the function call completes successfully and the free pages space is above the threshold,
    32 						 If the function call completes successfully and the free pages space is above the threshold,
    33 						 the aFreePageCallback.iThreshold will be set to contain the free pages count.
    33 						 the aFreePageCallback.iThreshold will be set to contain the free pages count.
    34 						 Otherwise aFreePageCallback.iThreshold will be initizized with zero.
    34 						 Otherwise aFreePageCallback.iThreshold will be initialized with zero.
    35 
    35 
    36 @return A pointer to the created CSqlCompactConn instance
    36 @return A pointer to the created CSqlCompactConn instance
    37 
    37 
    38 @leave KErrNoMemory, an out of memory condition has occurred,
    38 @leave KErrNoMemory, an out of memory condition has occurred,
    39 	   KErrArgument, invalid data in the aFreePageCallback object;
    39 	   KErrArgument, invalid data in the aFreePageCallback object;
   127 	__SQLASSERT(aFreePageCallback.IsValid(), ESqlPanicBadArgument);
   127 	__SQLASSERT(aFreePageCallback.IsValid(), ESqlPanicBadArgument);
   128 	__SQLASSERT(!iHandle, ESqlPanicInternalError);
   128 	__SQLASSERT(!iHandle, ESqlPanicInternalError);
   129 	
   129 	
   130 	TBuf8<KMaxFileName + 1> fname;
   130 	TBuf8<KMaxFileName + 1> fname;
   131 	(void)::UTF16ToUTF8Z(aFullName, fname);//The file is first open by the main connection. 
   131 	(void)::UTF16ToUTF8Z(aFullName, fname);//The file is first open by the main connection. 
   132 										   //The conversion here should always succeeds.
   132 										   //The conversion here should always succeed.
   133 										   //Even in a case of a conversion failure, the next line will report a failure.
   133 										   //Even in a case of a conversion failure, the next line will report a failure.
   134 	__SQLLEAVE_IF_ERROR(::CreateDbHandle8(fname, iHandle));
   134 	__SQLLEAVE_IF_ERROR(::CreateDbHandle8(fname, iHandle));
   135 	
   135 	
   136 	TInt pageSize = PageSizeL();
   136 	TInt pageSize = PageSizeL();
   137 	TInt64 freePageThersholdKb = aFreePageCallback.iThreshold;//"TInt64" because the calculation of the pages may cause an overflow on the next line
   137 	TInt64 freePageThresholdKb = aFreePageCallback.iThreshold;//"TInt64" because the calculation of the pages may cause an overflow on the next line
   138 	aFreePageCallback.iThreshold = (freePageThersholdKb * 1024) / pageSize;//the threshold can be 0
   138 	aFreePageCallback.iThreshold = (freePageThresholdKb * 1024) / pageSize;//the threshold can be 0
   139 	
   139 	
   140 	TBuf8<sizeof(KMainDb8) + 1> dbName;
   140 	TBuf8<sizeof(KMainDb8) + 1> dbName;
   141 	dbName.Copy(KMainDb8);
   141 	dbName.Copy(KMainDb8);
   142 	TInt err = sqlite3_file_control(iHandle, (const char *)dbName.PtrZ(), KSqlFcntlRegisterFreePageCallback, &aFreePageCallback);
   142 	TInt err = sqlite3_file_control(iHandle, (const char *)dbName.PtrZ(), KSqlFcntlRegisterFreePageCallback, &aFreePageCallback);
   143 	__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   143 	__SQLLEAVE_IF_ERROR(::Sql2OsErrCode(err, sqlite3SymbianLastOsError()));
   144 
   144 
   145 	TInt64 freePageCount = FreePageCountL();//"TInt64" because the calculation of the free space may cause an overflow on the next line
   145 	TInt64 freePageCount = FreePageCountL();//"TInt64" because the calculation of the free space may cause an overflow on the next line
   146 	TInt freePageSpaceKb = (freePageCount * pageSize) / 1024;
   146 	TInt freePageSpaceKb = (freePageCount * pageSize) / 1024;
   147 	//Set iThreshold with the free pages count, if right now the database has free space above the threshold.
   147 	//Set iThreshold with the free pages count, if right now the database has free space above the threshold.
   148 	aFreePageCallback.iThreshold = freePageSpaceKb >= freePageThersholdKb ? freePageCount : 0;
   148 	aFreePageCallback.iThreshold = freePageSpaceKb >= freePageThresholdKb ? freePageCount : 0;
   149 	}
   149 	}
   150 
   150 
   151 /**
   151 /**
   152 Retrieves the database free pages count.
   152 Retrieves the database free pages count.
   153 
   153