Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed calculation of milliseconds until removal #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
26 changes: 13 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
</repository>
<repository>
<id>codecrafter47-repo</id>
<url>http://nexus.codecrafter47.dyndns.eu/content/repositories/public/</url>
<url>https://nexus.codecrafter47.dyndns.eu/content/repositories/public/</url>
</repository>
<repository>
<id>dakani</id>
<name>Dakani Nexus Repo</name>
<url>http://repo.dakanilabs.com/content/repositories/public</url>
<url>https://repo.dakanilabs.com/content/repositories/public</url>
</repository>
<repository>
<id>CodeMC</id>
Expand All @@ -31,26 +31,26 @@
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.8-SNAPSHOT</version>
<version>1.20-R0.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.2.0</version>
<version>2.11.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand All @@ -62,7 +62,7 @@
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bungeecord</artifactId>
<version>1.4</version>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -83,16 +83,16 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.12.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -130,12 +130,12 @@
<repository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://nexus.codecrafter47.dyndns.eu/content/repositories/releases/</url>
<url>https://nexus.codecrafter47.dyndns.eu/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://nexus.codecrafter47.dyndns.eu/content/repositories/snapshots/</url>
<url>https://nexus.codecrafter47.dyndns.eu/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
25 changes: 8 additions & 17 deletions src/main/java/codecrafter47/bungeemail/BungeeMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ public void onEnable() {
return;
}
// schedule saving
getProxy().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
fileBackend.saveData();
}
}, 2, 2, TimeUnit.MINUTES);
getProxy().getScheduler().schedule(this, fileBackend::saveData, 2, 2, TimeUnit.MINUTES);
storage = fileBackend;
} else {
storage = new MySQLBackend(this);
Expand All @@ -83,7 +78,7 @@ public void run() {
instance = this;

// Start metrics
Metrics metrics = new Metrics(this);
new Metrics(this, 4570);

TabCompleteCache tabCompleteCache = null;
if (config.getBoolean("enable_tab_complete")) {
Expand All @@ -94,14 +89,11 @@ public void run() {
getProxy().getPluginManager().registerListener(this, new PlayerListener(this, tabCompleteCache));

if (config.getBoolean("cleanup_enabled", false)) {
getProxy().getScheduler().schedule(this, new Runnable() {
@Override
public void run() {
try {
storage.deleteOlder(System.currentTimeMillis() - (60L * 60L * 24L * config.getLong("cleanup_threshold", 7L)), false);
} catch (StorageException e) {
getLogger().log(Level.WARNING, "Automatic database cleanup failed", e);
}
getProxy().getScheduler().schedule(this, () -> {
try {
storage.deleteOlder(System.currentTimeMillis() - (1000L * 60L * 60L * 24L * config.getLong("cleanup_threshold", 7L)), false);
} catch (StorageException e) {
getLogger().log(Level.WARNING, "Automatic database cleanup failed", e);
}
}, 1, 120, TimeUnit.MINUTES);
}
Expand Down Expand Up @@ -147,8 +139,7 @@ public void listMessages(CommandSender sender, int start, boolean listIfNotAvail
int i = 1;
int end = start + 9;
if (end >= messages.size()) end = messages.size();
List<BaseComponent> output = new ArrayList<>();
output.addAll(Arrays.asList(ChatUtil.parseBBCode(headerTemplate.
List<BaseComponent> output = new ArrayList<>(Arrays.asList(ChatUtil.parseBBCode(headerTemplate.
replace("%start%", "" + start).replace("%end%", "" + end).
replace("%max%", "" + messages.size()).replace("%list%", listReadMessages ? "listall" : "list").
replace("%next%", "" + (end + 1)).replace("%visible%", messages.size() > 10 ? "" + 10 : ("" + messages.size())))));
Expand Down
67 changes: 29 additions & 38 deletions src/main/java/codecrafter47/bungeemail/FlatFileBackend.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReadWriteLock;
Expand All @@ -16,14 +16,14 @@
import java.util.logging.Logger;

public class FlatFileBackend implements IStorageBackend {
private Logger logger;
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
private File saveFile;
private File tmpSaveFile;
private final Logger logger;
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final File saveFile;
private final File tmpSaveFile;
private Data data;
private ReadWriteLock mailLock = new ReentrantReadWriteLock();
private ReadWriteLock uuidLock = new ReentrantReadWriteLock();
private ReadWriteLock fileLock = new ReentrantReadWriteLock();
private final ReadWriteLock mailLock = new ReentrantReadWriteLock();
private final ReadWriteLock uuidLock = new ReentrantReadWriteLock();
private final ReadWriteLock fileLock = new ReentrantReadWriteLock();
private boolean saveRequested = false;

public FlatFileBackend(BungeeMail plugin) {
Expand Down Expand Up @@ -67,37 +67,33 @@ public boolean readData() {

/**
* Attempts to save the mail data to a file
*
* @return true on success, false otherwise
*/
public boolean saveData() {
public void saveData() {
if (saveRequested) {
saveRequested = false;
mailLock.readLock().lock();
uuidLock.readLock().lock();
fileLock.writeLock().lock();
try {
if (tmpSaveFile.exists()) {
if (!tmpSaveFile.delete()) return false;
if (!tmpSaveFile.delete()) return;
}
if (!tmpSaveFile.createNewFile()) return false;
Writer writer = new OutputStreamWriter(new FileOutputStream(tmpSaveFile), Charsets.UTF_8);
if (!tmpSaveFile.createNewFile()) return;
Writer writer = new OutputStreamWriter(Files.newOutputStream(tmpSaveFile.toPath()), Charsets.UTF_8);
gson.toJson(data, writer);
writer.close();
if (saveFile.exists()) {
if (!saveFile.delete()) return false;
if (!saveFile.delete()) return;
}
return tmpSaveFile.renameTo(saveFile);
tmpSaveFile.renameTo(saveFile);
} catch (IOException ex) {
logger.log(Level.WARNING, "Failed to save file to disk", ex);
return false;
} finally {
fileLock.writeLock().unlock();
uuidLock.readLock().unlock();
mailLock.readLock().unlock();
}
}
return true;
}

/**
Expand All @@ -108,7 +104,7 @@ private void requestSave() {
}

@Override
public List<Message> getMessagesFor(UUID uuid, boolean onlyNew) throws StorageException {
public List<Message> getMessagesFor(UUID uuid, boolean onlyNew) {
mailLock.readLock().lock();
try {
ArrayList<Message> messages = new ArrayList<>();
Expand All @@ -122,7 +118,7 @@ public List<Message> getMessagesFor(UUID uuid, boolean onlyNew) throws StorageEx
}

@Override
public Message saveMessage(String senderName, UUID senderUUID, UUID recipient, String message, boolean read, long time) throws StorageException {
public Message saveMessage(String senderName, UUID senderUUID, UUID recipient, String message, boolean read, long time) {
mailLock.writeLock().lock();
try {
FlatFileMessage mail = new FlatFileMessage(time, read, message, recipient, senderUUID, senderName);
Expand All @@ -135,7 +131,7 @@ public Message saveMessage(String senderName, UUID senderUUID, UUID recipient, S
}

@Override
public int saveMessageToAll(String senderName, UUID senderUUID, String message, boolean read, long time) throws StorageException {
public int saveMessageToAll(String senderName, UUID senderUUID, String message, boolean read, long time) {
Collection<UUID> targets = getAllKnownUUIDs();
mailLock.writeLock().lock();
try {
Expand All @@ -151,7 +147,7 @@ public int saveMessageToAll(String senderName, UUID senderUUID, String message,
}

@Override
public void markRead(Message message) throws StorageException {
public void markRead(Message message) {
Preconditions.checkArgument(message instanceof FlatFileMessage);
mailLock.writeLock().lock();
try {
Expand All @@ -163,7 +159,7 @@ public void markRead(Message message) throws StorageException {
}

@Override
public void delete(Message message) throws StorageException {
public void delete(Message message) {
Preconditions.checkArgument(message instanceof FlatFileMessage);
mailLock.writeLock().lock();
try {
Expand All @@ -175,7 +171,7 @@ public void delete(Message message) throws StorageException {
}

@Override
public boolean delete(long id, UUID recipient) throws StorageException {
public boolean delete(long id, UUID recipient) {
boolean deleted = false;
mailLock.writeLock().lock();
try {
Expand All @@ -195,23 +191,18 @@ public boolean delete(long id, UUID recipient) throws StorageException {
}

@Override
public void deleteOlder(long time, boolean deleteUnread) throws StorageException {
public void deleteOlder(long time, boolean deleteUnread) {
mailLock.writeLock().lock();
try {
for (Iterator<FlatFileMessage> iterator = data.data.iterator(); iterator.hasNext(); ) {
Message message = iterator.next();
if (message.getTime() < time && (deleteUnread || message.isRead())) {
iterator.remove();
}
}
data.data.removeIf(message -> message.getTime() < time && (deleteUnread || message.isRead()));
requestSave();
} finally {
mailLock.writeLock().unlock();
}
}

@Override
public UUID getUUIDForName(String name) throws StorageException {
public UUID getUUIDForName(String name) {
if ("Console".equals(name)) {
return BungeeMail.CONSOLE_UUID;
}
Expand All @@ -233,7 +224,7 @@ public UUID getUUIDForName(String name) throws StorageException {
}

@Override
public Collection<UUID> getAllKnownUUIDs() throws StorageException {
public Collection<UUID> getAllKnownUUIDs() {
uuidLock.readLock().lock();
try {
return ImmutableSet.copyOf(data.uuidMap.values());
Expand All @@ -243,7 +234,7 @@ public Collection<UUID> getAllKnownUUIDs() throws StorageException {
}

@Override
public Collection<String> getKnownUsernames() throws StorageException {
public Collection<String> getKnownUsernames() {
uuidLock.readLock().lock();
try {
return ImmutableSet.copyOf(data.uuidMap.keySet());
Expand All @@ -253,7 +244,7 @@ public Collection<String> getKnownUsernames() throws StorageException {
}

@Override
public void updateUserEntry(UUID uuid, String username) throws StorageException {
public void updateUserEntry(UUID uuid, String username) {
uuidLock.writeLock().lock();
try {
data.uuidMap.put(username, uuid);
Expand All @@ -272,7 +263,7 @@ private static class FlatFileMessage implements Message {
private long time;
private transient final long id;

private static AtomicLong idSupplier = new AtomicLong(1);
private static final AtomicLong idSupplier = new AtomicLong(1);

public FlatFileMessage(long time, boolean read, String message, UUID recipient, UUID senderUUID, String senderName) {
this();
Expand Down Expand Up @@ -339,7 +330,7 @@ public boolean equals(Object other) {
}

private static class Data {
private List<FlatFileMessage> data = new ArrayList<>();
private Map<String, UUID> uuidMap = new HashMap<>();
private final List<FlatFileMessage> data = new ArrayList<>();
private final Map<String, UUID> uuidMap = new HashMap<>();
}
}
Loading