Migrator Renaming

This commit is contained in:
Olof Larsson 2017-03-24 16:05:55 +01:00
parent 930e81c37e
commit ed93c837bb
7 changed files with 46 additions and 46 deletions

View File

@ -12,7 +12,7 @@ import com.massivecraft.massivecore.command.type.Type;
import com.massivecraft.massivecore.command.type.TypeWrapper; import com.massivecraft.massivecore.command.type.TypeWrapper;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.particleeffect.ReflectionUtils; import com.massivecraft.massivecore.particleeffect.ReflectionUtils;
import com.massivecraft.massivecore.store.migration.VersionMigrationUtil; import com.massivecraft.massivecore.store.migrator.MigratorUtil;
import com.massivecraft.massivecore.util.ReflectionUtil; import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -232,7 +232,7 @@ public class PropertyReflection<O, V> extends Property<O, V>
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) ret = false; if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isFinal(modifiers)) ret = false;
// Fill > Version // Fill > Version
if (field.getName().equals(VersionMigrationUtil.VERSION_FIELD_NAME)) ret = false; if (field.getName().equals(MigratorUtil.VERSION_FIELD_NAME)) ret = false;
// Fill > Annotation // Fill > Annotation
ret = getAnnotationValue(field, EditorEditable.class, ret); ret = getAnnotationValue(field, EditorEditable.class, ret);

View File

