os/persistentdata/persistentstorage/dbms/ustor/US_INDEX.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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
#include "D32COMP.H"
sl@0
    18
sl@0
    19
// Class TDbStoreIndexStats::TBound
sl@0
    20
sl@0
    21
inline void TDbStoreIndexStats::TBound::Set(TReal64 aBound)
sl@0
    22
	{iValue=aBound;}
sl@0
    23
sl@0
    24
void TDbStoreIndexStats::TBound::Set(const TInt64& aBound)
sl@0
    25
	{iValue=I64REAL(aBound);}
sl@0
    26
sl@0
    27
void TDbStoreIndexStats::TBound::Set(const TText8* aPtr,TInt aLen,const TTextOps& aConv)
sl@0
    28
	{
sl@0
    29
	TInt64 v(0u);
sl@0
    30
	const TText8* const end=aPtr+aLen;
sl@0
    31
	TInt shift=48;
sl@0
    32
	do
sl@0
    33
		{
sl@0
    34
		if (aPtr==end)
sl@0
    35
			break;
sl@0
    36
		TInt64 t(aConv.Fold(*aPtr++));
sl@0
    37
		t<<=shift;
sl@0
    38
		v+=t;
sl@0
    39
		} while ((shift-=8)>=0);
sl@0
    40
	Set(v);
sl@0
    41
	}
sl@0
    42
sl@0
    43
void TDbStoreIndexStats::TBound::Set(const TText16* aPtr,TInt aLen,const TTextOps& aConv)
sl@0
    44
	{
sl@0
    45
	TInt64 v(0u);
sl@0
    46
	const TText16* const end=aPtr+aLen;
sl@0
    47
	TInt shift=32;
sl@0
    48
	do
sl@0
    49
		{
sl@0
    50
		if (aPtr==end)
sl@0
    51
			break;
sl@0
    52
		TInt64 t(aConv.Fold(*aPtr++));
sl@0
    53
		t<<=shift;
sl@0
    54
		v+=t;
sl@0
    55
		} while ((shift-=16)>=0);
sl@0
    56
	Set(v);
sl@0
    57
	}
sl@0
    58
sl@0
    59
void TDbStoreIndexStats::TBound::Set(const TDbLookupKey::SColumn& aBound,const TTextOps& aConv)
sl@0
    60
	{
sl@0
    61
	switch (aBound.iType)
sl@0
    62
		{
sl@0
    63
	default:
sl@0
    64
		__ASSERT(0);
sl@0
    65
	case EDbColInt64:
sl@0
    66
		Set(aBound.iInt64);
sl@0
    67
		break;
sl@0
    68
	case EDbColDateTime:
sl@0
    69
		Set(aBound.iTime().Int64());
sl@0
    70
		break;
sl@0
    71
	case EDbColReal64:
sl@0
    72
		Set(aBound.iReal64);
sl@0
    73
		break;
sl@0
    74
	case EDbColText8:
sl@0
    75
		Set(aBound.iDes8.iPtr,aBound.iDes8.iLength,aConv);
sl@0
    76
		break;
sl@0
    77
	case EDbColText16:
sl@0
    78
		Set(aBound.iDes16.iPtr,aBound.iDes16.iLength,aConv);
sl@0
    79
		break;
sl@0
    80
		}
sl@0
    81
	}
sl@0
    82
sl@0
    83
// Class TDbStoreIndexStats
sl@0
    84
sl@0
    85
inline TBool TDbStoreIndexStats::NeedsRefresh() const
sl@0
    86
	{
sl@0
    87
	return iRefresh<=ERefresh;
sl@0
    88
	}
sl@0
    89
sl@0
    90
inline TInt TDbStoreIndexStats::Cardinality() const
sl@0
    91
	{
sl@0
    92
	return iCardinality;
sl@0
    93
	}
sl@0
    94
sl@0
    95
// update the refresh count
sl@0
    96
inline void TDbStoreIndexStats::Touch()
sl@0
    97
	{
sl@0
    98
	TInt r=iRefresh-1;
sl@0
    99
	if (r>=ERefresh) 
sl@0
   100
		iRefresh=r;
sl@0
   101
	}
sl@0
   102
sl@0
   103
// an entry is inserted
sl@0
   104
inline void TDbStoreIndexStats::Inc()
sl@0
   105
	{
sl@0
   106
	++iCardinality;
sl@0
   107
	Touch();
sl@0
   108
	}
sl@0
   109
sl@0
   110
// an entry is removed
sl@0
   111
inline void TDbStoreIndexStats::Dec()
sl@0
   112
	{
sl@0
   113
	--iCardinality;
sl@0
   114
	Touch();
sl@0
   115
	}
sl@0
   116
sl@0
   117
// contents have changed radically. provoke an immediate refresh
sl@0
   118
inline void TDbStoreIndexStats::Reset(TInt aCardinality)
sl@0
   119
	{
sl@0
   120
	iCardinality=aCardinality;
sl@0
   121
	iRefresh=ERefresh;
sl@0
   122
	}
sl@0
   123
sl@0
   124
// stats have been refreshed. set for next update
sl@0
   125
inline void TDbStoreIndexStats::Refresh(TDbStoreIndexStats::TType aType)
sl@0
   126
	{
sl@0
   127
	iFlags=(iFlags&~EFlgDiscrete)|aType;
sl@0
   128
	iRefresh=(iCardinality>>ERefreshFactor)+1;
sl@0
   129
	}
sl@0
   130
sl@0
   131
//
sl@0
   132
// Internalize the index statistics
sl@0
   133
// This must handle a stream externalized by builds before 052
sl@0
   134
//
sl@0
   135
void TDbStoreIndexStats::InternalizeL(RReadStream& aStream)
sl@0
   136
	{
sl@0
   137
	TCardinality c;
sl@0
   138
	aStream>>c;
sl@0
   139
	iCardinality=c;
sl@0
   140
	TUint refresh=aStream.ReadUint32L();
sl@0
   141
	iFlags=refresh&EFlagsMask;
sl@0
   142
	iRefresh=TInt(refresh>>ERefreshShift)+EInvalid;
sl@0
   143
//
sl@0
   144
// pre-build-052 data would run out here
sl@0
   145
// if there is no more data, mark the (non-cardinality) stats as invalid
sl@0
   146
//
sl@0
   147
	TRAPD(r,aStream>>iLower.iValue>>iUpper.iValue;)
sl@0
   148
	if (r==KErrEof)
sl@0
   149
		iRefresh=EInvalid;		// mark as invalid data
sl@0
   150
	else
sl@0
   151
		__LEAVE_IF_ERROR(r);	// just an "ordinary" error
sl@0
   152
	}
sl@0
   153
sl@0
   154
//
sl@0
   155
// Notes: iCardinality cannot exceed 2^29 (due to storage mechanism)
sl@0
   156
//		thus iRefresh cannot exceed 2^(29-ERefreshFactor)+1
sl@0
   157
//		this leaves the 5 m.s.b. clear in iRefresh-EInvalid
sl@0
   158
//		the flags bits are stored there
sl@0
   159
//
sl@0
   160
void TDbStoreIndexStats::ExternalizeL(RWriteStream& aStream) const
sl@0
   161
	{
sl@0
   162
	__ASSERT((iFlags&~EFlagsMask)==0);
sl@0
   163
	aStream<<TCardinality(iCardinality);
sl@0
   164
	aStream<<TUint32((TUint(iRefresh-EInvalid)<<ERefreshShift)|iFlags);
sl@0
   165
	aStream<<iLower.iValue<<iUpper.iValue;
sl@0
   166
	}
sl@0
   167
sl@0
   168
//
sl@0
   169
// Reverse the span and bounds
sl@0
   170
//
sl@0
   171
TInt TDbStoreIndexStats::ReverseSpan(TUint aInclusion,const TDbLookupKey* aLower,const TDbLookupKey* aUpper,const TTextOps& aConv) const
sl@0
   172
	{
sl@0
   173
	__ASSERT(iLower.iValue>iUpper.iValue);
sl@0
   174
	TDbStoreIndexStats stats(*this);
sl@0
   175
	stats.iLower=iUpper;
sl@0
   176
	stats.iUpper=iLower;
sl@0
   177
	return stats.Span((aInclusion<<1)|(aInclusion>>1),aUpper,aLower,aConv);
sl@0
   178
	}
