blob: 31a9fb353978aee9f68e4e9b907f3514aef65e5e [file] [log] [blame]
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#include "ccnx-content-store.h"
22#include "ns3/log.h"
23
24
25NS_LOG_COMPONENT_DEFINE ("CcnxContentStore");
26
27namespace ns3
28{
29
30CcnxContentStore::CcnxContentStore( int maxSize )
31 : m_maxSize(maxSize) { }
32
33CcnxContentStore::~CcnxContentStore( )
34 { }
35
36//Find corresponding CS entry for the given content name
37CsEntry*
38CcnxContentStore::Lookup(const string prefix )
39{
40 CriticalSection section(m_csMutex);
41
42 CsEntry *result = &(m_contentStore.at(prefix));
43
44 if(result != NULL)
45 Promote (*result);
46
47 return result;
48}
49
50//move the given CS entry to the head of the list
51void
52CcnxContentStore::Promote(CsEntry &ce )
53{
54 // should not lock mutex. Otherwise deadlocks will be welcome
55 if( m_LRU.front() == &ce ) return;
56
57 //assert( *(ce.lruPosition)==&ce ); // should point to the same object
58
59 // swaping positions in _lru
60 m_LRU.erase( ce.lruPosition );
61 m_LRU.push_front( &ce );
62 ce.lruPosition = m_LRU.begin( );
63
64 //assert( *(ce.lruPosition)==&ce ); // should point to the same object
65}
66
67//Add entry to content store, if content store is full, use LRU replacement
68void
69CcnxContentStore::Add( const string contentName, int contentSize )
70{
71 CriticalSection section(m_csMutex);
72
73 m_contentStore.erase(m_contentStore.find(contentName));
74
75 if((int)m_contentStore.size() == m_maxSize )
76 {
77 CsEntry *entry = m_LRU.back();
78 m_contentStore.erase(m_contentStore.find(entry->contentName));
79 m_LRU.pop_back( );
80 }
81
82 CsEntry ce;
83 ce.contentName = contentName;
84 ce.contentSize = contentSize;
85
86 m_contentStore[contentName] = ce;
87
88 CsEntry *ce_in_hash = &(m_contentStore.at(contentName));
89 m_LRU.push_front( ce_in_hash );
90 ce_in_hash->lruPosition = m_LRU.begin( );
91}
92
93void
94CcnxContentStore::Dump()
95{
96 CriticalSection section(m_csMutex);
97
98 BOOST_FOREACH(string_key_hash_t<CsEntry>::value_type i, m_contentStore)
99 {
100 NS_LOG_INFO ("Key = " << i.first << " Value = " << i.second.contentName);
101 }
102}
103}