remotestoragefw/remotefileengine/src/rsfwlruprioritylist.cpp
branchRCL_3
changeset 20 1aa8c82cb4cb
equal deleted inserted replaced
19:88ee4cf65e19 20:1aa8c82cb4cb
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:  LRU priority list for the file cache
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "rsfwlruprioritylist.h"
       
    20 #include "rsfwlrulistnode.h"
       
    21 #include "rsfwfileentry.h"
       
    22 #include "mdebug.h"
       
    23 #include "rsfwvolumetable.h"
       
    24 #include "rsfwvolume.h"
       
    25 #include "rsfwfileengine.h"
       
    26 #include "rsfwfiletable.h"
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CRsfwLruPriorityList::CRsfwLruPriorityList
       
    30 // 
       
    31 // ----------------------------------------------------------------------------
       
    32 //
       
    33 CRsfwLruPriorityList::CRsfwLruPriorityList()
       
    34     : iHdr(CRsfwLruListNode::iOffset),iIter(iHdr) //construct header & iterator
       
    35     {}
       
    36     
       
    37 // ----------------------------------------------------------------------------
       
    38 // CRsfwLruPriorityList::~CRsfwLruPriorityList
       
    39 // 
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 CRsfwLruPriorityList::~CRsfwLruPriorityList()
       
    43     {
       
    44     CRsfwLruListNode* node;
       
    45     
       
    46     iIter.SetToFirst();
       
    47     node = iIter++;
       
    48     while (node)
       
    49         {
       
    50         node->iLink.Deque();
       
    51         delete node;
       
    52         node = iIter++;
       
    53         }
       
    54     }    
       
    55   
       
    56 // ----------------------------------------------------------------------------
       
    57 // CRsfwLruPriorityList::AddNodeL
       
    58 // 
       
    59 // ----------------------------------------------------------------------------
       
    60 //
       
    61 void CRsfwLruPriorityList::AddNodeL(CRsfwFileEntry *aFe, TInt aPriority) 
       
    62     {
       
    63     CRsfwLruListNode* currentNode;
       
    64     
       
    65     iIter.SetToFirst();
       
    66     
       
    67     currentNode = iIter++;
       
    68     while (currentNode) 
       
    69         {
       
    70         if (currentNode->iEntryPtr->Fid() == aFe->Fid()) 
       
    71             {
       
    72             DEBUGSTRING(("LRU list: '%d' already exists on the list",
       
    73                          aFe->Fid().iNodeId));
       
    74             return;
       
    75             }  
       
    76         currentNode = iIter++;
       
    77         }
       
    78       
       
    79     // Inserts the specified list element in descending priority order.
       
    80     // If there is an existing list element with the same priority, 
       
    81     // then the new element is added after the existing element.
       
    82     CRsfwLruListNode* newNode = CRsfwLruListNode::NewL(aFe, aPriority);
       
    83     iHdr.Add(*newNode);
       
    84     DEBUGSTRING(("LRU list: added fid '%d' to the list",
       
    85                      aFe->Fid().iNodeId));
       
    86     }
       
    87 
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // CRsfwLruPriorityList::RemoveNode
       
    91 // 
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 TInt CRsfwLruPriorityList::RemoveNode(CRsfwFileEntry *aFe)
       
    95     {
       
    96     DEBUGSTRING(("CRsfwLruPriorityList::RemoveNode"));
       
    97     // When file is opened, it must be removed from LRU list 
       
    98     // as it is not candidate for removal from cache. 
       
    99     // Returns KErrNotFound if the file is not found at all
       
   100     
       
   101     TInt err = KErrNotFound;
       
   102     CRsfwLruListNode* currentNode;
       
   103     
       
   104     iIter.SetToFirst();
       
   105     
       
   106     currentNode = iIter++;
       
   107     while (currentNode) 
       
   108         {
       
   109         if (currentNode->iEntryPtr->Fid() == aFe->Fid()) 
       
   110             {
       
   111             currentNode->iLink.Deque();
       
   112             delete currentNode;
       
   113             err = KErrNone;
       
   114             DEBUGSTRING(("LRU list: removed fid '%d' from the list",
       
   115                              aFe->Fid().iNodeId));
       
   116             break;             
       
   117             }  
       
   118         currentNode = iIter++;
       
   119         }
       
   120     return err;
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CRsfwLruPriorityList::GetAndRemoveFirstEntry
       
   125 // 
       
   126 // ----------------------------------------------------------------------------
       
   127 //
       
   128 CRsfwFileEntry* CRsfwLruPriorityList::GetAndRemoveFirstEntry() 
       
   129     {
       
   130     
       
   131     CRsfwLruListNode* firstNode = iHdr.First();
       
   132     CRsfwFileEntry* firstEntry = NULL;
       
   133      
       
   134     if (iHdr.IsHead(firstNode)) 
       
   135         {
       
   136         return NULL; // the head has been reached, and must not be removed  
       
   137         }
       
   138      
       
   139     if (firstNode) 
       
   140         {
       
   141         firstEntry = firstNode->iEntryPtr;
       
   142         firstNode->iLink.Deque();
       
   143         delete firstNode;
       
   144         }
       
   145 
       
   146     DEBUGSTRING(("LRU list: first fid on the list removed, '%d'",
       
   147                       firstEntry->Fid().iNodeId));        
       
   148     return firstEntry;
       
   149     }
       
   150     
       
   151 // ----------------------------------------------------------------------------
       
   152 // CRsfwFileEntry::ExternalizeL
       
   153 // ----------------------------------------------------------------------------
       
   154 //
       
   155 void CRsfwLruPriorityList::ExternalizeL(RWriteStream& aStream)
       
   156     {    
       
   157     // start from the end!
       
   158     iIter.SetToLast();
       
   159     CRsfwLruListNode* currentNode = iIter--;
       
   160     while (currentNode) 
       
   161         {
       
   162         CRsfwFileEntry* currentEntry = currentNode->iEntryPtr;
       
   163         if (currentEntry)
       
   164             {
       
   165             TFid fid = currentEntry->Fid();
       
   166             aStream.WriteInt32L(fid.iNodeId);
       
   167             aStream.WriteInt32L(fid.iVolumeId);            
       
   168             }
       
   169         currentNode = iIter--;
       
   170         }  
       
   171     }
       
   172 
       
   173 // ----------------------------------------------------------------------------
       
   174 // CRsfwFileEntry::InternalizeL
       
   175 // ----------------------------------------------------------------------------
       
   176 //
       
   177 void CRsfwLruPriorityList::InternalizeL(RReadStream& aStream, CRsfwVolumeTable* aVolumeTable)
       
   178     {
       
   179     if ( !aVolumeTable )
       
   180         {
       
   181         User::Leave(KErrArgument);
       
   182         }
       
   183 
       
   184     // reset existing list
       
   185     iHdr.Reset();
       
   186 
       
   187     // get stream size
       
   188     MStreamBuf* streamBuf = aStream.Source();
       
   189     TInt streamSize = streamBuf->SizeL();    
       
   190 
       
   191     TInt i;
       
   192     // note i+8 as one entry takes two TInt32 (2 times 4 bytes)
       
   193     for ( i = 0; i < streamSize; i = i+8 )
       
   194         {
       
   195         TFid fid;
       
   196         fid.iNodeId = aStream.ReadInt32L();
       
   197         fid.iVolumeId = aStream.ReadInt32L();
       
   198 
       
   199         // check whether there is no trash in the data being internalized
       
   200         if (fid.iVolumeId >= 0 && fid.iVolumeId < KMaxVolumes && fid.iNodeId > 0)
       
   201             {
       
   202             // find existing CRsfwFileEntry object based on TFid
       
   203             CRsfwVolume* volume = aVolumeTable->iVolumes[fid.iVolumeId];
       
   204             if ( volume )
       
   205                 {
       
   206                 CRsfwFileEntry* entry = volume->iFileEngine->iFileTable->Root()->Lookup(fid);
       
   207                 if ( entry )
       
   208                     {
       
   209                     AddNodeL(entry, ECachePriorityNormal);
       
   210                     }
       
   211                 }            
       
   212             }
       
   213         else
       
   214             {
       
   215             DEBUGSTRING(("LRU List: wrong item on the list being internalized!"));
       
   216             }
       
   217         }
       
   218     }
       
   219