sl@0
   179
sl@0
   180
//
sl@0
   181
// Evaluate the probable proportion of the index set contained within the bounds
sl@0
   182
//
sl@0
   183
TInt TDbStoreIndexStats::Span(TUint aInclusion,const TDbLookupKey* aLower,const TDbLookupKey* aUpper,const TTextOps& aConv) const
sl@0
   184
	{
sl@0
   185
	if (!IsValid())
sl@0
   186
		return CDbTable::EUnavailableSpan;	// No valid index data
sl@0
   187
	if (iCardinality==0)
sl@0
   188
		return 0;					// no rows at all
sl@0
   189
//
sl@0
   190
	if (iLower.iValue>iUpper.iValue)
sl@0
   191
		return ReverseSpan(aInclusion,aLower,aUpper,aConv);	// descending index
sl@0
   192
//
sl@0
   193
	aInclusion&=CDbRecordIndex::EIncludeBoth;
sl@0
   194
	TBound l(iLower);
sl@0
   195
	if (aLower)
sl@0
   196
		{
sl@0
   197
		TBound bound;
sl@0
   198
		bound.Set(*aLower->First(),aConv);
sl@0
   199
		if (bound.iValue>=l.iValue)
sl@0
   200
			l=bound;
sl@0
   201
		else
sl@0
   202
			aInclusion|=CDbRecordIndex::EIncludeLower;
sl@0
   203
		}
sl@0
   204
	else
sl@0
   205
		aInclusion|=CDbRecordIndex::EIncludeLower;
sl@0
   206
//
sl@0
   207
	TBound h(iUpper);
sl@0
   208
	if (aUpper)
sl@0
   209
		{
sl@0
   210
		TBound bound;
sl@0
   211
		bound.Set(*aUpper->First(),aConv);
sl@0
   212
		if (bound.iValue<=h.iValue)
sl@0
   213
			h=bound;
sl@0
   214
		else
sl@0
   215
			aInclusion|=CDbRecordIndex::EIncludeUpper;
sl@0
   216
		}
sl@0
   217
	else
sl@0
   218
		aInclusion|=CDbRecordIndex::EIncludeUpper;
sl@0
   219
//
sl@0
   220
	TRealX restrict(h.iValue);
sl@0
   221
	restrict.SubEq(l.iValue);		// extended precision--no errors
sl@0
   222
	TRealX span(iUpper.iValue);
sl@0
   223
	span.SubEq(iLower.iValue);
sl@0
   224
	if (iFlags&EFlgDiscrete)
sl@0
   225
		{
sl@0
   226
		if (aInclusion==0)
sl@0
   227
			--restrict;
sl@0
   228
		else if (aInclusion==CDbRecordIndex::EIncludeBoth)
sl@0
   229
			++restrict;
sl@0
   230
		++span;
sl@0
   231
		}
sl@0
   232
	else if (restrict.IsZero())		// single value continuous range
sl@0
   233
		{
sl@0
   234
		return (span.IsZero() && aInclusion==CDbRecordIndex::EIncludeBoth)
sl@0
   235
			? CDbTable::EFullIndexSpan : 0;
sl@0
   236
		}
sl@0
   237
	if (restrict<=TRealX(0))
sl@0
   238
		return 0;	// no overlap
sl@0
   239
	restrict.DivEq(span);
sl@0
   240
	restrict.MultEq(TInt(CDbTable::EFullIndexSpan));
sl@0
   241
	return TInt(restrict);
sl@0
   242
	}
sl@0
   243
sl@0
   244
sl@0
   245
// Class CDbStoreIndex
sl@0
   246
sl@0
   247
inline CDbStoreIndex::HKey& CDbStoreIndex::Key() const
sl@0
   248
	{
sl@0
   249
	return *iKey;
sl@0
   250
	}
sl@0
   251
	
sl@0
   252
inline const TBtree& CDbStoreIndex::Tree() const
sl@0
   253
	{
sl@0
   254
	return iTree;
sl@0
   255
	}
sl@0
   256
	
sl@0
   257
inline TInt CDbStoreIndex::Count() const
sl@0
   258
	{
sl@0
   259
	return iStats.Cardinality();
sl@0
   260
	}
sl@0
   261
sl@0
   262
sl@0
   263
// Class CDbStoreIndex::HKey
sl@0
   264
sl@0
   265
NONSHARABLE_CLASS(CDbStoreIndex::HKey) : public MBtreeKey
sl@0
   266
	{
sl@0
   267
public:
sl@0
   268
	static HKey* NewL(const CDbKey& aKey,const HDbColumnSet& aColSet);
sl@0
   269
//
sl@0
   270
	inline TInt EntrySize() const;
sl@0
   271
	virtual TInt KeySize() const;
sl@0
   272
	virtual TBool IncompleteKey() const;
sl@0
   273
//
sl@0
   274
	TInt EntryL(TAny* aPtr,const RDbTableRow& aRow,TDbRecordId aRecord);
sl@0
   275
	TInt EntryL(TAny* aPtr,const TDbLookupKey& aKey);
sl@0
   276
	inline const TAny* Restriction() const;
sl@0
   277
	inline void Restrict(const TAny* aRestriction);
sl@0
   278
//
sl@0
   279
	void Bound(TDbStoreIndexStats::TBound& aBound,const TAny* aEntry);
sl@0
   280
	TDbStoreIndexStats::TType KeyType() const;
sl@0
   281
// from MBtreeKey
sl@0
   282
	const TAny* Key(const TAny* aRecord) const;
sl@0
   283
	TInt Compare(const TAny* aLeft,const TAny* aRight) const;
sl@0
   284
protected:
sl@0
   285
	HKey() {}
sl@0
   286
	inline TBool FullComparison() const;
sl@0
   287
// from MBtreeKey
sl@0
   288
	void Between(const TAny* aLeft,const TAny* aRight,TBtreePivot& aPivot) const;
sl@0
   289
private:
sl@0
   290
	struct SKeyCol
sl@0
   291
		{
sl@0
   292
		TDbColNo iOrdinal;
sl@0
   293
		TInt iSize;
sl@0
   294
		TUint8 iType;
sl@0
   295
		TUint8 iOrder;
sl@0
   296
		};
sl@0
   297
	const TTextOps* iTextOps;
sl@0
   298
	TInt iSize;
sl@0
   299
	const SKeyCol* iEndOfKeys;
sl@0
   300
	const SKeyCol* iRestrictedEndOfKeys;
sl@0
   301
	SKeyCol iKeys[1];	// one or more keys
sl@0
   302
	};
sl@0
   303
sl@0
   304
NONSHARABLE_CLASS(CDbStoreIndex::HDupKey) : public CDbStoreIndex::HKey
sl@0
   305
	{
sl@0
   306
	friend class CDbStoreIndex::HKey;
sl@0
   307
private:
sl@0
   308
	HDupKey() {}
sl@0
   309
	TInt KeySize() const;
sl@0
   310
	const TAny* Key(const TAny* aRecord) const;
sl@0
   311
	TInt Compare(const TAny* aLeft,const TAny* aRight) const;
sl@0
   312
	TBool IncompleteKey() const;
sl@0
   313
	};
sl@0
   314
sl@0
   315
inline TInt CDbStoreIndex::HKey::EntrySize() const
sl@0
   316
	{
sl@0
   317
	return iSize;
sl@0
   318
	}
sl@0
   319
	
sl@0
   320
inline const TAny* CDbStoreIndex::HKey::Restriction() const
sl@0
   321
	{
sl@0
   322
	return iRestrictedEndOfKeys;
sl@0
   323
	}
sl@0
   324
	
sl@0
   325
inline void CDbStoreIndex::HKey::Restrict(const TAny* aRestriction)
sl@0
   326
	{
sl@0
   327
	__ASSERT(aRestriction==0||(aRestriction>=iKeys&&aRestriction<=iEndOfKeys));
sl@0
   328
	iRestrictedEndOfKeys=(const SKeyCol*)aRestriction;
sl@0
   329
	}
sl@0
   330
	
sl@0
   331
inline TBool CDbStoreIndex::HKey::FullComparison() const
sl@0
   332
	{
sl@0
   333
	return iRestrictedEndOfKeys==NULL;
sl@0
   334
	}
