Conditionally run operations

This commit is contained in:
M66B 2020-02-11 16:09:29 +01:00
parent eaf857535d
commit a0ae00ba2c
2 changed files with 76 additions and 5 deletions

View File

@ -36,6 +36,7 @@ import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.RemoteInput; import androidx.core.app.RemoteInput;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -134,7 +135,7 @@ class Core {
Context context, Context context,
EntityAccount account, EntityFolder folder, List<TupleOperationEx> ops, EntityAccount account, EntityFolder folder, List<TupleOperationEx> ops,
Store istore, Folder ifolder, Store istore, Folder ifolder,
State state) State state, int priority, long sequence)
throws JSONException { throws JSONException {
try { try {
Log.i(folder.name + " start process"); Log.i(folder.name + " start process");
@ -145,7 +146,8 @@ class Core {
boolean group = true; boolean group = true;
Log.i(folder.name + " executing operations=" + ops.size()); Log.i(folder.name + " executing operations=" + ops.size());
while (retry < OPERATION_RETRY_MAX && ops.size() > 0 && while (retry < OPERATION_RETRY_MAX && ops.size() > 0 &&
state.isRunning() && state.isRecoverable() && ifolder.isOpen()) { state.batchCanRun(folder.id, priority, sequence) &&
state.isRunning() && state.isRecoverable()) {
TupleOperationEx op = ops.get(0); TupleOperationEx op = ops.get(0);
try { try {
@ -481,8 +483,9 @@ class Core {
} }
} }
// Check account/folder if (ops.size() == 0)
if (ops.size() > 0) state.batchCompleted(folder.id, priority, sequence);
else // Check account/folder
state.error(new OperationCanceledException()); state.error(new OperationCanceledException());
} finally { } finally {
Log.i(folder.name + " end process state=" + state); Log.i(folder.name + " end process state=" + state);
@ -3335,6 +3338,9 @@ class Core {
private boolean maxConnections = false; private boolean maxConnections = false;
private Long lastActivity = null; private Long lastActivity = null;
private Map<FolderPriority, Long> sequence = new HashMap<>();
private Map<FolderPriority, Long> batch = new HashMap<>();
State(ConnectionHelper.NetworkState networkState) { State(ConnectionHelper.NetworkState networkState) {
this.networkState = networkState; this.networkState = networkState;
} }
@ -3408,6 +3414,10 @@ class Core {
recoverable = true; recoverable = true;
maxConnections = false; maxConnections = false;
lastActivity = null; lastActivity = null;
synchronized (this) {
for (FolderPriority key : sequence.keySet())
batch.put(key, sequence.get(key));
}
} }
private void yield() { private void yield() {
@ -3460,6 +3470,41 @@ class Core {
return (lastActivity == null ? 0 : SystemClock.elapsedRealtime() - lastActivity); return (lastActivity == null ? 0 : SystemClock.elapsedRealtime() - lastActivity);
} }
long getSequence(long folder, int priority) {
synchronized (this) {
FolderPriority key = new FolderPriority(folder, priority);
if (!sequence.containsKey(key)) {
sequence.put(key, 0L);
batch.put(key, 0L);
}
long result = sequence.get(key);
sequence.put(key, result + 1);
if (BuildConfig.DEBUG)
Log.i("=== Get " + folder + ":" + priority + " sequence=" + result);
return result;
}
}
boolean batchCanRun(long folder, int priority, long current) {
synchronized (this) {
FolderPriority key = new FolderPriority(folder, priority);
boolean can = batch.get(key).equals(current);
if (BuildConfig.DEBUG)
Log.i("=== Can " + folder + ":" + priority + " can=" + can);
return can;
}
}
void batchCompleted(long folder, int priority, long current) {
synchronized (this) {
FolderPriority key = new FolderPriority(folder, priority);
if (batch.get(key).equals(current))
batch.put(key, batch.get(key) + 1);
if (BuildConfig.DEBUG)
Log.i("=== Completed " + folder + ":" + priority + " next=" + batch.get(key));
}
}
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {
@ -3467,5 +3512,29 @@ class Core {
",recoverable=" + recoverable + ",recoverable=" + recoverable +
",idle=" + getIdleTime() + "]"; ",idle=" + getIdleTime() + "]";
} }
private static class FolderPriority {
private long folder;
private int priority;
FolderPriority(long folder, int priority) {
this.folder = folder;
this.priority = priority;
}
@Override
public int hashCode() {
return (int) (this.folder * 37 + priority);
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof FolderPriority) {
FolderPriority other = (FolderPriority) obj;
return (this.folder == other.folder && this.priority == other.priority);
} else
return false;
}
}
} }
} }

View File

@ -1195,6 +1195,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
try { try {
final long sequence = state.getSequence(folder.id, key.getPriority());
executor.submit(new Helper.PriorityRunnable(key.getPriority(), key.getOrder()) { executor.submit(new Helper.PriorityRunnable(key.getPriority(), key.getOrder()) {
@Override @Override
public void run() { public void run() {
@ -1243,7 +1245,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
account, folder, account, folder,
partition, partition,
iservice.getStore(), ifolder, iservice.getStore(), ifolder,
state); state, key.getPriority(), sequence);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);