AutoSave.java
4.29 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.jcr.vault.fs.io;
import com.day.jcr.vault.fs.spi.ProgressTracker;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AutoSave {
private static final Logger log = LoggerFactory.getLogger(AutoSave.class);
private int numModified;
private int lastSave;
private int threshold = 1024;
private final Set<String> missingMandatory = new HashSet<String>();
private ProgressTracker tracker;
private boolean dryRun;
private int debugFailEach;
private int debugSaveCount;
public AutoSave() {
}
public AutoSave(int threshold) {
this.threshold = threshold;
}
public AutoSave copy() {
AutoSave ret = new AutoSave();
ret.threshold = this.threshold;
ret.numModified = this.numModified;
ret.lastSave = this.lastSave;
ret.tracker = this.tracker;
ret.dryRun = this.dryRun;
ret.missingMandatory.addAll(this.missingMandatory);
ret.debugFailEach = this.debugFailEach;
return ret;
}
public void setTracker(ProgressTracker tracker) {
this.tracker = tracker;
}
public boolean isDryRun() {
return this.dryRun;
}
public void setDryRun(boolean dryRun) {
this.dryRun = dryRun;
}
public void setDebugFailEach(int debugFailEach) {
this.debugFailEach = debugFailEach;
}
public boolean needsSave() {
boolean res;
boolean bl = res = this.numModified - this.lastSave >= this.threshold;
if (res && !this.missingMandatory.isEmpty()) {
log.info("Threshold of {} reached but still unresolved mandatory items.", (Object)this.threshold);
res = false;
}
return res;
}
public void save(Session session) throws RepositoryException {
if (this.threshold == Integer.MAX_VALUE) {
log.debug("Save disabled.");
return;
}
int diff = this.numModified - this.lastSave;
Object[] arrobject = new Object[4];
arrobject[0] = this.threshold;
arrobject[1] = this.dryRun ? "dry run, reverting" : "saving";
arrobject[2] = diff;
arrobject[3] = this.missingMandatory.size();
log.info("Threshold of {} reached. {} approx {} transient changes. {} unresolved", arrobject);
if (this.tracker != null) {
if (this.dryRun) {
this.tracker.track("reverting approx " + diff + " nodes... (dry run)", "");
} else {
this.tracker.track("saving approx " + diff + " nodes...", "");
}
}
if (session != null) {
if (this.debugFailEach > 0 && this.debugSaveCount > 0 && this.debugSaveCount % this.debugFailEach == 0) {
String msg = String.format("Debugging provoked failure after %s saves.", this.debugSaveCount);
log.error(msg);
throw new RepositoryException(msg);
}
if (this.dryRun) {
session.refresh(false);
} else {
try {
session.save();
++this.debugSaveCount;
}
catch (RepositoryException e) {
log.error("error during auto save - retrying after refresh...");
session.refresh(true);
session.save();
++this.debugSaveCount;
}
}
}
this.lastSave = this.numModified;
}
public int getThreshold() {
return this.threshold;
}
public void setThreshold(int threshold) {
this.threshold = threshold;
}
public boolean modified(int num) {
this.numModified += num;
return this.needsSave();
}
public void markMissing(String path) {
this.missingMandatory.add(path);
}
public void markResolved(String path) {
this.missingMandatory.remove(path);
}
public String toString() {
return String.valueOf(this.threshold);
}
}