sl@0
   335
sl@0
   336
//
sl@0
   337
// Construct the key based on the key definition (must be valid for the table)
sl@0
   338
// and the column set provided
sl@0
   339
//
sl@0
   340
CDbStoreIndex::HKey* CDbStoreIndex::HKey::NewL(const CDbKey& aKey,const HDbColumnSet& aColumns)
sl@0
   341
	{
sl@0
   342
	TInt count=aKey.Count();
sl@0
   343
	HKey* self=(HKey*)User::AllocLC(_FOFF(HKey,iKeys[count]));
sl@0
   344
	if (aKey.IsUnique())
sl@0
   345
		new(self) HKey;
sl@0
   346
	else
sl@0
   347
		new(self) HDupKey;
sl@0
   348
	self->iTextOps=&TTextOps::Ops(aKey.Comparison());
sl@0
   349
	self->iEndOfKeys=&self->iKeys[count];
sl@0
   350
	self->iRestrictedEndOfKeys=NULL;
sl@0
   351
	TInt len=sizeof(TDbRecordId);
sl@0
   352
	SKeyCol* pKey=&self->iKeys[0];
sl@0
   353
	for (TInt ii=0;ii<count;++pKey,++ii)
sl@0
   354
		{
sl@0
   355
		const TDbKeyCol& key=aKey[ii];
sl@0
   356
		pKey->iOrder=TUint8(key.iOrder);
sl@0
   357
		pKey->iOrdinal=aColumns.ColNoL(key.iName);
sl@0
   358
		if (pKey->iOrdinal==KDbNullColNo)
sl@0
   359
			__LEAVE(KErrCorrupt);
sl@0
   360
		const TDbColumnDef& col=aColumns[pKey->iOrdinal];
sl@0
   361
		pKey->iType=col.iType;
sl@0
   362
		pKey->iSize=CDbStoreIndexDef::KeySize(key,col);
sl@0
   363
		len+=Align4(pKey->iSize);
sl@0
   364
		}
sl@0
   365
	self->iSize=len;
sl@0
   366
	if (self->KeySize()>KMaxIndexKeySize)
sl@0
   367
		__LEAVE(KErrCorrupt);
sl@0
   368
	CleanupStack::Pop();
sl@0
   369
	return self;
sl@0
   370
	}
sl@0
   371
sl@0
   372
//
sl@0
   373
// Construct an entry at aPtr from the record given
sl@0
   374
//
sl@0
   375
TInt CDbStoreIndex::HKey::EntryL(TAny* aPtr,const RDbTableRow& aRow,TDbRecordId aRecord)
sl@0
   376
	{
sl@0
   377
	TUint8* ptr=(TUint8*)aPtr;
sl@0
   378
	Mem::FillZ(ptr,iSize);
sl@0
   379
	*REINTERPRET_CAST(TDbRecordId*,ptr)=aRecord;
sl@0
   380
	ptr+=sizeof(TDbRecordId);
sl@0
   381
	const SKeyCol* const end=iEndOfKeys;
sl@0
   382
	for (const SKeyCol* key=&iKeys[0];key<end;++key)
sl@0
   383
		{
sl@0
   384
		const TDbCell* cell=aRow.ColCell(key->iOrdinal);
sl@0
   385
		TInt size=key->iSize;
sl@0
   386
		if (cell->Length()!=0)
sl@0
   387
			{
sl@0
   388
#ifdef __DOUBLE_WORDS_SWAPPED__
sl@0
   389
			if (key->iType==EDbColReal64)
sl@0
   390
				{
sl@0
   391
				const TUint32* data=(TUint32*)cell->Data();
sl@0
   392
				((TUint32*)ptr)[0]=data[1];
sl@0
   393
				((TUint32*)ptr)[1]=data[0];
sl@0
   394
				}
sl@0
   395
			else
sl@0
   396
#endif
sl@0
   397
			if (TDbCol::IsLong(TDbColType(key->iType)))
sl@0
   398
				{
sl@0
   399
				const TDbBlob& blob=*(const TDbBlob*)cell->Data();
sl@0
   400
				if (blob.IsInline())
sl@0
   401
					Mem::Copy(ptr,blob.Data(),Min(size,blob.Size()));
sl@0
   402
				else
sl@0
   403
					{
sl@0
   404
					aRow.Table().BlobsL()->ReadLC(blob.Id(),TDbColType(key->iType))->ReadL(ptr,size);
sl@0
   405
					CleanupStack::PopAndDestroy();	// stream buffer
sl@0
   406
					}
sl@0
   407
				}
sl@0
   408
			else
sl@0
   409
				Mem::Copy(ptr,cell->Data(),Min(size,cell->Length()));
sl@0
   410
			}
sl@0
   411
		ptr+=Align4(size);
sl@0
   412
		}
sl@0
   413
	iRestrictedEndOfKeys=NULL;		// use the full key for comparison
sl@0
   414
	return EntrySize();
sl@0
   415
	}
sl@0
   416
sl@0
   417
//
sl@0
   418
// Construct an entry from a lookup key
sl@0
   419
//
sl@0
   420
