Baseline changes, cleaned some things

This commit is contained in:
Treeku
2014-05-11 09:54:50 +01:00
parent 5582c3d1ba
commit 7fd08bc7ad
76 changed files with 1428 additions and 1572 deletions
+73 -86
View File
@@ -22,16 +22,12 @@
package resources.objects;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import main.NGECore;
@@ -39,44 +35,38 @@ import org.apache.mina.core.buffer.IoBuffer;
import org.python.google.common.collect.ArrayListMultimap;
import org.python.google.common.collect.Multimap;
import org.python.google.common.collect.Multiset;
import org.python.google.common.collect.Ordering;
import org.python.google.common.collect.TreeMultimap;
import resources.common.StringUtilities;
import com.sleepycat.persist.model.NotPersistent;
import com.sleepycat.persist.model.Persistent;
import engine.resources.objects.Baseline;
import engine.resources.objects.Delta;
import engine.resources.objects.IDelta;
import engine.resources.objects.SWGObject;
@SuppressWarnings("unused")
@Persistent
public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
private static final long serialVersionUID = 1L;
private Multimap<K, V> map = ArrayListMultimap.create();
@NotPersistent
private transient int updateCounter = 0;
private long objectId;
private transient ObjectMessageBuilder messageBuilder;
private byte viewType;
private short updateType;
@NotPersistent
private boolean addByte;
protected transient Object objectMutex = new Object();
private transient SWGObject object;
public SWGMultiMap() { }
public SWGMultiMap(long objectId, int viewType, int updateType) {
this.objectId = objectId;
public SWGMultiMap(SWGObject object, int viewType, int updateType, boolean addByte) {
this.objectId = object.getObjectId();
this.viewType = (byte) viewType;
this.updateType = (short) updateType;
this.addByte = addByte;
this.object = object;
}
public SWGMultiMap(Multimap<K, V> m) {
if (m instanceof SWGMultiMap) {
this.objectId = ((SWGMultiMap<K, V>) m).objectId;
this.messageBuilder = ((SWGMultiMap<K, V>) m).messageBuilder;
this.object = ((SWGMultiMap<K, V>) m).object;
this.viewType = ((SWGMultiMap<K, V>) m).viewType;
this.updateType = ((SWGMultiMap<K, V>) m).updateType;
map.putAll(m);
@@ -85,8 +75,9 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
public void init() {
objectMutex = new Object();
object = NGECore.getInstance().objectService.getObject(objectId);
}
public Map<K, Collection<V>> asMap() {
synchronized(objectMutex) {
return map.asMap();
@@ -147,11 +138,9 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
public boolean put(K key, V value) {
synchronized(objectMutex) {
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long ||
value instanceof IDelta) {
if (valid(key) && valid(value)) {
if (map.put(key, value)) {
queue(item(0, (String) key, ((IDelta) value).getBytes(), true, true));
queue(item(0, key, Baseline.toBytes(value), true, true));
return true;
}
}
@@ -162,14 +151,13 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
public boolean putAll(K key, Iterable<? extends V> values) {
synchronized(objectMutex) {
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long) {
if (valid(key)) {
List<byte[]> buffer = new ArrayList<byte[]>();
for (V value : values) {
if (value instanceof IDelta) {
if (valid(value)) {
if (map.put(key, value)) {
buffer.add(item(0, (String) key, ((IDelta) value).getBytes(), true, true));
buffer.add(item(0, key, Baseline.toBytes(value), true, true));
}
}
}
@@ -192,11 +180,9 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
K key = entry.getKey();
V value = entry.getValue();
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long ||
value instanceof IDelta) {
if (valid(key) && valid(value)) {
if (this.map.put(key, value)) {
buffer.add(item(0, (String) key, ((IDelta) value).getBytes(), true, true));
buffer.add(item(0, key, Baseline.toBytes(value), true, true));
}
}
}
@@ -210,13 +196,11 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
}
}
@SuppressWarnings("unchecked")
public boolean remove(Object key, Object value) {
synchronized(objectMutex) {
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long) {
if (valid(key)) {
if (map.remove(key, value)) {
queue(item(1, (String) key, ((IDelta) map.get((K) key)).getBytes(), true, true));
queue(item(1, key, Baseline.toBytes(value), true, true));
return true;
}
@@ -229,14 +213,13 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
@SuppressWarnings("unchecked")
public Collection<V> removeAll(Object key) {
synchronized(objectMutex) {
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long) {
if (valid(key)) {
Collection<V> collection = map.get((K) key);
List<byte[]> buffer = new ArrayList<byte[]>();
for (V value : map.get((K) key)) {
if (map.remove(key, value)) {
buffer.add(item(1, (String) key, ((IDelta) map.get((K) key)).getBytes(), true, true));
buffer.add(item(1, key, Baseline.toBytes(value), true, true));
}
}
@@ -252,15 +235,14 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
public Collection<V> replaceValues(K key, Iterable<? extends V> values) {
synchronized(objectMutex) {
if (key instanceof String || key instanceof Byte || key instanceof Short ||
key instanceof Integer || key instanceof Float || key instanceof Long) {
if (valid(key)) {
if (map.containsKey(key)) {
List<byte[]> buffer = new ArrayList<byte[]>();
for (V value : values) {
if (value instanceof IDelta) {
if (valid(value)) {
if (!map.get(key).contains(value)) {
buffer.add(item(2, (String) key, ((IDelta) value).getBytes(), true, true));
buffer.add(item(2, key, Baseline.toBytes(value), true, true));
}
} else {
return null;
@@ -300,6 +282,46 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
return objectMutex;
}
public byte[] getBytes() {
synchronized(objectMutex) {
byte[] objects = { };
int size = 0;
for (Entry<?, ?> entry : map.entries()) {
byte[] key = Baseline.toBytes(entry.getKey());
byte[] value = Baseline.toBytes(entry.getValue());
size += ((addByte) ? 1 : 0) + key.length + value.length;
IoBuffer buffer = Baseline.createBuffer(size);
buffer.put(objects);
if (addByte) buffer.put((byte) 0);
buffer.put(key);
buffer.put(value);
buffer.flip();
objects = buffer.array();
}
IoBuffer buffer = Baseline.createBuffer(8 + size);
buffer.putInt(map.size());
buffer.putInt(updateCounter);
buffer.put(objects);
buffer.flip();
return buffer.array();
}
}
private boolean valid(Object o) {
if (o instanceof String || o instanceof Byte || o instanceof Short ||
o instanceof Integer || o instanceof Float || o instanceof Long ||
o instanceof IDelta) {
return true;
} else {
return false;
}
}
private byte[] item(int type, Object index, byte[] data, boolean useIndex, boolean useData) {
if (useIndex && !(index instanceof Byte) && !(index instanceof Short)
&& !(index instanceof Integer) && !(index instanceof Float)
@@ -307,28 +329,13 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
throw new IllegalArgumentException();
}
int size = 1 + ((useIndex) ? (2 + index.toString().getBytes().length) : 0) + ((useData) ? data.length : 0);
int size = 1 + ((useIndex) ? (2 + Baseline.toBytes(index).length) : 0) + ((useData) ? data.length : 0);
IoBuffer buffer = Delta.createBuffer(size);
buffer.put((byte) type);
if (useIndex) {
if (index instanceof String) {
buffer.put(StringUtilities.getAsciiString((String) index));
} else if (index instanceof Byte) {
buffer.put(((Byte) index).byteValue());
} else if (index instanceof Short) {
buffer.putShort(((Short) index).shortValue());
} else if (index instanceof Integer) {
buffer.putInt(((Integer) index).intValue());
} else if (index instanceof Float) {
buffer.putFloat(((Float) index).floatValue());
} else if (index instanceof Long) {
buffer.putLong(((Long) index).longValue());
} else {
throw new IllegalArgumentException();
}
}
if (useIndex) buffer.put(Baseline.toBytes(index));
if (useData) buffer.put(data);
buffer.flip();
updateCounter++;
@@ -336,22 +343,12 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
}
private void queue(byte[] data) {
if (messageBuilder == null) {
try {
SWGObject object = NGECore.getInstance().objectService.getObject(objectId);
this.messageBuilder = (ObjectMessageBuilder) object.getClass().getMethod("getMessageBuilder", new Class[] {}).invoke(object, new Object[] { });
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
IoBuffer buffer = Delta.createBuffer((data.length + 8));
buffer.putInt(1);
buffer.putInt(updateCounter);
buffer.put(data);
messageBuilder.sendListDelta(viewType, updateType, buffer);
buffer.flip();
object.sendListDelta(viewType, updateType, buffer);
}
private void queue(List<byte[]> data) {
@@ -361,23 +358,13 @@ public class SWGMultiMap<K, V> implements Multimap<K, V>, Serializable {
size += queued.length;
}
if (messageBuilder == null) {
try {
SWGObject object = NGECore.getInstance().objectService.getObject(objectId);
this.messageBuilder = (ObjectMessageBuilder) object.getClass().getMethod("getMessageBuilder", new Class[] {}).invoke(object, new Object[] { });
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
IoBuffer buffer = Delta.createBuffer((size + 8));
buffer.putInt(data.size());
buffer.putInt(updateCounter);
for (byte[] queued : data) buffer.put(queued);
buffer.flip();
messageBuilder.sendListDelta(viewType, updateType, buffer);
object.sendListDelta(viewType, updateType, buffer);
}
}