os/persistentdata/persistentstorage/dbms/ustor/US_RCOVR.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include "US_STD.H"
sl@0
    17
sl@0
    18
// Class CDbStoreIndex::CRecover
sl@0
    19
sl@0
    20
NONSHARABLE_CLASS(CDbStoreIndex::CRecover) : public CDbTableDatabase::CStepper
sl@0
    21
	{
sl@0
    22
private:
sl@0
    23
	enum {EClearStep=1};
sl@0
    24
public:
sl@0
    25
	CRecover(CDbStoreDatabase& aDatabase,const CDbStoreDef& aTable,const CDbStoreIndexDef& anIndex);
sl@0
    26
	~CRecover();
sl@0
    27
	static inline TInt Steps(TInt aCardinality);
sl@0
    28
private:
sl@0
    29
	TInt StepL(TInt aStep);
sl@0
    30
private:
sl@0
    31
	CDbStoreDatabase& iDatabase;
sl@0
    32
	const CDbStoreDef& iTable;
sl@0
    33
	const CDbStoreIndexDef& iIndex;
sl@0
    34
	CDbTableDatabase::CBuildIndex* iBuild;
sl@0
    35
	};
sl@0
    36
sl@0
    37
inline TInt CDbStoreIndex::CRecover::Steps(TInt aCardinality)
sl@0
    38
	{return CDbTableDatabase::CBuildIndex::Steps(aCardinality)+EClearStep;}
sl@0
    39
sl@0
    40
CDbStoreIndex::CRecover::CRecover(CDbStoreDatabase& aDatabase,const CDbStoreDef& aTable,const CDbStoreIndexDef& anIndex)
sl@0
    41
	: iDatabase(aDatabase),iTable(aTable),iIndex(anIndex)
sl@0
    42
	{}
sl@0
    43
sl@0
    44
CDbStoreIndex::CRecover::~CRecover()
sl@0
    45
	{
sl@0
    46
	delete iBuild;
sl@0
    47
	}
sl@0
    48
sl@0
    49
TInt CDbStoreIndex::CRecover::StepL(TInt aStep)
sl@0
    50
	{
sl@0
    51
	if (iBuild==NULL)
sl@0
    52
		{	// first step
sl@0
    53
		iBuild=CDbTableDatabase::CBuildIndex::NewL(iDatabase,iTable,iIndex);
sl@0
    54
		((CDbStoreIndex&)iBuild->Index()).DiscardL();
sl@0
    55
		return aStep-EClearStep;
sl@0
    56
		}
sl@0
    57
	aStep=iBuild->StepL(aStep);
sl@0
    58
	if (aStep==0)
sl@0
    59
		{	// final step
sl@0
    60
		iDatabase.IndexRecoveredL();
sl@0
    61
		delete iBuild;
sl@0
    62
		iBuild=NULL;
sl@0
    63
		}
sl@0
    64
	return aStep;
sl@0
    65
	}
sl@0
    66
sl@0
    67
// Class CDbStoreIndex::CRepair
sl@0
    68
sl@0
    69
NONSHARABLE_CLASS(CDbStoreIndex::CRepair) : public CDbTableDatabase::CStepper
sl@0
    70
	{
sl@0
    71
private:
sl@0
    72
	enum {ERepairStep=1};
sl@0
    73
public:
sl@0
    74
	CRepair(CDbStoreDatabase& aDatabase,const CDbStoreDef& aTable,const CDbStoreIndexDef& anIndex);
sl@0
    75
	static inline TInt Steps();
sl@0
    76
private:
sl@0
    77
	TInt StepL(TInt aStep);
sl@0
    78
private:
sl@0
    79
	CDbStoreDatabase& iDatabase;
sl@0
    80
	const CDbStoreDef& iTable;
sl@0
    81
	const CDbStoreIndexDef& iIndex;
sl@0
    82
	};
sl@0
    83
sl@0
    84
inline TInt CDbStoreIndex::CRepair::Steps()
sl@0
    85
	{return ERepairStep;}
sl@0
    86
sl@0
    87