TInt CDbStoreIndex::HKey::EntryL(TAny* aPtr,const TDbLookupKey& aKey)
sl@0
   421
	{
sl@0
   422
	TUint8* ptr=(TUint8*)aPtr;
sl@0
   423
	Mem::FillZ(ptr,iSize);
sl@0
   424
	ptr+=sizeof(TDbRecordId);
sl@0
   425
	const TDbLookupKey::SColumn* lkey=aKey.First();
sl@0
   426
	const TDbLookupKey::SColumn* const lend=lkey+aKey.Count();
sl@0
   427
	__ASSERT(lkey<lend);
sl@0
   428
	const SKeyCol* const end=iEndOfKeys;
sl@0
   429
	const SKeyCol* key=&iKeys[0];
sl@0
   430
	for (;;)
sl@0
   431
		{
sl@0
   432
		TDbColType ltype=lkey->iType;
sl@0
   433
		TInt size=key->iSize;
sl@0
   434
		switch (key->iType)
sl@0
   435
			{
sl@0
   436
		default:
sl@0
   437
			__ASSERT(0);
sl@0
   438
		case EDbColBit:
sl@0
   439
		case EDbColUint8:
sl@0
   440
		case EDbColUint16:
sl@0
   441
		case EDbColUint32:
sl@0
   442
			if (ltype==EDbColUint32)
sl@0
   443
				{
sl@0
   444
				*(TUint32*)ptr=lkey->iUint32;
sl@0
   445
				break;
sl@0
   446
				}
sl@0
   447
			else if (ltype==EDbColInt32)
sl@0
   448
				{
sl@0
   449
				if (lkey->iInt32>=0)	// domain check, unsigned value
sl@0
   450
					{
sl@0
   451
					*(TUint32*)ptr=lkey->iInt32;
sl@0
   452
					break;
sl@0
   453
					}
sl@0
   454
				}
sl@0
   455
			else if (ltype==EDbColInt64)
sl@0
   456
				{
sl@0
   457
				const TInt64& v=lkey->iInt64;
sl@0
   458
				if (I64HIGH(v)==0)		// domain check, in unsigned 32-bit range
sl@0
   459
					{
sl@0
   460
					*(TUint32*)ptr=I64LOW(v);
sl@0
   461
					break;
sl@0
   462
					}
sl@0
   463
				}
sl@0
   464
			Panic(EDbWrongType);
sl@0
   465
			break;
sl@0
   466
		case EDbColInt8:
sl@0
   467
		case EDbColInt16:
sl@0
   468
		case EDbColInt32:
sl@0
   469
			if (ltype==EDbColInt32)
sl@0
   470
				{
sl@0
   471
				*(TInt32*)ptr=lkey->iInt32;
sl@0
   472
				break;
sl@0
   473
				}
sl@0
   474
			else if (ltype==EDbColUint32)
sl@0
   475
				{
sl@0
   476
				if (lkey->iUint32<=TUint(KMaxTInt))	// domain check, in signed range
sl@0
   477
					{
sl@0
   478
					*(TInt32*)ptr=lkey->iUint32;
sl@0
   479
					break;
sl@0
   480
					}
sl@0
   481
				}
sl@0
   482
			else if (ltype==EDbColInt64)
sl@0
   483
				{
sl@0
   484
				const TInt64& v=lkey->iInt64;
sl@0
   485
				TInt32 l=I64LOW(v);
sl@0
   486
				TInt32 h=I64HIGH(v);
sl@0
   487
				if (h==(l>>31))	// domain check, in signed 32-bit range
sl@0
   488
					{
sl@0
   489
					*(TInt32*)ptr=l;
sl@0
   490
					break;
sl@0
   491
					}
sl@0
   492
				}
sl@0
   493
			Panic(EDbWrongType);
sl@0
   494
			break;
sl@0
   495
		case EDbColReal32:
sl@0
   496
			if (ltype==EDbColReal32)
sl@0
   497
				{
sl@0
   498
				*(TReal32*)ptr=lkey->iReal32;
sl@0
   499
				break;
sl@0
   500
				}
sl@0
   501
			else if (ltype==EDbColReal64)
sl@0
   502
				{	// convert to TReal32, storing +-#inf if reqd.
sl@0
   503
				TRealX(lkey->iReal64).GetTReal(*(TReal32*)ptr);
sl@0
   504
				break;
sl@0
   505
				}
sl@0
   506
			Panic(EDbWrongType);
sl@0
   507
			break;
sl@0
   508
		case EDbColReal64:
sl@0
   509
#ifdef __DOUBLE_WORDS_SWAPPED__
sl@0
   510
			if (ltype==EDbColReal64)
sl@0
   511
				{
sl@0
   512
				const TUint32* data=(TUint32*)&lkey->iReal64;
sl@0
   513
				((TUint32*)ptr)[0]=data[1];
sl@0
   514
				((TUint32*)ptr)[1]=data[0];
sl@0
   515
				break;
sl@0
   516
				}
sl@0
   517
			// drop through
sl@0
   518
#endif
sl@0
   519
		case EDbColInt64:
sl@0
   520
		case EDbColDateTime:
sl@0
   521
			if (ltype==key->iType)
sl@0
   522
				Mem::Copy(ptr,&lkey->iInt64,size);	// all at same address
sl@0
   523
			else
sl@0
   524
				Panic(EDbWrongType);
sl@0
   525
			break;
sl@0
   526
		case EDbColText8:
sl@0
   527
		case EDbColLongText8:
sl@0
   528
			if (ltype==EDbColText8)
sl@0
   529
				Mem::Copy(ptr,lkey->iDes8.iPtr,Min(size,lkey->iDes8.iLength));
sl@0
   530
			else
sl@0
   531
				Panic(EDbWrongType);
sl@0
   532
			break;
sl@0
   533
		case EDbColText16:
sl@0
   534
		case EDbColLongText16:
sl@0
   535
			if (ltype==EDbColText16)
sl@0
   536
				Mem::Copy(ptr,lkey->iDes16.iPtr,Min(size,lkey->iDes16.iLength<<1));
sl@0
   537
			else
sl@0
   538
				Panic(EDbWrongType);
sl@0
   539
			break;
sl@0
   540
			}
sl@0
   541
		++key;
sl@0
   542
		if (++lkey==lend)
sl@0
   543
			break;							// end of lookup key
sl@0
   544
		if (key==end)
sl@0
   545
			__LEAVE(KErrArgument);		// too many keys
sl@0
   546
		ptr+=Align4(size);
sl@0
   547
		}
sl@0
   548
	iRestrictedEndOfKeys=key;				// use only the keys in the lookup for comparison
sl@0
   549
	return EntrySize();
sl@0
   550
	}
sl@0
   551
sl@0
   552
void CDbStoreIndex::HKey::Bound(TDbStoreIndexStats::TBound& aBound,const TAny* aEntry)
sl@0
   553
	{
sl@0
   554
	aEntry=PtrAdd(aEntry,sizeof(TDbRecordId));	// get to real key data
sl@0
   555
	switch (iKeys[0].iType)
sl@0
   556
		{
sl@0
   557
	default:
sl@0
   558
		__ASSERT(0);
sl@0
   559
	case EDbColBit:
sl@0
   560
	case EDbColUint8:
sl@0
   561
	case EDbColUint16:
sl@0
   562
	case EDbColUint32:
sl@0
   563
		aBound.Set(TInt64(TUint(*STATIC_CAST(const TUint32*,aEntry))));
sl@0
   564
		break;
sl@0
   565
	case EDbColInt8:
sl@0
   566
	case EDbColInt16:
sl@0
   567
	case EDbColInt32:
sl@0
   568
		aBound.Set(TInt64(TInt(*STATIC_CAST(const TInt32*,aEntry))));
sl@0
   569
		break;
sl@0
   570
	case EDbColInt64:
sl@0
   571
		aBound.Set(*STATIC_CAST(const TInt64*,aEntry));
sl@0
   572
		break;
sl@0
   573
	case EDbColDateTime:
sl@0
   574
		aBound.Set(STATIC_CAST(const TTime*,aEntry)->Int64());
sl@0
   575
		break;
sl@0
   576
	case EDbColReal32:
sl@0
   577
		aBound.Set(TReal64(*STATIC_CAST(const TReal32*,aEntry)));
sl@0
   578
		break;
sl@0
   579
	case EDbColReal64:
sl@0
   580
#if !defined(__DOUBLE_WORDS_SWAPPED__)
sl@0
   581
		aBound.Set(*STATIC_CAST(const TReal64*,aEntry));
sl@0
   582
#else
sl@0
   583
		{
sl@0
   584
		TReal64 xKey;
sl@0
   585
		((TUint32*)&xKey)[0]=STATIC_CAST(const TUint32*,aEntry)[1];
sl@0
   586
		((TUint32*)&xKey)[1]=STATIC_CAST(const TUint32*,aEntry)[0];
sl@0
   587
		aBound.Set(xKey);
sl@0
   588
		}
sl@0
   589
#endif
sl@0
   590
		break;
sl@0
   591
	case EDbColText8:
sl@0
   592
	case EDbColLongText8:
sl@0
   593
		aBound.Set(STATIC_CAST(const TUint8*,aEntry),iKeys[0].iSize,*iTextOps);
sl@0
   594
		break;
sl@0
   595
	case EDbColText16:
sl@0
   596
	case EDbColLongText16:
sl@0
   597
		aBound.Set(STATIC_CAST(const TUint16*,aEntry),iKeys[0].iSize>>1,*iTextOps);
sl@0
   598
		break;
sl@0
   599
		}
sl@0
   600
	}
sl@0
   601
sl@0
   602
// Is the index key discrete or continous?
sl@0
   603
inline TDbStoreIndexStats::TType CDbStoreIndex::HKey::KeyType() const
sl@0
   604
	{
sl@0
   605
	return iKeys[0].iType==EDbColReal32 || iKeys[0].iType==EDbColReal64
sl@0
   606
		? TDbStoreIndexStats::EContinuous : TDbStoreIndexStats::EDiscrete;
sl@0
   607
	}
sl@0
   608
sl@0
   609
TInt CDbStoreIndex::HKey::KeySize() const
sl@0
   610
	{
sl@0
   611
	return EntrySize()-sizeof(TDbRecordId);
sl@0
   612
	}
sl@0
   613
sl@0
   614
//
sl@0
   615
// Report 'true' if the lookup key is not the entire B+tree key
sl@0
   616
// For a unique index this is if there is a restriction to less than the full key
sl@0
   617
//
sl@0
   618
TBool CDbStoreIndex::HKey::IncompleteKey() const
sl@0
   619
	{
sl@0
   620
	return iRestrictedEndOfKeys!=0 && iRestrictedEndOfKeys!=iEndOfKeys;
sl@0
   621
	}
sl@0
   622
sl@0
   623
//
sl@0
   624
// For unique keys, key is after record id
sl@0
   625
//
sl@0
   626
const TAny* CDbStoreIndex::HKey::Key(const TAny* aRecord) const
sl@0
   627
	{
sl@0
   628
	return PtrAdd(aRecord,sizeof(TDbRecordId));
sl@0
   629
	}
sl@0
   630
sl@0
   631
//
sl@0
   632
// compare the key part of the entry
sl@0
   633
//
sl@0
   634
