From 95fc62dfd7b229a855117cc1bd1d0e0e527cc4ab Mon Sep 17 00:00:00 2001 From: Cicucci Date: Tue, 24 Jun 2014 15:55:37 -0400 Subject: [PATCH] Added ability to retrieve all existing job IDs from the JobManager Added ability to remove a job by ID from the JobManager --- .../com/path/android/jobqueue/JobManager.java | 45 +++++++++++++++++++ .../com/path/android/jobqueue/JobQueue.java | 7 +++ .../jobqueue/cachedQueue/CachedJobQueue.java | 7 +++ .../jobqueue/nonPersistentQueue/JobSet.java | 2 + .../nonPersistentQueue/MergedQueue.java | 7 +++ .../NonPersistentJobSet.java | 7 +++ .../NonPersistentPriorityQueue.java | 5 +++ .../persistentQueue/sqlite/SqlHelper.java | 3 ++ .../sqlite/SqliteJobQueue.java | 18 ++++++++ 9 files changed, 101 insertions(+) diff --git a/jobqueue/src/com/path/android/jobqueue/JobManager.java b/jobqueue/src/com/path/android/jobqueue/JobManager.java index 1ac6968..d91d341 100644 --- a/jobqueue/src/com/path/android/jobqueue/JobManager.java +++ b/jobqueue/src/com/path/android/jobqueue/JobManager.java @@ -12,6 +12,7 @@ import com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue; import java.util.Collection; +import java.util.List; import java.util.concurrent.*; /** @@ -353,6 +354,50 @@ public JobStatus getJobStatus(long id, boolean isPersistent) { return JobStatus.WAITING_READY; } + public List getJobIds(boolean isPersistent) { + if (isPersistent) { + synchronized (persistentJobQueue) { + return persistentJobQueue.getJobIds(); + } + } else { + synchronized (nonPersistentJobQueue) { + return nonPersistentJobQueue.getJobIds(); + } + } + } + + public BaseJob getJob(long id, boolean isPersistent) { + JobHolder holder; + if(isPersistent) { + synchronized (persistentJobQueue) { + holder = persistentJobQueue.findJobById(id); + } + } else { + synchronized (nonPersistentJobQueue) { + holder = nonPersistentJobQueue.findJobById(id); + } + } + if (holder != null) { + return holder.getBaseJob(); + } else { + return null; + } + } + + public void removeJob(long id, boolean isPersistent) { + JobHolder holder; + if(isPersistent) { + synchronized (persistentJobQueue) { + holder = persistentJobQueue.findJobById(id); + } + } else { + synchronized (nonPersistentJobQueue) { + holder = nonPersistentJobQueue.findJobById(id); + } + } + removeJob(holder); + } + private void removeJob(JobHolder jobHolder) { if (jobHolder.getBaseJob().isPersistent()) { synchronized (persistentJobQueue) { diff --git a/jobqueue/src/com/path/android/jobqueue/JobQueue.java b/jobqueue/src/com/path/android/jobqueue/JobQueue.java index a149a00..1869aa7 100644 --- a/jobqueue/src/com/path/android/jobqueue/JobQueue.java +++ b/jobqueue/src/com/path/android/jobqueue/JobQueue.java @@ -1,6 +1,7 @@ package com.path.android.jobqueue; import java.util.Collection; +import java.util.List; /** * Interface that any JobQueue should implement @@ -79,4 +80,10 @@ public interface JobQueue { */ JobHolder findJobById(long id); + /** + * returns an array of all the job ids stored in the queue + * @return Array of the current job ids. + */ + List getJobIds(); + } diff --git a/jobqueue/src/com/path/android/jobqueue/cachedQueue/CachedJobQueue.java b/jobqueue/src/com/path/android/jobqueue/cachedQueue/CachedJobQueue.java index c373c99..b086223 100644 --- a/jobqueue/src/com/path/android/jobqueue/cachedQueue/CachedJobQueue.java +++ b/jobqueue/src/com/path/android/jobqueue/cachedQueue/CachedJobQueue.java @@ -4,6 +4,7 @@ import com.path.android.jobqueue.JobQueue; import java.util.Collection; +import java.util.List; /** * a class that implements {@link JobQueue} interface, wraps another {@link JobQueue} and caches @@ -98,6 +99,12 @@ public JobHolder findJobById(long id) { return delegate.findJobById(id); } + @Override + public List getJobIds() { + return delegate.getJobIds(); + } + + private static class Cache { Integer count; DelayUntil delayUntil; diff --git a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/JobSet.java b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/JobSet.java index b084a0e..c524c94 100644 --- a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/JobSet.java +++ b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/JobSet.java @@ -3,6 +3,7 @@ import com.path.android.jobqueue.JobHolder; import java.util.Collection; +import java.util.List; /** * An interface for Job Containers @@ -12,6 +13,7 @@ public interface JobSet { public JobHolder peek(Collection excludeGroupIds); public JobHolder poll(Collection excludeGroupIds); public JobHolder findById(long id); + public List getJobIds(); public boolean offer(JobHolder holder); public boolean remove(JobHolder holder); public void clear(); diff --git a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/MergedQueue.java b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/MergedQueue.java index f32fd7c..795db95 100644 --- a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/MergedQueue.java +++ b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/MergedQueue.java @@ -214,6 +214,13 @@ public JobHolder findById(long id) { return q0 == null ? queue1.findById(id) : q0; } + @Override + public List getJobIds() { + List ids = queue0.getJobIds(); + ids.addAll(queue1.getJobIds()); + return ids; + } + /** * simple enum to identify queues */ diff --git a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentJobSet.java b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentJobSet.java index aec9272..d4cdbe0 100644 --- a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentJobSet.java +++ b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentJobSet.java @@ -3,10 +3,12 @@ import com.path.android.jobqueue.JobHolder; import com.path.android.jobqueue.log.JqLog; +import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -75,6 +77,11 @@ public JobHolder findById(long id) { return idCache.get(id); } + @Override + public List getJobIds() { + return new ArrayList(idCache.keySet()); + } + @Override public boolean offer(JobHolder holder) { if(holder.getId() == null) { diff --git a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentPriorityQueue.java b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentPriorityQueue.java index 627d09b..eba28d2 100644 --- a/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentPriorityQueue.java +++ b/jobqueue/src/com/path/android/jobqueue/nonPersistentQueue/NonPersistentPriorityQueue.java @@ -107,6 +107,11 @@ public JobHolder findJobById(long id) { return jobs.findById(id); } + @Override + public List getJobIds() { + return jobs.getJobIds(); + } + public final Comparator jobComparator = new Comparator() { @Override public int compare(JobHolder holder1, JobHolder holder2) { diff --git a/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqlHelper.java b/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqlHelper.java index 50e3ad7..70e3a16 100644 --- a/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqlHelper.java +++ b/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqlHelper.java @@ -10,12 +10,14 @@ public class SqlHelper { /**package**/ String FIND_BY_ID_QUERY; + String FIND_ALL_IDS_QUERY; private SQLiteStatement insertStatement; private SQLiteStatement insertOrReplaceStatement; private SQLiteStatement deleteStatement; private SQLiteStatement onJobFetchedForRunningStatement; private SQLiteStatement countStatement; + private SQLiteStatement idsStatement; private SQLiteStatement nextJobDelayedUntilWithNetworkStatement; private SQLiteStatement nextJobDelayedUntilWithoutNetworkStatement; @@ -33,6 +35,7 @@ public SqlHelper(SQLiteDatabase db, String tableName, String primaryKeyColumnNam this.primaryKeyColumnName = primaryKeyColumnName; this.sessionId = sessionId; FIND_BY_ID_QUERY = "SELECT * FROM " + tableName + " WHERE " + DbOpenHelper.ID_COLUMN.columnName + " = ?"; + FIND_ALL_IDS_QUERY = "Select " + DbOpenHelper.ID_COLUMN.columnName + " FROM " + tableName; } public static String create(String tableName, Property primaryKey, Property... properties) { diff --git a/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqliteJobQueue.java b/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqliteJobQueue.java index eefc667..deb318e 100644 --- a/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqliteJobQueue.java +++ b/jobqueue/src/com/path/android/jobqueue/persistentQueue/sqlite/SqliteJobQueue.java @@ -17,7 +17,9 @@ import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; /** * Persistent Job Queue that keeps its data in an sqlite database. @@ -178,6 +180,22 @@ public JobHolder findJobById(long id) { } } + @Override + public List getJobIds() { + ArrayList ids = new ArrayList(); + Cursor cursor = db.rawQuery(sqlHelper.FIND_ALL_IDS_QUERY, null); + try { + if (cursor.moveToFirst()) { + do { + ids.add(cursor.getLong(0)); + } while (cursor.moveToNext()); + } + } finally { + cursor.close(); + } + return ids; + } + /** * {@inheritDoc} */