2015-12-09 04:32:54 +00:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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) {
|
2015-12-09 04:32:54 +00:00
|
|
|
Log.w(TAG, this+"");
|
2017-10-02 21:54:55 +00:00
|
|
|
Log.w(TAG, String.format("Early receipt: (%d, %s)", timestamp, origin.serialize()));
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
Log.w(TAG, this+"");
|
|
|
|
Log.w(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
|
|
|
}
|
|
|
|
}
|