Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Make lastRaw storage optional
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusulf committed Apr 29, 2018
1 parent c923689 commit 17526c3
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 51 deletions.
2 changes: 2 additions & 0 deletions src/com/massivecraft/massivecore/ConfServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ConfServer extends SimpleConfig
// FIELDS
// -------------------------------------------- //

public static boolean localPollingEnabled = true;

public static String serverid = UUID.randomUUID().toString();

public static String dburi = "default";
Expand Down
2 changes: 1 addition & 1 deletion src/com/massivecraft/massivecore/MassiveCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void onEnableInner()

// Start the examine threads
// Start AFTER initializing the MConf, because they rely on the MConf.
ModificationPollerLocal.get().start();
if (ConfServer.localPollingEnabled) ModificationPollerLocal.get().start();
ModificationPollerRemote.get().start();

// Delete Files (at once and additionally after all plugins loaded)
Expand Down
122 changes: 93 additions & 29 deletions src/com/massivecraft/massivecore/store/Coll.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.massivecraft.massivecore.store;

import com.massivecraft.massivecore.ConfServer;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.MassiveCoreMConf;
import com.massivecraft.massivecore.MassivePlugin;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.comparator.ComparatorNaturalOrder;
import com.massivecraft.massivecore.mixin.MixinModification;
import com.massivecraft.massivecore.store.migrator.MigratorUtil;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.Gson;
Expand Down Expand Up @@ -156,6 +158,7 @@ public synchronized void putIdentifiedModificationFixed(String id, Modification
{
if (id == null) throw new NullPointerException("id");
if (modification == null) throw new NullPointerException("modification");

Modification old = this.identifiedModifications.get(id);
if (old != null && modification.getPriority() <= old.getPriority()) return;
this.identifiedModifications.put(id, modification);
Expand All @@ -168,6 +171,13 @@ public synchronized void removeIdentifiedModificationFixed(String id)
this.identifiedModifications.remove(id);
}

@Override
public Modification getIdentifiedModificationFixed(String id)
{
if (id == null) throw new NullPointerException("id");
return this.identifiedModifications.get(id);
}

// -------------------------------------------- //
// SYNCLOG
// -------------------------------------------- //
Expand Down Expand Up @@ -243,7 +253,7 @@ public synchronized void saveToRemoteFixed(String id)
entity.clearSyncLogFields();

JsonObject raw = this.getGson().toJsonTree(entity, this.getEntityClass()).getAsJsonObject();
entity.setLastRaw(raw);
if (ConfServer.localPollingEnabled) entity.setLastRaw(raw);

if (this.isDefault(entity))
{
Expand Down Expand Up @@ -341,7 +351,7 @@ public synchronized void loadFromRemoteFixed(String id, Entry<JsonObject, Long>
// this.putIdentifiedModificationFixed(id, Modification.UNKNOWN);
}

entity.setLastRaw(raw);
if (ConfServer.localPollingEnabled) entity.setLastRaw(raw);
entity.setLastMtime(mtime);
entity.setLastDefault(false);

Expand Down Expand Up @@ -429,11 +439,11 @@ public Modification examineIdFixed(String id, Long remoteMtime, boolean local, b
// ...and it was modified remotely.
if ( ! remoteMtime.equals(lastMtime)) return Modification.REMOTE_ALTER;
}
// ...and we are looking for local changes
// ...and we are looking for local changes ...
if (local)
{
// ...and it was modified locally.
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
// ... and it was modified locally.
if (this.examineHasLocalAlterFixed(id, localEntity)) return Modification.LOCAL_ALTER;
}
}
// Otherwise, if we only have it locally...
Expand Down Expand Up @@ -464,6 +474,16 @@ else if (remote && existsRemote)

protected boolean examineHasLocalAlterFixed(String id, E entity)
{
if (!ConfServer.localPollingEnabled)
{
if (MStore.DEBUG_ENABLED)
{
this.getPlugin().log("Attempted examineHasLocalAlterFixed in " + this.getDebugName() + " for " + id);
MUtil.stackTraceDebug("examineHasLocalAlterFixed");
}
return false;
}

JsonObject lastRaw = entity.getLastRaw();
JsonObject currentRaw = null;

Expand All @@ -479,32 +499,18 @@ protected boolean examineHasLocalAlterFixed(String id, E entity)
MassiveCore.get().log(Txt.parse("<k>Collection: <v>%s", this.getName()));
throw new RuntimeException(e);
}

return !MStore.equal(lastRaw, currentRaw);
}