CDbStoreIndex::CRepair::CRepair(CDbStoreDatabase& aDatabase,const CDbStoreDef& aTable,const CDbStoreIndexDef& anIndex)
sl@0
    88
	: iDatabase(aDatabase),iTable(aTable),iIndex(anIndex)
sl@0
    89
	{}
sl@0
    90
sl@0
    91
TInt CDbStoreIndex::CRepair::StepL(TInt)
sl@0
    92
	{
sl@0
    93
	CDbStoreIndex* index=CDbStoreIndex::NewL(iDatabase,iIndex,iTable);
sl@0
    94
	CleanupStack::PushL(index);
sl@0
    95
	index->OpenL();
sl@0
    96
	index->RepairL();
sl@0
    97
	CleanupStack::PopAndDestroy();
sl@0
    98
	return 0;
sl@0
    99
	}
sl@0
   100
sl@0
   101
// Class CDbStoreDatabase
sl@0
   102
sl@0
   103
CDbTableDatabase::CStepper* CDbStoreDatabase::RecoverL(TInt& aStep)
sl@0
   104
	{
sl@0
   105
	TSglQueIterC<CDbStoreDef> iter(SchemaL());	// ensure we have the schema
sl@0
   106
	TInt count=0;
sl@0
   107
	const CDbStoreDef* tDef;
sl@0
   108
	while ((tDef=iter++)!=NULL)
sl@0
   109
		count+=tDef->Indexes().Count();
sl@0
   110
	CMultiStepper* mStepper=CMultiStepper::NewL(count);
sl@0
   111
	CleanupStack::PushL(mStepper);
sl@0
   112
// damaged ones first (as they are invoked last
sl@0
   113
	count=0;
sl@0
   114
	iter.SetToFirst();
sl@0
   115
	while ((tDef=iter++)!=NULL)
sl@0
   116
		{
sl@0
   117
		TInt records=CDbStoreRecords::CardinalityL(Store(),*tDef);
sl@0
   118
		TSglQueIterC<CDbStoreIndexDef> xIter(tDef->Indexes().AsQue());
sl@0
   119
		for (const CDbStoreIndexDef* xDef;(xDef=xIter++)!=NULL;)
sl@0
   120
			{
sl@0
   121
			if (!CDbStoreIndex::IsDamagedL(*this,*xDef))
sl@0
   122
				continue;
sl@0
   123
			// rebuild from scratch
sl@0
   124
			CDbStoreIndex::CRecover* rebuild=new(ELeave) CDbStoreIndex::CRecover(*this,*tDef,*xDef);
sl@0
   125
			mStepper->AddStepper(rebuild,rebuild->Steps(records));
sl@0
   126
			++count;
sl@0
   127
			}
sl@0
   128
		}
sl@0
   129
	if (count==0)
sl@0
   130
		{	// no indexes are actually damaged
sl@0
   131
		aStep=0;
sl@0
   132
		CleanupStack::PopAndDestroy();
sl@0
   133
		return NULL;
sl@0
   134
		}
sl@0
   135
	// repair the other ones after a reclaim
sl@0
   136
	iter.SetToFirst();
sl@0
   137
	while ((tDef=iter++)!=NULL)
sl@0
   138
		{
sl@0
   139
		TSglQueIterC<CDbStoreIndexDef> xIter(tDef->Indexes().AsQue());
sl@0
   140
		for (const CDbStoreIndexDef* xDef;(xDef=xIter++)!=NULL;)
sl@0
   141
			{
sl@0
   142
			if (CDbStoreIndex::IsDamagedL(*this,*xDef))
sl@0
   143
				continue;
sl@0
   144
			// repair from sequence set
sl@0
   145
			CDbStoreIndex::CRepair* repair=new(ELeave) CDbStoreIndex::CRepair(*this,*tDef,*xDef);
sl@0
   146
			mStepper->AddStepper(repair,repair->Steps());
sl@0
   147
			}
sl@0
   148
		}
sl@0
   149
	iPagePool->Discard();
sl@0
   150
	iPagePool->ReclaimAllL();	// recover all page pool space
sl@0
   151
	aStep=mStepper->TotalSteps();
sl@0
   152
	CleanupStack::Pop();
sl@0
   153
	return mStepper;
sl@0
   154
	}