Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * File: ndn_cs.cpp |
| 3 | * Author: cawka |
| 4 | * |
| 5 | * Created on December 15, 2010, 2:17 PM |
| 6 | */ |
| 7 | |
| 8 | #include "ndn_cs.h" |
| 9 | |
| 10 | NdnCs::NdnCs( int maxSize ) |
| 11 | : _maxSize(maxSize) { } |
| 12 | |
| 13 | //NdnCs::NdnCs( constNdnCs& orig ) { } |
| 14 | |
| 15 | NdnCs::~NdnCs( ) { } |
| 16 | |
| 17 | //Find corresponding CS entry for the given content name |
| 18 | CsIterator NdnCs::lookup( const string &prefix ) |
| 19 | { |
| 20 | QNThreadLock lock( &_csMutex ); |
| 21 | |
| 22 | CsIterator entry=_cs.find( prefix ); |
| 23 | if( entry!=_cs.end() ) promote( entry->second ); |
| 24 | return entry; |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | //Add entry to content store, if content store is full, use LRU replacement |
| 30 | void NdnCs::add( const string &contentName, int contentSize ) |
| 31 | { |
| 32 | QNThreadLock lock( &_csMutex ); |
| 33 | |
| 34 | _cs.erase( contentName ); // removing the old record |
| 35 | |
| 36 | if( _cs.size()==_maxSize ) |
| 37 | { |
| 38 | CsEntry *entry=_lru.back(); |
| 39 | _cs.erase( entry->contentName ); |
| 40 | _lru.pop_back( ); |
| 41 | } |
| 42 | |
| 43 | CsEntry ce; |
| 44 | ce.contentName = contentName; |
| 45 | ce.contentSize = contentSize; |
| 46 | |
| 47 | _cs[ contentName ] = ce; |
| 48 | |
| 49 | CsEntry *ce_in_hash = &(_cs[ contentName ]); |
| 50 | _lru.push_front( ce_in_hash ); |
| 51 | ce_in_hash->lruPosition = _lru.begin( ); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | //move the given CS entry to the head of the list |
| 56 | void NdnCs::promote( CsEntry &ce ) |
| 57 | { |
| 58 | // should not lock mutex. Otherwise deadlocks will be welcome |
| 59 | if( _lru.front() == &ce ) return; |
| 60 | |
| 61 | assert( *(ce.lruPosition)==&ce ); // should point to the same object |
| 62 | |
| 63 | // swaping positions in _lru |
| 64 | _lru.erase( ce.lruPosition ); |
| 65 | _lru.push_front( &ce ); |
| 66 | ce.lruPosition = _lru.begin( ); |
| 67 | |
| 68 | assert( *(ce.lruPosition)==&ce ); // should point to the same object |
| 69 | } |
| 70 | |
| 71 | void NdnCs::dump() |
| 72 | { |
| 73 | QNThreadLock lock( &_csMutex ); |
| 74 | |
| 75 | for( CsRangeIterator it=_cs.begin(); it!=_cs.end(); ++it ) |
| 76 | { |
| 77 | printf("-%s-", it->second.contentName.c_str() ); |
| 78 | } |
| 79 | |
| 80 | printf("\n"); |
| 81 | // list<CsEntry *>::reverse_iterator rit; |
| 82 | // |
| 83 | // for (rit = contentList->rbegin(); rit != contentList->rend(); rit ++) |
| 84 | // { |
| 85 | // temp = *rit; |
| 86 | // printf("=%s=", temp->contentName); |
| 87 | // } |
| 88 | // |
| 89 | // printf("\n"); |
| 90 | } |