This commit is contained in:
Daniel Mills 2021-08-10 06:06:32 -04:00
parent 97a62752c6
commit fc4377abaf
3 changed files with 45 additions and 13 deletions

View File

@ -44,6 +44,7 @@ import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPInputStream;
/** /**
* The mantle can store any type of data slice anywhere and manage regions & IO on it's own. * The mantle can store any type of data slice anywhere and manage regions & IO on it's own.
@ -103,7 +104,7 @@ public class Mantle {
public void flag(int x, int z, MantleFlag flag, boolean flagged) public void flag(int x, int z, MantleFlag flag, boolean flagged)
{ {
try { try {
get(x >> 5, z >> 5).get().get(x & 31, z & 31).flag(flag, flagged); get(x >> 5, z >> 5).get().getOrCreate(x & 31, z & 31).flag(flag, flagged);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -121,7 +122,7 @@ public class Mantle {
} }
try { try {
get(x >> 5, z >> 5).get().get(x & 31, z & 31).iterate(type, iterator); get(x >> 5, z >> 5).get().getOrCreate(x & 31, z & 31).iterate(type, iterator);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -131,7 +132,7 @@ public class Mantle {
public boolean hasFlag(int x, int z, MantleFlag flag) public boolean hasFlag(int x, int z, MantleFlag flag)
{ {
try { try {
get(x >> 5, z >> 5).get().get(x & 31, z & 31).isFlagged(flag); return get(x >> 5, z >> 5).get().getOrCreate(x & 31, z & 31).isFlagged(flag);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -297,8 +298,15 @@ public class Mantle {
*/ */
@RegionCoordinates @RegionCoordinates
private CompletableFuture<TectonicPlate> get(int x, int z) { private CompletableFuture<TectonicPlate> get(int x, int z) {
return ioBurst.completeValue(() -> hyperLock.withResult(x, z, () -> {
Long k = key(x, z); Long k = key(x, z);
TectonicPlate p = loadedRegions.get(k);
if(p != null)
{
return CompletableFuture.completedFuture(p);
}
return ioBurst.completeValue(() -> hyperLock.withResult(x, z, () -> {
lastUse.put(k, M.ms()); lastUse.put(k, M.ms());
TectonicPlate region = loadedRegions.get(k); TectonicPlate region = loadedRegions.get(k);
@ -330,7 +338,7 @@ public class Mantle {
region = new TectonicPlate(worldHeight); region = new TectonicPlate(worldHeight);
loadedRegions.put(k, region); loadedRegions.put(k, region);
Iris.debug("Created new Tectonic Plate (Due to Load Failure) " + C.DARK_GREEN + x + " " + z); Iris.debug("Created new Tectonic Plate " + C.DARK_GREEN + x + " " + z);
return region; return region;
})); }));
} }
@ -349,4 +357,25 @@ public class Mantle {
public static Long key(int x, int z) { public static Long key(int x, int z) {
return Cache.key(x, z); return Cache.key(x, z);
} }
public void saveAll() {
Iris.debug("Saving The Mantle " + C.DARK_AQUA + dataFolder.getAbsolutePath());
if (closed.get()) {
throw new RuntimeException("The Mantle is closed");
}
BurstExecutor b = ioBurst.burst(loadedRegions.size());
for (Long i : loadedRegions.keySet()) {
b.queue(() -> {
try {
loadedRegions.get(i).write(fileForRegion(dataFolder, i));
} catch (IOException e) {
e.printStackTrace();
}
});
}
b.complete();
Iris.debug("The Mantle has Saved " + C.DARK_AQUA + dataFolder.getAbsolutePath());
}
} }

View File

@ -18,6 +18,7 @@
package com.volmit.iris.util.mantle; package com.volmit.iris.util.mantle;
import com.volmit.iris.Iris;
import com.volmit.iris.util.collection.KSet; import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.collection.StateList; import com.volmit.iris.util.collection.StateList;
import com.volmit.iris.util.data.Varint; import com.volmit.iris.util.data.Varint;
@ -38,7 +39,6 @@ import java.util.concurrent.atomic.AtomicReferenceArray;
* Mantle Chunks are fully atomic & thread safe * Mantle Chunks are fully atomic & thread safe
*/ */
public class MantleChunk { public class MantleChunk {
private static final StateList state = MantleFlag.getStateList();
private final AtomicIntegerArray flags; private final AtomicIntegerArray flags;
private final AtomicReferenceArray<Matter> sections; private final AtomicReferenceArray<Matter> sections;
@ -51,6 +51,11 @@ public class MantleChunk {
public MantleChunk(int sectionHeight) { public MantleChunk(int sectionHeight) {
sections = new AtomicReferenceArray<>(sectionHeight); sections = new AtomicReferenceArray<>(sectionHeight);
flags = new AtomicIntegerArray(MantleFlag.values().length); flags = new AtomicIntegerArray(MantleFlag.values().length);
for (int i = 0; i < flags.length(); i++)
{
flags.set(i, 0);
}
} }
/** /**
@ -65,9 +70,9 @@ public class MantleChunk {
this(sectionHeight); this(sectionHeight);
int s = din.readByte(); int s = din.readByte();
for(String i : state.getEnabled(Varint.readUnsignedVarLong(din))) for(int i = 0; i < flags.length(); i++)
{ {
flags.set(MantleFlag.valueOf(i).ordinal(), 1); flags.set(i, din.readBoolean() ? 1 : 0);
} }
for (int i = 0; i < s; i++) { for (int i = 0; i < s; i++) {
@ -82,7 +87,7 @@ public class MantleChunk {
} }
public boolean isFlagged(MantleFlag flag) { public boolean isFlagged(MantleFlag flag) {
return flags.get(flags.get(flag.ordinal())) == 1; return flags.get(flag.ordinal()) == 1;
} }
/** /**
@ -152,14 +157,11 @@ public class MantleChunk {
*/ */
public void write(DataOutputStream dos) throws IOException { public void write(DataOutputStream dos) throws IOException {
dos.writeByte(sections.length()); dos.writeByte(sections.length());
long data = 0;
for (int i = 0; i < flags.length(); i++) { for (int i = 0; i < flags.length(); i++) {
state.set(data, MantleFlag.values()[i].name(), flags.get(i) == 1); dos.writeBoolean(flags.get(i) == 1);
} }
Varint.writeUnsignedVarLong(data, dos);
for (int i = 0; i < sections.length(); i++) { for (int i = 0; i < sections.length(); i++) {
if (exists(i)) { if (exists(i)) {
dos.writeBoolean(true); dos.writeBoolean(true);

View File

@ -24,6 +24,7 @@ import com.volmit.iris.util.format.C;
import java.io.*; import java.io.*;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.zip.GZIPOutputStream;
/** /**
* Tectonic Plates are essentially representations of regions in minecraft. * Tectonic Plates are essentially representations of regions in minecraft.