Well, some useful work on caching and data retreival

master
rarkenin 2013-06-20 15:29:10 -04:00
parent 6d7e59f9d5
commit 8af8fd83d9
5 changed files with 87 additions and 74 deletions

View File

@ -7,6 +7,7 @@
<classpathentry kind="lib" path="lib/js-14.jar"/>
<classpathentry kind="lib" path="lib/commons-cli-1.2-sources.jar"/>
<classpathentry kind="lib" path="lib/db4o-8.0.249.16098-all-java5.jar"/>
<classpathentry kind="lib" path="lib/KittyCache-1.2.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="lib/eventbus.jar"/>
<classpathentry kind="lib" path="lib/j-ogg-oggd.jar"/>

View File

@ -9,4 +9,8 @@ public ChunkNotFoundException(long x, long y, long z) {
this.z = z;
}
public ChunkNotFoundException(Position pos) {
// TODO Auto-generated constructor stub
}
}

View File

@ -20,97 +20,77 @@ import com.db4o.query.Query;
import java.util.UUID;
public class MapDatabase {
static ObjectContainer mapDb;
static ObjectContainer nodeDb;
static ObjectContainer entityDb;
static ObjectContainer banDb;
public MapDatabase(String name, boolean create) throws MapDatabaseException, MossWorldLoadException {
if(!name.matches("^[A-Z[a-z[0-9[ ]]]]+$")){
throw new MossWorldLoadException("World name contains invalid characters");
private static ObjectContainer mapDb;
private static ObjectContainer nodeDb;
private static ObjectContainer entityDb;
private static ObjectContainer banDb;
public static void init(String name, boolean create)
throws MapDatabaseException, MossWorldLoadException {
if (!name.matches("^[A-Z[a-z[0-9[ ]]]]+$")) {
throw new MossWorldLoadException(
"World name contains invalid characters");
}
try {
mapDb=Db4oEmbedded.openFile("worlds/"+name+"mapdb");
entityDb=Db4oEmbedded.openFile("worlds/"+name+"entities");
banDb=Db4oEmbedded.openFile("worlds/"+name+"nodes");
mapDb = Db4oEmbedded.openFile("worlds/" + name + "mapdb");
entityDb = Db4oEmbedded.openFile("worlds/" + name + "entities");
banDb = Db4oEmbedded.openFile("worlds/" + name + "nodes");
} catch (Db4oIOException | DatabaseFileLockedException
| IncompatibleFileFormatException | OldFormatException
| DatabaseReadOnlyException e) {
e.printStackTrace();
throw new MossWorldLoadException("Database loading failed.");
}
}
public void close() {
public static void close() {
mapDb.commit();
mapDb.close();
}
public MapChunk getChunk(final long x, final long y, final long z) throws ChunkNotFoundException, MapDatabaseException{
//comment out slower crap
/* List<MapChunkPacked> list = mapDb.query(new Predicate<MapChunkPacked>() {
public boolean match(MapChunkPacked candidate) {
return (candidate.x==x&&candidate.y==y&&candidate.z==z);
}
});
*/
Query query=mapDb.query();
public static MapChunk getChunk(final Position pos, final boolean generate)
throws ChunkNotFoundException, MapDatabaseException {
// comment out slower crap
/*
* List<MapChunkPacked> list = mapDb.query(new
* Predicate<MapChunkPacked>() { public boolean match(MapChunkPacked
* candidate) { return (candidate.x==x&&candidate.y==y&&candidate.z==z);
* } });
*/
Query query = mapDb.query();
query.constrain(MapChunkPacked.class);
query.descend("x").constrain(new Long(x));
query.descend("y").constrain(new Long(y));
query.descend("z").constrain(new Long(z));
List<MapChunkPacked> list=query.execute();
if(list.size()==0) throw new ChunkNotFoundException(x, y, z);
if(list.size()>1) throw new MapDatabaseException(MapDatabaseException.SEVERITY_CORRUPT_REPARABLE, "Duplicate chunk "+x+", "+y+", "+z+".");
query.descend("pos").constrain(pos);
List<MapChunkPacked> list = query.execute();
if (list.size() == 0){
if(generate){
return /* Mapgen.generate(pos);*/ null;
}
}
if (list.size() > 1)
throw new MapDatabaseException(
MapDatabaseException.SEVERITY_CORRUPT_REPARABLE,
"Duplicate chunk " + pos.toString() + ".");
return list.get(0).unpack();
}
/**
* @param args
* @throws MossWorldLoadException
* @throws MapDatabaseException
* @throws MossWorldLoadException
* @throws MapDatabaseException
*/
public static void main(String[] args) throws MapDatabaseException, MossWorldLoadException {
//TESTING ONLY
MapDatabase ourDb=new MapDatabase("test2", true);
for(int i=1; i<=320; i++){
//ourDb.add(new MapChunkPacked(i, i, i, UUID.randomUUID().toString(), UUID.randomUUID().toString()));
System.out.println(i);
}
System.out.println("begin prefetch");
try {
ourDb.getChunk(1, 1, 1);
} catch (ChunkNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("end prefetch");
final long startTime = System.currentTimeMillis();
int ed=0;
for(int i=1; i<=64000; i++){
//System.out.println(i);
try {
ourDb.getChunk(i, i, i);
} catch (ChunkNotFoundException e) {
ed++;
}
}System.out.println("Total execution time: " + (System.currentTimeMillis() - startTime) );
System.out.println(ed);
}
private void addMapChunk(MapChunkPacked mapChunkPacked) {
static void addMapChunk(MapChunkPacked mapChunkPacked) {
mapDb.store(mapChunkPacked);
}
private void addMapChunk(MapChunk mapChunk) {
mapDb.store(mapChunk.pack());
}
static void addMapChunk(MapChunk mapChunk) {
mapDb.store(mapChunk.pack());
}
}

View File

@ -2,7 +2,12 @@ package org.nodetest.servercore;
import java.util.ArrayList;
import java.util.HashMap;
/**
* IMPORTANT, IMPORTANT, IMPORTANT. VERY IMPORTANT. THIS CLASS IS THE ONLY CLASS THAT
* SCRIPTS CAN ACCESS. MAKE ALL FIELDS AND METHODS PRIVATE UNLESS IT IS
* INTENDED TO FACE UNTRUSTED SCRIPTS, OR REQUIRE THAT SCRIPTS PASS AN INSTANCE OF
* ScriptSandboxBorderToken.
*/
public class MossScriptEnv {
public interface MossEventHandler {
/*
@ -15,7 +20,7 @@ public class MossScriptEnv {
}
/*
* IMPORTANT, IMPORTANT, IMPORTANT. THIS CLASS IS THE ONLY CLASS THAT
* IMPORTANT, IMPORTANT, IMPORTANT. VERY IMPORTANT. THIS CLASS IS THE ONLY CLASS THAT
* SCRIPTS CAN ACCESS. MAKE ALL FIELDS AND METHODS PRIVATE UNLESS IT IS
* INTENDED TO FACE UNTRUSTED SCRIPTS.
*/
@ -96,16 +101,15 @@ public class MossScriptEnv {
public static void sendChatMessage(Player recipient, Player from, String message) {
//TODO
}
public static void sendChatAll(Player actor, String initiatingMessage) {
//TODO
}
public static void setHp(Entity actor, int i) {
//TODO Once we have players doing stuff
}
public static void damageTool(Player actor, MapNode nodeBefore) throws AntiCheatException{
@ -115,7 +119,6 @@ public class MossScriptEnv {
public static void givePlayer(Player player, MapNode nodeBefore) {
// TODO Auto-generated method stub
}

View File

@ -0,0 +1,25 @@
package org.nodetest.servercore;
import com.spaceprogram.kittycache.KittyCache;
public class NodeCache {
static KittyCache<Position, MapChunk> chunkData = new KittyCache<>(
EngineSettings.getInt("maxCachedChunks", 256));
public static MapChunk getChunk(Position pos, boolean generate) throws ChunkNotFoundException, MapDatabaseException {
MapChunk ourChunk=null;
try {
ourChunk = chunkData.get(pos);
} catch (Exception e) {
ourChunk = MapDatabase.getChunk(pos, generate);
}
if(ourChunk==null){
ourChunk = MapDatabase.getChunk(pos, generate);
}
return ourChunk;
}
public static void setChunk(Position pos, MapChunk chunk){
chunkData.put(pos, chunk, 3600);
MapDatabase.addMapChunk(chunk);
}
}