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 @Override
public FPlayer load(FPlayer that) public FPlayer load(FPlayer that)
{ {
this.factionId = that.factionId; this.setFactionId(that.factionId);
this.role = that.role; this.role = that.role;
this.title = that.title; this.title = that.title;
this.power = that.power; this.power = that.power;
@ -65,17 +65,79 @@ public class FPlayer extends SenderEntity<FPlayer> implements EconomyParticipato
// -------------------------------------------- // // -------------------------------------------- //
// FIELD: factionId // FIELD: factionId
private String factionId; // TODO: Ensure this one always is null in the nofaction case and never actually the ID of the nofaction-faction.
public Faction getFaction() { if(this.factionId == null) {return null;} return FactionColl.get().get(this.factionId); } // TODO: The getFactionId should however NEVER return null!
public String getFactionId() { return this.factionId; }
public boolean hasFaction() { return this.factionId != null && ! factionId.equals(Const.FACTIONID_NONE); } private String factionId = null;
public void setFaction(Faction faction)
// 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(this, null);
SpoutFeatures.updateTitle(null, this); 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 // FIELD: role
private Rel role; private Rel role;
public Rel getRole() { return this.role; } public Rel getRole() { return this.role; }

View File

@ -62,6 +62,37 @@ public class FPlayerColl extends SenderColl<FPlayer>
oldFile.renameTo(newFile); 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 // EXTRAS
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -16,6 +16,7 @@ import com.massivecraft.factions.util.*;
import com.massivecraft.mcore.mixin.Mixin; import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
import com.massivecraft.mcore.xlib.gson.annotations.SerializedName; 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); return RelationUtil.getColorOfThatToMe(this, observer);
} }
// TODO: Implement a has enough feature.
// -------------------------------------------- // // -------------------------------------------- //
// POWER // POWER
// -------------------------------------------- // // -------------------------------------------- //
// TODO: Implement a has enough feature.
public double getPower() public double getPower()
{ {
@ -685,7 +686,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{ {
ret = ConfServer.powerFactionMax; ret = ConfServer.powerFactionMax;
} }
return ret + this.powerBoost; return ret + this.getPowerBoost();
} }
public double getPowerMax() public double getPowerMax()
@ -704,7 +705,7 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
{ {
ret = ConfServer.powerFactionMax; ret = ConfServer.powerFactionMax;
} }
return ret + this.powerBoost; return ret + this.getPowerBoost();
} }
public int getPowerRounded() public int getPowerRounded()
@ -735,49 +736,72 @@ public class Faction extends Entity<Faction> implements EconomyParticipator
// FOREIGN KEYS: FPLAYERS // 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()) for (FPlayer fplayer : FPlayerColl.get().getAll())
{ {
if (fplayer.getFaction() != this) continue; if (!MUtil.equals(factionId, fplayer.getFactionId())) continue;
ret.add(fplayer); this.fplayers.add(fplayer);
} }
return ret; }
public List<FPlayer> getFPlayers()
{
return new ArrayList<FPlayer>(this.fplayers);
} }
public List<FPlayer> getFPlayersWhereOnline(boolean online) public List<FPlayer> getFPlayersWhereOnline(boolean online)
{ {
List<FPlayer> ret = new ArrayList<FPlayer>(); List<FPlayer> ret = this.getFPlayers();
for (FPlayer fplayer : FPlayerColl.get().getAll()) Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{ {
if (fplayer.getFaction() != this) continue; FPlayer fplayer = iter.next();
if (fplayer.isOnline() != online) continue; if (fplayer.isOnline() != online)
ret.add(fplayer); {
iter.remove();
}
} }
return ret; return ret;
} }
public List<FPlayer> getFPlayersWhereRole(Rel role) public List<FPlayer> getFPlayersWhereRole(Rel role)
{ {
List<FPlayer> ret = new ArrayList<FPlayer>(); List<FPlayer> ret = this.getFPlayers();
for (FPlayer fplayer : FPlayerColl.get().getAll()) Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{ {
if (fplayer.getFaction() != this) continue; FPlayer fplayer = iter.next();
if (fplayer.getRole() != role) continue; if (fplayer.getRole() != role)
ret.add(fplayer); {
iter.remove();
}
} }
return ret; return ret;
} }
public FPlayer getLeader() public FPlayer getLeader()
{ {
for (FPlayer fplayer : FPlayerColl.get().getAll()) List<FPlayer> ret = this.getFPlayers();
Iterator<FPlayer> iter = ret.iterator();
while (iter.hasNext())
{
FPlayer fplayer = iter.next();
if (fplayer.getRole() == Rel.LEADER)
{ {
if (fplayer.getFaction() != this) continue;
if (fplayer.getRole() != Rel.LEADER) continue;
return fplayer; return fplayer;
} }
}
return null; return null;
} }

View File

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

View File

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