TInt CDbStoreIndex::HKey::Compare(const TAny* aLeft,const TAny* aRight) const
sl@0
   635
	{
sl@0
   636
	const SKeyCol* end=iRestrictedEndOfKeys;
sl@0
   637
	if (end==NULL)
sl@0
   638
		end=iEndOfKeys;
sl@0
   639
	const SKeyCol* key=&iKeys[0];
sl@0
   640
	for (;;)
sl@0
   641
		{
sl@0
   642
		TInt size=key->iSize;
sl@0
   643
		TInt rr;
sl@0
   644
		switch (key->iType)
sl@0
   645
			{
sl@0
   646
		default:
sl@0
   647
			__ASSERT(0);
sl@0
   648
		case EDbColBit:
sl@0
   649
		case EDbColUint8:
sl@0
   650
		case EDbColUint16:
sl@0
   651
		case EDbColUint32:
sl@0
   652
			rr=Comp::Compare(*STATIC_CAST(const TUint32*,aLeft),*STATIC_CAST(const TUint32*,aRight));
sl@0
   653
			break;
sl@0
   654
		case EDbColInt8:
sl@0
   655
		case EDbColInt16:
sl@0
   656
		case EDbColInt32:
sl@0
   657
			rr=Comp::Compare(*STATIC_CAST(const TInt32*,aLeft),*STATIC_CAST(const TInt32*,aRight));
sl@0
   658
			break;
sl@0
   659
		case EDbColInt64:
sl@0
   660
			rr=Comp::Compare(*STATIC_CAST(const TInt64*,aLeft),*STATIC_CAST(const TInt64*,aRight));
sl@0
   661
			break;
sl@0
   662
		case EDbColDateTime:
sl@0
   663
			rr=Comp::Compare(*STATIC_CAST(const TTime*,aLeft),*STATIC_CAST(const TTime*,aRight));
sl@0
   664
			break;
sl@0
   665
		case EDbColReal32:
sl@0
   666
			rr=Comp::Compare(*STATIC_CAST(const TReal32*,aLeft),*STATIC_CAST(const TReal32*,aRight));
sl@0
   667
			break;
sl@0
   668
		case EDbColReal64:
sl@0
   669
#if !defined(__DOUBLE_WORDS_SWAPPED__)
sl@0
   670
			rr=Comp::Compare(*STATIC_CAST(const TReal64*,aLeft),*STATIC_CAST(const TReal64*,aRight));
sl@0
   671
#else
sl@0
   672
			TReal64 xLeft;
sl@0
   673
			((TUint32*)&xLeft)[0]=STATIC_CAST(const TUint32*,aLeft)[1];
sl@0
   674
			((TUint32*)&xLeft)[1]=STATIC_CAST(const TUint32*,aLeft)[0];
sl@0
   675
			TReal64 xRight;
sl@0
   676
			((TUint32*)&xRight)[0]=STATIC_CAST(const TUint32*,aRight)[1];
sl@0
   677
			((TUint32*)&xRight)[1]=STATIC_CAST(const TUint32*,aRight)[0];
sl@0
   678
			rr=Comp::Compare(xLeft,xRight);
sl@0
   679
#endif
sl@0
   680
			break;
sl@0
   681
		case EDbColText8:
sl@0
   682
		case EDbColLongText8:
sl@0
   683
			rr=iTextOps->Compare(STATIC_CAST(const TUint8*,aLeft),size,STATIC_CAST(const TUint8*,aRight),size);
sl@0
   684
			break;
sl@0
   685
		case EDbColText16:
sl@0
   686
		case EDbColLongText16:
sl@0
   687
			rr=iTextOps->Order(STATIC_CAST(const TUint16*,aLeft),size>>1,STATIC_CAST(const TUint16*,aRight),size>>1);
sl@0
   688
			break;
sl@0
   689
			}
sl@0
   690
		if (rr!=0)
sl@0
   691
			return key->iOrder==TDbKeyCol::EAsc ? rr : -rr;
sl@0
   692
		if (++key==end)
sl@0
   693
			return rr;
sl@0
   694
		size=Align4(size);
sl@0
   695
		aLeft=PtrAdd(aLeft,size);
sl@0
   696
		aRight=PtrAdd(aRight,size);
sl@0
   697
		}
sl@0
   698
	}
sl@0
   699
sl@0
   700
//
sl@0
   701
// No clever stuff yet
sl@0
   702
//
sl@0
   703
void CDbStoreIndex::HKey::Between(const TAny* aLeft,const TAny* /*aRight*/,TBtreePivot& aPivot) const
sl@0
   704
	{
sl@0
   705
	aPivot.Copy((const TUint8*)aLeft,KeySize());
sl@0
   706
	}
sl@0
   707
sl@0
   708
// Class CDbStoreIndex::HDupKey
sl@0
   709
sl@0
   710
TInt CDbStoreIndex::HDupKey::KeySize() const
sl@0
   711
	{
sl@0
   712
	return EntrySize();
sl@0
   713
	}
sl@0
   714
sl@0
   715
//
sl@0
   716
// Report 'true' if the lookup key is not the entire B+tree key
sl@0
   717
// For a duplicates index this is if there is a restriction (as record id is ingored)
sl@0
   718
//
sl@0
   719
TBool CDbStoreIndex::HDupKey::IncompleteKey() const
sl@0
   720
	{
sl@0
   721
	return Restriction()!=0;
sl@0
   722
	}
sl@0
   723
sl@0
   724
//
sl@0
   725
// The key includes the record id
sl@0
   726
//
sl@0
   727
const TAny* CDbStoreIndex::HDupKey::Key(const TAny* aRecord) const
sl@0
   728
	{
sl@0
   729
	return aRecord;
sl@0
   730
	}
sl@0
   731
sl@0
   732
TInt CDbStoreIndex::HDupKey::Compare(const TAny* aLeft,const TAny* aRight) const
sl@0
   733
	{
sl@0
   734
	const TDbRecordId* const left=(const TDbRecordId*)aLeft;
sl@0
   735
	const TDbRecordId* const right=(const TDbRecordId*)aRight;
sl@0
   736
	TInt rr=HKey::Compare(left+1,right+1);
sl@0
   737
	if (rr==0 && FullComparison())
sl@0
   738
		return Comp::Compare(left->Value(),right->Value());
sl@0
   739
	return rr;
sl@0
   740
	}
sl@0
   741
sl@0
   742
// Class CDbStoreIndex::CIter
sl@0
   743
sl@0
   744
NONSHARABLE_CLASS(CDbStoreIndex::CIter) : public CDbRecordIter
sl@0
   745
	{
sl@0
   746
public:
sl@0
   747
	static CIter* NewL(CDbStoreIndex& aIndex,TUint aInclusion,const TDbLookupKey* aLowerBound,const TDbLookupKey* aUpperBound);
sl@0
   748
private:
sl@0
   749
	inline CIter(CDbStoreIndex& aIndex);
sl@0
   750
	~CIter();
sl@0
   751
	void ConstructL(TUint aInclusion,const TDbLookupKey* aLowerBound,const TDbLookupKey* aUpperBound);
sl@0
   752
	TAny* BoundL(const TDbLookupKey& aBound,const TAny*& aRestriction);
sl@0
   753
//
sl@0
   754
	inline const CDbStoreIndex& Index() const;
sl@0
   755
	TBool FindL(TDbRecordId aRecordId,const RDbTableRow& aRow,TBtree::TFind aFind);
sl@0
   756
	TInt CompareL(const TAny* aBound,const TAny* aRestriction);
sl@0
   757
	TBool _GotoL(TDbPosition aPosition);
sl@0
   758
//
sl@0
   759
	TInt Count() const;
sl@0
   760
	TDbRecordId CurrentL();
sl@0
   761
	TBool GotoL(TDbPosition aPosition);
sl@0
   762
	TBool GotoL(TDbRecordId aRecordId,RDbTableRow& aBuffer);
sl@0
   763
	TBool SeekL(const TDbLookupKey& aKey,RDbTable::TComparison aComparison);
sl@0
   764
	TDeleted DoDeletedL(TDbPosition aPosition,TDbRecordId aRecordId,const RDbTableRow* aRow);
sl@0
   765
private:
sl@0
   766
	TBtreePos iPos;
sl@0
   767
	TUint8 iLowerSeek;
sl@0
   768
	TUint8 iLowerCheck;
sl@0
   769
	TUint8 iUpperSeek;
sl@0
   770
	TUint8 iUpperCheck;
sl@0
   771
	TAny* iLowerBound;
sl@0
   772
	const TAny* iLowerRestriction;
sl@0
   773
	TAny* iUpperBound;
sl@0
   774
	const TAny* iUpperRestriction;
sl@0
   775
	};
