Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.52</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>clean package install</defaultGoal>
Expand Down Expand Up @@ -79,6 +85,8 @@
<artifactSet>
<includes>
<include>org.mongodb:mongo-java-driver</include>
<include>org.apache.tomcat:tomcat-jdbc</include>
<include>org.apache.tomcat:tomcat-juli</include>
</includes>
</artifactSet>
</configuration>
Expand Down
68 changes: 27 additions & 41 deletions src/org/melonbrew/fe/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

public abstract class Database {
private final Fe plugin;
private final Set<Account> cachedAccounts;
private final HashMap<UUID, Account> cachedAccountsUUID;
private final HashMap<String, Account> cachedAccountsUsername;
protected boolean cacheAccounts;

public Database(Fe plugin) {
this.plugin = plugin;

this.cachedAccounts = new HashSet<Account>();
this.cachedAccountsUUID = new HashMap<UUID, Account>();
this.cachedAccountsUsername = new HashMap<String, Account>();
}

public boolean init() {
Expand All @@ -26,38 +28,27 @@ public boolean init() {
}

public List<Account> getTopAccounts(int size) {
List<Account> topAccounts = loadTopAccounts(size * 2);

if (!cachedAccounts.isEmpty()) {
for (Account account : cachedAccounts) {
List<Account> topAccounts = this.loadTopAccounts(size * 2);
if (!this.cachedAccountsUUID.isEmpty()) {
final Account lowest = topAccounts.get(topAccounts.size() - 1);
final List<Account> cachedTopAccounts = new ArrayList<Account>();
for (final Account account : this.cachedAccountsUUID.values()) {
topAccounts.remove(account);
}

List<Account> cachedTopAccounts = new ArrayList<Account>(cachedAccounts);

Collections.sort(cachedTopAccounts, new Comparator<Account>() {
public int compare(Account account1, Account account2) {
return (int) (account2.getMoney() - account1.getMoney());
if (lowest.getMoney() <= account.getMoney()) {
cachedTopAccounts.add(account);
}
});

if (cachedAccounts.size() > size) {
cachedTopAccounts = cachedTopAccounts.subList(0, size);
}

topAccounts.addAll(cachedTopAccounts);
}

Collections.sort(topAccounts, new Comparator<Account>() {
public int compare(Account account1, Account account2) {
return (int) (account2.getMoney() - account1.getMoney());
@Override
public int compare(final Account account1, final Account account2) {
return (int)(account2.getMoney() - account1.getMoney());
}
});

if (topAccounts.size() > size) {
topAccounts = topAccounts.subList(0, size);
}

return topAccounts;
}

Expand All @@ -82,9 +73,8 @@ public void removeAccount(String name, String uuid) {
public abstract void clean();

public void removeAllAccounts() {
for (Account account : new HashSet<Account>(cachedAccounts)) {
cachedAccounts.remove(account);
}
cachedAccountsUUID.clear();
cachedAccountsUsername.clear();
}

protected boolean convertToUUID() {
Expand Down Expand Up @@ -146,15 +136,11 @@ protected boolean convertToUUID() {
}

public void close() {
Iterator<Account> iterator = cachedAccounts.iterator();

while (iterator.hasNext()) {
Account account = iterator.next();

for (final Account account : this.cachedAccountsUUID.values()) {
account.save(account.getMoney());

iterator.remove();
}
this.cachedAccountsUUID.clear();
this.cachedAccountsUsername.clear();
}

public abstract String getName();
Expand Down Expand Up @@ -202,7 +188,8 @@ private Account createAndAddAccount(String name, String uuid, double money) {
Player player = plugin.getServer().getPlayerExact(name);

if (player != null) {
cachedAccounts.add(account);
this.cachedAccountsUsername.put(name, account);
this.cachedAccountsUUID.put(UUID.fromString(uuid), account);
}
}

Expand All @@ -218,17 +205,16 @@ public boolean cacheAccounts() {
}

public Account getCachedAccount(String name, String uuid) {
for (Account account : cachedAccounts) {
if (account.getName().equals(name)) {
return account;
}
if (uuid != null) {
return this.cachedAccountsUUID.get(UUID.fromString(uuid));
}

return null;
return this.cachedAccountsUsername.get(name);
}

public boolean removeCachedAccount(Account account) {
return cachedAccounts.remove(account);
this.cachedAccountsUUID.remove(UUID.fromString(account.getUUID()));
this.cachedAccountsUsername.remove(account.getName());
return true;
}

public abstract int getVersion();
Expand Down
Loading