Partition by operation type

pull/172/head
M66B 5 years ago
parent 23e7722f6c
commit e5e17a7e9d

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

@ -19,6 +19,9 @@ package eu.faircode.email;
Copyright 2018-2020 by Marcel Bokhorst (M66B) Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/ */
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Objects; import java.util.Objects;
public class TupleOperationEx extends EntityOperation { public class TupleOperationEx extends EntityOperation {
@ -39,4 +42,45 @@ public class TupleOperationEx extends EntityOperation {
} else } else
return false; 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;
}
}
} }

Loading…
Cancel
Save