Debian Codegraph @

src/lib/object_store/Generation.cpp

raw
1 /*
2 * Copyright (c) 2013 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 Generation.cpp
29
30 Helper for generation number handling.
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "Generation.h"
36
37 // Factory
38 Generation* Generation::create(const std::string path, bool isToken /* = false */)
39 {
40 Generation* gen = new Generation(path, isToken);
41 if ((gen != NULL) && isToken && (gen->genMutex == NULL))
42 {
43 delete gen;
44
45 return NULL;
46 }
47 return gen;
48 }
49
50 // Destructor
51 Generation::~Generation()
52 {
53 if (isToken)
54 {
55 MutexFactory::i()->recycleMutex(genMutex);
56 }
57 }
58
59 // Synchronize from locked disk file
60 bool Generation::sync(File &objectFile)
61 {
62 if (isToken)
63 {
64 ERROR_MSG("Generation sync() called for a token");
65
66 return false;
67 }
68
69 unsigned long onDisk;
70
71 if (!objectFile.readULong(onDisk))
72 {
73 if (objectFile.isEOF())
74 {
75 onDisk = 0;
76 }
77 else
78 {
79 return false;
80 }
81 }
82
83 currentValue = onDisk;
84
85 return objectFile.seek(0L);
86 }
87
88 // Check if the target was updated
89 bool Generation::wasUpdated()
90 {
91 if (isToken)
92 {
93 MutexLocker lock(genMutex);
94
95 File genFile(path);
96
97 if (!genFile.isValid())
98 {
99 return true;
100 }
101
102 genFile.lock();
103
104 unsigned long onDisk;
105
106 if (!genFile.readULong(onDisk))
107 {
108 return true;
109 }
110
111 if (onDisk != currentValue)
112 {
113 currentValue = onDisk;
114 return true;
115 }
116
117 return false;
118 }
119 else
120 {
121 File objectFile(path);
122
123 if (!objectFile.isValid())
124 {
125 return true;
126 }
127
128 objectFile.lock();
129
130 unsigned long onDisk;
131
132 if (!objectFile.readULong(onDisk))
133 {
134 return true;
135 }
136
137 return (onDisk != currentValue);
138 }
139 }
140
141 // Update
142 void Generation::update()
143 {
144 pendingUpdate = true;
145 }
146
147 // Commit
148 void Generation::commit()
149 {
150 if (isToken)
151 {
152 MutexLocker lock(genMutex);
153
154 File genFile(path, true, true, true, false);
155
156 if (!genFile.isValid())
157 {
158 return;
159 }
160
161 genFile.lock();
162
163 if (genFile.isEmpty())
164 {
165 currentValue++;
166
167 if (currentValue == 0)
168 {
169 currentValue++;
170 }
171
172 pendingUpdate = false;
173
174 (void) genFile.writeULong(currentValue);
175
176 genFile.unlock();
177
178 return;
179 }
180
181 unsigned long onDisk;
182
183 bool bOK = true;
184
185 bOK = bOK && genFile.readULong(onDisk);
186 bOK = bOK && genFile.seek(0L);
187
188 if (pendingUpdate)
189 {
190 onDisk++;
191
192 if (onDisk == 0)
193 {
194 onDisk++;
195 }
196 }
197
198 bOK = bOK && genFile.writeULong(onDisk);
199
200 if (bOK)
201 {
202 currentValue = onDisk;
203
204 pendingUpdate = false;
205 }
206
207 genFile.unlock();
208 }
209 }
210
211 // Set the current value when read from disk
212 void Generation::set(unsigned long onDisk)
213 {
214 currentValue = onDisk;
215 }
216
217 // Return new value
218 unsigned long Generation::get()
219 {
220 pendingUpdate = false;
221
222 currentValue++;
223
224 if (currentValue == 0)
225 {
226 currentValue = 1;
227 }
228
229 return currentValue;
230 }
231
232 // Rollback (called when the new value failed to be written)
233 void Generation::rollback()
234 {
235 pendingUpdate = true;
236
237 if (currentValue != 1)
238 {
239 currentValue--;
240 }
241 }
242
243 // Constructor
244 Generation::Generation(const std::string inPath, bool inIsToken)
245 {
246 path = inPath;
247 isToken = inIsToken;
248 pendingUpdate = false;
249 currentValue = 0;
250 genMutex = NULL;
251
252 if (isToken)
253 {
254 genMutex = MutexFactory::i()->getMutex();
255
256 if (genMutex != NULL)
257 {
258 commit();
259 }
260 }
261 }