@@ -33,36 +33,40 @@ public class EtchStore extends AStore {
33
33
* Etch file instance for the current store
34
34
*/
35
35
private Etch etch ;
36
-
36
+
37
37
/**
38
38
* Etch file instance for GC destination
39
39
*/
40
40
private Etch target ;
41
41
42
42
public EtchStore (Etch etch ) {
43
43
this .etch = etch ;
44
- this .target = null ;
44
+ this .target = null ;
45
45
etch .setStore (this );
46
46
}
47
-
47
+
48
48
/**
49
- * Starts a GC cycle. Creates a new Etch file for collection, and directs all new writes to
50
- * the new store
49
+ * Starts a GC cycle. Creates a new Etch file for collection, and directs all
50
+ * new writes to the new store
51
+ *
51
52
* @throws IOException If an IO exception occurs
52
53
*/
53
54
public synchronized void startGC () throws IOException {
54
- if (target !=null ) throw new Error ("Already collecting!" );
55
- File temp =new File (etch .getFile ().getCanonicalPath ()+"~" );
56
- target =Etch .create (temp );
57
-
55
+ if (target != null )
56
+ throw new Error ("Already collecting!" );
57
+ File temp = new File (etch .getFile ().getCanonicalPath () + "~" );
58
+ target = Etch .create (temp );
59
+
58
60
// copy across current root hash
59
61
target .setRootHash (etch .getRootHash ());
60
62
}
61
-
63
+
62
64
private Etch getWriteEtch () {
63
- if (target !=null ) synchronized (this ) {
64
- if (target !=null ) return target ;
65
- }
65
+ if (target != null )
66
+ synchronized (this ) {
67
+ if (target != null )
68
+ return target ;
69
+ }
66
70
return etch ;
67
71
}
68
72
@@ -75,7 +79,7 @@ private Etch getWriteEtch() {
75
79
* @throws IOException If an IO error occurs
76
80
*/
77
81
public static EtchStore create (File file ) throws IOException {
78
- file = Utils .ensurePath (file );
82
+ file = Utils .ensurePath (file );
79
83
Etch etch = Etch .create (file );
80
84
return new EtchStore (etch );
81
85
}
@@ -114,19 +118,22 @@ public static EtchStore createTemp() {
114
118
public <T extends ACell > Ref <T > refForHash (Hash hash ) {
115
119
try {
116
120
Ref <ACell > existing = (Ref <ACell >) refCache .getCell (hash );
117
- if (existing !=null ) return (Ref <T >) existing ;
118
-
119
- if (hash ==Hash .NULL_HASH ) return (Ref <T >) Ref .NULL_VALUE ;
120
- existing = readStoreRef (hash );
121
+ if (existing != null )
122
+ return (Ref <T >) existing ;
123
+
124
+ if (hash == Hash .NULL_HASH )
125
+ return (Ref <T >) Ref .NULL_VALUE ;
126
+ existing = readStoreRef (hash );
121
127
return (Ref <T >) existing ;
122
128
} catch (IOException e ) {
123
129
throw Utils .sneakyThrow (e );
124
130
}
125
131
}
126
132
127
133
public <T extends ACell > Ref <T > readStoreRef (Hash hash ) throws IOException {
128
- Ref <T > ref =etch .read (hash );
129
- if (ref !=null ) refCache .putCell (ref );
134
+ Ref <T > ref = etch .read (hash );
135
+ if (ref != null )
136
+ refCache .putCell (ref );
130
137
return ref ;
131
138
}
132
139
@@ -145,17 +152,18 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
145
152
boolean topLevel ) {
146
153
// TODO: remove this?, probably dangerous if in different store
147
154
// first check if the Ref is already persisted to required level
148
- //if (ref.getStatus() >= requiredStatus) {
149
- // // we are done as long as not top level
150
- // if (!topLevel) return ref;
151
- //}
155
+ // if (ref.getStatus() >= requiredStatus) {
156
+ // // we are done as long as not top level
157
+ // if (!topLevel) return ref;
158
+ // }
152
159
153
160
// Get the value. If we are persisting, should be there!
154
161
ACell cell = ref .getValue ();
155
-
162
+
156
163
// Quick handling for null
157
- if (cell == null ) return (Ref <T >) Ref .NULL_VALUE ;
158
-
164
+ if (cell == null )
165
+ return (Ref <T >) Ref .NULL_VALUE ;
166
+
159
167
// check store for existing ref first.
160
168
boolean embedded = cell .isEmbedded ();
161
169
Hash hash = null ;
@@ -170,16 +178,16 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
170
178
}
171
179
}
172
180
}
173
-
174
- if (requiredStatus < Ref .STORED ) {
181
+
182
+ if (requiredStatus < Ref .STORED ) {
175
183
if (topLevel || !embedded ) {
176
184
addToCache (ref );
177
185
}
178
186
return ref ;
179
187
}
180
-
188
+
181
189
// beyond STORED level, need to recursively persist child refs if they exist
182
- if ((requiredStatus > Ref .STORED )&& (cell .getRefCount ()> 0 )) {
190
+ if ((requiredStatus > Ref .STORED ) && (cell .getRefCount () > 0 )) {
183
191
// TODO: probably slow to rebuild these all the time!
184
192
IRefFunction func = r -> {
185
193
return storeRef ((Ref <ACell >) r , requiredStatus , noveltyHandler , false );
@@ -191,21 +199,21 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
191
199
192
200
// perhaps need to update Ref
193
201
if (cell != newObject ) {
194
-
202
+
195
203
ref = ref .withValue ((T ) newObject );
196
- cell = newObject ;
204
+ cell = newObject ;
197
205
cell .attachRef (ref ); // make sure we are using current ref within new cell
198
206
}
199
207
}
200
208
201
209
// Actually write top level an non-embedded cells only
202
210
if (topLevel || !embedded ) {
203
-
211
+
204
212
// Do actual write to store
205
213
final Hash fHash = (hash != null ) ? hash : ref .getHash ();
206
214
if (log .isTraceEnabled ()) {
207
- log .trace ( "Etch persisting at status=" + requiredStatus + " hash = 0x"
208
- + fHash . toHexString () + " ref of class " + Utils .getClassName (cell ) + " with store " + this );
215
+ log .trace ("Etch persisting at status=" + requiredStatus + " hash = 0x" + fHash . toHexString ()
216
+ + " ref of class " + Utils .getClassName (cell ) + " with store " + this );
209
217
}
210
218
211
219
Ref <ACell > result ;
@@ -214,10 +222,10 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
214
222
ref = ref .withMinimumStatus (requiredStatus );
215
223
cell .attachRef (ref ); // make sure we are using current ref within cell
216
224
result = etch .write (fHash , (Ref <ACell >) ref );
217
-
225
+
218
226
if (!embedded ) {
219
227
// Ensure we have soft Refpointing to this store
220
- result = result .toSoft (this );
228
+ result = result .toSoft (this );
221
229
}
222
230
223
231
cell .attachRef (result );
@@ -228,12 +236,13 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
228
236
229
237
// call novelty handler if newly persisted non-embedded
230
238
if (noveltyHandler != null ) {
231
- if (!embedded ) noveltyHandler .accept (result );
239
+ if (!embedded )
240
+ noveltyHandler .accept (result );
232
241
}
233
242
return (Ref <T >) result ;
234
243
} else {
235
244
// no need to write, just tag updated status
236
- ref = ref .withMinimumStatus (requiredStatus );
245
+ ref = ref .withMinimumStatus (requiredStatus );
237
246
cell .attachRef (ref );
238
247
return ref ;
239
248
}
@@ -263,12 +272,14 @@ public void close() {
263
272
264
273
/**
265
274
* Ensure the store is fully persisted to disk
275
+ *
266
276
* @throws IOException If an IO error occurs
267
277
*/
268
- public void flush () throws IOException {
278
+ public void flush () throws IOException {
269
279
etch .flush ();
270
- Etch target =this .target ;
271
- if (target !=null ) target .flush ();
280
+ Etch target = this .target ;
281
+ if (target != null )
282
+ target .flush ();
272
283
}
273
284
274
285
public File getFile () {
@@ -283,16 +294,17 @@ public Hash getRootHash() throws IOException {
283
294
@ Override
284
295
public <T extends ACell > Ref <T > setRootData (T data ) throws IOException {
285
296
// Ensure data if persisted at sufficient level
286
- Ref <T > ref = storeTopRef (data .getRef (), Ref .PERSISTED ,null );
287
- Hash h = ref .getHash ();
288
- Etch etch = getWriteEtch ();
297
+ Ref <T > ref = storeTopRef (data .getRef (), Ref .PERSISTED , null );
298
+ Hash h = ref .getHash ();
299
+ Etch etch = getWriteEtch ();
289
300
etch .setRootHash (h );
290
301
etch .writeDataLength (); // ensure data length updated for root data addition
291
302
return ref ;
292
303
}
293
304
294
305
/**
295
306
* Gets the underlying Etch instance
307
+ *
296
308
* @return Etch instance
297
309
*/
298
310
public Etch getEtch () {
0 commit comments