Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.gawst.asyncdb.purge;
  
  
  import  android.database.Cursor;
  import  android.support.annotation.NonNull;
 import  android.support.annotation.Nullable;

Abstract helper class to purge a database.

Parameters:
<LAST_ELEMENT> Type of the last element to keep during the purge.
 
 public abstract class DatabaseSourcePurger<LAST_ELEMENT> implements PurgeHandler {
 	private final TypedDatabaseSource<?, ?, ? extends Cursor> dataSource;
 	private final int maxItems;
 	private final int checkInsertFrequency;
 	private Integer nextCheck;

Constructor for the purger. It will purge the database after each insertion.

Parameters:
maxItems Maximum number of items to keep in the database.
databaseSource Database source (Sqlite, ContentProvider)
 
 	public DatabaseSourcePurger(int maxItemsTypedDatabaseSource<?, ?, ?> databaseSource) {
 		this(maxItems, 1, databaseSource);
 	}

Constructor for the purger.

Parameters:
maxItems Maximum number of items to keep in the database.
checkInsertFrequency The number of insertion before a purge is done. A purge is done after the first insertion.
databaseSource Database source (Sqlite, ContentProvider)
 
 	public DatabaseSourcePurger(int maxItemsint checkInsertFrequencyTypedDatabaseSource<?, ?, ?> databaseSource) {
 		this. = databaseSource;
 		if (maxItems <= 0) throw new IllegalArgumentException("the max item in AsyncHandlerPurge must be positive");
 		if (checkInsertFrequency <= 0) throw new IllegalArgumentException("the insert purge frequency in AsyncHandlerPurge must be positive");
 
 		this. = maxItems;
 		this. = 0; // we should purge at the first insert, in case the app is closed too often without purging
 		 = checkInsertFrequency;
 	}

Returns:
The list of fields to read from the database when looking for the LAST_ELEMENT.
 
 	@NonNull
 	protected abstract String[] getFilterFields();

Returns:
The order to apply to elements to find elements to delete. It must use fields returned by getFilterFields()
 
 	@NonNull
 	protected abstract String getFilterOrder();

Transform the Cursor into the LAST_ELEMENT to keep during the purge.

Parameters:
cursor The Cursor positioned on the last element.
Returns:
The LAST_ELEMENT to keep during the purge or null if there is no last element.
See also:
getDeleteClause(Object)
getDeleteArgs(Object)
 
 	@Nullable
 	protected abstract LAST_ELEMENT getLastFilteredElement(Cursor cursor);

Get the delete() SQL clause to remove elements 'older' than lastElement

Parameters:
lastElement The LAST_ELEMENT to keep in the database
See also:
getDeleteArgs(Object)
 
 	@NonNull
 	protected abstract String getDeleteClause(@NonNull LAST_ELEMENT lastElement);

Get the delete() SQL clause arguments to remove elements 'older' than lastElement

Parameters:
lastElement The LAST_ELEMENT to keep in the database
See also:
getDeleteClause(Object)
 
 	@NonNull
 	protected abstract String[] getDeleteArgs(@NonNull LAST_ELEMENT lastElement);

Returns:
A Select clause to filter the elements handled by the purge or null for no filtering.
 
 	@Nullable
 	protected String getPurgeFilterClause() {
 		return null;
 	}

Returns:
The arguments corresponding to the getPurgeFilterClause() or null for no filtering.
 
 	@Nullable
	protected String[] getPurgeFilterArgs() {
		return null;
	}
	public void onElementsAdded(AsynchronousDbHelper<?, ?> db) {
		if ( != null && -- < 0) {
			 = null// pending purge
				public void runInMemoryDbOperation(AsynchronousDbHelper<?, ?> db) {
					int deleted = 0;
					try {
						LAST_ELEMENT lastElement = null;
						Cursor c = .query(getFilterFields(), getPurgeFilterClause(), getPurgeFilterArgs(), nullnullgetFilterOrder(), Integer.toString() + ", 1");
						try {
							if (c.moveToFirst())
								lastElement = getLastFilteredElement(c);
finally {
							c.close();
						}
						if (lastElement != null) {
							try {
								deleted = .delete(getDeleteClause(lastElement), getDeleteArgs(lastElement));
catch (IllegalStateException e) {
								// in some case (2.x) the DB is closed unexpectedly
catch (Exception e) {
								// in some case (4.1) we get "cannot rollback - no transaction is active"
							}
						}
catch (Exception e) {
						// can crash on Samsung GT-P1000 2.3.3
					}
					if (deleted > 0) {
						LogManager.getLogger().d(."purged " + deleted + " elements in " + db);
					}
				}
			});
		}
	}
New to GrepCode? Check out our FAQ X