-
Notifications
You must be signed in to change notification settings - Fork 160
/
CleanerIteration.java
86 lines (70 loc) · 2.35 KB
/
CleanerIteration.java
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) 2022 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/
package org.eclipse.rdf4j.sail.helpers;
import java.lang.ref.Cleaner;
import org.eclipse.rdf4j.common.concurrent.locks.diagnostics.ConcurrentCleaner;
import org.eclipse.rdf4j.common.iteration.CloseableIteration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class CleanerIteration<E, X extends Exception> implements CloseableIteration<E, X> {
private static final Logger logger = LoggerFactory.getLogger(CleanerIteration.class);
private final CloseableIteration<E, X> delegate;
private final Cleaner.Cleanable cleanable;
private final CleanableState<E, X> state;
public CleanerIteration(CloseableIteration<E, X> delegate, ConcurrentCleaner cleaner) {
this.delegate = delegate;
this.state = new CleanableState<>(delegate);
this.cleanable = cleaner.register(this, state);
}
@Override
public void close() throws X {
state.close();
cleanable.clean();
}
@Override
public boolean hasNext() throws X {
return delegate.hasNext();
}
@Override
public E next() throws X {
return delegate.next();
}
@Override
public void remove() throws X {
delegate.remove();
}
private final static class CleanableState<E, X extends Exception> implements Runnable {
private final CloseableIteration<E, X> iteration;
private boolean closed = false;
public CleanableState(CloseableIteration<E, X> iteration) {
this.iteration = iteration;
}
@Override
public void run() {
if (!closed) {
try {
logger.warn(
"Forced closing of unclosed iteration. Set the system property 'org.eclipse.rdf4j.repository.debug' to 'true' to get stack traces.");
iteration.close();
} catch (Exception e) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new RuntimeException(e);
}
}
}
public void close() throws X {
closed = true;
iteration.close();
}
}
}