comparison artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 314:31ee2b3b5a57

forward list of deleted collections and artifacts from data cleaner to backend to backend listeners. artifacts/trunk@2445 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 02 Aug 2011 11:11:59 +0000
parents f33401ea2a6c
children 03e508e57b85
comparison
equal deleted inserted replaced
313:ddc35c950e97 314:31ee2b3b5a57
59 * @param bytes The bytes of the serialized artifact. 59 * @param bytes The bytes of the serialized artifact.
60 * @return The revived artfiact. 60 * @return The revived artfiact.
61 */ 61 */
62 Artifact reviveArtifact(String factoryName, byte [] bytes); 62 Artifact reviveArtifact(String factoryName, byte [] bytes);
63 63
64 void killedArtifacts(List<String> identifiers);
65 void killedCollections(List<String> identifiers);
66
64 } // interface ArtifactReviver 67 } // interface ArtifactReviver
65 68
66 public interface LockedIdsProvider { 69 public interface LockedIdsProvider {
67 Set<Integer> getLockedIds(); 70 Set<Integer> getLockedIds();
68 } // interface LockedIdsProvider 71 } // interface LockedIdsProvider
203 logger.warn("Cleaner sleep time defaults to " + SLEEP_DEFAULT); 206 logger.warn("Cleaner sleep time defaults to " + SLEEP_DEFAULT);
204 } 207 }
205 return SLEEP_DEFAULT; 208 return SLEEP_DEFAULT;
206 } 209 }
207 210
208 private static final class IdData { 211 private static class IdIdentifier {
209 212
210 int id; 213 int id;
214 String identifier;
215
216 private IdIdentifier(int id, String identifier) {
217 this.id = id;
218 this.identifier = identifier;
219 }
220 } // class IdIdentifier
221
222 private static final class IdData
223 extends IdIdentifier
224 {
211 byte [] data; 225 byte [] data;
212 String factoryName; 226 String factoryName;
213 227
214 public IdData(int id, String factoryName, byte [] data) { 228 public IdData(
215 this.id = id; 229 int id,
230 String factoryName,
231 byte [] data,
232 String identifier
233 ) {
234 super(id, identifier);
216 this.factoryName = factoryName; 235 this.factoryName = factoryName;
217 this.data = data; 236 this.data = data;
218 } 237 }
219 } // class IdData 238 } // class IdData
220 239
234 Connection connection = null; 253 Connection connection = null;
235 PreparedStatement fetchIds = null; 254 PreparedStatement fetchIds = null;
236 PreparedStatement stmnt = null; 255 PreparedStatement stmnt = null;
237 ResultSet result = null; 256 ResultSet result = null;
238 257
239 int removedCollections = 0;
240 int removedArtifacts = 0;
241
242 DataSource dataSource = dbConnection.getDataSource(); 258 DataSource dataSource = dbConnection.getDataSource();
243 259
244 Set<Integer> lockedIds = lockedIdsProvider != null 260 Set<Integer> lockedIds = lockedIdsProvider != null
245 ? lockedIdsProvider.getLockedIds() 261 ? lockedIdsProvider.getLockedIds()
246 : EMPTY_IDS; 262 : EMPTY_IDS;
247 263
248 String questionMarks = lockedIds.isEmpty() 264 String questionMarks = lockedIds.isEmpty()
249 ? "-666" // XXX: A bit hackish. 265 ? "-666" // XXX: A bit hackish.
250 : StringUtils.repeat('?', lockedIds.size(), ','); 266 : StringUtils.repeat('?', lockedIds.size(), ',');
267
268 List<String> deletedCollections = new ArrayList<String>();
269 List<String> deletedArtifacts = new ArrayList<String>();
251 270
252 try { 271 try {
253 connection = dataSource.getConnection(); 272 connection = dataSource.getConnection();
254 connection.setAutoCommit(false); 273 connection.setAutoCommit(false);
255 274
271 fetchIds.setInt(idx, id); 290 fetchIds.setInt(idx, id);
272 stmnt .setInt(idx, id); 291 stmnt .setInt(idx, id);
273 ++idx; 292 ++idx;
274 } 293 }
275 294
276 ArrayList<Integer> cs = new ArrayList<Integer>(); 295 ArrayList<IdIdentifier> cs = new ArrayList<IdIdentifier>();
277 result = stmnt.executeQuery(); 296 result = stmnt.executeQuery();
278 while (result.next()) { 297 while (result.next()) {
279 cs.add(result.getInt(1)); 298 cs.add(new IdIdentifier(
299 result.getInt(1),
300 result.getString(2)));
280 } 301 }
281 302
282 result.close(); result = null; 303 result.close(); result = null;
283 stmnt.close(); stmnt = null; 304 stmnt.close(); stmnt = null;
284 305
285 // delete collection items 306 // delete collection items
286 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION_ITEMS); 307 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION_ITEMS);
287 308
288 for (Integer id: cs) { 309 for (IdIdentifier id: cs) {
289 stmnt.setInt(1, id); 310 stmnt.setInt(1, id.id);
290 stmnt.execute(); 311 stmnt.execute();
291 } 312 }
292 313
293 stmnt.close(); stmnt = null; 314 stmnt.close(); stmnt = null;
294 315
295 // delete collections 316 // delete collections
296 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION); 317 stmnt = connection.prepareStatement(SQL_DELETE_COLLECTION);
297 318
298 for (Integer id: cs) { 319 for (IdIdentifier id: cs) {
299 stmnt.setInt(1, id); 320 stmnt.setInt(1, id.id);
300 stmnt.execute(); 321 stmnt.execute();
322 deletedCollections.add(id.identifier);
301 } 323 }
302 324
303 stmnt.close(); stmnt = null; 325 stmnt.close(); stmnt = null;
304 connection.commit(); 326 connection.commit();
305 327
306 removedCollections = cs.size(); cs = null; 328 cs = null;
307 329
308 // remove artifacts 330 // remove artifacts
309 stmnt = connection.prepareStatement(SQL_DELETE_ARTIFACT); 331 stmnt = connection.prepareStatement(SQL_DELETE_ARTIFACT);
310 332
311 for (;;) { 333 for (;;) {
315 337
316 while (result.next()) { 338 while (result.next()) {
317 ids.add(new IdData( 339 ids.add(new IdData(
318 result.getInt(1), 340 result.getInt(1),
319 result.getString(2), 341 result.getString(2),
320 result.getBytes(3))); 342 result.getBytes(3),
343 result.getString(4)));
321 } 344 }
322 345
323 result.close(); result = null; 346 result.close(); result = null;
324 347
325 if (ids.isEmpty()) { 348 if (ids.isEmpty()) {
342 } 365 }
343 } 366 }
344 catch (Exception e) { 367 catch (Exception e) {
345 logger.error(e.getLocalizedMessage(), e); 368 logger.error(e.getLocalizedMessage(), e);
346 } 369 }
370
371 deletedArtifacts.add(idData.identifier);
347 } // for all fetched data 372 } // for all fetched data
348
349 removedArtifacts += ids.size();
350 } 373 }
351 } 374 }
352 catch (SQLException sqle) { 375 catch (SQLException sqle) {
353 logger.error(sqle.getLocalizedMessage(), sqle); 376 logger.error(sqle.getLocalizedMessage(), sqle);
354 } 377 }
369 try { connection.close(); } 392 try { connection.close(); }
370 catch (SQLException sqle) {} 393 catch (SQLException sqle) {}
371 } 394 }
372 } 395 }
373 396
374 logger.info("collections removed: " + removedCollections); 397 reviver.killedCollections(deletedCollections);
375 logger.info("artifacts removed: " + removedArtifacts); 398 reviver.killedArtifacts(deletedArtifacts);
399
400 if (logger.isDebugEnabled()) {
401 logger.debug(
402 "collections removed: " + deletedCollections.size());
403 logger.debug(
404 "artifacts removed: " + deletedArtifacts.size());
405 }
376 } 406 }
377 407
378 /** 408 /**
379 * The main code of the cleaner. It sleeps for the configured 409 * The main code of the cleaner. It sleeps for the configured
380 * nap time, cleans up the database, sleeps again and so on. 410 * nap time, cleans up the database, sleeps again and so on.

http://dive4elements.wald.intevation.org