use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class NonPersistentJobSet method countReadyJobs.
@Override
public CountWithGroupIdsResult countReadyJobs(Collection<String> excludeGroups) {
if (existingGroups.size() == 0) {
return new CountWithGroupIdsResult(set.size(), null);
} else {
// todo we can actually count from existingGroups set if we start counting numbers there as well
int total = 0;
Set<String> existingGroupIds = null;
for (JobHolder holder : set) {
if (holder.getGroupId() != null) {
if (excludeGroups != null && excludeGroups.contains(holder.getGroupId())) {
continue;
} else if (existingGroupIds == null) {
existingGroupIds = new HashSet<String>();
existingGroupIds.add(holder.getGroupId());
} else if (existingGroupIds.add(holder.getGroupId()) == false) {
continue;
}
}
total++;
}
return new CountWithGroupIdsResult(total, existingGroupIds);
}
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class SqliteJobQueue method nextJobAndIncRunCount.
/**
* {@inheritDoc}
*/
@Override
public JobHolder nextJobAndIncRunCount(boolean hasNetwork, Collection<String> excludeGroups) {
// we can even keep these prepared but not sure the cost of them in db layer
String selectQuery = nextJobsQueryCache.get(hasNetwork, excludeGroups);
if (selectQuery == null) {
String where = createReadyJobWhereSql(hasNetwork, excludeGroups, false);
selectQuery = sqlHelper.createSelect(where, 1, new SqlHelper.Order(DbOpenHelper.PRIORITY_COLUMN, SqlHelper.Order.Type.DESC), new SqlHelper.Order(DbOpenHelper.CREATED_NS_COLUMN, SqlHelper.Order.Type.ASC), new SqlHelper.Order(DbOpenHelper.ID_COLUMN, SqlHelper.Order.Type.ASC));
nextJobsQueryCache.set(selectQuery, hasNetwork, excludeGroups);
}
Cursor cursor = db.rawQuery(selectQuery, new String[] { Long.toString(sessionId), Long.toString(System.nanoTime()) });
try {
if (!cursor.moveToNext()) {
return null;
}
JobHolder holder = createJobHolderFromCursor(cursor);
onJobFetchedForRunning(holder);
return holder;
} catch (InvalidBaseJobException e) {
// delete
Long jobId = cursor.getLong(0);
delete(jobId);
return nextJobAndIncRunCount(true, null);
} finally {
cursor.close();
}
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class MergedQueue method peek.
/**
* {@inheritDoc}
*/
@Override
public JobHolder peek(Collection<String> excludeGroupIds) {
while (true) {
JobHolder delayed = queue0.peek(excludeGroupIds);
// if queue for this job has changed, re-add it and try peek from scratch
if (delayed != null && decideQueue(delayed) != SetId.S0) {
queue1.offer(delayed);
queue0.remove(delayed);
// retry
continue;
}
JobHolder nonDelayed = queue1.peek(excludeGroupIds);
// if queue for this job has changed, re-add it and try peek from scratch
if (nonDelayed != null && decideQueue(nonDelayed) != SetId.S1) {
queue0.offer(nonDelayed);
queue1.remove(nonDelayed);
// retry
continue;
}
if (delayed == null) {
return nonDelayed;
}
if (nonDelayed == null) {
return delayed;
}
int cmp = retrieveComparator.compare(delayed, nonDelayed);
if (cmp == -1) {
return delayed;
}
return nonDelayed;
}
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class MergedQueue method poll.
/**
* {@inheritDoc}
*/
@Override
public JobHolder poll(Collection<String> excludeGroupIds) {
JobHolder delayed = queue0.peek(excludeGroupIds);
if (delayed == null) {
return queue1.poll(excludeGroupIds);
}
// if queue for this job has changed, re-add it and try poll from scratch
if (decideQueue(delayed) != SetId.S0) {
// should be moved to the other queue
queue0.remove(delayed);
queue1.offer(delayed);
return poll(excludeGroupIds);
}
JobHolder nonDelayed = queue1.peek(excludeGroupIds);
if (nonDelayed == null) {
queue0.remove(delayed);
return delayed;
}
// if queue for this job has changed, re-add it and try poll from scratch
if (decideQueue(nonDelayed) != SetId.S1) {
queue0.offer(nonDelayed);
queue1.remove(nonDelayed);
return poll(excludeGroupIds);
}
// both are not null, need to compare and return the better
int cmp = retrieveComparator.compare(delayed, nonDelayed);
if (cmp == -1) {
queue0.remove(delayed);
return delayed;
} else {
queue1.remove(nonDelayed);
return nonDelayed;
}
}
use of com.path.android.jobqueue.JobHolder in project android-priority-jobqueue by path.
the class NonPersistentJobSet method countReadyJobs.
@Override
public CountWithGroupIdsResult countReadyJobs(long now, Collection<String> excludeGroups) {
// TODO we can cache most of this
int total = 0;
int groupCnt = existingGroups.keySet().size();
Set<String> groupIdSet = null;
if (groupCnt > 0) {
// we have to track :/
groupIdSet = new HashSet<String>();
}
for (JobHolder holder : set) {
if (holder.getDelayUntilNs() < now) {
// we write unit tests around NonPersistentJobSet
if (holder.getGroupId() != null) {
if (excludeGroups != null && excludeGroups.contains(holder.getGroupId())) {
continue;
}
// we write unit tests around NonPersistentJobSet
if (groupCnt > 0) {
if (groupIdSet.add(holder.getGroupId())) {
total++;
}
}
// else skip, we already counted this group
} else {
total++;
}
}
}
return new CountWithGroupIdsResult(total, groupIdSet);
}
Aggregations