2015-12-09 04:32:54 +00:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2015-12-09 04:32:54 +00:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-12-09 04:32:54 +00:00
|
|
|
public class EarlyReceiptCache {
|
|
|
|
|
|
|
|
private static final String TAG = EarlyReceiptCache.class.getSimpleName();
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
private final LRUCache<Long, Map<Address, Long>> cache = new LRUCache<>(100);
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
public synchronized void increment(long timestamp, Address origin) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, this+"");
|
|
|
|
Log.i(TAG, String.format("Early receipt: (%d, %s)", timestamp, origin.serialize()));
|
2017-10-02 21:54:55 +00:00
|
|
|
|
|
|
|
Map<Address, Long> receipts = cache.get(timestamp);
|
|
|
|
|
|
|
|
if (receipts == null) {
|
|
|
|
receipts = new HashMap<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Long count = receipts.get(origin);
|
2015-12-09 04:32:54 +00:00
|
|
|
|
|
|
|
if (count != null) {
|
2017-10-02 21:54:55 +00:00
|
|
|
receipts.put(origin, ++count);
|
2015-12-09 04:32:54 +00:00
|
|
|
} else {
|
2017-10-02 21:54:55 +00:00
|
|
|
receipts.put(origin, 1L);
|
2015-12-09 04:32:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
cache.put(timestamp, receipts);
|
2015-12-09 04:32:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
public synchronized Map<Address, Long> remove(long timestamp) {
|
|
|
|
Map<Address, Long> receipts = cache.remove(timestamp);
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, this+"");
|
|
|
|
Log.i(TAG, String.format("Checking early receipts (%d): %d", timestamp, receipts == null ? 0 : receipts.size()));
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
return receipts != null ? receipts : new HashMap<>();
|
2015-12-09 04:32:54 +00:00
|
|
|
}
|
|
|
|
}
|