163 |
221 |
164 ASSERT(iTransactionCount >= 0); |
222 ASSERT(iTransactionCount >= 0); |
165 |
223 |
166 if (++iTransactionCount == 1) |
224 if (++iTransactionCount == 1) |
167 { |
225 { |
168 TInt err = iDatabase.Exec(KBeginTransaction); |
226 DoBeginL(); |
|
227 } |
|
228 } |
|
229 |
|
230 void CMPXDbManager::DoBeginL() |
|
231 { |
|
232 MPX_FUNC("CMPXDbManager::DoBeginL"); |
|
233 |
|
234 TInt err = iDatabase.Exec(KBeginTransaction); |
169 |
235 |
170 // transforms SQL error to KErrNotReady |
236 // transforms SQL error to KErrNotReady |
171 if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired ) |
237 if( (err <= KSqlErrGeneral && err >= KSqlErrNotDb) || err == KSqlErrStmtExpired ) |
172 { |
238 { |
173 User::Leave(KErrNotReady); |
239 User::Leave(KErrNotReady); |
174 } |
240 } |
175 else |
241 else |
176 { |
242 { |
177 User::LeaveIfError(err); |
243 User::LeaveIfError(err); |
178 } |
244 } |
179 } |
245 } |
180 } |
246 |
|
247 // ---------------------------------------------------------------------------- |
|
248 // Copy all DBs to RAM disk |
|
249 // ---------------------------------------------------------------------------- |
|
250 // |
|
251 EXPORT_C void CMPXDbManager::CopyDBsToRamL( TBool aIsMTPInUse ) |
|
252 { |
|
253 #ifdef __RAMDISK_PERF_ENABLE |
|
254 MPX_DEBUG1("-->CMPXDbManager::CopyDBsToRamL"); |
|
255 if( iRAMDiskPerfEnabled ) |
|
256 { |
|
257 if ( !IsRamDiskSpaceAvailable() ) |
|
258 { |
|
259 return; |
|
260 } |
|
261 |
|
262 // Check if we are over the allowed ram space. |
|
263 TInt dbSize=0; |
|
264 TInt err = GetTotalDatabasesSize(dbSize); |
|
265 if ( err || (dbSize > iMaximumAllowedRAMDiskSpaceToCopy) ) |
|
266 { |
|
267 MPX_DEBUG2("<--CMPXDbManager::CopyDBsToRamL Over the allowed Ram disk limit %Lu", iMaximumAllowedRAMDiskSpaceToCopy ); |
|
268 return; |
|
269 } |
|
270 |
|
271 TInt transactionCount = iTransactionCount; |
|
272 if (iTransactionCount > 0) |
|
273 { |
|
274 iTransactionCount = 0; |
|
275 DoCommitL(); |
|
276 } |
|
277 |
|
278 TInt count(iDatabaseHandles.Count()); |
|
279 TBool ret = EFalse; |
|
280 for ( TInt i = 0; i < count ; ++i ) |
|
281 { |
|
282 if ( iDatabaseHandles[i].iUseRAMdb ) |
|
283 { |
|
284 // already used |
|
285 MPX_DEBUG1("CMPXDbManager::CopyDBsToRamL iUseRAMdb already ETrue"); |
|
286 continue; |
|
287 } |
|
288 iDatabaseHandles[i].iUseRAMdb = ETrue; |
|
289 TRAPD(err, ret = DoCopyDBToRamL( iDatabaseHandles[i].iDrive, aIsMTPInUse )) |
|
290 if ( err != KErrNone ) |
|
291 { |
|
292 MPX_DEBUG2("CMPXDbManager::CopyDBsToRamL error=%d", err); |
|
293 // remove dymmy file |
|
294 RemoveDummyFile(i); |
|
295 // try to close database that is opened from RAM disk |
|
296 TRAP_IGNORE(CloseDatabaseL( iDatabaseHandles[i].iDrive )); |
|
297 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
298 // reopen database from drive not from RAM |
|
299 OpenDatabaseL( iDatabaseHandles[i].iDrive ); |
|
300 continue; // continue to copy for next drive |
|
301 } |
|
302 if ( !ret ) |
|
303 { |
|
304 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
305 } |
|
306 } |
|
307 |
|
308 if (transactionCount > 0) |
|
309 { |
|
310 DoBeginL(); |
|
311 iTransactionCount = transactionCount; |
|
312 } |
|
313 } |
|
314 iRAMInUse = ETrue; |
|
315 |
|
316 MPX_DEBUG1("<--CMPXDbManager::CopyDBsToRamL"); |
|
317 #endif //__RAMDISK_PERF_ENABLE |
|
318 |
|
319 } |
|
320 |
|
321 |
|
322 // ---------------------------------------------------------------------------- |
|
323 // CMPXDbManager::DoCopyDBsToRamL |
|
324 // ---------------------------------------------------------------------------- |
|
325 // |
|
326 TBool CMPXDbManager::DoCopyDBToRamL( TDriveUnit aDrive, TBool aIsMTPInUse ) |
|
327 { |
|
328 #ifdef __RAMDISK_PERF_ENABLE |
|
329 MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsToRamL drive=%d", (TInt)aDrive); |
|
330 TFileName dst; |
|
331 TFileName src; |
|
332 dst.Append(iRAMFolder); |
|
333 src.Append(aDrive.Name()); |
|
334 src.Append(KDBFilePath); |
|
335 TRAPD( err, BaflUtils::EnsurePathExistsL( iFs, dst )); |
|
336 if ( err != KErrNone ) |
|
337 { |
|
338 return EFalse; |
|
339 } |
|
340 TFileName filename; |
|
341 filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db |
|
342 src.Append(filename); |
|
343 MPX_DEBUG2("RAMDisk src path=%S", &src); |
|
344 TEntry entry; |
|
345 iFs.Entry( src, entry ); |
|
346 if (!BlockDiskSpace( aDrive, entry.iSize, aIsMTPInUse ) ) |
|
347 { |
|
348 MPX_DEBUG1("CMPXDbManager::DoCopyDBsToRamL() BlockDiskSpace failed"); |
|
349 return EFalse; // continue for next drive |
|
350 } |
|
351 TBuf<2> d; |
|
352 d.Append(aDrive.Name()); |
|
353 HBufC* temp = HBufC::NewLC(KMaxFileName); |
|
354 temp->Des().Append(d.Left(1)); |
|
355 temp->Des().Append(iDbFile->Des()); |
|
356 filename.Format(KSecurePath, User::Identity().iUid, temp); |
|
357 CleanupStack::PopAndDestroy(temp); |
|
358 dst.Append(filename); |
|
359 MPX_DEBUG2("RAMDisk dst path=%S", &dst); |
|
360 TInt index( GetDatabaseIndex((TInt)aDrive) ); |
|
361 delete iDatabaseHandles[index].iOrigFullFilePath; |
|
362 iDatabaseHandles[index].iOrigFullFilePath = 0; |
|
363 delete iDatabaseHandles[index].iTargetFullFilePath; |
|
364 iDatabaseHandles[index].iTargetFullFilePath = 0; |
|
365 |
|
366 // Save these path so it is convenient to copy back |
|
367 iDatabaseHandles[index].iOrigFullFilePath = HBufC::NewL(src.Length()); |
|
368 iDatabaseHandles[index].iTargetFullFilePath = HBufC::NewL(dst.Length()); |
|
369 |
|
370 iDatabaseHandles[index].iOrigFullFilePath->Des().Append(src); |
|
371 iDatabaseHandles[index].iTargetFullFilePath->Des().Append(dst); |
|
372 |
|
373 TRAP(err, CloseDatabaseL(aDrive)); |
|
374 if ( err != KErrNone ) |
|
375 { |
|
376 MPX_DEBUG2("<--CMPXDbManager::DoCopyDBsToRamL error=%d", err); |
|
377 TInt index(GetDatabaseIndex((TInt)aDrive)); |
|
378 if ( index >= 0 ) |
|
379 { |
|
380 RemoveDummyFile( index ); |
|
381 } |
|
382 return EFalse; |
|
383 } |
|
384 User::LeaveIfError( BaflUtils::CopyFile(iFs, src, dst )); |
|
385 OpenDatabaseL((TInt)aDrive); |
|
386 |
|
387 MPX_DEBUG2("RAMDisk Database opened=%d", (TInt)aDrive); |
|
388 MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsToRamL"); |
|
389 return ETrue; |
|
390 #endif //__RAMDISK_PERF_ENABLE |
|
391 } |
|
392 |
|
393 // ---------------------------------------------------------------------------- |
|
394 // Copy all DBs from RAM disk back to normal drives |
|
395 // ---------------------------------------------------------------------------- |
|
396 // |
|
397 EXPORT_C void CMPXDbManager::CopyDBsFromRamL() |
|
398 { |
|
399 #ifdef __RAMDISK_PERF_ENABLE |
|
400 MPX_DEBUG1("-->CMPXDbManager::CopyDBsFromRamL"); |
|
401 if( iRAMDiskPerfEnabled ) |
|
402 { |
|
403 TInt transactionCount = iTransactionCount; |
|
404 if (iTransactionCount > 0) |
|
405 { |
|
406 iTransactionCount = 0; |
|
407 DoCommitL(); |
|
408 } |
|
409 |
|
410 TInt count(iDatabaseHandles.Count()); |
|
411 for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb; ++i) |
|
412 { |
|
413 TRAPD(err, DoCopyDBFromRamL(i)); |
|
414 if ( err != KErrNone ) |
|
415 { |
|
416 MPX_DEBUG2("<--CMPXDbManager::CopyDBsFromRamL error=%d", err); |
|
417 //anyting wrong, delete the temp file and open database from drive |
|
418 RemoveDummyFile(i); |
|
419 // delete Db on RAM |
|
420 User::LeaveIfError( BaflUtils::DeleteFile(iFs, |
|
421 *iDatabaseHandles[i].iTargetFullFilePath)); |
|
422 } |
|
423 iDatabaseHandles[i].iUseRAMdb = EFalse; |
|
424 // open db from drive |
|
425 OpenDatabaseL( iDatabaseHandles[i].iDrive ); |
|
426 } |
|
427 |
|
428 if (transactionCount > 0) |
|
429 { |
|
430 DoBeginL(); |
|
431 iTransactionCount = transactionCount; |
|
432 } |
|
433 } |
|
434 iRAMInUse = EFalse; |
|
435 |
|
436 MPX_DEBUG1("<--CMPXDbManager::CopyDBsFromRamL"); |
|
437 #endif //__RAMDISK_PERF_ENABLE |
|
438 } |
|
439 |
|
440 |
|
441 // ---------------------------------------------------------------------------- |
|
442 // CMPXDbManager::DoCopyDBsToRamL |
|
443 // ---------------------------------------------------------------------------- |
|
444 // |
|
445 void CMPXDbManager::DoCopyDBFromRamL( TInt aIndex ) |
|
446 { |
|
447 #ifdef __RAMDISK_PERF_ENABLE |
|
448 MPX_DEBUG1("-->CMPXDbManager::DoCopyDBsFromRamL"); |
|
449 MPX_DEBUG2("-->CMPXDbManager::DoCopyDBsFromRamL Drive %d will be closed before copying db from RAM.", |
|
450 iDatabaseHandles[aIndex].iDrive); |
|
451 |
|
452 CloseDatabaseL(iDatabaseHandles[aIndex].iDrive); |
|
453 |
|
454 // Delete existing DB on drive |
|
455 User::LeaveIfError( BaflUtils::DeleteFile(iFs, |
|
456 *iDatabaseHandles[aIndex].iOrigFullFilePath)); |
|
457 MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL old DB on drive deleted"); |
|
458 |
|
459 // Rename dummy file to be orignal file name |
|
460 User::LeaveIfError( BaflUtils::RenameFile(iFs, |
|
461 iDatabaseHandles[aIndex].iDummyFilePath, |
|
462 *iDatabaseHandles[aIndex].iOrigFullFilePath) ); |
|
463 MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk renamed."); |
|
464 |
|
465 //Copy Db from RAM to replace dummy file |
|
466 ReplaceFileL( *iDatabaseHandles[aIndex].iTargetFullFilePath, *iDatabaseHandles[aIndex].iOrigFullFilePath); |
|
467 |
|
468 MPX_DEBUG1("CMPXDbManager::CopyDBsFromRamL RAMDisk copied back."); |
|
469 |
|
470 // Delete existing DB on RAM |
|
471 User::LeaveIfError( BaflUtils::DeleteFile(iFs, *iDatabaseHandles[aIndex].iTargetFullFilePath)); |
|
472 MPX_DEBUG1("CMPXDbManager::DoCopyDBsFromRamL RAMDisk deleted"); |
|
473 |
|
474 MPX_DEBUG1("<--CMPXDbManager::DoCopyDBsFromRamL"); |
|
475 #endif //__RAMDISK_PERF_ENABLE |
|
476 } |
|
477 |
|
478 // ---------------------------------------------------------------------------- |
|
479 // CMPXDbManager::ReplaceFileL |
|
480 // |
|
481 // Replaces a file with another writing over the destination file. |
|
482 // Leaves on error. |
|
483 // Implementation follows CFileMan::Copy except that |
|
484 // - we don't resize target file to zero |
|
485 // - we can assume that files already exist |
|
486 // - we don't copy file attributes & timestamp |
|
487 // ---------------------------------------------------------------------------- |
|
488 // |
|
489 void CMPXDbManager::ReplaceFileL( const TDesC& aSrcName, const TDesC& aDstName ) |
|
490 { |
|
491 // open files |
|
492 RFile srcFile; |
|
493 User::LeaveIfError( srcFile.Open(iFs, aSrcName, EFileRead|EFileShareReadersOnly) ); |
|
494 CleanupClosePushL( srcFile ); |
|
495 |
|
496 RFile dstFile; |
|
497 User::LeaveIfError( dstFile.Open(iFs, aDstName, EFileWrite|EFileWriteDirectIO|EFileShareExclusive) ); |
|
498 CleanupClosePushL( dstFile ); |
|
499 |
|
500 // resize destination file |
|
501 TInt remainingBytes; |
|
502 User::LeaveIfError( srcFile.Size(remainingBytes) ); |
|
503 User::LeaveIfError( dstFile.SetSize(remainingBytes) ); |
|
504 |
|
505 // allocate buffer |
|
506 const TInt KBigBufSize = 512 * 1024; |
|
507 const TInt KMediumBufSize = 32 * 1024; |
|
508 const TInt KSmallBufSize = 4 * 1024; |
|
509 HBufC8* bufPtr=HBufC8::New( Min(KBigBufSize, remainingBytes) ); |
|
510 if (bufPtr==NULL) |
|
511 bufPtr=HBufC8::New(KMediumBufSize); |
|
512 if (bufPtr==NULL) |
|
513 bufPtr=HBufC8::New(KSmallBufSize); |
|
514 if (bufPtr == NULL) |
|
515 User::Leave(KErrNoMemory); |
|
516 CleanupStack::PushL(bufPtr); |
|
517 |
|
518 // copy |
|
519 TPtr8 copyBuf=bufPtr->Des(); |
|
520 TInt pos=0; |
|
521 while( remainingBytes > 0 ) |
|
522 { |
|
523 TInt s = Min( remainingBytes, copyBuf.MaxSize() ); |
|
524 TInt ret = srcFile.Read(pos, copyBuf, s); |
|
525 if (ret == KErrNone && copyBuf.Length()!= s ) |
|
526 { |
|
527 ret = KErrCorrupt; |
|
528 } |
|
529 if (ret == KErrNone) |
|
530 { |
|
531 ret = dstFile.Write(pos, copyBuf, s); |
|
532 } |
|
533 User::LeaveIfError (ret); |
|
534 pos += s; |
|
535 remainingBytes -= s; |
|
536 } |
|
537 User::LeaveIfError( dstFile.Flush() ); |
|
538 CleanupStack::PopAndDestroy(3); // bufPtr, dstFile, srcFile |
|
539 } |
|
540 |
|
541 // ---------------------------------------------------------------------------- |
|
542 // CMPXDbManager::RemoveDummyFile |
|
543 // ---------------------------------------------------------------------------- |
|
544 // |
|
545 void CMPXDbManager::RemoveDummyFile( TInt index ) |
|
546 { |
|
547 #ifdef __RAMDISK_PERF_ENABLE |
|
548 MPX_DEBUG1("-->CMPXDbManager::RemoveDummyFile"); |
|
549 |
|
550 if ( iDatabaseHandles[index].iDummyFilePath.Length() ) |
|
551 { |
|
552 TInt err = BaflUtils::DeleteFile(iFs, iDatabaseHandles[index].iDummyFilePath); |
|
553 if ( !err ) |
|
554 { |
|
555 iDatabaseHandles[index].iDummyFilePath.Zero(); |
|
556 } |
|
557 } |
|
558 MPX_DEBUG1("<--CMPXDbManager::RemoveDummyFile"); |
|
559 #endif //__RAMDISK_PERF_ENABLE |
|
560 |
|
561 } |
|
562 |
181 |
563 |
182 // ---------------------------------------------------------------------------- |
564 // ---------------------------------------------------------------------------- |
183 // Commits a transaction on all databases. |
565 // Commits a transaction on all databases. |
184 // ---------------------------------------------------------------------------- |
566 // ---------------------------------------------------------------------------- |
185 // |
567 // |
2046 CreateTablesL(database, corrupt); |
2515 CreateTablesL(database, corrupt); |
2047 |
2516 |
2048 CleanupStack::PopAndDestroy(filename); |
2517 CleanupStack::PopAndDestroy(filename); |
2049 CleanupStack::PopAndDestroy(&database); |
2518 CleanupStack::PopAndDestroy(&database); |
2050 } |
2519 } |
2051 |
2520 |
|
2521 // --------------------------------------------------------------------------- |
|
2522 // CMPXDbManager::GetRAMDiskPath |
|
2523 // --------------------------------------------------------------------------- |
|
2524 // |
|
2525 TInt CMPXDbManager::GetRAMDiskPath() |
|
2526 { |
|
2527 TInt error = KErrNotSupported; |
|
2528 #ifdef __RAMDISK_PERF_ENABLE |
|
2529 MPX_DEBUG1("-->CMPXDbManager::GetRAMDiskPath"); |
|
2530 TDriveList driveList; |
|
2531 TBool ramDriveFound = EFalse; |
|
2532 TInt driveOffset = 'A'; |
|
2533 iRAMFolder.Zero(); |
|
2534 |
|
2535 error = iFs.DriveList( driveList ); |
|
2536 if ( error == KErrNone ) |
|
2537 { |
|
2538 for ( TInt i = 0; i < driveList.Length(); i++ ) |
|
2539 { |
|
2540 if ( driveList[i] != 0 ) |
|
2541 { |
|
2542 TDriveInfo info; |
|
2543 TInt err = iFs.Drive( info, i ); |
|
2544 if ( !err && info.iType == EMediaRam ) |
|
2545 { |
|
2546 iRAMDrive = driveOffset + i; |
|
2547 iRAMFolder.Append(iRAMDrive); |
|
2548 iRAMFolder.Append(_L(":")); |
|
2549 iRAMFolder.Append(KDBFilePath); |
|
2550 ramDriveFound = ETrue; |
|
2551 MPX_DEBUG2("RAMDisk path=%S", &iRAMFolder); |
|
2552 break; |
|
2553 } |
|
2554 } |
|
2555 } |
|
2556 |
|
2557 // Check if ram drive is found. |
|
2558 if ( !ramDriveFound ) |
|
2559 { |
|
2560 error = KErrNotFound; |
|
2561 } |
|
2562 } |
|
2563 MPX_DEBUG2("CMPXDbManager::GetRAMDiskPath Get DriveList error=%d", error); |
|
2564 MPX_DEBUG1("<--CMPXDbManager::GetRAMDiskPath"); |
|
2565 #endif //__RAMDISK_PERF_ENABLE |
|
2566 return error; |
|
2567 } |
|
2568 |
|
2569 // --------------------------------------------------------------------------- |
|
2570 // CMPXDbManager::IsRamDiskSpaceAvailable |
|
2571 // --------------------------------------------------------------------------- |
|
2572 // |
|
2573 TBool CMPXDbManager::IsRamDiskSpaceAvailable() |
|
2574 { |
|
2575 |
|
2576 #ifdef __RAMDISK_PERF_ENABLE |
|
2577 |
|
2578 MPX_DEBUG1("-->CMPXDbManager::IsDiskSpaceAvailable" ); |
|
2579 TInt driveIndex; |
|
2580 RFs::CharToDrive(iRAMDrive, driveIndex); |
|
2581 TVolumeInfo vol; |
|
2582 TInt err = iFs.Volume( vol, driveIndex ); |
|
2583 if ( err == KErrNone ) |
|
2584 { |
|
2585 MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Free in bytes =%Lu", vol.iFree); |
|
2586 if ( vol.iFree > KMPMinimumRAMSizeToRun ) |
|
2587 { |
|
2588 MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable Ok to copy"); |
|
2589 return ETrue; |
|
2590 } |
|
2591 MPX_DEBUG1("CMPXDbManager::IsRamDiskSpaceAvailable NOT Ok to copy"); |
|
2592 return EFalse; |
|
2593 } |
|
2594 |
|
2595 MPX_DEBUG2("CMPXDbManager::IsRamDiskSpaceAvailable Disk Not available to use. %d", err); |
|
2596 MPX_DEBUG1("<--CMPXDbManager::IsDiskSpaceAvailable"); |
|
2597 |
|
2598 #endif //__RAMDISK_PERF_ENABLE |
|
2599 |
|
2600 return EFalse; |
|
2601 } |
|
2602 |
|
2603 |
|
2604 // --------------------------------------------------------------------------- |
|
2605 // CMPXDbManager::BlockDiskSpaceL |
|
2606 // --------------------------------------------------------------------------- |
|
2607 // |
|
2608 TBool CMPXDbManager::BlockDiskSpace( TDriveUnit aDrive, TInt aOrigDbSize, TBool aIsMTPInUse ) |
|
2609 { |
|
2610 #ifdef __RAMDISK_PERF_ENABLE |
|
2611 |
|
2612 MPX_DEBUG2("-->CMPXDbManager::BlockDiskSpaceL %d", (TInt)aDrive ); |
|
2613 |
|
2614 // if current DB size can not fit in RAM, abort now |
|
2615 TInt ramDrive; |
|
2616 RFs::CharToDrive(iRAMDrive, ramDrive); |
|
2617 TVolumeInfo vol; |
|
2618 TInt err = iFs.Volume( vol, ramDrive ); |
|
2619 if ( vol.iFree <= aOrigDbSize + KMPMinimumRAMSizeToRun ) |
|
2620 { |
|
2621 MPX_DEBUG1("-->CMPXDbManager::BlockDiskSpaceL Not enough even for copy original DB file, leave" ); |
|
2622 return EFalse; |
|
2623 } |
|
2624 |
|
2625 // ensure you have the disk volume and database |
|
2626 err = iFs.Volume( vol, (TInt)aDrive ); |
|
2627 if (err != KErrNone) |
|
2628 { |
|
2629 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Volume not available on drive %d", (TInt)aDrive); |
|
2630 return EFalse; |
|
2631 } |
|
2632 |
|
2633 TInt index( GetDatabaseIndex((TInt)aDrive) ); |
|
2634 if (index < 0) |
|
2635 { |
|
2636 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Database not available for drive %d", (TInt)aDrive); |
|
2637 return EFalse; |
|
2638 } |
|
2639 |
|
2640 // Check if the drive has enough space to block |
|
2641 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk total free space in bytes =%Lu", vol.iFree); |
|
2642 TInt64 blockingSize( CalculateInitalDummyDBSize( vol, aOrigDbSize, aIsMTPInUse )); |
|
2643 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Disk blocking size =%Lu", blockingSize); |
|
2644 if ( vol.iFree <= blockingSize + 1*KMPMegaByte ) |
|
2645 { |
|
2646 MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL NOk to block"); |
|
2647 return EFalse; |
|
2648 } |
|
2649 |
|
2650 // Create and resize the dummy file |
|
2651 TFileName dummyDbFileName; |
|
2652 TChar ch; |
|
2653 RFs::DriveToChar((TInt)aDrive, ch ); |
|
2654 dummyDbFileName.Format( KDummyDbFile, (TUint)ch); |
|
2655 RFile dummyDb; |
|
2656 err = dummyDb.Replace( iFs, dummyDbFileName, EFileWrite ); |
|
2657 if (err != KErrNone) |
|
2658 { |
|
2659 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't open dummy file %d", err); |
|
2660 return EFalse; |
|
2661 } |
|
2662 err = dummyDb.SetSize( blockingSize ); |
|
2663 if ( err ) |
|
2664 { |
|
2665 MPX_DEBUG2("CMPXDbManager::BlockDiskSpaceL Can't resize dummy file %d", err); |
|
2666 dummyDb.Close(); |
|
2667 return EFalse; |
|
2668 } |
|
2669 |
|
2670 dummyDb.Close(); |
|
2671 MPX_DEBUG1("CMPXDbManager::BlockDiskSpaceL Ok to block"); |
|
2672 |
|
2673 iDatabaseHandles[index].iDummyFilePath.Copy(dummyDbFileName); |
|
2674 MPX_DEBUG1("<--CMPXDbManager::BlockDiskSpace"); |
|
2675 |
|
2676 return ETrue; |
|
2677 #else |
|
2678 return EFalse; |
|
2679 #endif //__RAMDISK_PERF_ENABLE |
|
2680 } |
|
2681 |
|
2682 |
|
2683 // --------------------------------------------------------------------------- |
|
2684 // CMPXDbManager::CalculateInitalDummyDBSizeL |
|
2685 // |
|
2686 //a) MTP case |
|
2687 //------------- |
|
2688 // totalNumOfSongsCanFit = <disk free space> / 2 MB; |
|
2689 // metadataSize = totalNumOfSongsCanFit * 3000B |
|
2690 // estimatedDBSize = metadataSize + <orig DB size>; |
|
2691 // dummyDBSize = MIN (iMaximumAllowedRAMDiskSpaceToCopy , estimatedDBSize ) |
|
2692 |
|
2693 //b) Harvesting case |
|
2694 //------------------- |
|
2695 // totalNumOfSongsCanFit = <disk total size>/ 2 MB |
|
2696 // metadataSize = totalNumOfSongsCanFit * 3000B |
|
2697 // estimatedSize = metadataSize+ <orig DB size> |
|
2698 // freeDiskSpace = <disk free space> - 1 MB |
|
2699 // dummyDBSize = MIN (freeDiskSpace, iMaximumAllowedRAMDiskSpaceToCopy , estimatedSize); |
|
2700 // |
|
2701 // --------------------------------------------------------------------------- |
|
2702 // |
|
2703 TInt64 CMPXDbManager::CalculateInitalDummyDBSize( const TVolumeInfo& aVol, TInt aOrigDbSize, TBool aIsMTPInUse ) |
|
2704 { |
|
2705 #ifdef __RAMDISK_PERF_ENABLE |
|
2706 MPX_DEBUG1("-->CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2707 |
|
2708 if ( aIsMTPInUse ) |
|
2709 { |
|
2710 TInt64 totalNumOfSongsCanFit = aVol.iFree / KMPEstimatedSongInBytes; |
|
2711 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize aVol.iFree=%Lu", aVol.iFree ); |
|
2712 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize totalNumOfSongsCanFit=%Lu", totalNumOfSongsCanFit ); |
|
2713 TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize; |
|
2714 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize (MTP case) estimated DB size from calculation=%Lu", estimatedSize ); |
|
2715 if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy ) |
|
2716 { |
|
2717 MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned iMaximumAllowedRAMDiskSpaceToCopy %d", iMaximumAllowedRAMDiskSpaceToCopy); |
|
2718 return iMaximumAllowedRAMDiskSpaceToCopy; |
|
2719 } |
|
2720 else |
|
2721 { |
|
2722 MPX_DEBUG2("<--CMPXDbManager::CalculateInitalDummyDBSize returned %Lu", estimatedSize ); |
|
2723 return estimatedSize; |
|
2724 } |
|
2725 } |
|
2726 else |
|
2727 { |
|
2728 TInt64 totalNumOfSongsCanFit = aVol.iSize / KMPEstimatedSongInBytes; |
|
2729 TInt64 estimatedSize = totalNumOfSongsCanFit * (TInt64) KMPEstimatedSizePerDBEntry + aOrigDbSize; |
|
2730 MPX_DEBUG2("-->CMPXDbManager::CalculateInitalDummyDBSize estimated DB size from calculation=%Lu", estimatedSize ); |
|
2731 if ( estimatedSize > iMaximumAllowedRAMDiskSpaceToCopy ) |
|
2732 { |
|
2733 MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2734 // If estimated size is larger than expected free RAM size, |
|
2735 // and if the RAM size is larger than free disk space, |
|
2736 // then use free disk space. 1*KMPMegaByte prevent MP to use up all diskspace |
|
2737 //return iMaximumAllowedRAMDiskSpaceToCopy > aVol.iFree - 1*KMPMegaByte |
|
2738 // ? aVol.iFree - 1*KMPMegaByte : iMaximumAllowedRAMDiskSpaceToCopy; |
|
2739 return iMaximumAllowedRAMDiskSpaceToCopy; |
|
2740 } |
|
2741 else |
|
2742 { |
|
2743 MPX_DEBUG1("<--CMPXDbManager::CalculateInitalDummyDBSize"); |
|
2744 // If estimated size is larger than disk free size, use free diskspace size, |
|
2745 //return estimatedSize > aVol.iFree - 1*KMPMegaByte |
|
2746 // ? aVol.iFree - 1*KMPMegaByte : estimatedSize; |
|
2747 return estimatedSize; |
|
2748 } |
|
2749 } |
|
2750 |
|
2751 #endif //__RAMDISK_PERF_ENABLE |
|
2752 } |
|
2753 |
|
2754 |
|
2755 // --------------------------------------------------------------------------- |
|
2756 // CMPXDbManager::GetDatabaseIndex |
|
2757 // --------------------------------------------------------------------------- |
|
2758 // |
|
2759 TInt CMPXDbManager::GetDatabaseIndex(TInt aDrive) |
|
2760 { |
|
2761 #ifdef __RAMDISK_PERF_ENABLE |
|
2762 MPX_DEBUG2("-->CMPXDbManager::GetDatabaseIndex %d", aDrive ); |
|
2763 TInt count(iDatabaseHandles.Count()); |
|
2764 for (TInt i = 0; i < count; ++i) |
|
2765 { |
|
2766 if ( iDatabaseHandles[i].iDrive == aDrive ) |
|
2767 { |
|
2768 return i; |
|
2769 } |
|
2770 } |
|
2771 #endif //__RAMDISK_PERF_ENABLE |
|
2772 MPX_DEBUG1("<--CMPXDbManager::GetDatabaseIndex returned -1"); |
|
2773 return -1; |
|
2774 } |
|
2775 |
|
2776 |
|
2777 // --------------------------------------------------------------------------- |
|
2778 // CMPXDbManager::EnsureRamSpaceL |
|
2779 // --------------------------------------------------------------------------- |
|
2780 // |
|
2781 EXPORT_C void CMPXDbManager::EnsureRamSpaceL() |
|
2782 { |
|
2783 #ifdef __RAMDISK_PERF_ENABLE |
|
2784 MPX_DEBUG1("-->CMPXDbManager::EnsureRamSpaceL"); |
|
2785 |
|
2786 if ( iRAMInUse ) |
|
2787 { |
|
2788 TVolumeInfo vol; |
|
2789 TInt driveIndex; |
|
2790 RFs::CharToDrive( iRAMDrive, driveIndex ); |
|
2791 |
|
2792 TInt errRAM = iFs.Volume( vol, driveIndex ); |
|
2793 if ( errRAM == KErrNone && vol.iFree < KMPMinimumRAMSizeToRun ) |
|
2794 { |
|
2795 // RAM not enough, copy back to normal drive and continue to harvest. |
|
2796 MPX_DEBUG1("CMPXDbManager::EnsureRamSpaceL RAM diskspace is full, copy dbs back"); |
|
2797 CopyDBsFromRamL(); |
|
2798 } |
|
2799 else |
|
2800 { |
|
2801 TInt size=0; |
|
2802 TInt err = GetTotalRamDatabasesSize(size); |
|
2803 if ( err || (size > iMaximumAllowedRAMDiskSpaceToCopy) ) |
|
2804 { |
|
2805 // Databases using too much RAM space, copy back to normal drive and continue to harvest. |
|
2806 if ( err ) |
|
2807 { |
|
2808 MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL Get DBs Size Err = %d, copy dbs back", err); |
|
2809 } |
|
2810 else |
|
2811 { |
|
2812 MPX_DEBUG2("CMPXDbManager::EnsureRamSpaceL DBs using too much RAM space size = %d, copy dbs back", size); |
|
2813 } |
|
2814 CopyDBsFromRamL(); |
|
2815 } |
|
2816 } |
|
2817 } |
|
2818 MPX_DEBUG1("<--CMPXDbManager::EnsureRamSpaceL"); |
|
2819 #endif //__RAMDISK_PERF_ENABLE |
|
2820 } |
|
2821 |
|
2822 |
|
2823 // --------------------------------------------------------------------------- |
|
2824 // CMPXDbManager::EnsureDiskSpaceL |
|
2825 // --------------------------------------------------------------------------- |
|
2826 // |
|
2827 void CMPXDbManager::EnsureDiskSpaceL(TInt aDrive) |
|
2828 { |
|
2829 MPX_DEBUG2("-->CMPXDbManager::EnsureDiskSpaceL for drive %d", aDrive); |
|
2830 // handle the case of C drive |
|
2831 TDriveUnit drive(aDrive); |
|
2832 TDriveUnit cdrive(KRootDrive()); |
|
2833 |
|
2834 if(drive == cdrive) |
|
2835 { |
|
2836 if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, aDrive)) |
|
2837 { |
|
2838 MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full"); |
|
2839 User::Leave(KErrDiskFull); |
|
2840 } |
|
2841 |
|
2842 return; |
|
2843 } |
|
2844 |
|
2845 // handle other drives (eg. removable EDriveE) |
|
2846 TInt count(iDatabaseHandles.Count()); |
|
2847 for (TInt i = 0; i < count; ++i) |
|
2848 { |
|
2849 if (((KDbManagerAllDrives == aDrive) || |
|
2850 (aDrive == iDatabaseHandles[i].iDrive)) && |
|
2851 iDatabaseHandles[i].iOpen) |
|
2852 { |
|
2853 if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, 0, |
|
2854 iDatabaseHandles[i].iDrive)) |
|
2855 { |
|
2856 MPX_DEBUG1("CMPXDbManager::EnsureDiskSpaceL Error diskspace full"); |
|
2857 User::Leave(KErrDiskFull); |
|
2858 } |
|
2859 } |
|
2860 |
|
2861 if (aDrive == iDatabaseHandles[i].iDrive) |
|
2862 { |
|
2863 // exit if just one drive to check |
|
2864 break; |
|
2865 } |
|
2866 } |
|
2867 MPX_DEBUG1("<--CMPXDbManager::EnsureDiskSpaceL"); |
|
2868 } |
|
2869 |
|
2870 |
|
2871 // --------------------------------------------------------------------------- |
|
2872 // CMPXDbManager::DoBackupDBs |
|
2873 // --------------------------------------------------------------------------- |
|
2874 // |
|
2875 /*EXPORT_C void CMPXDbManager::BackupDBsL() |
|
2876 { |
|
2877 #ifdef __RAMDISK_PERF_ENABLE |
|
2878 MPX_DEBUG1("-->CMPXDbManager::BackupDBsL"); |
|
2879 |
|
2880 TInt transactionCount = iTransactionCount; |
|
2881 if (iTransactionCount > 0) |
|
2882 { |
|
2883 iTransactionCount = 0; |
|
2884 DoCommitL(); |
|
2885 } |
|
2886 |
|
2887 TInt count(iDatabaseHandles.Count()); |
|
2888 for (TInt i = 0; i < count && iDatabaseHandles[i].iUseRAMdb ; ++i) |
|
2889 { |
|
2890 CloseDatabaseL( iDatabaseHandles[i].iDrive ); |
|
2891 |
|
2892 TInt err= BaflUtils::CopyFile(iFs, |
|
2893 iDatabaseHandles[i].iTargetFullFilePath->Des(), |
|
2894 iDatabaseHandles[i].iOrigFullFilePath->Des()); |
|
2895 |
|
2896 MPX_DEBUG2("CMPXDbManager::BackupDBsL err = %d", err); |
|
2897 |
|
2898 OpenDatabaseL( iDatabaseHandles[i].iDrive ); |
|
2899 } |
|
2900 |
|
2901 if (transactionCount > 0) |
|
2902 { |
|
2903 DoBeginL(); |
|
2904 iTransactionCount = transactionCount; |
|
2905 } |
|
2906 MPX_DEBUG1("<--CMPXDbManager::BackupDBsL"); |
|
2907 #endif //__RAMDISK_PERF_ENABLE |
|
2908 }*/ |
|
2909 |
|
2910 // --------------------------------------------------------------------------- |
|
2911 // CMPXDbManager::GetTotalDatabasesSize |
|
2912 // --------------------------------------------------------------------------- |
|
2913 // |
|
2914 TInt CMPXDbManager::GetTotalDatabasesSize(TInt& aSize) |
|
2915 { |
|
2916 MPX_FUNC("CMPXDbManager::GetTotalDatabasesSize"); |
|
2917 TInt err = KErrNotSupported; |
|
2918 #ifdef __RAMDISK_PERF_ENABLE |
|
2919 TInt size=0; |
|
2920 TInt count(iDatabaseHandles.Count()); |
|
2921 err = KErrNone; |
|
2922 for ( TInt i = 0; i < count ; ++i ) |
|
2923 { |
|
2924 // Generate database name. |
|
2925 TFileName dbFilename; |
|
2926 TDriveUnit drive(iDatabaseHandles[i].iDrive); |
|
2927 dbFilename.Append(drive.Name()); |
|
2928 dbFilename.Append(KDBFilePath); |
|
2929 TFileName filename; |
|
2930 filename.Format(KSecurePath, User::Identity().iUid, iDbFile); //x:\private\10281e17\[sldfdsf]mpxv2_5.db |
|
2931 dbFilename.Append(filename); |
|
2932 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Database name = %S", &dbFilename); |
|
2933 TEntry entry; |
|
2934 err = iFs.Entry( dbFilename, entry ); |
|
2935 if (err == KErrNotFound || err == KErrNotReady ) |
|
2936 { |
|
2937 MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Ignored %S, error = %d", &dbFilename, err); |
|
2938 err = KErrNone; |
|
2939 continue; |
|
2940 } |
|
2941 if ( err != KErrNone ) |
|
2942 { |
|
2943 break; |
|
2944 } |
|
2945 MPX_DEBUG3("CMPXDbManager::GetTotalDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize); |
|
2946 // sum up size |
|
2947 size += entry.iSize; |
|
2948 } |
|
2949 aSize = size; |
|
2950 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Total Size of Dbs = %d", size); |
|
2951 |
|
2952 #endif //__RAMDISK_PERF_ENABLE |
|
2953 MPX_DEBUG2("CMPXDbManager::GetTotalDatabasesSize - Return err = %d", err); |
|
2954 return err; |
|
2955 } |
|
2956 |
|
2957 // --------------------------------------------------------------------------- |
|
2958 // CMPXDbManager::GetTotalRamDatabasesSize |
|
2959 // --------------------------------------------------------------------------- |
|
2960 // |
|
2961 TInt CMPXDbManager::GetTotalRamDatabasesSize(TInt& aSize) |
|
2962 { |
|
2963 MPX_FUNC("CMPXDbManager::GetTotalRamDatabasesSize"); |
|
2964 TInt err = KErrNotSupported; |
|
2965 #ifdef __RAMDISK_PERF_ENABLE |
|
2966 TInt size=0; |
|
2967 TInt count(iDatabaseHandles.Count()); |
|
2968 err = KErrNone; |
|
2969 for ( TInt i = 0; i < count ; ++i ) |
|
2970 { |
|
2971 // make sure this db is in ram drive. |
|
2972 if ( !iDatabaseHandles[i].iUseRAMdb ) |
|
2973 { |
|
2974 continue; |
|
2975 } |
|
2976 // Generate database name. |
|
2977 TFileName dbFilename; |
|
2978 dbFilename.Append(iRAMFolder); |
|
2979 TBuf<2> d; |
|
2980 TDriveUnit drive(iDatabaseHandles[i].iDrive); |
|
2981 d.Append(drive.Name()); |
|
2982 HBufC* temp = HBufC::NewLC(KMaxFileName); |
|
2983 temp->Des().Append(d.Left(1)); |
|
2984 temp->Des().Append(iDbFile->Des()); |
|
2985 TFileName filename; |
|
2986 filename.Format(KSecurePath, User::Identity().iUid, temp); |
|
2987 CleanupStack::PopAndDestroy(temp); |
|
2988 dbFilename.Append(filename); |
|
2989 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Database name = %S", &dbFilename); |
|
2990 TEntry entry; |
|
2991 err = iFs.Entry( dbFilename, entry ); |
|
2992 if ( (err != KErrNone) && (err != KErrNotFound) ) |
|
2993 { |
|
2994 break; |
|
2995 } |
|
2996 MPX_DEBUG3("CMPXDbManager::GetTotalRamDatabasesSize - Size of Db %S = %d", &dbFilename, entry.iSize); |
|
2997 // sum up size |
|
2998 size += entry.iSize; |
|
2999 } |
|
3000 aSize = size; |
|
3001 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Total Size of Dbs = %d", size); |
|
3002 #endif //__RAMDISK_PERF_ENABLE |
|
3003 MPX_DEBUG2("CMPXDbManager::GetTotalRamDatabasesSize - Return err = %d", err); |
|
3004 return err; |
|
3005 } |
|
3006 |
2052 // End of File |
3007 // End of File |