Search in sources :

Example 56 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method updateJobs_memoryOnly_doesNotWriteToDatabase.

@Test
public void updateJobs_memoryOnly_doesNotWriteToDatabase() {
    JobDatabase database = fixedDataDatabase(DataSetMemory.FULL_SPECS);
    FastJobStorage subject = new FastJobStorage(database);
    List<JobSpec> jobs = Collections.singletonList(new JobSpec("id1", "f1", null, 1, 1, 1, 1, 1, EMPTY_DATA, null, false, false));
    subject.init();
    subject.updateJobs(jobs);
    verify(database, times(0)).updateJobs(jobs);
}
Also used : JobDatabase(org.thoughtcrime.securesms.database.JobDatabase) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Example 57 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method getPendingJobsWithNoDependenciesInCreatedOrder_migrationJobTakesPrecedence.

@Test
public void getPendingJobsWithNoDependenciesInCreatedOrder_migrationJobTakesPrecedence() {
    FullSpec plainSpec = new FullSpec(new JobSpec("1", "f1", "q", 0, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FullSpec migrationSpec = new FullSpec(new JobSpec("2", "f2", Job.Parameters.MIGRATION_QUEUE_KEY, 5, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FastJobStorage subject = new FastJobStorage(fixedDataDatabase(Arrays.asList(plainSpec, migrationSpec)));
    subject.init();
    List<JobSpec> jobs = subject.getPendingJobsWithNoDependenciesInCreatedOrder(10);
    assertEquals(1, jobs.size());
    assertEquals("2", jobs.get(0).getId());
}
Also used : FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Example 58 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by signalapp.

the class JobDatabase method insertJobs.

public synchronized void insertJobs(@NonNull List<FullSpec> fullSpecs) {
    if (Stream.of(fullSpecs).map(FullSpec::getJobSpec).allMatch(JobSpec::isMemoryOnly)) {
        return;
    }
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        for (FullSpec fullSpec : fullSpecs) {
            insertJobSpec(db, fullSpec.getJobSpec());
            insertConstraintSpecs(db, fullSpec.getConstraintSpecs());
            insertDependencySpecs(db, fullSpec.getDependencySpecs());
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : SQLiteDatabase(net.zetetic.database.sqlcipher.SQLiteDatabase) FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)

Example 59 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by signalapp.

the class JobDatabase method updateJobs.

public synchronized void updateJobs(@NonNull List<JobSpec> jobs) {
    if (Stream.of(jobs).allMatch(JobSpec::isMemoryOnly)) {
        return;
    }
    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        Stream.of(jobs).filterNot(JobSpec::isMemoryOnly).forEach(job -> {
            ContentValues values = new ContentValues();
            values.put(Jobs.JOB_SPEC_ID, job.getId());
            values.put(Jobs.FACTORY_KEY, job.getFactoryKey());
            values.put(Jobs.QUEUE_KEY, job.getQueueKey());
            values.put(Jobs.CREATE_TIME, job.getCreateTime());
            values.put(Jobs.NEXT_RUN_ATTEMPT_TIME, job.getNextRunAttemptTime());
            values.put(Jobs.RUN_ATTEMPT, job.getRunAttempt());
            values.put(Jobs.MAX_ATTEMPTS, job.getMaxAttempts());
            values.put(Jobs.LIFESPAN, job.getLifespan());
            values.put(Jobs.SERIALIZED_DATA, job.getSerializedData());
            values.put(Jobs.SERIALIZED_INPUT_DATA, job.getSerializedInputData());
            values.put(Jobs.IS_RUNNING, job.isRunning() ? 1 : 0);
            String query = Jobs.JOB_SPEC_ID + " = ?";
            String[] args = new String[] { job.getId() };
            db.update(Jobs.TABLE_NAME, values, query, args);
        });
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(net.zetetic.database.sqlcipher.SQLiteDatabase) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)

Example 60 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by signalapp.

the class FastJobStorage method updateJobs.

@Override
public void updateJobs(@NonNull List<JobSpec> jobSpecs) {
    List<JobSpec> durable = new ArrayList<>(jobSpecs.size());
    for (JobSpec update : jobSpecs) {
        JobSpec found = getJobById(update.getId());
        if (found == null || !found.isMemoryOnly()) {
            durable.add(update);
        }
    }
    if (durable.size() > 0) {
        jobDatabase.updateJobs(durable);
    }
    Map<String, JobSpec> updates = Stream.of(jobSpecs).collect(Collectors.toMap(JobSpec::getId));
    ListIterator<JobSpec> iter = jobs.listIterator();
    while (iter.hasNext()) {
        JobSpec existing = iter.next();
        JobSpec update = updates.get(existing.getId());
        if (update != null) {
            iter.set(update);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)

Aggregations

JobSpec (org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)66 FullSpec (org.thoughtcrime.securesms.jobmanager.persistence.FullSpec)38 Test (org.junit.Test)36 WorkerThread (androidx.annotation.WorkerThread)10 DependencySpec (org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec)10 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 ConstraintSpec (org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec)8 Nullable (androidx.annotation.Nullable)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Map (java.util.Map)6 JobStorage (org.thoughtcrime.securesms.jobmanager.persistence.JobStorage)6 Application (android.app.Application)4 NonNull (androidx.annotation.NonNull)4 Collectors (com.annimon.stream.Collectors)4 Stream (com.annimon.stream.Stream)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4