Android Open Source - InMemoryDb Async Database Handler






From Project

Back to project page InMemoryDb.

License

The source code is released under:

Apache License

If you think the Android project InMemoryDb listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.gawst.asyncdb;
/*  w ww .  j a v  a2s  .c om*/
import org.gawst.asyncdb.source.DatabaseSource;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Handler;
import android.os.Looper;

/**
 * Class similar to Android's {@link android.content.AsyncQueryHandler AsyncQueryHandler} to work with an
 * {@link org.gawst.asyncdb.AsynchronousDbHelper} instead of a ContentProvider source
 *
 * @see AsyncDbHelperHandler AsyncDbHelperHandler for a ready to use version
 */
public class AsyncDatabaseHandler<INSERT_ID, DATABASE_ID> {

  private final AsynchronousDbHelper<?, INSERT_ID> asynchronousDbHelper;
  private static final Handler mHandler = new Handler(Looper.getMainLooper());
  protected final DatabaseSource<INSERT_ID, DATABASE_ID> dataSource;

  /**
   * Constructor.
   *
   * @param asynchronousDbHelper The {@link org.gawst.asyncdb.AsynchronousDbHelper} database to work with.
   * @param dataSource           The {@link org.gawst.asyncdb.source.DatabaseSource} source used by the {@code asynchronousDbHelper}.
   */
  public AsyncDatabaseHandler(AsynchronousDbHelper<?, INSERT_ID> asynchronousDbHelper, DatabaseSource<INSERT_ID, DATABASE_ID> dataSource) {
    this.asynchronousDbHelper = asynchronousDbHelper;
    this.dataSource = dataSource;
  }

  private void checkDatabaseId(DATABASE_ID databaseId) {
    if (databaseId != null && databaseId != dataSource.getDatabaseId())
      throw new IllegalArgumentException("wrong database id " + databaseId + " expected " + dataSource.getDatabaseId() + " try startRunnable()");
  }

  public AsynchronousDbHelper<?, INSERT_ID> getAsynchronousDbHelper() {
    return asynchronousDbHelper;
  }

  public DatabaseSource<INSERT_ID, DATABASE_ID> getDataSource() {
    return dataSource;
  }

  /**
   * This method begins an asynchronous query. When the query is done
   * {@link #onQueryComplete} is called.
   *
   * @param token         A token passed into {@link #onQueryComplete} to identify
   *                      the query.
   * @param cookie        An object that gets passed into {@link #onQueryComplete}
   * @param projection    A list of which columns to return. Passing null will
   *                      return all columns, which is discouraged to prevent reading data
   *                      from storage that isn't going to be used.
   * @param selection     A filter declaring which rows to return, formatted as an
   *                      SQL WHERE clause (excluding the WHERE itself). Passing null will
   *                      return all rows for the given URI.
   * @param selectionArgs You may include ?s in selection, which will be
   *                      replaced by the values from selectionArgs, in the order that they
   *                      appear in the selection. The values will be bound as Strings.
   * @param orderBy       How to order the rows, formatted as an SQL ORDER BY
   */
  public void startQuery(final int token, final Object cookie,
                         final String[] projection, final String selection, final String[] selectionArgs,
                         final String orderBy) {
    startQuery(token, cookie, projection, selection, selectionArgs, orderBy, null);
  }

