landmarks/locationlandmarks/server/inc/EPos_CPosLmNameIndex.h
changeset 0 667063e416a2
equal deleted inserted replaced
-1:000000000000 0:667063e416a2
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Declaration of CPosLmNameIndex class
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef CPOSLMNAMEINDEX_H_
       
    21 #define CPOSLMNAMEINDEX_H_
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <EPos_Landmarks.h>
       
    25 
       
    26 class CPosLandmark;
       
    27 class CPosLmLocalDbAccess;
       
    28 class RDbNamedDatabase;
       
    29 class RDbTable;
       
    30 class RWriteStream;
       
    31 
       
    32 class CPosLmNameIndex : public CBase
       
    33     {
       
    34     public:
       
    35 
       
    36         class CItem : public CBase
       
    37             {
       
    38             public:
       
    39                 virtual TPosLmItemId Id() const = 0;
       
    40                 virtual TPtrC Name() const = 0;
       
    41     
       
    42                 virtual TInt Size() const = 0;
       
    43                 virtual void ExternalizeL( RWriteStream& aOut ) const = 0;
       
    44             };
       
    45 
       
    46     protected:
       
    47     
       
    48         class CIndexItem : public CItem
       
    49             {
       
    50             public:
       
    51                 enum TState
       
    52                     {
       
    53                     EStateTemp      = 0,
       
    54                     EStateValid     = 1,
       
    55                     EStateInvalid   = 2
       
    56                     };
       
    57             
       
    58                 CIndexItem();
       
    59                 CIndexItem( TPosLmItemId aId );
       
    60                 CIndexItem( TPosLmItemId aId, HBufC* aName );
       
    61                 ~CIndexItem();
       
    62         
       
    63                 static CIndexItem* NewLC( TPosLmItemId aId, const TDesC& aName );
       
    64                 static CIndexItem* NewLC( RReadStream& aIn );
       
    65         
       
    66             public: // from CItem
       
    67                 inline TPosLmItemId Id() const;
       
    68                 TPtrC Name() const;
       
    69 
       
    70                 TInt Size() const;
       
    71                 void ExternalizeL( RWriteStream& aOut ) const;
       
    72                 
       
    73             public:    
       
    74                 static TInt CompareById( const CIndexItem& aLeft, const CIndexItem& aRight );
       
    75                 
       
    76                 inline TUint32* IdPtr();
       
    77                 inline HBufC* NamePtr();
       
    78                 
       
    79                 inline void SetId( TPosLmItemId aLmid );
       
    80 
       
    81                 inline TBool IsValid();
       
    82                 inline TBool IsTemp();
       
    83                 inline void SetValid();
       
    84                 inline void SetInvalid();
       
    85                 
       
    86             protected:    
       
    87                 TPosLmItemId    iId;
       
    88                 HBufC*          iName;
       
    89                 TInt32          iState;
       
    90             };
       
    91     
       
    92         class TLmIndexNameKey : public TKeyArrayFix
       
    93             {
       
    94             public:
       
    95                 TLmIndexNameKey();
       
    96                 TAny *At( TInt anIndex ) const;
       
    97             };
       
    98             
       
    99         class TLmIndexIdKey : public TKeyArrayFix
       
   100             {
       
   101             public:
       
   102                 TLmIndexIdKey();
       
   103                 TAny *At( TInt anIndex ) const;
       
   104             };
       
   105 
       
   106     public:
       
   107     
       
   108         static CPosLmNameIndex* NewL( CPosLmLocalDbAccess& aDbAccess );
       
   109         static CPosLmNameIndex* NewLC( CPosLmLocalDbAccess& aDbAccess );
       
   110         ~CPosLmNameIndex();
       
   111         
       
   112         /** Stores index data to database
       
   113          *  @param[in] aDrive The disk where database is located. This method
       
   114          *      will check if the drive has enough space to store the index. 
       
   115          *  @leave KErrDiskFull if database drive cannot accomodate index data. */
       
   116         void SaveL( TChar aDrive );
       
   117         
       
   118         /** Performs one step of index evaluation and returns progress. */
       
   119         TReal32 Evaluate();
       
   120         
       
   121         /** Starts transaction on the index. From now on and until 
       
   122          *  transaction is committed or rolled back, all inserted items
       
   123          *  are left in EStateTemp state and all removed items are
       
   124          *  only marked as EStateInvalid */
       
   125         void StartTransaction();
       
   126         /** Marks all temp items as valid and removes all invalid ones */
       
   127         void CommitTransaction();
       
   128         /** Removes all invalid and temp items */
       
   129         void RollbackTransaction();
       
   130         
       
   131         /** @return status of the index object: 
       
   132          *  - KErrNone - ready
       
   133          *  - KErrNotReady - evaluating or in transaction
       
   134          *  - other - error. */
       
   135         inline TInt Status() const;
       
   136         
       
   137         /** @return timestamp of the index store */
       
   138         inline TTime TimeStamp() const;
       
   139 
       
   140         /** Inserts landmark to the correct place in the index. 
       
   141          * ID and name must be valid */
       
   142         void InsertL( const CPosLandmark& aLandmark );
       
   143         /** Inserts ID and name pair to the correct place in the index. */
       
   144         void InsertL( TPosLmItemId aId, const TDesC& aName );
       
   145         /** Inserts ID and name pair to the correct place in the index. 
       
   146          * This object takes ownership of the aName */
       
   147         void InsertL( TPosLmItemId aLmid, HBufC* aName );
       
   148         /** Updates name for the given ID and moves it to the correct place in the index. */
       
   149         void UpdateL( TPosLmItemId aId, const TDesC& aName );
       
   150         /** Updates name for the given ID and moves it to the correct place in the index. 
       
   151          * This object takes ownership of the aName */
       
   152         void UpdateL( TPosLmItemId aLmid, HBufC* aName );
       
   153         /** Removes an item from the index */
       
   154         void Remove( TPosLmItemId aId );
       
   155 
       
   156         /** Total amount of items in index */
       
   157         TInt Count() const;
       
   158         /** Access to a particular item in index */
       
   159         const CItem& Item( TInt aIndex ) const;
       
   160         /** Returns index of valid item with given ID, or KErrNotFound if not found */
       
   161         TInt Find( TPosLmItemId aLmid ) const;
       
   162         
       
   163         /** Returns list of all IDs sorted by landmark name */
       
   164         CArrayFixSeg<TPosLmItemId>* GetSortedIdsLC() const;
       
   165         /** Returns a part of the list of IDs sorted by landmark name */
       
   166         CArrayFixSeg<TPosLmItemId>* GetSortedIdsLC( TInt aFirst, TInt aCount ) const;
       
   167         /** Returns a part of the list of IDs sorted by landmark name 
       
   168          *  @param[out] aTarget pointer to array of TPosLmItemId elements. 
       
   169          *      It must be able to accomodate aCount items 
       
   170          *  @param[out] aRemider on return contains amount of items left in the index
       
   171          *      (this is Count() - (aFirst + aCount) ) */
       
   172         TInt GetSortedIds( 
       
   173             TInt aFirst, TInt aCount, 
       
   174             TPosLmItemId* aTarget, TInt& aReminder ) const;
       
   175         
       
   176         /** @return size of this index if externalized */
       
   177         TInt DataSize() const;
       
   178         
       
   179         /** @return size of given item if externalized */
       
   180         TInt DataSize( const CItem& aItem ) const;
       
   181 
       
   182         /** Clears the index */
       
   183         void Reset();
       
   184         
       
   185         /** Writes single item onto given stream.
       
   186          *  Stream will contain ID, length of the name, name data */
       
   187         void ExternalizeL( RWriteStream& aOut, const CItem& aItem ) const;
       
   188         
       
   189     protected:
       
   190         CPosLmNameIndex( CPosLmLocalDbAccess& aDbAccess );
       
   191         
       
   192         void ConstructL();
       
   193         
       
   194         void ExternalizeL( RWriteStream& aOut ) const;
       
   195         void InternalizeL( RReadStream& aIn );
       
   196 
       
   197         /** Inserts ID and name pair to the correct place in the index. 
       
   198          *  Takes ownership of the aName */
       
   199         CIndexItem* DoInsertL( TPosLmItemId aLmid, HBufC* aName );
       
   200 
       
   201         void DoEvaluateL( TReal32& aProgress );
       
   202         void StopEvaluation();
       
   203         
       
   204         void LoadL();
       
   205         void CreateByIdSortingL();
       
   206         void RemoveInvalidItems();
       
   207 
       
   208         CIndexItem* ItemById( TPosLmItemId aId ) const;
       
   209         void AppendL( TPosLmItemId aLmid, HBufC* aName );
       
   210         void AppendL( CIndexItem* aLandmark );
       
   211         void DoRemove( TInt aIndex );
       
   212         
       
   213     private:
       
   214     
       
   215     	CArrayPtrSeg<CIndexItem>*   iArray;
       
   216         
       
   217         TInt                        iDataSize;
       
   218 
       
   219         CPosLmLocalDbAccess&        iDbAccess;
       
   220         RDbNamedDatabase            iDatabase;
       
   221         RDbTable                    iTable;
       
   222         TInt                        iTablePosition;
       
   223         TBool						iIsTableOpen;
       
   224         
       
   225         TInt                        iStatus;
       
   226         TTime                       iTimeStamp;
       
   227 
       
   228         RPointerArray<CIndexItem>   iIdSortedArray; // does not own items!
       
   229         CIndexItem*                 iKeyItem;
       
   230         
       
   231         TBool                       iInTransaction;
       
   232     };
       
   233 
       
   234 #include "epos_cposlmnameindex.inl"    
       
   235     
       
   236 #endif /*CPOSLMNAMEINDEX_H_*/