Partition by operation type

This commit is contained in:
M66B 2020-01-25 18:26:44 +01:00
parent 23e7722f6c
commit e5e17a7e9d
2 changed files with 61 additions and 8 deletions

View File

@ -1125,18 +1125,27 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
" init=" + folder.initialize + " poll=" + folder.poll);
// Partition operations by priority
Map<Integer, List<TupleOperationEx>> partitions = new TreeMap<>();
Map<TupleOperationEx.PartitionKey, List<TupleOperationEx>> partitions = new TreeMap<>(new Comparator<TupleOperationEx.PartitionKey>() {
@Override
public int compare(TupleOperationEx.PartitionKey k1, TupleOperationEx.PartitionKey k2) {
Integer p1 = k1.getPriority();
Integer p2 = k2.getPriority();
return p1.compareTo(p2);
}
});
for (TupleOperationEx op : submit) {
if (!partitions.containsKey(op.priority))
partitions.put(op.priority, new ArrayList<>());
partitions.get(op.priority).add(op);
TupleOperationEx.PartitionKey key = op.getPartitionKey();
if (!partitions.containsKey(key))
partitions.put(key, new ArrayList<>());
partitions.get(key).add(op);
}
for (int priority : partitions.keySet()) {
List<TupleOperationEx> partition = partitions.get(priority);
Log.i(folder.name + " queuing operations=" + partition.size() + " priority=" + priority);
for (TupleOperationEx.PartitionKey key : partitions.keySet()) {
List<TupleOperationEx> partition = partitions.get(key);
Log.i(folder.name + " queuing operations=" + partition.size() + " key=" + key);
executor.submit(new Helper.PriorityRunnable(priority) {
executor.submit(new Helper.PriorityRunnable(key.getPriority()) {
@Override
public void run() {
super.run();

View File

@ -19,6 +19,9 @@ package eu.faircode.email;
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Objects;
public class TupleOperationEx extends EntityOperation {
@ -39,4 +42,45 @@ public class TupleOperationEx extends EntityOperation {
} else
return false;
}
PartitionKey getPartitionKey() {
PartitionKey key = new PartitionKey();
key.id = (MOVE.equals(name) || FETCH.equals(name) ? 0 : this.id);
key.priority = this.priority;
key.operation = this.name;
return key;
}
class PartitionKey {
private long id;
private int priority;
@NonNull
private String operation;
int getPriority() {
return this.priority;
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof PartitionKey) {
PartitionKey other = (PartitionKey) obj;
return (this.id == other.id &&
this.priority == other.priority &&
this.operation.equals(other.operation));
} else
return false;
}
@NonNull
@Override
public String toString() {
return operation + ":" + priority + ":" + id;
}
}
}