Search in sources :

Example 1 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DbRowOpFactory method createRows.

Collection<? extends DbRowOp> createRows(ObjectDiff diff) {
    updateDiff(diff);
    DbEntity rootEntity = descriptor.getEntity().getDbEntity();
    DbRowOp row = getOrCreate(rootEntity, object.getObjectId(), DbRowOpType.forObject(object));
    rootRowOpProcessor.setDiff(diff);
    row.accept(rootRowOpProcessor);
    return dbRows.values();
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp)

Example 2 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDataDomainFlushAction method createDbRowOps.

/**
 * Create ops based on incoming graph changes
 * @param objectStore originating object store
 * @param changes object graph diff
 * @return collection of {@link DbRowOp}
 */
protected List<DbRowOp> createDbRowOps(ObjectStore objectStore, ObjectStoreGraphDiff changes) {
    EntityResolver resolver = dataDomain.getEntityResolver();
    Map<Object, ObjectDiff> changesByObjectId = changes.getChangesByObjectId();
    List<DbRowOp> ops = new ArrayList<>(changesByObjectId.size());
    Set<ArcTarget> processedArcs = new HashSet<>();
    DbRowOpFactory factory = new DbRowOpFactory(resolver, objectStore, processedArcs);
    // ops.addAll() method is slower in this case as it will allocate new array for all values
    // noinspection UseBulkOperation
    changesByObjectId.forEach((obj, diff) -> factory.createRows(diff).forEach(ops::add));
    return ops;
}
Also used : ObjectDiff(org.apache.cayenne.access.ObjectDiff) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) ArrayList(java.util.ArrayList) EntityResolver(org.apache.cayenne.map.EntityResolver) HashSet(java.util.HashSet)

Example 3 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDbRowOpSorterTest method sortById.