sl@0
   776
sl@0
   777
inline CDbStoreIndex::CIter::CIter(CDbStoreIndex& aIndex) :
sl@0
   778
	CDbRecordIter(aIndex)
sl@0
   779
	{
sl@0
   780
	}
sl@0
   781
	
sl@0
   782
inline const CDbStoreIndex& CDbStoreIndex::CIter::Index() const
sl@0
   783
	{
sl@0
   784
	return STATIC_CAST(CDbStoreIndex&,Host());
sl@0
   785
	}
sl@0
   786
sl@0
   787
CDbStoreIndex::CIter* CDbStoreIndex::CIter::NewL(CDbStoreIndex& aIndex,TUint aInclusion,const TDbLookupKey* aLowerBound,const TDbLookupKey* aUpperBound)
sl@0
   788
	{
sl@0
   789
	CIter* self=new(ELeave) CIter(aIndex);
sl@0
   790
	CleanupStack::PushL(self);
sl@0
   791
	self->ConstructL(aInclusion,aLowerBound,aUpperBound);
sl@0
   792
	CleanupStack::Pop();
sl@0
   793
	return self;
sl@0
   794
	}
sl@0
   795
sl@0
   796
CDbStoreIndex::CIter::~CIter()
sl@0
   797
	{
sl@0
   798
	User::Free(iLowerBound);
sl@0
   799
	User::Free(iUpperBound);
sl@0
   800
	}
sl@0
   801
sl@0
   802
void CDbStoreIndex::CIter::ConstructL(TUint aInclusion,const TDbLookupKey* aLowerBound,const TDbLookupKey* aUpperBound)
sl@0
   803
	{
sl@0
   804
	if (aLowerBound)
sl@0
   805
		{
sl@0
   806
		TBtree::TFind seek=TBtree::EGreaterEqual;
sl@0
   807
		if ((aInclusion&CDbRecordIndex::EIncludeLower)==0)
sl@0
   808
			{
sl@0
   809
			seek=TBtree::EGreaterThan;
sl@0
   810
			iLowerCheck=1;
sl@0
   811
			}
sl@0
   812
		iLowerSeek=TUint8(seek);
sl@0
   813
		iLowerBound=BoundL(*aLowerBound,iLowerRestriction);
sl@0
   814
		}
sl@0
   815
	if (aUpperBound)
sl@0
   816
		{
sl@0
   817
		TBtree::TFind seek=TBtree::ELessThan;
sl@0
   818
		if (aInclusion&CDbRecordIndex::EIncludeUpper)
sl@0
   819
			{
sl@0
   820
			seek=TBtree::ELessEqual;
sl@0
   821
			iUpperCheck=1;
sl@0
   822
			}
sl@0
   823
		iUpperSeek=TUint8(seek);
sl@0
   824
		iUpperBound=BoundL(*aUpperBound,iUpperRestriction);
sl@0
   825
		}
sl@0
   826
	}
sl@0
   827
sl@0
   828
//
sl@0
   829
// Construct and allocate a key for the boundary value
sl@0
   830
//
sl@0
   831
TAny* CDbStoreIndex::CIter::BoundL(const TDbLookupKey& aBound,const TAny*& aRestriction)
sl@0
   832
	{
sl@0
   833
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
   834
	HKey& key=Index().Key();
sl@0
   835
	TInt size=key.EntryL(entry,aBound);
sl@0
   836
	const TUint8* ekey=(const TUint8*)key.Key(entry);
sl@0
   837
	size-=ekey-entry;
sl@0
   838
	TAny* e=User::AllocL(size);
sl@0
   839
	Mem::Copy(e,ekey,size);
sl@0
   840
	aRestriction=key.Restriction();
sl@0
   841
	return e;
sl@0
   842
	}
sl@0
   843
sl@0
   844
//
sl@0
   845
// Extract the current key and compare it with a boundary key
sl@0
   846
//
sl@0
   847
TInt CDbStoreIndex::CIter::CompareL(const TAny* aBound,const TAny* aRestriction)
sl@0
   848
	{
sl@0
   849
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
   850
	HKey& key=Index().Key();
sl@0
   851
	key.Restrict(aRestriction);
sl@0
   852
	Index().Tree().ExtractAtL(iPos,entry,key.EntrySize());
sl@0
   853
	return key.Compare(key.Key(entry),aBound);
sl@0
   854
	}
sl@0
   855
sl@0
   856
//
sl@0
   857
// return the cardinality of the index
sl@0
   858
//
sl@0
   859
TInt CDbStoreIndex::CIter::Count() const
sl@0
   860
	{
sl@0
   861
	if (iLowerBound)
sl@0
   862
		return KDbUndefinedCount;
sl@0
   863
	if (iUpperBound)
sl@0
   864
		return KDbUndefinedCount;
sl@0
   865
	return Index().Count();
sl@0
   866
	}
sl@0
   867
sl@0
   868
//
sl@0
   869
// return the current record id
sl@0
   870
//
sl@0
   871
TDbRecordId CDbStoreIndex::CIter::CurrentL()
sl@0
   872
	{
sl@0
   873
	TDbRecordId id;
sl@0
   874
	Index().Tree().ExtractAtL(iPos,&id,sizeof(id));
sl@0
   875
	return id;
sl@0
   876
	}
sl@0
   877
sl@0
   878
//
sl@0
   879
// iterate to the required position, does not test the boundary condition
sl@0
   880
//
sl@0
   881
TBool CDbStoreIndex::CIter::_GotoL(TDbPosition aPosition)
sl@0
   882
	{
sl@0
   883
	const TBtree& tree=Index().Tree();
sl@0
   884
	switch (aPosition)
sl@0
   885
		{
sl@0
   886
	default:	// all control paths return a value
sl@0
   887
		__ASSERT(0);
sl@0
   888
	case EDbNext:
sl@0
   889
		return tree.NextL(iPos);
sl@0
   890
	case EDbPrevious:
sl@0
   891
		return tree.PreviousL(iPos);
sl@0
   892
	case EDbFirst:
sl@0
   893
		if (!iLowerBound)
sl@0
   894
			return tree.FirstL(iPos);
sl@0
   895
		Index().Key().Restrict(iLowerRestriction);
sl@0
   896
		return tree.FindL(iPos,iLowerBound,TBtree::TFind(iLowerSeek));
sl@0
   897
	case EDbLast:
sl@0
   898
		if (!iUpperBound)
sl@0
   899
			return tree.LastL(iPos);
sl@0
   900
		Index().Key().Restrict(iUpperRestriction);
sl@0
   901
		return tree.FindL(iPos,iUpperBound,TBtree::TFind(iUpperSeek));
sl@0
   902
		}
sl@0
   903
	}
sl@0
   904
sl@0
   905
//
sl@0
   906
// iterate to the required position and check that it is in bounds
sl@0
   907
//
sl@0
   908
TBool CDbStoreIndex::CIter::GotoL(TDbPosition aPosition)
sl@0
   909
	{
sl@0
   910
	TBool r=_GotoL(aPosition);
sl@0
   911
	if (r)
sl@0
   912
		{
sl@0
   913
		if (aPosition==EDbFirst || aPosition==EDbNext)
sl@0
   914
			{
sl@0
   915
			if (iUpperBound)
sl@0
   916
				return CompareL(iUpperBound,iUpperRestriction)-iUpperCheck<0;
sl@0
   917
			}
sl@0
   918
		else
sl@0
   919
			{
sl@0
   920
			if (iLowerBound)
sl@0
   921
				return CompareL(iLowerBound,iLowerRestriction)-iLowerCheck>=0;
sl@0
   922
			}
sl@0
   923
		}
sl@0
   924
	return r;
sl@0
   925
	}
sl@0
   926
sl@0
   927
//
sl@0
   928
// Construct the Btree key for the row and lookup
sl@0
   929
//
sl@0
   930
