Skip to content

Commit f5c484e

Browse files
committed
More DLFS based refactoring and GUI updates
1 parent 1bb0299 commit f5c484e

File tree

10 files changed

+109
-62
lines changed

10 files changed

+109
-62
lines changed

convex-core/src/main/java/convex/core/data/ACell.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public void attachMemorySize(long memorySize) {
493493
this.memorySize=memorySize;
494494
assert (this.memorySize>0) : "Attempting to attach memory size "+memorySize+" to object of class "+Utils.getClassName(this);
495495
} else {
496-
assert (this.memorySize==memorySize) : "Attempting to attach memory size "+memorySize+" to object of class "+Utils.getClassName(this)+" which already has memorySize "+this.memorySize;
496+
assert (this.memorySize==memorySize) : "Trying to change memory size to "+memorySize+" with object of class "+Utils.getClassName(this)+" which already has memorySize "+this.memorySize;
497497
}
498498
}
499499

@@ -512,10 +512,10 @@ public void attachRef(Ref<?> ref) {
512512
* complete Cell can be represented in a single encoding.
513513
* @return true if completely encoded, false otherwise
514514
*/
515-
public boolean isCompletelyEncoded() {
515+
boolean isCompletelyEncoded() {
516516
if (memorySize==Format.FULL_EMBEDDED_MEMORY_SIZE) return true; // fast path for fully embedded
517517
if (!isCanonical()) {
518-
throw new Error("Checking whether a non-canonical cell is encoded. Not a good idea, any ref assumptions may be invalid: "+this.getType());
518+
throw new Error("Checking whether a non-canonical cell is encoded. Not a good idea: any Ref assumptions may be invalid for "+this.getType());
519519
}
520520
int n=getRefCount();
521521
for (int i=0; i<n; i++) {

convex-core/src/main/java/convex/core/data/Cells.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ public static boolean isCVM(ACell a) {
7979
return a.isCVMValue();
8080
}
8181

82+
/**
83+
* Checks if a Cell is completely encoded, i.e. has not external Refs
84+
* @param a Cell to check
85+
* @return True if completely encoded, false otherwise
86+
*/
87+
public static boolean isCompletelyEncoded(ACell a) {
88+
if (a==null) return true;
89+
return a.isCompletelyEncoded();
90+
}
91+
8292
/**
8393
* Checks if a Cell is a first class value
8494
* @param a Cell to check

convex-core/src/main/java/convex/dlfs/DLFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static DLFSProvider provider() {
2525
return PROVIDER;
2626
}
2727

28-
public static DLFileSystem createLocal() {
28+
public static DLFSLocal createLocal() {
2929
return DLFSLocal.create(provider());
3030
}
3131

convex-core/src/main/java/convex/dlfs/DLFileSystem.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import convex.core.data.Cells;
2020
import convex.core.data.Hash;
2121
import convex.core.data.prim.CVMLong;
22+
import convex.core.util.Utils;
2223
import convex.dlfs.impl.DLDirectoryStream;
2324
import convex.dlfs.impl.DLFSFileAttributes;
2425

@@ -86,6 +87,13 @@ public synchronized CVMLong updateTimestamp(long newTimestamp) {
8687
}
8788
return timestamp;
8889
}
90+
91+
/**
92+
* Updates the timestamp of the drive to the current system timestamp
93+
*/
94+
public synchronized CVMLong updateTimestamp() {
95+
return updateTimestamp(Utils.getCurrentTimestamp());
96+
}
8997

9098
@Override
9199
public boolean isOpen() {

convex-core/src/main/java/convex/dlfs/impl/DLFSLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public DLFSLocal(DLFSProvider dlfsProvider, String uriPath, AVector<ACell> rootN
3737
this.rootNode=rootNode;
3838
}
3939

40-
public static DLFileSystem create(DLFSProvider provider) {
40+
public static DLFSLocal create(DLFSProvider provider) {
4141
return new DLFSLocal(provider,null,DLFSNode.createDirectory(CVMLong.ZERO));
4242
}
4343

convex-core/src/main/java/etch/EtchStore.java

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,40 @@ public class EtchStore extends AStore {
3333
* Etch file instance for the current store
3434
*/
3535
private Etch etch;
36-
36+
3737
/**
3838
* Etch file instance for GC destination
3939
*/
4040
private Etch target;
4141

4242
public EtchStore(Etch etch) {
4343
this.etch = etch;
44-
this.target=null;
44+
this.target = null;
4545
etch.setStore(this);
4646
}
47-
47+
4848
/**
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+
*
5152
* @throws IOException If an IO exception occurs
5253
*/
5354
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+
5860
// copy across current root hash
5961
target.setRootHash(etch.getRootHash());
6062
}
61-
63+
6264
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+
}
6670
return etch;
6771
}
6872

@@ -75,7 +79,7 @@ private Etch getWriteEtch() {
7579
* @throws IOException If an IO error occurs
7680
*/
7781
public static EtchStore create(File file) throws IOException {
78-
file=Utils.ensurePath(file);
82+
file = Utils.ensurePath(file);
7983
Etch etch = Etch.create(file);
8084
return new EtchStore(etch);
8185
}
@@ -114,19 +118,22 @@ public static EtchStore createTemp() {
114118
public <T extends ACell> Ref<T> refForHash(Hash hash) {
115119
try {
116120
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);
121127
return (Ref<T>) existing;
122128
} catch (IOException e) {
123129
throw Utils.sneakyThrow(e);
124130
}
125131
}
126132

127133
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);
130137
return ref;
131138
}
132139

@@ -145,17 +152,18 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
145152
boolean topLevel) {
146153
// TODO: remove this?, probably dangerous if in different store
147154
// 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+
// }
152159

153160
// Get the value. If we are persisting, should be there!
154161
ACell cell = ref.getValue();
155-
162+
156163
// 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+
159167
// check store for existing ref first.
160168
boolean embedded = cell.isEmbedded();
161169
Hash hash = null;
@@ -170,16 +178,16 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
170178
}
171179
}
172180
}
173-
174-
if (requiredStatus<Ref.STORED) {
181+
182+
if (requiredStatus < Ref.STORED) {
175183
if (topLevel || !embedded) {
176184
addToCache(ref);
177185
}
178186
return ref;
179187
}
180-
188+
181189
// 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)) {
183191
// TODO: probably slow to rebuild these all the time!
184192
IRefFunction func = r -> {
185193
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
191199

192200
// perhaps need to update Ref
193201
if (cell != newObject) {
194-
202+
195203
ref = ref.withValue((T) newObject);
196-
cell=newObject;
204+
cell = newObject;
197205
cell.attachRef(ref); // make sure we are using current ref within new cell
198206
}
199207
}
200208

201209
// Actually write top level an non-embedded cells only
202210
if (topLevel || !embedded) {
203-
211+
204212
// Do actual write to store
205213
final Hash fHash = (hash != null) ? hash : ref.getHash();
206214
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);
209217
}
210218

