private final TIntObjectMap<TIntObjectMap<byte[]>> _pledgeCrestLarge = new TIntObjectHashMap<>();
private final TIntObjectMap<TIntObjectMap<byte[]>> _pledgeCrestLargeTemp = new TIntObjectHashMap<>();
private final TIntIntMap _pledgeCrestLargeId = new TIntIntHashMap();
private void loadLargeCrests()
{
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
int pledgeId, crestId, crestPartId;
byte[] crest;
int count = 0;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("SELECT clan_id, data FROM clan_largecrests WHERE crest_part=0 AND data IS NOT NULL");
rset = statement.executeQuery();
while (rset.next())
{
count++;
pledgeId = rset.getInt("clan_id");
crest = rset.getBytes("data");
crestId = getCrestId(pledgeId, crest);
_pledgeCrestLargeId.put(pledgeId, crestId);
}
DbUtils.close(statement, rset);
statement = con.prepareStatement("SELECT clan_id, crest_part, data FROM clan_largecrests WHERE data IS NOT NULL ORDER BY clan_id asc, crest_part asc");
rset = statement.executeQuery();
while (rset.next())
{
pledgeId = rset.getInt("clan_id");
crestPartId = rset.getInt("crest_part");
crest = rset.getBytes("data");
if (!_pledgeCrestLargeId.containsKey(pledgeId))
{
warn("Clan large crest has crashed. Clan ID: " + pledgeId);
continue;
}
crestId = _pledgeCrestLargeId.get(pledgeId);
TIntObjectMap<byte[]> crestMap = _pledgeCrestLarge.get(crestId);
if (crestMap == null)
{
crestMap = new TIntObjectHashMap<>();
}
crestMap.put(crestPartId, crest);
_pledgeCrestLarge.put(crestId, crestMap);
}
DbUtils.close(statement, rset);
info("CrestCache: Loaded " + count + " Large crests");
}
catch (final SQLException e)
{
warn("There was an error while loading large crests from database:", e);
}
}
public void removePledgeCrestLarge(int pledgeId)
{
try
{
_pledgeCrestLarge.remove(_pledgeCrestLargeId.remove(pledgeId));
}
finally
{}
Connection con = null;
PreparedStatement statement = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("DELETE FROM clan_largecrests WHERE clan_id=?");
statement.setInt(1, pledgeId);
statement.execute();
}
catch (final Exception e)
{
_log.log(Level.FINE, e.getMessage(), e);
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, statement);
}
}
public static int getByteMapSize(TIntObjectMap<byte[]> map)
{
int size = 0;
if (map != null && !map.isEmpty())
{
for (final byte[] tempCrest : map.valueCollection())
{
size += tempCrest.length;
}
}
return size;
}
public int savePledgeCrestLarge(int pledgeId, int crestPart, int crestTotalSize, byte[] data)
{
int crestId = 0;
try
{
TIntObjectMap<byte[]> crest = _pledgeCrestLargeTemp.get(pledgeId);
if (crestPart == 0)
{
_pledgeCrestLargeTemp.remove(pledgeId);
crest = new TIntObjectHashMap<>();
}
if (crest != null)
{
crest.put(crestPart, data);
final int tempSize = getByteMapSize(crest);
if (crestTotalSize > tempSize)
{
_pledgeCrestLargeTemp.put(pledgeId, crest);
}
else if (crestTotalSize < tempSize)
{
_pledgeCrestLargeTemp.remove(pledgeId);
_log.log(Level.WARNING, "Error while save pledge large crest, clan_id: " + pledgeId + ", crest_part: " + crestPart + ", crest_total_size: " + crestTotalSize + ", temp_size: " + tempSize);
}
else
{
crestId = getCrestId(pledgeId, crest.get(0));
_pledgeCrestLargeId.put(pledgeId, crestId);
_pledgeCrestLarge.put(crestId, crest);
Connection con = null;
PreparedStatement statement = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
statement = con.prepareStatement("DELETE FROM clan_largecrests WHERE clan_id=?");
statement.setInt(1, pledgeId);
statement.execute();
DbUtils.closeQuietly(statement);
for (final TIntObjectIterator<byte[]> iterator = crest.iterator(); iterator.hasNext();)
{
iterator.advance();
statement = con.prepareStatement("REPLACE INTO clan_largecrests(clan_id, crest_part, data) VALUES (?,?,?)");
statement.setInt(1, pledgeId);
statement.setInt(2, iterator.key());
statement.setBytes(3, iterator.value());
statement.execute();
DbUtils.closeQuietly(statement);
}
}
catch (final Exception e)
{
_log.log(Level.FINE, e.getMessage(), e);
e.printStackTrace();
}
finally
{
DbUtils.closeQuietly(con, statement);
}
_pledgeCrestLargeTemp.remove(pledgeId);
}
}
else
{
_log.log(Level.WARNING, "Error while save pledge large crest, clan_id: " + pledgeId + ", crest_part: " + crestPart + ", crest_total_size: " + crestTotalSize);
}
}
finally
{}
return crestId;
}