Optimized indexer but that was not enough. Added back the cached index again but with new technology.

This commit is contained in:
Olof Larsson 2013-04-17 14:44:08 +02:00
parent 0776e5ae55
commit 6ad243c014
5 changed files with 157 additions and 32 deletions

View File

@ -40,7 +40,7 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
@Override
public FPlayer load(FPlayer that)
{
this.factionId = that.factionId;
this.setFactionId(that.factionId);
this.role = that.role;
this.title = that.title;
this.power = that.power;
@ -65,17 +65,79 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// -------------------------------------------- //
// FIELD: factionId
private String factionId;
public Faction getFaction() { if(this.factionId == null) {return null;} return FactionColl.get().get(this.factionId); }
public String getFactionId() { return this.factionId; }
public boolean hasFaction() { return this.factionId != null && ! factionId.equals(Const.FACTIONID_NONE); }
public void setFaction(Faction faction)
// TODO: Ensure this one always is null in the nofaction case and never actually the ID of the nofaction-faction.
// TODO: The getFactionId should however NEVER return null!
private String factionId = null;
// The get methods never return null.
public String getFactionId()
{
this.factionId = faction.getId();
if (this.factionId == null) return Const.FACTIONID_NONE;
return this.factionId;
}
public Faction getFaction()
{
Faction ret = FactionColl.get().get(this.getFactionId());
if (ret == null) ret = FactionColl.get().get(Const.FACTIONID_NONE);
return ret;
}
// TODO: When is this one used?
public boolean hasFaction()
{
// TODO: Broken logic
return !this.getFactionId().equals(Const.FACTIONID_NONE);
}
// This setter is so long because it search for default/null case and takes care of updating the faction member index
public void setFactionId(String factionId)
{
// Avoid null input
if (factionId == null) factionId = Const.FACTIONID_NONE;
// Get the old value
String oldFactionId = this.getFactionId();
// Ignore nochange
if (factionId.equals(oldFactionId)) return;
// Apply change
if (factionId.equals(Const.FACTIONID_NONE))
{
this.factionId = null;
}
else
{
this.factionId = factionId;
}
// Next we must be attached and inited
if (!this.attached()) return;
if (!this.getColl().inited()) return;
// Spout Derp
SpoutFeatures.updateTitle(this, null);
SpoutFeatures.updateTitle(null, this);
// Update index
Faction oldFaction = FactionColl.get().get(oldFactionId);
Faction faction = FactionColl.get().get(factionId);
oldFaction.fplayers.remove(this);
faction.fplayers.add(this);
// Mark as changed
this.changed();
}
public void setFaction(Faction faction)
{
this.setFactionId(faction.getId());
}
// FIELD: role
private Rel role;
public Rel getRole() { return this.role; }

View File

@ -62,6 +62,37 @@ public class FPlayerColl extends SenderColl<FPlayer>
oldFile.renameTo(newFile);
}
@Override
protected synchronized String attach(FPlayer entity, Object oid, boolean noteChange)
{
String ret = super.attach(entity, oid, noteChange);
// If inited ...
if (!this.inited()) return ret;
// ... update the index.
Faction faction = entity.getFaction();
faction.fplayers.add(entity);
return ret;
}
@Override
public FPlayer detachId(Object oid)
{
FPlayer ret = super.detachId(oid);
if (ret == null) return null;
// If inited ...
if (!this.inited()) return ret;
// ... update the index.
Faction faction = ret.getFaction();
faction.fplayers.remove(ret);
return ret;
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //

View File

@ -16,6 +16,7 @@ import com.massivecraft.factions.util.*;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.SenderUtil;
import com.massivecraft.mcore.xlib.gson.annotations.SerializedName;
@ -664,10 +665,10 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
return RelationUtil.getColorOfThatToMe(this, observer);
}
// TODO: Implement a has enough feature.
// -------------------------------------------- //
// POWER
// -------------------------------------------- //
// TODO: Implement a has enough feature.
public double getPower()
{
@ -685,7 +686,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{
ret = ConfServer.powerFactionMax;
}
return ret + this.powerBoost;
return ret + this.getPowerBoost();
}
public double getPowerMax()
@ -704,7 +705,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{
ret = ConfServer.powerFactionMax;
}
return ret + this.powerBoost;
return ret + this.getPowerBoost();
}
public int getPowerRounded()
@ -735,48 +736,71 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// FOREIGN KEYS: FPLAYERS
// -------------------------------------------- //
public List<FPlayer> getFPlayers()
// TODO: With this approach null must be used as default always.
// TODO: Take a moment and reflect upon the consequenses eeeeeeh...
// TODO: This one may be to slow after all :/ Thus I must maintain an index.
protected transient List<FPlayer> fplayers = null;
public void reindexFPlayers()
{
List<FPlayer> ret = new ArrayList<FPlayer>();
this.fplayers = new ArrayList<FPlayer>();
String factionId = this.getId();
if (factionId == null) return;
for (FPlayer fplayer : FPlayerColl.get().getAll())
{
if (fplayer.getFaction() != this) continue;
ret.add(fplayer);
if (!MUtil.equals(factionId, fplayer.getFactionId())) continue;
this.fplayers.add(fplayer);
}
return ret;
}
public List<FPlayer> getFPlayers()
{
return new ArrayList<FPlayer>(this.fplayers);
}
public List<FPlayer> getFPlayersWhereOnline(boolean online)
{
List<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer fplayer : FPlayerColl.get().getAll())
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
if (fplayer.getFaction() != this) continue;
if (fplayer.isOnline() != online) continue;
ret.add(fplayer);
FPlayer fplayer = iter.next();
if (fplayer.isOnline() != online)
{
iter.remove();
}
}
return ret;
}
public List<FPlayer> getFPlayersWhereRole(Rel role)
{
List<FPlayer> ret = new ArrayList<FPlayer>();
for (FPlayer fplayer : FPlayerColl.get().getAll())
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
if (fplayer.getFaction() != this) continue;
if (fplayer.getRole() != role) continue;
ret.add(fplayer);
FPlayer fplayer = iter.next();
if (fplayer.getRole() != role)
{
iter.remove();
}
}
return ret;
}
public FPlayer getLeader()
{
for (FPlayer fplayer : FPlayerColl.get().getAll())
List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
if (fplayer.getFaction() != this) continue;
if (fplayer.getRole() != Rel.LEADER) continue;
return fplayer;
FPlayer fplayer = iter.next();
if (fplayer.getRole() == Rel.LEADER)
{
return fplayer;
}
}
return null;
}

View File

@ -39,8 +39,8 @@ public class FactionColl extends Coll<Faction>
super.init();
this.migrate();
this.createDefaultFactions();
this.createDefaultFactions();
this.reindexFPlayers();
}
public void migrate()
@ -91,6 +91,14 @@ public class FactionColl extends Coll<Faction>
return ret;
}
public void reindexFPlayers()
{
for (Faction faction : this.getAll())
{
faction.reindexFPlayers();
}
}
// -------------------------------------------- //
// GET
// -------------------------------------------- //

View File

@ -34,7 +34,7 @@ public class FactionEqualsPredictate implements Predictate<CommandSender>, Seria
public boolean apply(CommandSender sender)
{
FPlayer fplayer = FPlayer.get(sender);
return this.getFactionId().equals(fplayer.getFactionId());
return this.factionId.equals(fplayer.getFactionId());
}
}