  /**
   * <b>Deprecated, the {@code uri} field will be ignored, the one from {@link org.gawst.asyncdb.source.DatabaseSource} will be used.</b>
   * <p/>
   * This method begins an asynchronous query. When the query is done
   * {@link #onQueryComplete} is called.
   *
   * @param token         A token passed into {@link #onQueryComplete} to identify
   *                      the query.
   * @param cookie        An object that gets passed into {@link #onQueryComplete}
   * @param databaseId
   * @param projection    A list of which columns to return. Passing null will
   *                      return all columns, which is discouraged to prevent reading data
   *                      from storage that isn't going to be used.
   * @param selection     A filter declaring which rows to return, formatted as an
   *                      SQL WHERE clause (excluding the WHERE itself). Passing null will
   *                      return all rows for the given URI.
   * @param selectionArgs You may include ?s in selection, which will be
   *                      replaced by the values from selectionArgs, in the order that they
   *                      appear in the selection. The values will be bound as Strings.
   * @param orderBy       How to order the rows, formatted as an SQL ORDER BY
   * @see #startQuery(int, Object, String[], String, String[], String)
   */
  @Deprecated
  public void startQuery(final int token, final Object cookie, DATABASE_ID databaseId,
                         final String[] projection, final String selection, final String[] selectionArgs,
                         final String orderBy) {
    startQuery(token, cookie, databaseId, projection, selection, selectionArgs, orderBy, null);
  }

  /**
   * This method begins an asynchronous query. When the query is done
   * {@link #onQueryComplete} is called.
   *
   * @param token         A token passed into {@link #onQueryComplete} to identify
   *                      the query.
   * @param cookie        An object that gets passed into {@link #onQueryComplete}
   * @param projection    A list of which columns to return. Passing null will
   *                      return all columns, which is discouraged to prevent reading data
   *                      from storage that isn't going to be used.
   * @param selection     A filter declaring which rows to return, formatted as an
   *                      SQL WHERE clause (excluding the WHERE itself). Passing null will
   *                      return all rows for the given URI.
   * @param selectionArgs You may include ?s in selection, which will be
   *                      replaced by the values from selectionArgs, in the order that they
   *                      appear in the selection. The values will be bound as Strings.
   * @param orderBy       How to order the rows, formatted as an SQL ORDER BY
   * @param limit         Limits the number of rows returned by the query,
   *                      formatted as LIMIT clause. Passing null denotes no LIMIT clause.
   */
  public void startQuery(final int token, final Object cookie,
                         final String[] projection, final String selection, final String[] selectionArgs,
                         final String orderBy, final String limit) {
    startQuery(token, cookie, dataSource.getDatabaseId(), projection, selection, selectionArgs, orderBy, limit);
  }

