Search in sources :

Example 16 with JobHolder

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);
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) HashSet(java.util.HashSet)

Example 17 with JobHolder

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();
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) Cursor(android.database.Cursor)

Example 18 with JobHolder

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;
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder)

Example 19 with JobHolder

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;
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder)

Example 20 with JobHolder

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);
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder)

Aggregations

JobHolder (com.path.android.jobqueue.JobHolder)27 Test (org.junit.Test)18 Params (com.path.android.jobqueue.Params)17 JobQueue (com.path.android.jobqueue.JobQueue)15 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)7 JobManager (com.path.android.jobqueue.JobManager)6 BaseJob (com.path.android.jobqueue.BaseJob)2 Job (com.path.android.jobqueue.Job)2 Cursor (android.database.Cursor)1 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 Configuration (com.path.android.jobqueue.config.Configuration)1 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1