TBool CDbStoreIndex::CIter::FindL(TDbRecordId aRecordId,const RDbTableRow& aRow,TBtree::TFind aFind)
sl@0
   931
	{
sl@0
   932
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
   933
	HKey& key=Index().Key();
sl@0
   934
	key.EntryL(entry,aRow,aRecordId);
sl@0
   935
	return Index().Tree().FindL(iPos,key.Key(entry),aFind);
sl@0
   936
	}
sl@0
   937
sl@0
   938
//
sl@0
   939
// Go directly to a row
sl@0
   940
//
sl@0
   941
TBool CDbStoreIndex::CIter::GotoL(TDbRecordId aRecordId,RDbTableRow& aRow)
sl@0
   942
	{
sl@0
   943
	aRow.ReadL(aRecordId);
sl@0
   944
	return FindL(aRecordId,aRow,TBtree::EEqualTo);
sl@0
   945
	}
sl@0
   946
sl@0
   947
//
sl@0
   948
// Do a keyed lookup in the index
sl@0
   949
//
sl@0
   950
TBool CDbStoreIndex::CIter::SeekL(const TDbLookupKey& aKey,RDbTable::TComparison aComparison)
sl@0
   951
	{
sl@0
   952
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
   953
	HKey& key=Index().Key();
sl@0
   954
	key.EntryL(entry,aKey);
sl@0
   955
	const TAny* ekey=key.Key(entry);
sl@0
   956
	TBtree::TFind find;
sl@0
   957
	switch (aComparison)
sl@0
   958
		{
sl@0
   959
	default:
sl@0
   960
		__ASSERT(0);
sl@0
   961
	case RDbTable::ELessThan:
sl@0
   962
		find=TBtree::ELessThan;
sl@0
   963
		break;
sl@0
   964
	case RDbTable::ELessEqual:
sl@0
   965
		find=TBtree::ELessEqual;
sl@0
   966
		break;
sl@0
   967
	case RDbTable::EEqualTo:
sl@0
   968
		if (key.IncompleteKey())
sl@0
   969
			{
sl@0
   970
			// The B+tree search code cannot correctly do a == search when the
sl@0
   971
			// comparison key is not complete. Instead we do a >= search and then
sl@0
   972
			// check the returned entry does match
sl@0
   973
			//
sl@0
   974
			if (!Index().Tree().FindL(iPos,ekey,TBtree::EGreaterEqual))
sl@0
   975
				return EFalse;	// off the end
sl@0
   976
			return CompareL(ekey,key.Restriction())==0;
sl@0
   977
			}
sl@0
   978
		find=TBtree::EEqualTo;
sl@0
   979
		break;
sl@0
   980
	case RDbTable::EGreaterEqual:
sl@0
   981
		find=TBtree::EGreaterEqual;
sl@0
   982
		break;
sl@0
   983
	case RDbTable::EGreaterThan:
sl@0
   984
		find=TBtree::EGreaterThan;
sl@0
   985
		break;
sl@0
   986
		}
sl@0
   987
	return Index().Tree().FindL(iPos,ekey,find);
sl@0
   988
	}
sl@0
   989
sl@0
   990
//
sl@0
   991
// Set the iterator following a record deletion
sl@0
   992
//
sl@0
   993
CDbStoreIndex::CIter::TDeleted CDbStoreIndex::CIter::DoDeletedL(TDbPosition aPosition,TDbRecordId aRecordId,const RDbTableRow* aRow)
sl@0
   994
	{
sl@0
   995
	if (aRow==0)
sl@0
   996
		return ENotSupported;
sl@0
   997
	return FindL(aRecordId,*aRow,aPosition==EDbNext ? TBtree::EGreaterEqual : TBtree::ELessEqual) ? EAtRow : ENoRow;
sl@0
   998
	}
sl@0
   999
sl@0
  1000
// Class CDbStoreIndex
sl@0
  1001
sl@0
  1002
CDbStoreIndex::CDbStoreIndex(CDbStoreDatabase& aDatabase,const CDbStoreIndexDef& aDef) :
sl@0
  1003
	iDatabase(aDatabase), 
sl@0
  1004
	iTree(EBtreeFast), 
sl@0
  1005
	iStats(MUTABLE_CAST(TDbStoreIndexStats&,aDef.iStats))
sl@0
  1006
	{
sl@0
  1007
	}
sl@0
  1008
sl@0
  1009
CDbStoreIndex::~CDbStoreIndex()
sl@0
  1010
	{
sl@0
  1011
	delete iKey;
sl@0
  1012
	}
sl@0
  1013
sl@0
  1014
//
sl@0
  1015
// Create the persistent representation of the index in the store
sl@0
  1016
//
sl@0
  1017
TStreamId CDbStoreIndex::CreateL(CDbStoreDatabase& aDatabase,const CDbStoreIndexDef& aDef)
sl@0
  1018
	{
sl@0
  1019
	MUTABLE_CAST(TDbStoreIndexStats&,aDef.iStats).Reset();
sl@0
  1020
	RStoreWriteStream strm;
sl@0
  1021
	TStreamId id=strm.CreateLC(aDatabase.Store());
sl@0
  1022
	strm<<KEmptyBtreeToken<<aDef.iStats;
sl@0
  1023
	strm.CommitL();
sl@0
  1024
	CleanupStack::PopAndDestroy();
sl@0
  1025
	return id;
sl@0
  1026
	}
sl@0
  1027
sl@0
  1028
//
sl@0
  1029
// Check the index for damage (without constructing the object)
sl@0
  1030
//
sl@0
  1031
TBool CDbStoreIndex::IsDamagedL(CDbStoreDatabase& aDatabase,const CDbStoreIndexDef& aDef)
sl@0
  1032
	{
sl@0
  1033
	RStoreReadStream strm;
sl@0
  1034
	strm.OpenLC(aDatabase.Store(),aDef.TokenId());
sl@0
  1035
	TBtreeToken token;
sl@0
  1036
	strm>>token;
sl@0
  1037
	CleanupStack::PopAndDestroy();
sl@0
  1038
	return token.IsBroken();
sl@0
  1039
	}
sl@0
  1040
sl@0
  1041
//
sl@0
  1042
// Create a StoreIndex object
sl@0
  1043
//
sl@0
  1044
CDbStoreIndex* CDbStoreIndex::NewL(CDbStoreDatabase& aDatabase,const CDbStoreIndexDef& aDef,const CDbTableDef& aTable)
sl@0
  1045
	{
sl@0
  1046
	CDbStoreIndex* self=new(ELeave) CDbStoreIndex(aDatabase,aDef);
sl@0
  1047
	CleanupStack::PushL(self);
sl@0
  1048
	self->iTokenId=aDef.TokenId();
sl@0
  1049
	HKey* key=self->iKey=HKey::NewL(aDef.Key(),aTable.Columns());
sl@0
  1050
	self->iLeafOrg.SetEntrySize(key->EntrySize());
sl@0
  1051
	self->iIndexOrg.SetEntrySize(key->KeySize());
sl@0
  1052
	self->iTree.Connect(&aDatabase.PagePoolL(),key,&self->iLeafOrg,&self->iIndexOrg);
sl@0
  1053
	CleanupStack::Pop();
sl@0
  1054
	return self;
sl@0
  1055
	}
sl@0
  1056
sl@0
  1057
//
sl@0
  1058
// restore from the Store
sl@0
  1059
//
sl@0
  1060
TBool CDbStoreIndex::RestoreL()
sl@0
  1061
	{
sl@0
  1062
	RStoreReadStream strm;
sl@0
  1063
	strm.OpenLC(iDatabase.Store(),iTokenId);
sl@0
  1064
	TBtreeToken token;
sl@0
  1065
	strm>>token>>iStats;
sl@0
  1066
	CleanupStack::PopAndDestroy();
sl@0
  1067
	iTree.Set(token,EBtreeFast);
sl@0
  1068
	return iTree.IsBroken();
sl@0
  1069
	}
sl@0
  1070
sl@0
  1071
//
sl@0
  1072
// Update the index statistics from the index
sl@0
  1073
//
sl@0
  1074