211219
Ref<ACell> result;
@@ -214,10 +222,10 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
214222
ref = ref.withMinimumStatus(requiredStatus);
215223
cell.attachRef(ref); // make sure we are using current ref within cell
216224
result = etch.write(fHash, (Ref<ACell>) ref);
217-
225+
218226
if (!embedded) {
219227
// Ensure we have soft Refpointing to this store
220-
result=result.toSoft(this);
228+
result = result.toSoft(this);
221229
}
222230

223231
cell.attachRef(result);
@@ -228,12 +236,13 @@ public <T extends ACell> Ref<T> storeRef(Ref<T> ref, int requiredStatus, Consume
228236

229237
// call novelty handler if newly persisted non-embedded
230238
if (noveltyHandler != null) {
231-
if (!embedded) noveltyHandler.accept(result);
239+
if (!embedded)
240+
noveltyHandler.accept(result);
232241
}
233242
return (Ref<T>) result;
234243
} else {
235244
// no need to write, just tag updated status
236-
ref= ref.withMinimumStatus(requiredStatus);
245+
ref = ref.withMinimumStatus(requiredStatus);
237246
cell.attachRef(ref);
238247
return ref;
239248
}
@@ -263,12 +272,14 @@ public void close() {
263272

264273
/**
265274
* Ensure the store is fully persisted to disk
275+
*
266276
* @throws IOException If an IO error occurs
267277
*/
268-
public void flush() throws IOException {
278+
public void flush() throws IOException {
269279
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();
272283
}
273284

274285
public File getFile() {
@@ -283,16 +294,17 @@ public Hash getRootHash() throws IOException {
283294
@Override
284295
public <T extends ACell> Ref<T> setRootData(T data) throws IOException {
285296
// 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();
289300
etch.setRootHash(h);
290301
etch.writeDataLength(); // ensure data length updated for root data addition
291302
return ref;
292303
}
293304

294305
/**
295306
* Gets the underlying Etch instance
307+
*
296308
* @return Etch instance
297309
*/
298310
public Etch getEtch() {

convex-core/src/test/java/convex/core/TransactionTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import convex.core.data.AVector;
1313
import convex.core.data.Address;
14+
import convex.core.data.Cells;
1415
import convex.core.data.RecordTest;
1516
import convex.core.data.SignedData;
1617
import convex.core.data.Vectors;
@@ -70,7 +71,7 @@ public void testTransfer() {
7071
assertEquals(AMT+expectedFees,balanceDrop);
7172

7273
// We expect a Transfer to be completely encoded
73-
assertTrue(t1.isCompletelyEncoded());
74+
assertTrue(Cells.isCompletelyEncoded(t1));
7475

7576
doTransactionTests(t1);
7677
}
@@ -203,8 +204,8 @@ public void testCall() {
203204
assertCVMEquals(7,rc2.getResult());
204205

205206

206-
// We expect a short call to be completely encoded
207-
assertTrue(t1.isCompletelyEncoded());
207+
// We expect a short call transaction to be completely encoded
208+
assertTrue(Cells.isCompletelyEncoded(t1));
208209

209210
doTransactionTests(t1);
210211
doTransactionTests(t2);
@@ -218,7 +219,7 @@ public void testInvoke() {
218219
assertEquals(CVMLong.create(7),ctx.getResult());
219220

220221
// We expect a short Invoke to be completely encoded
221-
assertTrue(t1.isCompletelyEncoded());
222+
assertTrue(Cells.isCompletelyEncoded(t1));
222223

223224
doTransactionTests(t1);
224225
}

convex-core/src/test/java/convex/store/StoresTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public class StoresTest {
6060
// non-emebdded single Cell
6161
AString nv=Samples.NON_EMBEDDED_STRING;
6262
assertFalse(nv.isEmbedded());
63-
assertTrue(nv.isCompletelyEncoded());
63+
assertTrue(Cells.isCompletelyEncoded(nv));
6464

6565
// small fully embedded cell
6666
CVMLong ev=CVMLong.ONE;
6767
assertTrue(ev.isEmbedded());
68-
assertTrue(ev.isCompletelyEncoded());
68+
assertTrue(Cells.isCompletelyEncoded(ev));
6969

7070
AVector<?> v=Vectors.of(Vectors.of(nv,ev),nv,ev);
7171

convex-core/src/test/java/examples/EncodingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package examples;
22

33
import convex.core.data.AVector;
4+
import convex.core.data.Cells;
45
import convex.core.data.Vectors;
56
import convex.core.data.prim.CVMLong;
67

@@ -14,7 +15,7 @@ public static void main(String... args) {
1415
}
1516

1617
System.out.println(v.toString());
17-
System.out.println("Complete encoded: "+v.isCompletelyEncoded());
18+
System.out.println("Complete encoded: "+Cells.isCompletelyEncoded(v));
1819
System.out.println("Embedded: "+v.isEmbedded());
1920
System.out.println("Encoding Length: "+v.getEncodingLength());
2021
}

0 commit comments

Comments
 (0)