pimappservices/calendar/server/src/agsuidindex.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 #include "agsuidindex.h"
       
    18 
       
    19 #include "agmsimpleentry.h"
       
    20 
       
    21 const TInt KAgnIndexGranularity = 64;
       
    22 
       
    23 //
       
    24 // class TAgnUidIndexKey
       
    25 //
       
    26 
       
    27 TInt CAgnLocalUidIndex::TAgnUidIndexKey::Compare(TInt aLeft, TInt aRight) const
       
    28 /**	Sorted array comparison function
       
    29 	
       
    30 @internalComponent	
       
    31 */
       
    32 	{
       
    33 	const CAgnSimpleEntry* KLeft = *static_cast<CAgnSimpleEntry**>( At(aLeft) );
       
    34 	const CAgnSimpleEntry* KRight = *static_cast<CAgnSimpleEntry**>( At(aRight) );
       
    35 	
       
    36 	if ( KLeft->LocalUid() == KRight->LocalUid() )
       
    37 		{
       
    38 		return ( 0 );
       
    39 		}
       
    40 
       
    41 	return ( ( KLeft->LocalUid() < KRight->LocalUid() ) ? -1 : 1 );
       
    42 	}
       
    43 
       
    44 
       
    45 //
       
    46 // class CAgnLocalUidIndex
       
    47 //
       
    48 
       
    49 CAgnLocalUidIndex* CAgnLocalUidIndex::NewL(CAgnSimpleEntryAllocator& aAllocator)
       
    50 /**	Static constructor
       
    51 	
       
    52 @internalComponent	
       
    53 */
       
    54 	{
       
    55 	CAgnLocalUidIndex* self = new (ELeave) CAgnLocalUidIndex(aAllocator);
       
    56 	
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL();
       
    59 	CleanupStack::Pop();
       
    60 	
       
    61 	return ( self );
       
    62 	}
       
    63 
       
    64 void CAgnLocalUidIndex::ConstructL()
       
    65 /**	Construct array with granularity of 64
       
    66 	
       
    67 @internalComponent	
       
    68 */
       
    69 	{
       
    70 	iSearchEntry = iAllocator.CreateSimpleEntryL(CCalEntry::EAppt);	
       
    71 	
       
    72 	iIndex = new (ELeave) CArrayPtrSeg<CAgnSimpleEntry>(KAgnIndexGranularity);
       
    73 	}
       
    74 
       
    75 
       
    76 void CAgnLocalUidIndex::AddL(const CAgnSimpleEntry* aEntry)
       
    77 /**	Add a CAgnSimpleEntry reference
       
    78 
       
    79 @internalComponent
       
    80 */
       
    81 	{
       
    82 	TInt pos = DoFindByLocalUid(aEntry->LocalUid());
       
    83 	if (pos >= 0)
       
    84 		{
       
    85 		if ((*iIndex)[pos]->EntryId().Value() != aEntry->EntryId().Value())
       
    86 			{
       
    87 			User::Leave(KErrAlreadyExists);
       
    88 			}
       
    89 		// in this case entry is already in database
       
    90 		}
       
    91 	else
       
    92 		{
       
    93 		iIndex->InsertIsqL( const_cast<CAgnSimpleEntry*>(aEntry), iKey);
       
    94 		}
       
    95 	}
       
    96 
       
    97 
       
    98 void CAgnLocalUidIndex::Delete(const CAgnSimpleEntry* aEntry)
       
    99 /**	Delete a CAgnSimpleEntry reference
       
   100 
       
   101 @internalComponent
       
   102 */
       
   103 	{
       
   104 	const TInt KIndex = DoFindByLocalUid(aEntry->LocalUid());
       
   105 
       
   106 	if ( KIndex != KErrNotFound )
       
   107 		{
       
   108 		iIndex->Delete(KIndex);
       
   109 		}
       
   110 	}
       
   111 
       
   112 
       
   113 TInt CAgnLocalUidIndex::DoFindByLocalUid(TCalLocalUid aLocalUid) const
       
   114 /**	Get the index of the specified LocalUid, or KErrNotFound if not found
       
   115 
       
   116 @internalComponent
       
   117 @return Index of the specified LocalUid or KErrNotFound if not found
       
   118 */
       
   119 	{
       
   120 	iSearchEntry->SetLocalUid(aLocalUid);
       
   121 	iSearchEntry->SetEntryId(TAgnEntryId());
       
   122 
       
   123 	TInt pos = 0;	
       
   124 
       
   125 	if ( iIndex->FindIsq(iSearchEntry, (TKeyArrayFix&) iKey, pos) == 0 )
       
   126 		{
       
   127 		return ( pos );
       
   128 		}		
       
   129 
       
   130 	return ( KErrNotFound );
       
   131 	}
       
   132 
       
   133 
       
   134 TAgnEntryId CAgnLocalUidIndex::FindByLocalUid(TCalLocalUid aLocalUid) const
       
   135 /**	Find EntryId by LocalUid 
       
   136 
       
   137 @internalComponent
       
   138 @return Agenda entry id matching the passed unique id
       
   139 */
       
   140 	{
       
   141 	const TInt KIndex = DoFindByLocalUid(aLocalUid);
       
   142 
       
   143 	if ( KIndex != KErrNotFound )
       
   144 		{
       
   145 		return ( (*iIndex)[KIndex]->EntryId() );
       
   146 		}
       
   147 
       
   148 	return TAgnEntryId();
       
   149 	}
       
   150 
       
   151 
       
   152 //
       
   153 // class TGsGuidHashIndexKey
       
   154 //
       
   155 
       
   156 TInt CGsGuidHashIndex::TGsGuidHashIndexKey::Compare(TInt aLeft, TInt aRight) const
       
   157 /**	Sorted array comparison function
       
   158 	
       
   159 @internalComponent	
       
   160 */
       
   161 	{
       
   162 	const CAgnSimpleEntry* KLeft = *static_cast<CAgnSimpleEntry**>( At(aLeft) );
       
   163 	const CAgnSimpleEntry* KRight = *static_cast<CAgnSimpleEntry**>( At(aRight) );
       
   164 
       
   165 	// the sort order is by GuidHash first and by LocalUid second
       
   166 	// NOTE: don't change this logic, otherwise FindHash won't work correctly
       
   167 
       
   168 	if ( KLeft->GuidHash() == KRight->GuidHash() )
       
   169 		{
       
   170 		if ( KLeft->LocalUid() == KRight->LocalUid() )
       
   171 			{
       
   172 			return ( 0 );
       
   173 			}
       
   174 			
       
   175 		return ( ( KLeft->LocalUid() < KRight->LocalUid() ) ? -1 : 1 );
       
   176 		}
       
   177 		
       
   178 	return ( ( KLeft->GuidHash() < KRight->GuidHash() ) ? -1 : 1 );		
       
   179 	}
       
   180 
       
   181 
       
   182 //
       
   183 // class CGsGuidHashIndex
       
   184 //
       
   185 
       
   186 CGsGuidHashIndex* CGsGuidHashIndex::NewL(CAgnSimpleEntryAllocator& aAllocator)
       
   187 /**	Static constructor
       
   188 	
       
   189 @internalComponent	
       
   190 */
       
   191 	{
       
   192 	CGsGuidHashIndex* self = new (ELeave) CGsGuidHashIndex(aAllocator);
       
   193 	
       
   194 	CleanupStack::PushL(self);
       
   195 	self->ConstructL();
       
   196 	CleanupStack::Pop();
       
   197 	
       
   198 	return ( self );
       
   199 	}
       
   200 
       
   201 
       
   202 void CGsGuidHashIndex::ConstructL()
       
   203 /**	Construct array with granularity of 64
       
   204 
       
   205 @internalComponent
       
   206 */
       
   207 	{
       
   208 	iSearchEntry = iAllocator.CreateSimpleEntryL(CCalEntry::EAppt);	
       
   209 	
       
   210 	iIndex = new (ELeave) CArrayPtrSeg<CAgnSimpleEntry>(KAgnIndexGranularity);
       
   211 	}
       
   212 
       
   213 void CGsGuidHashIndex::AddL(const CAgnSimpleEntry* aEntry)
       
   214 /**	Add a CAgnSimpleEntry reference
       
   215 
       
   216 @internalComponent
       
   217 */
       
   218 	{
       
   219 	TInt pos = DoFind(aEntry);
       
   220 	if (pos == KErrNotFound)
       
   221 		{
       
   222 		// in this case entry is not already in database
       
   223 		iIndex->InsertIsqL( const_cast<CAgnSimpleEntry*>(aEntry), iKey);	
       
   224 		}
       
   225 	}
       
   226 
       
   227 
       
   228 void CGsGuidHashIndex::Delete(const CAgnSimpleEntry* aEntry)
       
   229 /**	Delete a CAgnSimpleEntry reference
       
   230 
       
   231 @internalComponent
       
   232 */
       
   233 	{
       
   234 	const TInt KIndex = DoFind(aEntry);
       
   235 
       
   236 	if ( KIndex != KErrNotFound )
       
   237 		{
       
   238 		iIndex->Delete(KIndex);
       
   239 		}
       
   240 	}
       
   241 
       
   242 
       
   243 TInt CGsGuidHashIndex::DoFind(const CAgnSimpleEntry* aEntry) const
       
   244 /**	Get the index of the specified LocalUid / GuidHash, or KErrNotFound if not found
       
   245 
       
   246 @internalComponent
       
   247 @return Index of the specified LocalUid / GuidHash, or KErrNotFound if not found
       
   248 */
       
   249 	{
       
   250 	TInt pos = 0;	
       
   251 
       
   252 	if ( iIndex->FindIsq(const_cast<CAgnSimpleEntry*>(aEntry), (TKeyArrayFix&) iKey, pos) == 0 )
       
   253 		{
       
   254 		return ( pos );
       
   255 		}		
       
   256 
       
   257 	return ( KErrNotFound );	
       
   258 	}
       
   259 
       
   260 
       
   261 void CGsGuidHashIndex::FindByHashL(const TUint32& aGuidHash, RArray<TAgnEntryId>& aFoundIdList) const
       
   262 /**	Get all the LocalUid matching the passed GuidHash
       
   263 
       
   264 @internalComponent
       
   265 */
       
   266 	{
       
   267 	iSearchEntry->SetGuidHash(aGuidHash);
       
   268 	iSearchEntry->SetLocalUid(0);
       
   269 
       
   270 	// The array is sorted by GuidHash first and LocalUid second
       
   271 	// The FindIsq in this case looks for the first occurence of GuidHash (because LocalUid is 0)
       
   272 	// We ignore the return value by purpose because it will negative anyway and nothing bad will happen.
       
   273 
       
   274 	TInt pos = 0;
       
   275 	// coverity[check_return] coverity[unchecked_value]
       
   276 	iIndex->FindIsq(iSearchEntry, (TKeyArrayFix&) iKey, pos);	
       
   277 	
       
   278 	const TInt KSize = iIndex->Count();
       
   279 	for ( ; pos < KSize; ++pos )
       
   280 		{
       
   281 		const CAgnSimpleEntry* KEntry = (*iIndex)[pos];
       
   282 
       
   283 		if ( aGuidHash == KEntry->GuidHash() )
       
   284 			{
       
   285 			// append to the passed array any LocalUid related to the GuidHash match
       
   286 			
       
   287 			aFoundIdList.AppendL(KEntry->EntryId());
       
   288 			}
       
   289 		else
       
   290 			{
       
   291 			// there are no more items in the array matching the GuidHash
       
   292 			// break the for-loop and exit
       
   293 			break;
       
   294 			}
       
   295 		}	
       
   296 	}