Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce TransactionEndTask. Implements #531 #532

Merged
merged 1 commit into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BUILD_TOOLS_VERSION=26.0.2
MIN_SDK_VERSION=9
TARGET_SDK_VERSION=25
# dependency versions
SUPPORT_LIBRARY_VERSION=25.0.1
SUPPORT_LIBRARY_VERSION=25.4.0
CONTENTPAL_VERSION=3effe48
ROBOLECTRIC_VERSION=3.1.4
JEMS_VERSION=1.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import org.dmfs.iterables.SingletonIterable;
import org.dmfs.iterables.decorators.Flattened;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -46,11 +49,17 @@
abstract class SQLiteContentProvider extends ContentProvider
{

interface TransactionEndTask
{
void execute(SQLiteDatabase database);
}


@SuppressWarnings("unused")
private static final String TAG = "SQLiteContentProvider";

private SQLiteOpenHelper mOpenHelper;
private Set<Uri> mChangedUris;
private final Set<Uri> mChangedUris = new HashSet<>();

private final ThreadLocal<Boolean> mApplyingBatch = new ThreadLocal<Boolean>();
private static final int SLEEP_AFTER_YIELD_DELAY = 4000;
Expand All @@ -60,13 +69,20 @@ abstract class SQLiteContentProvider extends ContentProvider
*/
private static final int MAX_OPERATIONS_PER_YIELD_POINT = 500;

private final Iterable<TransactionEndTask> mTransactionEndTasks;


protected SQLiteContentProvider(Iterable<TransactionEndTask> transactionEndTasks)
{
// append a task to set the transaction to successful
mTransactionEndTasks = new Flattened<>(transactionEndTasks, new SingletonIterable<TransactionEndTask>(new SuccessfulTransactionEndTask()));
}


@Override
public boolean onCreate()
{
Context context = getContext();
mOpenHelper = getDatabaseHelper(context);
mChangedUris = new HashSet<Uri>();
mOpenHelper = getDatabaseHelper(getContext());
return true;
}

Expand Down Expand Up @@ -136,7 +152,7 @@ public Uri insert(Uri uri, ContentValues values)
try
{
result = insertInTransaction(db, uri, values, callerIsSyncAdapter);
db.setTransactionSuccessful();
endTransaction(db);
}
finally
{
Expand Down Expand Up @@ -167,7 +183,7 @@ public int bulkInsert(Uri uri, ContentValues[] values)
insertInTransaction(db, uri, values[i], callerIsSyncAdapter);
db.yieldIfContendedSafely();
}
db.setTransactionSuccessful();
endTransaction(db);
}
finally
{
Expand All @@ -192,7 +208,7 @@ public int update(Uri uri, ContentValues values, String selection, String[] sele
try
{
count = updateInTransaction(db, uri, values, selection, selectionArgs, callerIsSyncAdapter);
db.setTransactionSuccessful();
endTransaction(db);
}
finally
{
Expand Down Expand Up @@ -223,7 +239,7 @@ public int delete(Uri uri, String selection, String[] selectionArgs)
try
{
count = deleteInTransaction(db, uri, selection, selectionArgs, callerIsSyncAdapter);
db.setTransactionSuccessful();
endTransaction(db);
}
finally
{
Expand Down Expand Up @@ -275,7 +291,7 @@ public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> op
}
results[i] = operation.apply(this, results, i);
}
db.setTransactionSuccessful();
endTransaction(db);
return results;
}
finally
Expand Down Expand Up @@ -309,4 +325,25 @@ protected boolean syncToNetwork(Uri uri)
return false;
}


private void endTransaction(SQLiteDatabase database)
{
for (TransactionEndTask task : mTransactionEndTasks)
{
task.execute(database);
}
}


/**
* A {@link TransactionEndTask} which sets the transaction to be successful.
*/
private static class SuccessfulTransactionEndTask implements TransactionEndTask
{
@Override
public void execute(SQLiteDatabase database)
{
database.setTransactionSuccessful();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import android.os.HandlerThread;
import android.text.TextUtils;

import org.dmfs.iterables.EmptyIterable;
import org.dmfs.provider.tasks.TaskDatabaseHelper.OnDatabaseOperationListener;
import org.dmfs.provider.tasks.TaskDatabaseHelper.Tables;
import org.dmfs.provider.tasks.handler.PropertyHandler;
Expand Down Expand Up @@ -148,6 +149,13 @@ public final class TaskProvider extends SQLiteContentProvider implements OnAccou
private ProviderOperationsLog mOperationsLog = new ProviderOperationsLog();


public TaskProvider()
{
// for now we don't have anything specific to execute before the transaction ends.
super(EmptyIterable.<TransactionEndTask>instance());
}


@Override
public boolean onCreate()
{
Expand Down