sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "US_STD.H" sl@0: sl@0: // Class RClusterMap sl@0: sl@0: void RClusterMap::InsertL( TClusterId aCluster, TClusterId aPrevious ) sl@0: // sl@0: // insert the entry into the map sl@0: // sl@0: { sl@0: TIdPair* map = iMap; sl@0: if ( iEntries == iAlloc ) sl@0: { // ensure there is space sl@0: TInt size = iAlloc + EGranularity; sl@0: iMap = map = ( TIdPair* )User::ReAllocL( map, size * sizeof( TIdPair ) ); sl@0: iAlloc = size; sl@0: } sl@0: TInt l = 0; sl@0: TInt r = iEntries; sl@0: while ( r > l ) sl@0: { sl@0: TInt m = ( l + r ) >> 1; sl@0: TClusterId id = map[ m ].iId; sl@0: __ASSERT( aCluster != id ); // not already present sl@0: if ( aCluster < id ) sl@0: r = m; sl@0: else sl@0: l = m + 1; sl@0: } sl@0: TIdPair* p = map + r; sl@0: TIdPair* e = map + iEntries++; sl@0: Mem::Move( p + 1, p, ( TUint8* )e - ( TUint8* )p ); sl@0: p->iId = aCluster; sl@0: p->iPreviousId = aPrevious; sl@0: } sl@0: sl@0: RClusterMap::TIdPair* RClusterMap::At( TClusterId aCluster ) sl@0: { sl@0: TInt l = 0; sl@0: TInt r = iEntries; sl@0: while ( r > l ) sl@0: { sl@0: TInt m = ( l + r ) >> 1; sl@0: TClusterId id = iMap[ m ].iId; sl@0: if ( aCluster < id ) sl@0: r = m; sl@0: else if ( aCluster > id ) sl@0: l = m + 1; sl@0: else sl@0: return iMap + m; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: void RClusterMap::ResetL( TClusterId aHeadCluster ) sl@0: { sl@0: iComplete = EFalse; sl@0: iEntries = 0; sl@0: InsertL( aHeadCluster, KNullClusterId ); sl@0: iLastMapped = iLastBound = aHeadCluster; sl@0: iSkipped = ESeparation - 1; sl@0: } sl@0: sl@0: TBool RClusterMap::At( TClusterId aCluster, TClusterId& aPreviousCluster ) sl@0: { sl@0: TIdPair* p = At( aCluster ); sl@0: if ( p ) sl@0: aPreviousCluster = p->iPreviousId; sl@0: else if ( aCluster == iLastBound ) sl@0: aPreviousCluster = iLastMapped; sl@0: else sl@0: return EFalse; sl@0: return ETrue; sl@0: } sl@0: sl@0: void RClusterMap::AddL( TClusterId aCluster ) sl@0: { sl@0: __ASSERT( aCluster != KNullClusterId ); sl@0: if ( --iSkipped < 0 ) sl@0: { sl@0: InsertL( aCluster, iLastMapped ); sl@0: iLastMapped = aCluster; sl@0: iSkipped = ESeparation - 1; sl@0: } sl@0: iLastBound = aCluster; sl@0: } sl@0: sl@0: void RClusterMap::Drop( TClusterId aCluster, TClusterId aNext ) sl@0: // sl@0: // Cluster has been deleted, modify entry to contain the next cluster sl@0: // last cluster in table is never deleted sl@0: // sl@0: { sl@0: if ( aCluster == iLastBound ) sl@0: iLastBound = aNext; sl@0: TIdPair* entry = At( aCluster ); sl@0: if ( !entry ) sl@0: return; // not in the sparse map sl@0: // remove entry for cluster->prev sl@0: TClusterId prev = entry->iPreviousId; sl@0: Mem::Move( entry, entry + 1, ( TUint8* )( iMap + --iEntries ) - ( TUint8* )entry ); sl@0: // sl@0: if ( aCluster == iLastMapped ) sl@0: iLastMapped = aNext; sl@0: else sl@0: { // find the referring entry next->cluster sl@0: TIdPair* pnext = iMap; sl@0: while ( pnext->iPreviousId != aCluster ) sl@0: { sl@0: ++pnext; sl@0: __ASSERT( pnext < iMap + iEntries ); sl@0: } sl@0: if ( pnext->iId == aNext ) sl@0: { // referring entry is the next one => drop a link sl@0: pnext->iPreviousId = prev; sl@0: return; sl@0: } sl@0: // adjust next->new sl@0: pnext->iPreviousId = aNext; sl@0: } sl@0: // add in new link to replace deleted one sl@0: InsertL( aNext, prev ); // will not fail allocation as space available sl@0: } sl@0: sl@0: // Class TClusterLinkCache sl@0: sl@0: void TClusterLinkCache::Add( TClusterId aCluster, RClusterMap& aMap ) sl@0: // sl@0: // Add an entry to the cache sl@0: // sl@0: { sl@0: __ASSERT( iEnd != NULL ); sl@0: TClusterId id; sl@0: __ASSERT( aMap.At( iMap[0], id ) ); sl@0: // sl@0: TClusterId* p = iEnd; sl@0: if ( p == &iMap[ RClusterMap::ESeparation ] ) sl@0: { // full, requires a shift down sl@0: for ( ; !aMap.At( *p, id ); --p ) sl@0: { sl@0: __ASSERT( p > iMap ); sl@0: } sl@0: __ASSERT( p > iMap ); sl@0: __ASSERT( Has( id ) ); sl@0: sl@0: TClusterId* c = iMap; sl@0: --c; sl@0: while ( p <= iEnd ) sl@0: *++c = *p++; sl@0: p = c; sl@0: } sl@0: *++p = aCluster; sl@0: iEnd = p; sl@0: } sl@0: sl@0: void TClusterLinkCache::Add( const TClusterId* aFirst, const TClusterId* aLast ) sl@0: // sl@0: // Add several linked TClusterIds sl@0: // sl@0: { sl@0: __ASSERT( iEnd != NULL ); sl@0: // sl@0: TClusterId* p = iEnd; sl@0: while ( aFirst < aLast && p < &iMap[ RClusterMap::ESeparation ] ) sl@0: *++p = *aFirst++; sl@0: iEnd = p; sl@0: } sl@0: sl@0: void TClusterLinkCache::Drop( TClusterId aCluster, TClusterId aNext ) sl@0: // sl@0: // Drop the item if it is in the cache sl@0: // sl@0: { sl@0: TClusterId* p = iEnd; sl@0: if ( !p ) sl@0: return; sl@0: if ( *p == aCluster ) sl@0: { sl@0: *p = aNext; sl@0: return; sl@0: } sl@0: do sl@0: { sl@0: if ( p == iMap ) sl@0: return; sl@0: } while ( *--p != aCluster ); sl@0: __ASSERT( *( p + 1 ) == aNext ); sl@0: for ( ; p < iEnd; ++p ) sl@0: *p = *( p + 1 ); sl@0: iEnd = p - 1; sl@0: } sl@0: sl@0: TBool TClusterLinkCache::Has( TClusterId aCluster ) const sl@0: // sl@0: // Check if the cluster id is in the cache sl@0: // iEnd==0 is a valid state (empty cache) sl@0: // sl@0: { sl@0: for ( const TClusterId* p = iEnd; p >= iMap; ) sl@0: { sl@0: if ( *p-- == aCluster ) sl@0: return ETrue; sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: TBool TClusterLinkCache::At( TClusterId aCluster, TClusterId& aPrevious ) const sl@0: // sl@0: // If aCluster is in the cache, return the previous cluster in aPrevious sl@0: // iEnd==0 is a valid state (empty cache) sl@0: // sl@0: { sl@0: for ( const TClusterId* p = iEnd; p > iMap; ) sl@0: { sl@0: if ( *p-- == aCluster ) sl@0: { sl@0: aPrevious = *p; sl@0: return ETrue; sl@0: } sl@0: } sl@0: return EFalse; sl@0: } sl@0: sl@0: // Class TClusterDes sl@0: sl@0: void TClusterDes::InternalizeL(RReadStream& aStream) sl@0: { sl@0: aStream>>iNext; sl@0: iMembership=aStream.ReadUint16L(); sl@0: } sl@0: sl@0: void TClusterDes::ExternalizeL(RWriteStream& aStream) const sl@0: { sl@0: aStream<AddClusterL(); sl@0: } sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: LOCAL_C void DeleteCluster(CCluster* aCluster) sl@0: // sl@0: // helper function which matches the Apply() prototype sl@0: // sl@0: { sl@0: delete aCluster; sl@0: } sl@0: sl@0: CClusterCache::~CClusterCache() sl@0: { sl@0: Apply(DeleteCluster); sl@0: } sl@0: sl@0: LOCAL_C void DiscardCluster(CCluster* aCluster) sl@0: // sl@0: // helper function which matches the Apply() prototype sl@0: // sl@0: { sl@0: aCluster->Discard(); sl@0: } sl@0: sl@0: void CClusterCache::Discard() sl@0: // sl@0: // discard the current changes in all clusters sl@0: // sl@0: { sl@0: Apply(DiscardCluster); sl@0: } sl@0: sl@0: LOCAL_C void FlushClusterL(CCluster* aCluster) sl@0: // sl@0: // helper function which matches the Apply() prototype sl@0: // sl@0: { sl@0: aCluster->FlushL(); sl@0: } sl@0: sl@0: void CClusterCache::FlushL() sl@0: // sl@0: // Flush all the clusters in the cache sl@0: // sl@0: { sl@0: Apply(FlushClusterL); sl@0: } sl@0: sl@0: CCluster* CClusterCache::Cluster(TClusterId aCluster) sl@0: // sl@0: // Look for a cluster in the cache sl@0: // sl@0: { sl@0: TDblQueIter iter(iCache); sl@0: for (CCluster* cluster;(cluster=iter++)!=0;) sl@0: { sl@0: if (cluster->Id()==aCluster) sl@0: return cluster; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: CCluster& CClusterCache::ClusterL(TClusterId aCluster) sl@0: // sl@0: // Get a cluster from the cache or store and move it to the top of the cache sl@0: // Track hits to the two clusters which most recently dropped out of the cache sl@0: // sl@0: { sl@0: CCluster* cluster=Cluster(aCluster); // check if it is cached sl@0: iFollowOnHits<<=2; sl@0: if (!cluster) sl@0: { // get an empty cluster and read it sl@0: if (aCluster==iCachePlus1) sl@0: { // the cluster has recently been discarded sl@0: iCachePlus1=iCachePlus2; // re-sequence the cache follow-on sl@0: iFollowOnHits|=0x1; sl@0: } sl@0: else if (aCluster==iCachePlus2) sl@0: iFollowOnHits|=0x2; // the cluster has recently been discarded sl@0: cluster=&NewClusterL(); sl@0: cluster->ReadL(aCluster); sl@0: } sl@0: return Touch(*cluster); sl@0: } sl@0: sl@0: CCluster& CClusterCache::ClusterL() sl@0: // sl@0: // Get a new (empty) cluster from the cache and move it to the top sl@0: // sl@0: { sl@0: return Touch(NewClusterL()); sl@0: } sl@0: sl@0: CCluster& CClusterCache::Touch(CCluster& aCluster) sl@0: // sl@0: // Move a cluster to the top of the LRU list sl@0: // sl@0: { sl@0: aCluster.iLink.Deque(); sl@0: iCache.AddFirst(aCluster); sl@0: return aCluster; sl@0: } sl@0: sl@0: CCluster& CClusterCache::AddClusterL() sl@0: // sl@0: // Add a new cluster to the cache sl@0: // sl@0: { sl@0: __ASSERT(iClusters>1)&0x55); sl@0: return cluster; sl@0: } sl@0: sl@0: CCluster& CClusterCache::NewClusterL() sl@0: // sl@0: // Get an empty cluster from the cache, but do not touch it sl@0: // If the hit detector has registered enough near-misses the cache is expanded sl@0: // by adding another cluster object sl@0: // sl@0: { sl@0: CCluster* cluster=Cluster(KNullClusterId); // look for a discarded cluster first sl@0: if (cluster) sl@0: return *cluster; sl@0: // check for cache expansion sl@0: TUint detected=iFollowOnHits; sl@0: if ((detected&(detected-1))!=0 && iClustersFlushL(); sl@0: iCachePlus2=iCachePlus1; sl@0: iCachePlus1=cluster->Id(); sl@0: return *cluster; sl@0: } sl@0: sl@0: void CClusterCache::Apply(void (*aFunc)(CCluster*)) sl@0: // sl@0: // Apply the function paramater to all clusters in the cache sl@0: // This function may leave <==> the parameter function may leave sl@0: // sl@0: { sl@0: TDblQueIter iter(iCache); sl@0: for (CCluster* cluster;(cluster=iter++)!=0;) sl@0: aFunc(cluster); sl@0: } sl@0: sl@0: // Class CCluster sl@0: sl@0: CCluster* CCluster::NewL(CDbStoreDatabase& aDatabase) sl@0: { sl@0: return new(ELeave) CCluster(aDatabase); sl@0: } sl@0: sl@0: CCluster::~CCluster() sl@0: { sl@0: User::Free(iMap[0]); sl@0: } sl@0: sl@0: void CCluster::AdjustMap(TUint8** aMapEntry,TInt aAdjust) sl@0: // sl@0: // Adjust all map entries after aMapEntry sl@0: // sl@0: { sl@0: do *aMapEntry+=aAdjust; while (++aMapEntry<=&iMap[KMaxClustering]); sl@0: } sl@0: sl@0: TInt CCluster::SetSizeL(TInt aSize) sl@0: // sl@0: // Set the minimum size for the cluster buffer sl@0: // Return the offset between the new and old cells sl@0: // sl@0: { sl@0: if (iSize>=aSize) sl@0: return 0; sl@0: // sl@0: aSize+=EGranularity-1; // round to granularity sl@0: aSize&=~(EGranularity-1); sl@0: TUint8* base=iMap[0]; sl@0: TInt offset=(TUint8*)User::ReAllocL(base,aSize)-base; sl@0: iSize=aSize; sl@0: if (offset) sl@0: AdjustMap(&iMap[0],offset); sl@0: return offset; sl@0: } sl@0: sl@0: void CCluster::Discard() sl@0: // sl@0: // discard the current changes sl@0: // sl@0: { sl@0: iCluster=KNullClusterId; sl@0: iModified=EFalse; sl@0: } sl@0: sl@0: void CCluster::Create(TClusterId aClusterId) sl@0: // sl@0: // Create a new cluster sl@0: // sl@0: { sl@0: __ASSERT(!iModified); sl@0: // sl@0: iCluster=aClusterId; sl@0: iDes.iNext=KNullClusterId; sl@0: iDes.iMembership=0; sl@0: TUint8* base=iMap[0]; sl@0: for (TUint8** ptr=&iMap[1];ptr<=&iMap[KMaxClustering];++ptr) sl@0: *ptr=base; sl@0: iModified=ETrue; sl@0: } sl@0: sl@0: void CCluster::Relink(TClusterId aNextClusterId) sl@0: // sl@0: // Update the cluster to link to a different cluster sl@0: // sl@0: { sl@0: iDes.iNext=aNextClusterId; sl@0: iModified=ETrue; sl@0: } sl@0: sl@0: void CCluster::AlterL(MAlter& aAlterer) sl@0: // sl@0: // alter all records in the cluster sl@0: // sl@0: { sl@0: TUint members=iDes.iMembership; sl@0: TUint8* wptr=iMap[0]; sl@0: TUint8* rptr=wptr; sl@0: for (TUint8** map=&iMap[0];map<&iMap[KMaxClustering];members>>=1,++map) sl@0: { sl@0: if (members&1) sl@0: { sl@0: TInt size=map[1]-rptr; sl@0: TInt expand=wptr-rptr+aAlterer.RecordExpansion(rptr,size); sl@0: if (expand>0) sl@0: { // requires more space for alteration sl@0: AdjustL(map,expand+EExpandBuffer,rptr); sl@0: wptr=map[0]; // compensate for possible moving cache sl@0: rptr=map[1]-size; // record data is at end of this entry sl@0: } sl@0: wptr=aAlterer.AlterRecordL(wptr,rptr,size); sl@0: rptr+=size; sl@0: __ASSERT(wptr<=rptr); sl@0: } sl@0: else sl@0: { sl@0: __ASSERT(map[1]==rptr); sl@0: } sl@0: map[1]=wptr; sl@0: } sl@0: iModified=ETrue; sl@0: } sl@0: sl@0: TPtrC8 CCluster::RecordL(TInt aIndex) sl@0: // sl@0: // Read the cluster and return the record data sl@0: // sl@0: { sl@0: if (!((iDes.iMembership>>aIndex)&1)) sl@0: __LEAVE(KErrNotFound); sl@0: return TPtrC8(iMap[aIndex],iMap[aIndex+1]-iMap[aIndex]); sl@0: } sl@0: sl@0: TUint8* CCluster::UpdateL(TInt aIndex,TInt aNewSize) sl@0: // sl@0: // read the cluster and return a writable descriptor over the new record data sl@0: // sl@0: { sl@0: SetRecordL(aIndex,aNewSize); sl@0: iDes.iMembership|=(1<>=1) sl@0: { sl@0: ++map; sl@0: if (members&1) sl@0: { sl@0: TUint8* end=*map; sl@0: cluster << TCardinality(end-ptr); sl@0: ptr=end; sl@0: } sl@0: else sl@0: { sl@0: __ASSERT(*map==ptr); sl@0: } sl@0: } sl@0: cluster.FilterL(cluster.EMixed,iCluster); sl@0: cluster.WriteL(base,ptr-base); sl@0: cluster.CommitL(); sl@0: CleanupStack::PopAndDestroy(); sl@0: iModified=EFalse; sl@0: } sl@0: } sl@0: sl@0: void CCluster::ReadL(TClusterId aCluster) sl@0: // sl@0: // Internalize the cluster sl@0: // sl@0: { sl@0: __ASSERT(iCluster!=aCluster); sl@0: __ASSERT(!iModified); sl@0: // sl@0: iCluster=KNullClusterId; sl@0: RDbStoreReadStream cluster(iDatabase); sl@0: cluster.OpenLC(iDatabase.Store(),aCluster); sl@0: cluster>>iDes; sl@0: TUint8** map=&iMap[0]; sl@0: TUint8* base=*map; sl@0: TUint8* ptr=base; sl@0: for (TUint members=iDes.iMembership;members!=0;members>>=1) sl@0: { sl@0: if (members&1) sl@0: { sl@0: TCardinality card; sl@0: cluster >> card; sl@0: TInt size=card; sl@0: if (size>KDbStoreMaxRecordLength) sl@0: __LEAVE(KErrCorrupt); sl@0: ptr+=size; sl@0: } sl@0: *++map=ptr; sl@0: } sl@0: while (map<&iMap[KMaxClustering]) sl@0: *++map=ptr; sl@0: TInt len=ptr-base; sl@0: base+=SetSizeL(len); sl@0: cluster.FilterL(cluster.EMixed,aCluster); sl@0: cluster.ReadL(base,len); sl@0: CleanupStack::PopAndDestroy(); sl@0: iCluster=aCluster; sl@0: } sl@0: sl@0: void CCluster::SetRecordL(TInt aIndex,TInt aNewSize) sl@0: { sl@0: AdjustL(&iMap[aIndex],iMap[aIndex]+aNewSize-iMap[aIndex+1],iMap[aIndex+1]); sl@0: iModified=ETrue; sl@0: } sl@0: sl@0: void CCluster::AdjustL(TUint8** aMapEntry,TInt aAdjust,TUint8* aData) sl@0: // sl@0: // Adjust the record at map entry by aAdjust bytes sl@0: // Move that entry data as well as the ones following for AlterCluster sl@0: // sl@0: { sl@0: if (!aAdjust) sl@0: return; sl@0: // sl@0: __ASSERT(aAdjust+aMapEntry[1]>=aMapEntry[0]); // record cannot go -ve size sl@0: __ASSERT(aData>=aMapEntry[0]); // must not save data before this record sl@0: // sl@0: aData+=SetSizeL(iMap[KMaxClustering]-iMap[0]+aAdjust); sl@0: Mem::Copy(aData+aAdjust,aData,iMap[KMaxClustering]-aData); sl@0: AdjustMap(aMapEntry+1,aAdjust); sl@0: } sl@0: sl@0: // class CCluster::MAlter sl@0: sl@0: TInt CCluster::MAlter::RecordExpansion(const TUint8*,TInt) sl@0: // sl@0: // default to no expansion sl@0: // sl@0: { sl@0: return 0; sl@0: }