@Override
public Modification syncIdFixed(String id, Modification modification, Entry<JsonObject, Long> remoteEntry)
public Modification syncIdFixed(String id, final Modification suppliedModification, Entry<JsonObject, Long> remoteEntry)
{
if (id == null) throw new NullPointerException("id");
if (modification == null || modification.isUnknown())
{
Long remoteMtime = null;
if (remoteEntry != null) remoteMtime = remoteEntry.getValue();

Modification actualModification = this.examineIdFixed(id, remoteMtime, true, true);
if (MassiveCoreMConf.get().warnOnLocalAlter && modification == Modification.UNKNOWN_LOG && actualModification.isModified())
{
E entity = this.idToEntity.get(id);
if (entity != null)
{
this.logModification(entity, actualModification);
}
}
modification = actualModification;
}
if (MStore.DEBUG_ENABLED) System.out.println(this.getDebugName() + " syncronising " + modification + " on " + id);

Modification modification = getActualModification(id, suppliedModification, remoteEntry);

if (MStore.DEBUG_ENABLED) this.getPlugin().log((this.getDebugName() + " syncronising " + modification + " (" + suppliedModification + ") on " + id));

// DEBUG
// MassiveCore.get().log(Txt.parse("<a>syncId <k>Coll: <v>%s <k>Entity: <v>%s <k>Modification: <v>%s", this.getName(), id, modification));
Expand Down Expand Up @@ -553,10 +559,66 @@ public Modification syncIdFixed(String id, Modification modification, Entry<Json
return modification;
}

private Modification getActualModification(String id, Modification modification, Entry<JsonObject, Long> remoteEntry)
{
if (id == null) throw new NullPointerException("id");

if (modification != null && !modification.isUnknown())
{
return modification;
}

Long remoteMtime = null;
if (remoteEntry != null) remoteMtime = remoteEntry.getValue();

// We look only remotely for changes, because local ones should be caught by .changed()
// or by the poller. This way we are certain, that all local changes where .changed() is not called
// they are found by the poller and then reported appropriately.
Modification actualModification = this.examineIdFixed(id, remoteMtime, false, true);

if (actualModification == Modification.NONE && (modification == Modification.UNKNOWN_CHANGED || modification == Modification.UNKNOWN_LOG))
{
actualModification = Modification.LOCAL_ALTER;
checkActuallyModifiedFixed(id);
}

if (MassiveCoreMConf.get().warnOnLocalAlter && modification == Modification.UNKNOWN_LOG && actualModification.isModified())
{
E entity = this.idToEntity.get(id);
if (entity != null)
{
this.logModification(entity, actualModification);
}
}
return actualModification;
}

private void checkActuallyModifiedFixed(String id)
{
if (!ConfServer.localPollingEnabled || !MassiveCoreMConf.get().warnOnLocalAlter) return;

E entity = this.getFixed(id);
boolean modified = this.examineHasLocalAlterFixed(id, entity);
if (modified) return;

List<String> messages = new MassiveList<>();
messages.add(Txt.parse("<pink>%s", this.getDebugName()));
messages.add(Txt.parse("<aqua>%s", entity.getId()));
String change = Txt.implode(messages, Txt.parse("<silver> | "));
String message = Txt.parse("<b>[No Modification] %s", change);
this.getPlugin().log(message);
}

protected void logModification(E entity, Modification modification)
{
JsonObject lastRaw = entity.getLastRaw();

if (!ConfServer.localPollingEnabled)
{
if (MStore.DEBUG_ENABLED) this.getPlugin().log("Attempted logModification in " + this.getDebugName() + " for " + entity.getId());
return;
}

if (lastRaw == null)
{
List<String> messages = new MassiveList<>();
Expand Down Expand Up @@ -603,7 +665,7 @@ protected void logModification(E entity, Modification modification)
String change = Txt.implode(changes, Txt.parse("<silver> | "));
String message = Txt.parse("<b>[Unreported Modification] %s", change);

MassiveCore.get().log(message);
this.getPlugin().log(message);
}

@Override
Expand Down Expand Up @@ -694,7 +756,7 @@ protected void storeModificationIdentification(String id, Modification modificat
{
if (modification.isModified())
{
if (MStore.DEBUG_ENABLED) System.out.println(this.getDebugName() + " identified " + modification + " on " + id);
if (MStore.DEBUG_ENABLED) this.getPlugin().log(this.getDebugName() + " identified " + modification + " on " + id);
if (veto != null && ! modification.isSafe()) modification = veto;
this.putIdentifiedModificationFixed(id, modification);
}
Expand All @@ -714,7 +776,7 @@ public void syncIdentified()
@Override
public void syncAll()
{
this.identifyModifications(null);
this.identifyRemoteModifications(null);
this.syncIdentified();
}

Expand Down Expand Up @@ -891,8 +953,10 @@ public void setActive(boolean active)
this.getPusher().deinit();
}

// TODO: Save outwards only? We may want to avoid loads at this stage...
this.syncAll();
// syncIdentified is probably good enough. We need not load, and when
// lastRaw is not present we can't identify local modifications anyway.
//this.syncAll();
this.syncIdentified();

name2instance.remove(this.getName());
}
Expand Down
9 changes: 8 additions & 1 deletion src/com/massivecraft/massivecore/store/CollAbstract.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Modification syncId(Object oid, Modification modification, Entry<JsonObje
public Modification syncIdFixed(String id)
{
if (id == null) throw new NullPointerException("id");
return this.syncIdFixed(id, null);
return this.syncIdFixed(id, this.getIdentifiedModificationFixed(id));
}

@Override
Expand All @@ -109,4 +109,11 @@ public Modification syncIdFixed(String id, Modification modification)
return this.syncIdFixed(id, modification, null);
}

@Override
public Modification getIdentifiedModification(Object oid)
{
if (oid == null) throw new NullPointerException("oid");
return this.getIdentifiedModificationFixed(this.fixIdOrThrow(oid));
}

}
3 changes: 3 additions & 0 deletions src/com/massivecraft/massivecore/store/CollInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public interface CollInterface<E extends Entity<E>> extends Named, Active, Ident
void identifyRemoteModifications(Modification veto);
void identifyRemoteModificationFixed(String id, Long remoteMtime, Modification veto);

Modification getIdentifiedModification(Object oid);
Modification getIdentifiedModificationFixed(String id);

// Init
void initLoadAllFromRemote();

Expand Down
16 changes: 15 additions & 1 deletion src/com/massivecraft/massivecore/store/EntityInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.massivecraft.massivecore.Identified;
import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.store.accessor.Accessor;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.xlib.gson.Gson;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -96,7 +97,7 @@ public void changed()

//System.out.println(this.getColl().getName() + ": " +this.getId() + " was modified locally");

this.getContainer().putIdentifiedModificationFixed(this.getId(), Modification.UNKNOWN);
this.getContainer().putIdentifiedModificationFixed(this.getId(), Modification.UNKNOWN_CHANGED);
}