void CDbStoreIndex::RefreshStatsL()
sl@0
  1075
	{
sl@0
  1076
	HKey& key=Key();
sl@0
  1077
	TBtreePos pos;
sl@0
  1078
	if (iTree.FirstL(pos))
sl@0
  1079
		{
sl@0
  1080
		TUint8 entry[KMaxBtreeKeyLength];
sl@0
  1081
		Tree().ExtractAtL(pos,entry,key.EntrySize());
sl@0
  1082
		key.Bound(iStats.iLower,entry);
sl@0
  1083
		iTree.LastL(pos);
sl@0
  1084
		Tree().ExtractAtL(pos,entry,key.EntrySize());
sl@0
  1085
		key.Bound(iStats.iUpper,entry);
sl@0
  1086
		}
sl@0
  1087
	iStats.Refresh(key.KeyType());
sl@0
  1088
	}
sl@0
  1089
sl@0
  1090
//
sl@0
  1091
// Framework member: synchronise the persistent data
sl@0
  1092
//
sl@0
  1093
void CDbStoreIndex::SynchL()
sl@0
  1094
	{
sl@0
  1095
	if (iStats.NeedsRefresh())
sl@0
  1096
		RefreshStatsL();
sl@0
  1097
	RStoreWriteStream strm;
sl@0
  1098
	strm.ReplaceLC(iDatabase.Store(),iTokenId);
sl@0
  1099
	strm<<iTree.Token()<<iStats;
sl@0
  1100
	strm.CommitL();
sl@0
  1101
	CleanupStack::PopAndDestroy();
sl@0
  1102
	}
sl@0
  1103
sl@0
  1104
//
sl@0
  1105
// Framework member: mark the persistent token dirty
sl@0
  1106
//
sl@0
  1107
void CDbStoreIndex::AboutToModifyL()
sl@0
  1108
	{
sl@0
  1109
	iDatabase.MarkL();
sl@0
  1110
	if (!iTree.IsEmpty())	// empty btrees are unbreakable
sl@0
  1111
		{
sl@0
  1112
		TBtreeToken token=iTree.Token();
sl@0
  1113
		token.Touch();		// mark persistent data as broken
sl@0
  1114
		RStoreWriteStream strm;
sl@0
  1115
		strm.OpenLC(iDatabase.Store(),iTokenId);
sl@0
  1116
		strm<<token;
sl@0
  1117
		strm.CommitL();
sl@0
  1118
		CleanupStack::PopAndDestroy();
sl@0
  1119
		}
sl@0
  1120
	}
sl@0
  1121
sl@0
  1122
//
sl@0
  1123
// Try to find a record in the index
sl@0
  1124
// return ENoMatch if no key match, EEntryMatch if entire entry is present
sl@0
  1125
// EKeyMatch if only key matches (only for unique indexes)
sl@0
  1126
//
sl@0
  1127
CDbStoreIndex::TFind CDbStoreIndex::FindL(TDbRecordId aRecordId,const RDbTableRow& aRow)
sl@0
  1128
	{
sl@0
  1129
	__ASSERT((!iTree.IsEmpty())==(Count()!=0));
sl@0
  1130
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
  1131
	iKey->EntryL(entry,aRow,aRecordId);
sl@0
  1132
	TBtreePos pos;
sl@0
  1133
	if (!iTree.FindL(pos,iKey->Key(entry)))
sl@0
  1134
		return ENoMatch;
sl@0
  1135
	TDbRecordId id;
sl@0
  1136
	iTree.ExtractAtL(pos,&id,sizeof(id));
sl@0
  1137
	return id==aRecordId ? EEntryMatch : EKeyMatch;
sl@0
  1138
	}
sl@0
  1139
sl@0
  1140
//
sl@0
  1141
// Add the row to the index
sl@0
  1142
// return True if insertion was good, false if duplicate found
sl@0
  1143
//
sl@0
  1144
TBool CDbStoreIndex::DoInsertL(TDbRecordId aRecordId,const RDbTableRow& aRow)
sl@0
  1145
	{
sl@0
  1146
	__ASSERT((!iTree.IsEmpty())==(Count()!=0));
sl@0
  1147
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
  1148
	TInt len=iKey->EntryL(entry,aRow,aRecordId);
sl@0
  1149
	TBtreePos pos;
sl@0
  1150
	TBool insert=iTree.InsertL(pos,entry,len);
sl@0
  1151
	if (insert)
sl@0
  1152
		iStats.Inc();
sl@0
  1153
	return insert;
sl@0
  1154
	}
sl@0
  1155
sl@0
  1156
//
sl@0
  1157
// Remove row from index
sl@0
  1158
//
sl@0
  1159
void CDbStoreIndex::DoDeleteL(TDbRecordId aRecordId,const RDbTableRow& aRow)
sl@0
  1160
	{
sl@0
  1161
	__ASSERT((!iTree.IsEmpty())==(Count()!=0));
sl@0
  1162
	TUint8 entry[KMaxBtreeKeyLength];
sl@0
  1163
	iKey->EntryL(entry,aRow,aRecordId);
sl@0
  1164
	__DEBUG(TInt dbgchk=) iTree.DeleteL(iKey->Key(entry));
sl@0
  1165
	__ASSERT(dbgchk);
sl@0
  1166
	iStats.Dec();
sl@0
  1167
	}
sl@0
  1168
sl@0
  1169
//
sl@0
  1170
// Provide an iterator for the index ordering
sl@0
  1171
//
sl@0
  1172
CDbRecordIter* CDbStoreIndex::IteratorL(TUint aInclusion,const TDbLookupKey* aLowerBound,const TDbLookupKey* aUpperBound)
sl@0
  1173
	{
sl@0
  1174
	return CIter::NewL(*this,aInclusion,aLowerBound,aUpperBound);
sl@0
  1175
	}
sl@0
  1176
sl@0
  1177
//
sl@0
  1178
// repair the tree from the sequence set after reclamation of the page pool
sl@0
  1179
//
sl@0
  1180
void CDbStoreIndex::RepairL()
sl@0
  1181
	{
sl@0
  1182
	if (!iTree.IsEmpty())	// empty trees are unbreakable
sl@0
  1183
		{
sl@0
  1184
		TouchL();
sl@0
  1185
		iTree.MarkBroken();
sl@0
  1186
		iStats.Reset(iTree.RepairL());
sl@0
  1187
		SynchL();
sl@0
  1188
		}
sl@0
  1189
	}
sl@0
  1190
sl@0
  1191
//
sl@0
  1192
// Throw away the index data, also used prior to a recovering rebuild
sl@0
  1193
//
sl@0
  1194
void CDbStoreIndex::DiscardL()
sl@0
  1195
	{
sl@0
  1196
	TouchL();
sl@0
  1197
	iTree.ClearL();
sl@0
  1198
	iStats.Reset();
sl@0
  1199
	MarkIntact();
sl@0
  1200
	}
sl@0
  1201
sl@0
  1202
//
sl@0
  1203
// Throw away the token
sl@0
  1204
//
sl@0
  1205
void CDbStoreIndex::DestroyL()
sl@0
  1206
	{
sl@0
  1207
	iDatabase.Store().DeleteL(iTokenId);
sl@0
  1208
	}
sl@0
  1209
sl@0
  1210
// Class CDbStoreIndex::CDiscarder
sl@0
  1211
sl@0
  1212
CDbStoreIndex::CDiscarder::CDiscarder()
sl@0
  1213
	{}
sl@0
  1214
sl@0
  1215
CDbStoreIndex::CDiscarder::~CDiscarder()
sl@0
  1216
	{
sl@0
  1217
	delete iIndex;
sl@0
  1218
	}
sl@0
  1219
sl@0
  1220
TInt CDbStoreIndex::CDiscarder::Open(CDbStoreIndex* anIndex)
sl@0
  1221
	{
sl@0
  1222
	__ASSERT(!iIndex);
sl@0
  1223
	iIndex=anIndex;
sl@0
  1224
	return 1;
sl@0
  1225
	}
sl@0
  1226
sl@0
  1227
//
sl@0
  1228
// Do the single step of index discard
sl@0
  1229
//
sl@0
  1230
TInt CDbStoreIndex::CDiscarder::StepL(TInt)
sl@0
  1231
	{
sl@0
  1232
	__ASSERT(iIndex);
sl@0
  1233
	CDbStoreIndex& index=*iIndex;
sl@0
  1234
	index.DiscardL();
sl@0
  1235
	index.DestroyL();
sl@0
  1236
	return 0;
sl@0
  1237
	}