  /**
   * <b>Deprecated, the {@code uri} field will be ignored, the one from {@link org.gawst.asyncdb.source.DatabaseSource} will be used.</b>
   * <p/>
   * This method begins an asynchronous query. When the query is done
   * {@link #onQueryComplete} is called.
   *
   * @param token         A token passed into {@link #onQueryComplete} to identify
   *                      the query.
   * @param cookie        An object that gets passed into {@link #onQueryComplete}
   * @param databaseId
   * @param projection    A list of which columns to return. Passing null will
   *                      return all columns, which is discouraged to prevent reading data
   *                      from storage that isn't going to be used.
   * @param selection     A filter declaring which rows to return, formatted as an
   *                      SQL WHERE clause (excluding the WHERE itself). Passing null will
   *                      return all rows for the given URI.
   * @param selectionArgs You may include ?s in selection, which will be
   *                      replaced by the values from selectionArgs, in the order that they
   *                      appear in the selection. The values will be bound as Strings.
   * @param orderBy       How to order the rows, formatted as an SQL ORDER BY
   * @param limit         Limits the number of rows returned by the query,
   *                      formatted as LIMIT clause. Passing null denotes no LIMIT clause.
   * @see #startQuery(int, Object, String[], String, String[], String, String)
   */
  @Deprecated
  public void startQuery(final int token, final Object cookie, DATABASE_ID databaseId,
                         final String[] projection, final String selection, final String[] selectionArgs,
                         final String orderBy, final String limit) {
    checkDatabaseId(databaseId);
    asynchronousDbHelper.scheduleCustomOperation(new AsynchronousDbOperation() {
      @Override
      public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
        Cursor cursor1;
        try {
          cursor1 = dataSource.query(projection, selection, selectionArgs, null, null, orderBy, limit);
        } catch (Exception e) {
          cursor1 = null;
        }

        final Cursor cursor = cursor1;
        mHandler.post(new Runnable() {
          @Override
          public void run() {
            onQueryComplete(token, cookie, cursor);
          }
        });
      }
    });
  }

  /**
   * <b>Deprecated, the {@code uri} field will be ignored, the one from {@link org.gawst.asyncdb.source.DatabaseSource} will be used.</b>
   * <p/>
   * This method begins an asynchronous insert. When the insert operation is
   * done {@link #onInsertComplete} is called.
   *
   * @param token         A token passed into {@link #onInsertComplete} to identify
   *                      the insert operation.
   * @param cookie        An object that gets passed into {@link #onInsertComplete}
   * @param databaseId    the Uri passed to the insert operation.
   * @param initialValues the ContentValues parameter passed to the insert operation.
   * @see #startInsert(int, Object, android.content.ContentValues)
   */
  @Deprecated
  public void startInsert(final int token, final Object cookie, DATABASE_ID databaseId,
                          final ContentValues initialValues) {
    checkDatabaseId(databaseId);
    startInsert(token, cookie, initialValues);
  }

  /**
   * This method begins an asynchronous insert. When the insert operation is
   * done {@link #onInsertComplete} is called.
   *
   * @param token         A token passed into {@link #onInsertComplete} to identify
   *                      the insert operation.
   * @param cookie        An object that gets passed into {@link #onInsertComplete}
   * @param initialValues the ContentValues parameter passed to the insert operation.
   */
  public void startInsert(final int token, final Object cookie,
                          final ContentValues initialValues) {
    asynchronousDbHelper.scheduleCustomOperation(new AsynchronousDbOperation() {
      @Override
      public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
        INSERT_ID inserted1 = null;
        try {
          inserted1 = dataSource.insert(initialValues);
        } catch (Exception e) {
          inserted1 = null;
        } finally {
          if (inserted1 != null) {
            asynchronousDbHelper.triggerPurgeHandler();
          }
        }

        final INSERT_ID insertId = inserted1;
        mHandler.post(new Runnable() {
          @Override
          public void run() {
            onInsertComplete(token, cookie, insertId);
          }
        });
      }
    });
  }

  /**
   * <b>Deprecated, the {@code uri} field will be ignored, the one from {@link org.gawst.asyncdb.source.DatabaseSource} will be used.</b>
   * <p/>
   * This method begins an asynchronous update. When the update operation is
   * done {@link #onUpdateComplete} is called.
   *
   * @param token      A token passed into {@link #onUpdateComplete} to identify
   *                   the update operation.
   * @param cookie     An object that gets passed into {@link #onUpdateComplete}
   * @param databaseId the Uri passed to the update operation.
   * @param values     the ContentValues parameter passed to the update operation.
   * @see #startUpdate(int, Object, android.content.ContentValues, String, String[])
   */
  @Deprecated
  public void startUpdate(final int token, final Object cookie, DATABASE_ID databaseId,
                          final ContentValues values, final String selection, final String[] selectionArgs) {
    checkDatabaseId(databaseId);
    startUpdate(token, cookie, values, selection, selectionArgs);
  }

  /**
   * This method begins an asynchronous update. When the update operation is
   * done {@link #onUpdateComplete} is called.
   *
   * @param token  A token passed into {@link #onUpdateComplete} to identify
   *               the update operation.
   * @param cookie An object that gets passed into {@link #onUpdateComplete}
   * @param values the ContentValues parameter passed to the update operation.
   */
  public void startUpdate(final int token, final Object cookie,
                          final ContentValues values, final String selection, final String[] selectionArgs) {
    asynchronousDbHelper.scheduleCustomOperation(new AsynchronousDbOperation() {
      @Override
      public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
        int cursor1;
        try {
          cursor1 = dataSource.update(values, selection, selectionArgs);
        } catch (Exception e) {
          cursor1 = 0;
        }

        final int cursor = cursor1;
        mHandler.post(new Runnable() {
          @Override
          public void run() {
            onUpdateComplete(token, cookie, cursor);
          }
        });
      }
    });
  }

  /**
   * <b>Deprecated, the {@code uri} field will be ignored, the one from {@link org.gawst.asyncdb.source.DatabaseSource} will be used.</b>
   * <p/>
   * This method begins an asynchronous delete. When the delete operation is
   * done {@link #onDeleteComplete} is called.
   *
   * @param token      A token passed into {@link #onDeleteComplete} to identify
   *                   the delete operation.
   * @param cookie     An object that gets passed into {@link #onDeleteComplete}
   * @param databaseId the Uri passed to the delete operation.
   * @param selection  the where clause.
   * @see #startDelete(int, Object, String, String[])
   */
  @Deprecated
  public void startDelete(final int token, final Object cookie, DATABASE_ID databaseId,
                          final String selection, final String[] selectionArgs) {
    checkDatabaseId(databaseId);
    startDelete(token, cookie, selection, selectionArgs);
  }

  /**
   * This method begins an asynchronous delete. When the delete operation is
   * done {@link #onDeleteComplete} is called.
   *
   * @param token     A token passed into {@link #onDeleteComplete} to identify
   *                  the delete operation.
   * @param cookie    An object that gets passed into {@link #onDeleteComplete}
   * @param selection the where clause.
   */
  public void startDelete(final int token, final Object cookie,
                          final String selection, final String[] selectionArgs) {
    asynchronousDbHelper.scheduleCustomOperation(new AsynchronousDbOperation() {
      @Override
      public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
        int cursor1;
        try {
          cursor1 = dataSource.delete(selection, selectionArgs);
        } catch (Exception e) {
          cursor1 = 0;
        }

        final int cursor = cursor1;
        mHandler.post(new Runnable() {
          @Override
          public void run() {
            onDeleteComplete(token, cookie, cursor);
          }
        });
      }
    });
  }

  /**
   * This method begins an asynchronous processing of the {@code Runnable}. When the operation is
   * done {@link #onRunnableCompleted} is called.
   *
   * @param token  A token passed into {@link #onRunnableCompleted} to identify the operation.
   * @param cookie An object that gets passed into {@link #onRunnableCompleted}
   * @param job    The {@code Runnable} to run.
   */
  public final void startRunnable(final int token, final Object cookie, final Runnable job) {
    asynchronousDbHelper.scheduleCustomOperation(new AsynchronousDbOperation() {
      @Override
      public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
        job.run();

        mHandler.post(new Runnable() {
          @Override
          public void run() {
            onRunnableCompleted(token, cookie);
          }
        });
      }
    });
  }

  /**
   * Called when an asynchronous query is completed. The receiver is responsible to close the Cursor.
   * </p>Called in the UI thread
   *
   * @param token  the token to identify the query, passed in from
   *               {@link #startQuery}.
   * @param cookie the cookie object passed in from {@link #startQuery}.
   * @param cursor The cursor holding the results from the query.
   */
  protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
    // Empty
  }

  /**
   * Called when an asynchronous insert is completed.
   * </p>Called in the UI thread
   *
   * @param token    the token to identify the query, passed in from
   *                 {@link #startInsert}.
   * @param cookie   the cookie object that's passed in from
   *                 {@link #startInsert}.
   * @param insertId the uri returned from the insert operation.
   */
  protected void onInsertComplete(int token, Object cookie, INSERT_ID insertId) {
    // Empty
  }

  /**
   * Called when an asynchronous update is completed.
   * </p>Called in the UI thread
   *
   * @param token  the token to identify the query, passed in from
   *               {@link #startUpdate}.
   * @param cookie the cookie object that's passed in from
   *               {@link #startUpdate}.
   * @param result the result returned from the update operation
   */
  protected void onUpdateComplete(int token, Object cookie, int result) {
    // Empty
  }

  /**
   * Called when an asynchronous delete is completed.
   * </p>Called in the UI thread
   *
   * @param token  the token to identify the query, passed in from
   *               {@link #startDelete}.
   * @param cookie the cookie object that's passed in from
   *               {@link #startDelete}.
   * @param result the result returned from the delete operation
   */
  protected void onDeleteComplete(int token, Object cookie, int result) {
    // Empty
  }

  /**
   * Called when an asynchronous {@code Runnable} is completed.
   * </p>Called in the UI thread
   *
   * @param token  the token to identify the query, passed in from
   *               {@link #startRunnable}.
   * @param cookie the cookie object that's passed in from
   *               {@link #startRunnable}.
   */
  protected void onRunnableCompleted(int token, Object cookie) {
    // Empty
  }
}