@Test
public void sortById() {
    ObjectId id1 = ObjectId.of("test", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test", "id", 2);
    ObjectId id4 = ObjectId.of("test", "id", 3);
    DbEntity test = mockEntity("test");
    InsertDbRowOp op1 = new InsertDbRowOp(mockObject(id1), test, id1);
    InsertDbRowOp op2 = new InsertDbRowOp(mockObject(id2), test, id2);
    DeleteDbRowOp op3 = new DeleteDbRowOp(mockObject(id3), test, id3);
    DeleteDbRowOp op4 = new DeleteDbRowOp(mockObject(id4), test, id4);
    List<DbRowOp> rows = Arrays.asList(op1, op2, op3, op4);
    List<DbRowOp> expected = Arrays.asList(op1, op2, op3, op4);
    List<DbRowOp> sorted = sorter.sort(rows);
    assertEquals(expected, sorted);
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) Test(org.junit.Test)

Example 4 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDbRowOpSorterTest method sortReflexive.

@Test
public void sortReflexive() {
    ObjectId id1 = ObjectId.of("reflexive", "id", 1);
    ObjectId id2 = ObjectId.of("reflexive", "id", 2);
    ObjectId id3 = ObjectId.of("reflexive", "id", 3);
    ObjectId id4 = ObjectId.of("reflexive", "id", 4);
    DbEntity reflexive = mockEntity("reflexive");
    DbRowOp op1 = new InsertDbRowOp(mockObject(id1), reflexive, id1);
    DbRowOp op2 = new InsertDbRowOp(mockObject(id2), reflexive, id2);
    DbRowOp op3 = new InsertDbRowOp(mockObject(id3), reflexive, id3);
    DbRowOp op4 = new InsertDbRowOp(mockObject(id4), reflexive, id4);
    List<DbRowOp> rows = Arrays.asList(op1, op2, op3, op4);
    List<DbRowOp> expected = Arrays.asList(op1, op2, op3, op4);
    List<DbRowOp> sorted = sorter.sort(rows);
    // no actual sorting is done
    assertEquals(expected, sorted);
    // should call entity sorter
    verify(entitySorter).sortObjectsForEntity(isNull(), any(List.class), eq(false));
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 5 with DbRowOp

use of org.apache.cayenne.access.flush.operation.DbRowOp in project cayenne by apache.

the class DefaultDataDomainFlushActionTest method mergeSameObjectIds.

@Test
public void mergeSameObjectIds() {
    ObjectId id1 = ObjectId.of("test2", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test", "id", 2);
    ObjectId id4 = ObjectId.of("test", "id", 3);
    ObjectId id5 = ObjectId.of("test2", "id", 4);
    ObjectId id6 = ObjectId.of("test", "id", 5);
    ObjectId id7 = ObjectId.of("test", "id", 6);
    ObjectId id8 = ObjectId.of("test2", "id", 3);
    ObjectId id9 = ObjectId.of("test2", "id", 4);
    ObjectId id10 = ObjectId.of("test", "id", 6);
    DbEntity test = mockEntity("test");
    DbEntity test2 = mockEntity("test2");
    BaseDbRowOp[] op = new BaseDbRowOp[10];
    // +
    op[0] = new InsertDbRowOp(mockObject(id1), test2, id1);
    // -
    op[1] = new InsertDbRowOp(mockObject(id2), test, id2);
    // -
    op[2] = new DeleteDbRowOp(mockObject(id3), test, id3);
    // +
    op[3] = new UpdateDbRowOp(mockObject(id4), test, id4);
    // -
    op[4] = new InsertDbRowOp(mockObject(id5), test2, id5);
    // +
    op[5] = new DeleteDbRowOp(mockObject(id6), test, id6);
    // -
    op[6] = new InsertDbRowOp(mockObject(id7), test, id7);
    // +
    op[7] = new UpdateDbRowOp(mockObject(id8), test2, id8);
    // -
    op[8] = new DeleteDbRowOp(mockObject(id9), test2, id9);
    // -
    op[9] = new DeleteDbRowOp(mockObject(id10), test, id10);
    DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
    when(action.mergeSameObjectIds((List<DbRowOp>) any(List.class))).thenCallRealMethod();
    Collection<DbRowOp> merged = action.mergeSameObjectIds(new ArrayList<>(Arrays.asList(op)));
    assertEquals(7, merged.size());
    assertThat(merged, hasItems(op[0], op[3], op[5], op[7]));
    assertThat(merged, not(hasItem(sameInstance(op[1]))));
    assertThat(merged, not(hasItem(sameInstance(op[2]))));
    assertThat(merged, not(hasItem(sameInstance(op[4]))));
    assertThat(merged, not(hasItem(sameInstance(op[6]))));
    assertThat(merged, not(hasItem(sameInstance(op[8]))));
    assertThat(merged, not(hasItem(sameInstance(op[9]))));
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) DefaultDataDomainFlushAction(org.apache.cayenne.access.flush.DefaultDataDomainFlushAction) Test(org.junit.Test)

Aggregations

DbRowOp (org.apache.cayenne.access.flush.operation.DbRowOp)12 UpdateDbRowOp (org.apache.cayenne.access.flush.operation.UpdateDbRowOp)11 DeleteDbRowOp (org.apache.cayenne.access.flush.operation.DeleteDbRowOp)9 InsertDbRowOp (org.apache.cayenne.access.flush.operation.InsertDbRowOp)9 ObjectId (org.apache.cayenne.ObjectId)8 DbEntity (org.apache.cayenne.map.DbEntity)8 BaseDbRowOp (org.apache.cayenne.access.flush.operation.BaseDbRowOp)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 List (java.util.List)2 DefaultDataDomainFlushAction (org.apache.cayenne.access.flush.DefaultDataDomainFlushAction)2 HashSet (java.util.HashSet)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Persistent (org.apache.cayenne.Persistent)1 ObjectDiff (org.apache.cayenne.access.ObjectDiff)1 ObjectStore (org.apache.cayenne.access.ObjectStore)1 ObjectStoreGraphDiff (org.apache.cayenne.access.ObjectStoreGraphDiff)1 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbJoin (org.apache.cayenne.map.DbJoin)1