// -------------------------------------------- //
Expand Down Expand Up @@ -132,6 +133,19 @@ public <T> T convertSet(T value, T standard)
return Objects.equals(value, standard) ? null : value;
}

public <T> T convertSet(T value, T current, T standard)
{
current = this.convertGet(current, standard);
this.changed(value, current);
return Objects.equals(value, standard) ? null : value;
}

public void changed(Object o1, Object o2)
{
if (MUtil.equalsishObject(o1, o2)) return;
changed();
}

// BOOLEAN
public boolean convertGet(Boolean value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.massivecraft.massivecore.store;

import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.xlib.gson.JsonArray;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonNull;
Expand Down Expand Up @@ -139,7 +140,7 @@ public static boolean primitiveEquals(JsonPrimitive one, Object twoObject)
if (floating)
{
// Our epsilon is pretty big in order to see float and double as the same.
return Math.abs(oneNumber.doubleValue() - twoNumber.doubleValue()) < 0.0001D;
return MUtil.equalsishNumber(oneNumber, twoNumber);
}
else
{
Expand Down
28 changes: 16 additions & 12 deletions src/com/massivecraft/massivecore/store/Modification.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ public enum Modification
// ENUM
// -------------------------------------------- //

LOCAL_ALTER (true, 4),
LOCAL_ATTACH (true, 8),
LOCAL_DETACH (true, 9),
REMOTE_ALTER (true, 5),
REMOTE_ATTACH (true, 6),
REMOTE_DETACH (true, 7),
NONE (false, 1),
UNKNOWN (false, 3),
UNKNOWN_LOG(false, 2),
LOCAL_ALTER (true, 40),
LOCAL_ATTACH (true, 80),
LOCAL_DETACH (true, 90),
REMOTE_ALTER (true, 50),
REMOTE_ATTACH (true, 60),
REMOTE_DETACH (true, 70),
NONE (false, 10),
UNKNOWN (false, 30),
UNKNOWN_LOG(false, 20),
UNKNOWN_CHANGED(false, 35),
;

// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //

public static final int TOP_PRIORITY = 7;

// The modifications take priority over all others.
// It is local attach and local detach, because otherwise
// the system would think it was a remote attach or remote detach.
public static final int TOP_PRIORITY = 80;

// -------------------------------------------- //
// FIELDS
Expand Down Expand Up @@ -83,6 +87,6 @@ public boolean hasTopPriority()

public boolean isUnknown()
{
return this == Modification.UNKNOWN || this == Modification.UNKNOWN_LOG;
return this == Modification.UNKNOWN || this == Modification.UNKNOWN_LOG || this == Modification.UNKNOWN_CHANGED;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.massivecraft.massivecore.store;

import com.massivecraft.massivecore.ConfServer;

public abstract class ModificationPollerAbstract extends Thread
{
// -------------------------------------------- //
Expand Down Expand Up @@ -54,7 +56,7 @@ public void identify() throws InterruptedException
this.poll(coll);
}
}

// -------------------------------------------- //
// ABSTRACT
// -------------------------------------------- //
Expand Down
Loading

0 comments on commit 17526c3

Please sign in to comment.