Java Source Code List

org.gawst.asyncdb.AsyncDatabaseHandler.java
org.gawst.asyncdb.AsyncDbHelperHandler.java
org.gawst.asyncdb.AsyncQueryHandler.java
org.gawst.asyncdb.AsynchronousDatabase.java
org.gawst.asyncdb.AsynchronousDbErrorHandler.java
org.gawst.asyncdb.AsynchronousDbHelper.java
org.gawst.asyncdb.AsynchronousDbOperation.java
org.gawst.asyncdb.DataSource.java
org.gawst.asyncdb.InMemoryDbArrayList.java
org.gawst.asyncdb.InMemoryDbCopyOnWriteArrayList.java
org.gawst.asyncdb.InMemoryDbList.java
org.gawst.asyncdb.InMemoryDbListener.java
org.gawst.asyncdb.InMemoryDbMap.java
org.gawst.asyncdb.InMemoryDbSet.java
org.gawst.asyncdb.InMemoryDbTreeSet.java
org.gawst.asyncdb.InMemoryHashmapDb.java
org.gawst.asyncdb.InMemoryLruCache.java
org.gawst.asyncdb.InvalidDbEntry.java
org.gawst.asyncdb.InvalidEntry.java
org.gawst.asyncdb.LogManager.java
org.gawst.asyncdb.Logger.java
org.gawst.asyncdb.LruCache.java
org.gawst.asyncdb.MapDataSource.java
org.gawst.asyncdb.MapDatabaseElementHandler.java
org.gawst.asyncdb.MapEntry.java
org.gawst.asyncdb.adapter.InMemoryArrayListAdapter.java
org.gawst.asyncdb.adapter.InMemoryFilteredAdapter.java
org.gawst.asyncdb.adapter.InMemoryFilteredListAdapter.java
org.gawst.asyncdb.adapter.InMemoryFilteredTreeAdapter.java
org.gawst.asyncdb.adapter.InMemoryTreeSetAdapter.java
org.gawst.asyncdb.adapter.UIHandler.java
org.gawst.asyncdb.purge.DatabasePurgerMaxDate.java
org.gawst.asyncdb.purge.DatabaseSourcePurgerMax.java
org.gawst.asyncdb.purge.DatabaseSourcePurger.java
org.gawst.asyncdb.purge.PurgeHandler.java
org.gawst.asyncdb.source.ContentProviderDataSource.java
org.gawst.asyncdb.source.CursorDataSource.java
org.gawst.asyncdb.source.DatabaseElementHandler.java
org.gawst.asyncdb.source.DatabaseSource.java
org.gawst.asyncdb.source.SqliteDataSource.java
org.gawst.asyncdb.source.SqliteMapDataSource.java
org.gawst.asyncdb.source.typed.TypedContentProviderDataSource.java
org.gawst.asyncdb.source.typed.TypedCursorDataSource.java
org.gawst.asyncdb.source.typed.TypedDatabaseElementHandler.java
org.gawst.asyncdb.source.typed.TypedDatabaseSource.java
org.gawst.asyncdb.source.typed.TypedSqliteDataSource.java
org.gawst.asyncdb.source.typed.TypedSqliteMapDataSource.java