Merge branch 'master' of github.com:mosstest/mosstest

master
rarkenin 2014-07-13 13:22:48 -04:00
commit 8f3edf1673
10 changed files with 150 additions and 69 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
@ -10,6 +10,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.intellij:annotations:9.0.4" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math:2.2" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.1" level="project" />

View File

@ -1,8 +1,8 @@
package net.mosstest.scripting;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import net.mosstest.servercore.serialization.IByteArrayWritable;
public abstract class AbstractMapChunk implements IByteArrayWriteable {
public abstract class AbstractMapChunk implements IByteArrayWritable {
int CHUNK_DIMENSION = 16;
@Override

View File

@ -2,7 +2,7 @@ package net.mosstest.scripting;
import net.mosstest.servercore.ItemManager;
import net.mosstest.servercore.MosstestFatalDeathException;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import net.mosstest.servercore.serialization.IByteArrayWritable;
import net.mosstest.servercore.serialization.IManaged;
import org.apache.log4j.Logger;
@ -13,7 +13,7 @@ import java.io.*;
/**
* The Class MossInventory.
*/
public class MossInventory implements IByteArrayWriteable, IManaged<ItemManager> {
public class MossInventory implements IByteArrayWritable, IManaged<ItemManager> {
private ItemManager im;

View File

@ -1,6 +1,7 @@
package net.mosstest.scripting;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import net.mosstest.servercore.serialization.IByteArrayWritable;
import org.jetbrains.annotations.NonNls;
import java.io.IOException;
@ -8,7 +9,7 @@ import java.io.IOException;
/**
* The Class Position.
*/
public class Position implements IByteArrayWriteable{
public class Position implements IByteArrayWritable {
public static final int SERIALIZED_LENGTH = 16;
@NonNls
@Override

View File

@ -1,84 +1,166 @@
package net.mosstest.scripting;
import static org.fusesource.leveldbjni.JniDBFactory.factory;
import static org.fusesource.leveldbjni.JniDBFactory.*;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import net.mosstest.servercore.MosstestFatalDeathException;
import net.mosstest.servercore.serialization.IByteArrayWritable;
import org.apache.log4j.Logger;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.Options;
// TODO: Auto-generated Javadoc
/**
* The Class ScriptableDatabase.
*/
public class ScriptableDatabase {
private static final Logger logger = Logger.getLogger(ScriptableDatabase.class);
/**
* The base dir.
*/
File baseDir;
/** The base dir. */
File baseDir;
/**
* Instantiates a new scriptable database.
*
* @param baseDir the base dir
*/
public ScriptableDatabase(File baseDir) {
this.baseDir = baseDir;
/**
* Instantiates a new scriptable database.
*
* @param baseDir the base dir
*/
public ScriptableDatabase(File baseDir) {
this.baseDir = baseDir;
}
}
/**
* Gets the db.
*
* @param name the name
* @return the db
* @throws IOException Signals that an I/O exception has occurred.
*/
public DBase getDb(String name) throws IOException {
if (!name.matches("[a-zA-Z]{1,32}")) { //$NON-NLS-1$
throw new IllegalArgumentException(Messages.getString("ScriptableDatabase.DB_NAME_INVALID")); //$NON-NLS-1$
}
Options options = new Options();
options.createIfMissing(true);
return new DBase(factory.open(new File(this.baseDir, "sc_" + name //$NON-NLS-1$
+ ".db"), options), name); //$NON-NLS-1$
/**
* Gets the db.
*
* @param name the name
* @return the db
* @throws IOException Signals that an I/O exception has occurred.
*/
public DBase getDb(String name) throws IOException {
if (!name.matches("[a-zA-Z]{1,32}")) { //$NON-NLS-1$
throw new IllegalArgumentException(Messages.getString("ScriptableDatabase.DB_NAME_INVALID")); //$NON-NLS-1$
}
Options options = new Options();
options.createIfMissing(true);
return new DBase(factory.open(new File(this.baseDir, name //$NON-NLS-1$
+ ".scriptdb"), options), name); //$NON-NLS-1$
}
}
public class DBase {
// this class will contain a database that scripts may access.
/** The inner db. */
private final DB innerDb;
public class DBase {
// this class will contain a database that scripts may access.
/**
* The inner db.
*/
private final DB innerDb;
/**
* Instantiates a new database.
*
* @param innerDb the inner db
* @param name the name
*/
DBase(DB innerDb, String name) {
this.innerDb = innerDb;
}
/**
* Instantiates a new database.
*
* @param innerDb the inner db
* @param name the name
*/
DBase(DB innerDb, String name) {
this.innerDb = innerDb;
}
}
}
/**
* Workaround class for java.lang.String being declared final.
*/
public static final class DBString implements IByteArrayWritable, CharSequence {
public static class DBKey implements IByteArrayWriteable {
private List<IByteArrayWriteable> qualifiers;
private final String s;
// does not match for an equal string to keep this method's symmetric property
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DBString)) return false;
DBString dbString = (DBString) o;
if (!s.equals(dbString.s)) return false;
return true;
}
@Override
public int hashCode() {
return s.hashCode();
}
@Override
public IntStream chars() {
return s.chars();
}
@Override
public CharSequence subSequence(int beginIndex, int endIndex) {
return s.subSequence(beginIndex, endIndex);
}
@Override
public char charAt(int index) {
return s.charAt(index);
}
public DBString(String s) {
this.s = s;
}
@Override
public int length() {
return s.length();
}
@Override
public byte[] toBytes() {
return bytes(s);
}
}
public static final class DBKey implements IByteArrayWritable {
private List<IByteArrayWritable> qualifiers;
@Override
public byte[] toBytes() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dOut = new DataOutputStream(out);
try {
for (IByteArrayWritable qual : qualifiers) {
byte[] buf = qual.toBytes();
dOut.writeInt(buf.length);
dOut.write(buf);
}
} catch (IOException e) {
logger.fatal("Error serializing scriptable DB key. THE WORLD IS GOING DOWN SHORTLY.");
throw new MosstestFatalDeathException(e);
}
return null;
}
public DBKey(IByteArrayWritable... qualifiers) {
this.qualifiers = new ArrayList<>(Arrays.asList(qualifiers));
}
}
}

View File

@ -1,13 +1,10 @@
package net.mosstest.servercore;
import net.mosstest.scripting.Player;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import org.apache.commons.lang.NotImplementedException;
import java.io.IOException;
import net.mosstest.servercore.serialization.IByteArrayWritable;
public class InventoryManager {
public class PlayerKey implements IByteArrayWriteable {
public class PlayerKey implements IByteArrayWritable {
private final Player player;
private final String inventory;

View File

@ -89,13 +89,13 @@ public class MapDatabase {
}
/**
* Close.
*
* @throws MapDatabaseException the map database exception
* Closes the database, making it invalid to use for any stores or lookups.
*/
public void close() throws MapDatabaseException {
logger.info(Messages.getString("DB_SHUTDOWN_NORMAL"));
try {
this.map.close();
this.entities.close();

View File

@ -1,5 +1,5 @@
package net.mosstest.servercore.serialization;
public interface IByteArrayWriteable {
public interface IByteArrayWritable {
public byte[] toBytes();
}

View File

@ -19,7 +19,7 @@ import java.util.concurrent.ExecutionException;
* @param <V> Values to be stored
*/
// V need not extend any sort of marker interface, having a proper constructor passed in takes care of that.
public class LevelDBBackedMap<K extends IByteArrayWriteable, V extends IByteArrayWriteable> implements Map<K, V> {
public class LevelDBBackedMap<K extends IByteArrayWritable, V extends IByteArrayWritable> implements Map<K, V> {
// TODO a map backed by a levelDB datastore
// the in-memory cache
@ -93,8 +93,8 @@ public class LevelDBBackedMap<K extends IByteArrayWriteable, V extends IByteArra
public V remove(Object key) {
this.memoryBackingCache.invalidate(key);
this.memoryBackingCache.asMap().remove(key);
if (key instanceof IByteArrayWriteable)
this.diskBackingDatastore.delete(((IByteArrayWriteable) key).toBytes());
if (key instanceof IByteArrayWritable)
this.diskBackingDatastore.delete(((IByteArrayWritable) key).toBytes());
return null;
}
@ -212,7 +212,7 @@ public class LevelDBBackedMap<K extends IByteArrayWriteable, V extends IByteArra
}
}
public static class ManagedMap<K extends IByteArrayWriteable, V extends IByteArrayWriteable & IManaged<M>, M> extends LevelDBBackedMap<K, V> {
public static class ManagedMap<K extends IByteArrayWritable, V extends IByteArrayWritable & IManaged<M>, M> extends LevelDBBackedMap<K, V> {
protected M manager;
public ManagedMap(DB diskBackingDatastore, IByteArrayConstructor<V> constructor, M manager) {

View File

@ -2,7 +2,7 @@ package net.mosstest.tests;
import net.mosstest.scripting.MapChunk;
import net.mosstest.scripting.Position;
import net.mosstest.servercore.serialization.IByteArrayWriteable;
import net.mosstest.servercore.serialization.IByteArrayWritable;
import net.mosstest.servercore.serialization.LevelDBBackedMap;
import net.mosstest.servercore.MapGeneratorException;
import org.junit.Test;
@ -39,7 +39,7 @@ public class LevelDBBackedMapTest {
}
public static class TestByteArrayStorable implements IByteArrayWriteable{
public static class TestByteArrayStorable implements IByteArrayWritable {
byte[] buf;
public TestByteArrayStorable(byte[] buf) {