summaryrefslogtreecommitdiff
path: root/cpp/demo/Freeze/customEvictor/Evictor.h
blob: 8f3782a2f73148cf46df8cb57f167e0dca41edb3 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef EVICTOR_H
#define EVICTOR_H

#include <IceUtil/IceUtil.h>
#include <Ice/Ice.h>
#include <CurrentDatabase.h>
#include <ItemI.h>
#include <list>

struct EvictorEntry;
typedef IceUtil::Handle<EvictorEntry> EvictorEntryPtr;

//
// A map that allows lookups while loading from storage.
//
class EvictorCache : public IceUtil::Cache<Ice::Identity, EvictorEntry>
{
public:

    EvictorCache(CurrentDatabase&);

protected:

    virtual EvictorEntryPtr load(const Ice::Identity&);
    virtual void pinned(const EvictorEntryPtr&, Position);

private:

    CurrentDatabase& _currentDb;
};

//
// We'll keep our entries in a queue.
//
typedef std::list<EvictorEntryPtr> EvictorQueue;

struct EvictorEntry : public Ice::LocalObject
{
    EvictorEntry(const ItemIPtr&);

    //
    // Immutable after initialization / insertion into the Cache map.
    //
    const Ice::ObjectPtr servant;
    EvictorCache::Position cachePosition;
   
    //
    // Protected by the Evictor mutex.
    //
    EvictorQueue::iterator queuePosition;
    int useCount;
    bool stale; // stale is true when the entry is no longer (or not yet) in the Cache map
};

class Evictor : public Ice::ServantLocator
{
public:
    
    Evictor(CurrentDatabase&, int);
    
    virtual Ice::ObjectPtr locate(const Ice::Current&, Ice::LocalObjectPtr&);
    virtual void finished(const Ice::Current&, const Ice::ObjectPtr&, const Ice::LocalObjectPtr&);
    virtual void deactivate(const std::string&);
    
private:

    void evict();

    EvictorCache _cache;
    IceUtil::Mutex _mutex;

    EvictorQueue _queue;
    int _queueSize;
    int _size;
};

#endif