@ -9,7 +9,7 @@ import com.massivecraft.massivecore.comparator.ComparatorNaturalOrder;
import com.massivecraft.massivecore.mixin.MixinModification; import com.massivecraft.massivecore.mixin.MixinModification;
import com.massivecraft.massivecore.predicate.Predicate; import com.massivecraft.massivecore.predicate.Predicate;
import com.massivecraft.massivecore.predicate.PredicateEqualsIgnoreCase; import com.massivecraft.massivecore.predicate.PredicateEqualsIgnoreCase;
import com.massivecraft.massivecore.store.migration.VersionMigrationUtil; import com.massivecraft.massivecore.store.migrator.MigratorUtil;
import com.massivecraft.massivecore.util.ReflectionUtil; import com.massivecraft.massivecore.util.ReflectionUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.Gson; import com.massivecraft.massivecore.xlib.gson.Gson;
@ -514,7 +514,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
JsonObject raw = remoteEntry.getKey(); JsonObject raw = remoteEntry.getKey();
Long mtime = remoteEntry.getValue(); Long mtime = remoteEntry.getValue();
int version = VersionMigrationUtil.getVersion(raw); int version = MigratorUtil.getVersion(raw);
if (version > this.getEntityTargetVersion()) if (version > this.getEntityTargetVersion())
{ {
logLoadError(id, String.format("Cannot load entity of entity version %d", version)); logLoadError(id, String.format("Cannot load entity of entity version %d", version));
@ -522,7 +522,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
} }
// Migrate if another version is wanted // Migrate if another version is wanted
boolean migrated = VersionMigrationUtil.migrate(this.getEntityClass(), raw, this.getEntityTargetVersion()); boolean migrated = MigratorUtil.migrate(this.getEntityClass(), raw, this.getEntityTargetVersion());
// Calculate temp but handle raw cases. // Calculate temp but handle raw cases.
E temp; E temp;
@ -1003,7 +1003,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
int version = 0; int version = 0;
try try
{ {
version = ReflectionUtil.getField(this.getEntityClass(), VersionMigrationUtil.VERSION_FIELD_NAME, this.createNewInstance()); version = ReflectionUtil.getField(this.getEntityClass(), MigratorUtil.VERSION_FIELD_NAME, this.createNewInstance());
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1094,7 +1094,7 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
// TODO: Clean up this stuff below. It branches too late. // TODO: Clean up this stuff below. It branches too late.
if (active) if (active)
{ {
VersionMigrationUtil.validateMigratorsPresent(entityClass, 0, this.getEntityTargetVersion()); MigratorUtil.validateMigratorsPresent(entityClass, 0, this.getEntityTargetVersion());
if (this.supportsPusher()) if (this.supportsPusher())
{ {

View File

@ -1,8 +1,8 @@
package com.massivecraft.massivecore.store.migration; package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
public interface VersionMigrator public interface Migrator
{ {
// -------------------------------------------- // // -------------------------------------------- //
// MIGRATION // MIGRATION

View File

@ -1,11 +1,11 @@
package com.massivecraft.massivecore.store.migration; package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonNull; import com.massivecraft.massivecore.xlib.gson.JsonNull;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
import com.massivecraft.massivecore.xlib.gson.JsonPrimitive; import com.massivecraft.massivecore.xlib.gson.JsonPrimitive;
public abstract class VersionMigratorField implements VersionMigrator public abstract class MigratorFieldConvert implements Migrator
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS
@ -18,7 +18,7 @@ public abstract class VersionMigratorField implements VersionMigrator
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public VersionMigratorField(String fieldName) public MigratorFieldConvert(String fieldName)
{ {
if (fieldName == null) throw new NullPointerException("fieldName"); if (fieldName == null) throw new NullPointerException("fieldName");
this.fieldName = fieldName; this.fieldName = fieldName;

View File

@ -1,9 +1,9 @@
package com.massivecraft.massivecore.store.migration; package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.xlib.gson.JsonElement; import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject; import com.massivecraft.massivecore.xlib.gson.JsonObject;
public class VersionMigratorRename implements VersionMigrator public class MigratorFieldRename implements Migrator
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS
@ -19,8 +19,8 @@ public class VersionMigratorRename implements VersionMigrator
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public static VersionMigratorRename get(String from, String to) { return new VersionMigratorRename(from, to); } public static MigratorFieldRename get(String from, String to) { return new MigratorFieldRename(from, to); }
public VersionMigratorRename(String from, String to) public MigratorFieldRename(String from, String to)
{ {
if (from == null) throw new NullPointerException("from"); if (from == null) throw new NullPointerException("from");
if (to == null) throw new NullPointerException("to"); if (to == null) throw new NullPointerException("to");

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.store.migration; package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.Active; import com.massivecraft.massivecore.Active;
import com.massivecraft.massivecore.MassivePlugin; import com.massivecraft.massivecore.MassivePlugin;
@ -10,16 +10,16 @@ import com.massivecraft.massivecore.xlib.gson.JsonObject;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
public class VersionMigratorRoot implements VersionMigrator, Active public class MigratorRoot implements Migrator, Active
{ {
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS // FIELDS
// -------------------------------------------- // // -------------------------------------------- //
private List<VersionMigrator> innerMigrators = new MassiveList<>(); private List<Migrator> innerMigrators = new MassiveList<>();
public List<VersionMigrator> getInnerMigrators() { return innerMigrators; } public List<Migrator> getInnerMigrators() { return innerMigrators; }
public void setInnerMigrators(List<VersionMigrator> innerMigrators) { this.innerMigrators = new MassiveList<>(innerMigrators); } public void setInnerMigrators(List<Migrator> innerMigrators) { this.innerMigrators = new MassiveList<>(innerMigrators); }
public void addInnerMigrator(VersionMigrator innerMigrator) public void addInnerMigrator(Migrator innerMigrator)
{ {
this.innerMigrators.add(innerMigrator); this.innerMigrators.add(innerMigrator);
} }
@ -31,7 +31,7 @@ public class VersionMigratorRoot implements VersionMigrator, Active
// CONSTRUCT // CONSTRUCT
// -------------------------------------------- // // -------------------------------------------- //
public VersionMigratorRoot(Class<? extends Entity<?>> entityClass) public MigratorRoot(Class<? extends Entity<?>> entityClass)
{ {
this.entityClass = entityClass; this.entityClass = entityClass;
} }
@ -44,7 +44,7 @@ public class VersionMigratorRoot implements VersionMigrator, Active
@Override @Override
public boolean isActive() public boolean isActive()
{ {
return VersionMigrationUtil.isActive(this); return MigratorUtil.isActive(this);
} }
@Override @Override
@ -52,11 +52,11 @@ public class VersionMigratorRoot implements VersionMigrator, Active
{ {
if (active) if (active)
{ {
VersionMigrationUtil.addMigrator(this); MigratorUtil.addMigrator(this);
} }
else else
{ {
VersionMigrationUtil.removeMigrator(this); MigratorUtil.removeMigrator(this);
} }
} }
@ -107,7 +107,7 @@ public class VersionMigratorRoot implements VersionMigrator, Active
if (entity == null) throw new NullPointerException("entity"); if (entity == null) throw new NullPointerException("entity");
// Get current and expected entity version ... // Get current and expected entity version ...
int entityVersionCurrent = VersionMigrationUtil.getVersion(entity); int entityVersionCurrent = MigratorUtil.getVersion(entity);
int entityVersionExpected = this.getVersion() - 1; int entityVersionExpected = this.getVersion() - 1;
// ... make sure they match ... // ... make sure they match ...
@ -120,13 +120,13 @@ public class VersionMigratorRoot implements VersionMigrator, Active
private void migrateVersion(JsonObject entity) private void migrateVersion(JsonObject entity)
{ {
entity.addProperty(VersionMigrationUtil.VERSION_FIELD_NAME, this.getVersion()); entity.addProperty(MigratorUtil.VERSION_FIELD_NAME, this.getVersion());
} }
public void migrateInner(JsonObject entity) public void migrateInner(JsonObject entity)
{ {
// Look over all inner migrators. // Look over all inner migrators.
for (VersionMigrator migrator : this.getInnerMigrators()) for (Migrator migrator : this.getInnerMigrators())
{ {
migrator.migrate(entity); migrator.migrate(entity);
} }

View File

@ -1,4 +1,4 @@
package com.massivecraft.massivecore.store.migration; package com.massivecraft.massivecore.store.migrator;
import com.massivecraft.massivecore.collections.MassiveList; import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveMap;
@ -12,7 +12,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class VersionMigrationUtil public class MigratorUtil
{ {
// -------------------------------------------- // // -------------------------------------------- //
// CONSTANTS // CONSTANTS
@ -24,37 +24,37 @@ public class VersionMigrationUtil
// REGISTRY // REGISTRY
// -------------------------------------------- // // -------------------------------------------- //
private static Map<Class<? extends Entity<?>>, Map<Integer, VersionMigratorRoot>> migrators = new HashMap<>(); private static Map<Class<? extends Entity<?>>, Map<Integer, MigratorRoot>> migrators = new HashMap<>();
public static boolean isActive(VersionMigratorRoot migrator) public static boolean isActive(MigratorRoot migrator)
{ {
return getMigratorMap(migrator).get(migrator.getVersion()) == migrator; return getMigratorMap(migrator).get(migrator.getVersion()) == migrator;
} }
// ADD // ADD
public static void addMigrator(VersionMigratorRoot migrator) public static void addMigrator(MigratorRoot migrator)
{ {
VersionMigratorRoot old = getMigratorMap(migrator).put(migrator.getVersion(), migrator); MigratorRoot old = getMigratorMap(migrator).put(migrator.getVersion(), migrator);
// If there was an old one and it wasn't this one already: deactivate. // If there was an old one and it wasn't this one already: deactivate.
if (old != null && old != migrator) old.setActive(false); if (old != null && old != migrator) old.setActive(false);
} }
// REMOVE // REMOVE
public static void removeMigrator(VersionMigratorRoot migrator) public static void removeMigrator(MigratorRoot migrator)
{ {
VersionMigratorRoot current = getMigratorMap(migrator).get(migrator.getVersion()); MigratorRoot current = getMigratorMap(migrator).get(migrator.getVersion());
// If there wasn't a new one already: remove // If there wasn't a new one already: remove
if (current == migrator) getMigratorMap(migrator).remove(migrator.getVersion()); if (current == migrator) getMigratorMap(migrator).remove(migrator.getVersion());
} }
// GET // GET
public static VersionMigratorRoot getMigrator(Class<? extends Entity<?>> entityClass, int version) public static MigratorRoot getMigrator(Class<? extends Entity<?>> entityClass, int version)
{ {
Map<Integer, VersionMigratorRoot> migratorMap = getMigratorMap(entityClass); Map<Integer, MigratorRoot> migratorMap = getMigratorMap(entityClass);
VersionMigratorRoot migrator = migratorMap.get(version); MigratorRoot migrator = migratorMap.get(version);
if (migrator == null) if (migrator == null)
{ {
throw new RuntimeException(String.format("No VersionMigrator found for %s version %d", entityClass.getName(), version)); throw new RuntimeException(String.format("No VersionMigrator found for %s version %d", entityClass.getName(), version));
@ -64,14 +64,14 @@ public class VersionMigrationUtil
} }
// GET MAP // GET MAP
private static Map<Integer, VersionMigratorRoot> getMigratorMap(VersionMigratorRoot migrator) private static Map<Integer, MigratorRoot> getMigratorMap(MigratorRoot migrator)
{ {
return getMigratorMap(migrator.getEntityClass()); return getMigratorMap(migrator.getEntityClass());
} }
private static Map<Integer, VersionMigratorRoot> getMigratorMap(Class<? extends Entity<?>> entityClass) private static Map<Integer, MigratorRoot> getMigratorMap(Class<? extends Entity<?>> entityClass)
{ {
Map<Integer, VersionMigratorRoot> ret = migrators.get(entityClass); Map<Integer, MigratorRoot> ret = migrators.get(entityClass);
if (ret == null) if (ret == null)
{ {
ret = new MassiveMap<>(); ret = new MassiveMap<>();
@ -100,7 +100,7 @@ public class VersionMigrationUtil
// When downgrading we need the migrator we are downgrading from. // When downgrading we need the migrator we are downgrading from.
// This is done to preserve the same logic within the same class. // This is done to preserve the same logic within the same class.
// That is why when updating we don't use entityVersion and when downgrading we do. // That is why when updating we don't use entityVersion and when downgrading we do.
VersionMigrator migrator = getMigrator(entityClass, entityVersion+1); Migrator migrator = getMigrator(entityClass, entityVersion+1);
migrator.migrate(entity); migrator.migrate(entity);
} }
@ -113,7 +113,7 @@ public class VersionMigrationUtil
public static void validateMigratorsPresent(Class<? extends Entity<?>> entityClass, int from, int to) public static void validateMigratorsPresent(Class<? extends Entity<?>> entityClass, int from, int to)
{ {
List<Integer> missingMigrators = VersionMigrationUtil.getMissingMigratorVersions(entityClass, from, to); List<Integer> missingMigrators = MigratorUtil.getMissingMigratorVersions(entityClass, from, to);
if (missingMigrators.isEmpty()) return; if (missingMigrators.isEmpty()) return;
String versions = Txt.implodeCommaAndDot(missingMigrators); String versions = Txt.implodeCommaAndDot(missingMigrators);
@ -125,7 +125,7 @@ public class VersionMigrationUtil
{ {
if (from == to) return Collections.emptyList(); if (from == to) return Collections.emptyList();
if (from > to) throw new IllegalArgumentException(String.format("from: %d to: %d", from, to)); if (from > to) throw new IllegalArgumentException(String.format("from: %d to: %d", from, to));
Map<Integer, VersionMigratorRoot> migrators = getMigratorMap(entityClass); Map<Integer, MigratorRoot> migrators = getMigratorMap(entityClass);
// We need not the from but we need the to. // We need not the from but we need the to.
from++; from++;