summaryrefslogtreecommitdiff
path: root/cpp/src/Freeze/StrategyI.cpp
blob: edf5ec1f9eb6130c6158cb0ef6429bfe7adb89c6 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

#include <Freeze/StrategyI.h>

using namespace std;
using namespace Freeze;
using namespace Ice;

LocalObjectPtr
Freeze::EvictionStrategyI::activatedObject(const Identity& ident,
                                           const ObjectPtr& servant)
{
    CookiePtr cookie = new Cookie;
    cookie->mutated = false;
    return cookie;
}

void
Freeze::EvictionStrategyI::destroyedObject(const Identity& ident, const LocalObjectPtr& cookie)
{
    // Nothing to do
}

void
Freeze::EvictionStrategyI::evictedObject(const ObjectStorePtr& store,
                                         const Identity& ident,
                                         const ObjectPtr& servant,
                                         const LocalObjectPtr& cookie)
{
    //
    // Only store the object's persistent state if it has been mutated.
    //
    CookiePtr c = CookiePtr::dynamicCast(cookie);
    assert(c);

    if(c->mutated)
    {
        store->save(ident, servant);
        c->mutated = false;
    }
}


void
Freeze::EvictionStrategyI::savedObject(const ObjectStorePtr& store,
				       const Identity& ident,
				       const ObjectPtr& servant,
				       const LocalObjectPtr& cookie,
				       Ice::Int usageCount)
{
    assert(usageCount > 0);

    if(usageCount == 1)
    {
	CookiePtr c = CookiePtr::dynamicCast(cookie);
	assert(c);
	c->mutated = false;
    }    
}


void
Freeze::EvictionStrategyI::preOperation(const ObjectStorePtr& store,
                                        const Identity& ident,
                                        const ObjectPtr& servant,
                                        bool mutating,
                                        const LocalObjectPtr& cookie)
{
    if(mutating)
    {
        CookiePtr c = CookiePtr::dynamicCast(cookie);
        assert(c);
        c->mutated = true;
    }
}

void
Freeze::EvictionStrategyI::postOperation(const ObjectStorePtr& store,
                                         const Identity& ident,
                                         const ObjectPtr& servant,
                                         bool mutating,
                                         const LocalObjectPtr& cookie)
{
    // Nothing to do
}

void
Freeze::EvictionStrategyI::destroy()
{
    // Nothing to do
}

LocalObjectPtr
Freeze::IdleStrategyI::activatedObject(const Identity& ident,
                                       const ObjectPtr& servant)
{
    CookiePtr cookie = new Cookie;
    cookie->mutated = false;
    cookie->mutatingCount = 0;
    return cookie;
}

void
Freeze::IdleStrategyI::destroyedObject(const Identity& ident, const LocalObjectPtr& cookie)
{
    // Nothing to do
}

void
Freeze::IdleStrategyI::evictedObject(const ObjectStorePtr& store,
				     const Identity& ident,
				     const ObjectPtr& servant,
				     const LocalObjectPtr& cookie)
{
    //
    // The object must reach the idle state in order for it to be
    // evicted, therefore the object should have already been saved
    // by invokedObject.
    //
    CookiePtr c = CookiePtr::dynamicCast(cookie);
    assert(c);
    assert(!c->mutated);
}

void
Freeze::IdleStrategyI::savedObject(const ObjectStorePtr& store,
				   const Identity& ident,
				   const ObjectPtr& servant,
				   const LocalObjectPtr& cookie,
				   Ice::Int usageCount)
{
    assert(usageCount > 0);

    if(usageCount == 1)
    {
	CookiePtr c = CookiePtr::dynamicCast(cookie);
	assert(c);
	c->mutated = false;
    }
}



void
Freeze::IdleStrategyI::preOperation(const ObjectStorePtr& store,
                                    const Identity& ident,
                                    const ObjectPtr& servant,
                                    bool mutating,
                                    const LocalObjectPtr& cookie)
{
    CookiePtr c = CookiePtr::dynamicCast(cookie);
    assert(c);
    if(mutating)
    {
        ++c->mutatingCount;
        c->mutated = true;
    }
}

void
Freeze::IdleStrategyI::postOperation(const ObjectStorePtr& store,
                                     const Identity& ident,
                                     const ObjectPtr& servant,
                                     bool mutating,
                                     const LocalObjectPtr& cookie)
{
    CookiePtr c = CookiePtr::dynamicCast(cookie);
    assert(c);
    if(mutating)
    {
        assert(c->mutatingCount >= 1);
        --c->mutatingCount;
    }
    if(c->mutatingCount == 0 && c->mutated)
    {
        //
        // Only store the object's persistent state if the object is idle
        // and it has been mutated.
        //
        store->save(ident, servant);
        c->mutated = false;
    }
}

void
Freeze::IdleStrategyI::destroy()
{
    // Nothing to do
}