forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectcache.cc
47 lines (37 loc) · 1.09 KB
/
objectcache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) 2012-2014 Konstantin Isakov <[email protected]> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include "objectcache.hh"
ObjectCache::ObjectCache( unsigned maxSize_ ): maxObjects( maxSize_ ),
totalObjects( 0 )
{
}
bool ObjectCache::remove( ObjectId const & id )
{
Objects tmp;
tmp.push_back( Object() );
tmp.back().id = id;
ObjectMap::iterator i = objectMap.find( tmp.begin() );
if ( i == objectMap.end() )
return false;
// Make sure that in case a destructor raises an exception, the cache
// is left in a consistent state.
Reference * ref = (*i)->reference;
objects.erase( *i );
objectMap.erase( i );
--totalObjects;
delete ref;
return true;
}
void ObjectCache::clear()
{
for ( Objects::iterator i = objects.begin(); i != objects.end(); )
{
// Make sure that in case a destructor raises an exception, the cache
// is left in a consistent state.
Reference * ref = i->reference;
objectMap.erase( i );
objects.erase( i++ );
--totalObjects;
delete ref;
}
}