Search in sources :

Example 1 with ImmutableMetaField

use of com.torodb.core.transaction.metainf.ImmutableMetaField in project torodb by torodb.

the class DefaultToBackendFunction method apply.

public Iterable<BackendTransactionJob> apply(CollectionData collectionData) {
    ArrayList<BackendTransactionJob> jobs = new ArrayList<>();
    for (DocPartData docPartData : collectionData.orderedDocPartData()) {
        assert docPartData.getMetaDocPart() instanceof BatchMetaDocPart : "This function can only use inputs whose meta doc part information is an instance of " + BatchMetaDocPart.class;
        BatchMetaDocPart metaDocPart = (BatchMetaDocPart) docPartData.getMetaDocPart();
        if (metaDocPart.isCreatedOnCurrentBatch()) {
            jobs.add(factory.createAddDocPartDdlJob(database, collection, metaDocPart));
            metaDocPart.streamScalars().map((scalar) -> factory.createAddScalarDdlJob(database, collection, metaDocPart, scalar)).forEachOrdered((job) -> jobs.add(job));
            metaDocPart.streamFields().map((field) -> factory.createAddFieldDdlJob(database, collection, metaDocPart, field)).forEachOrdered((job) -> jobs.add(job));
        } else {
            //it already exists, we only need to add the new scalars and fields
            for (ImmutableMetaScalar newScalar : metaDocPart.getOnBatchModifiedMetaScalars()) {
                jobs.add(factory.createAddScalarDdlJob(database, collection, metaDocPart, newScalar));
            }
            for (ImmutableMetaField newField : metaDocPart.getOnBatchModifiedMetaFields()) {
                jobs.add(factory.createAddFieldDdlJob(database, collection, metaDocPart, newField));
            }
        }
        jobs.add(factory.insert(database, collection, docPartData));
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Executing the following jobs: {}", jobs);
    }
    return jobs;
}
Also used : MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Logger(org.apache.logging.log4j.Logger) DocPartData(com.torodb.core.d2r.DocPartData) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) CollectionData(com.torodb.core.d2r.CollectionData) BackendTransactionJobFactory(com.torodb.core.dsl.backend.BackendTransactionJobFactory) LogManager(org.apache.logging.log4j.LogManager) Function(java.util.function.Function) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) ArrayList(java.util.ArrayList) DocPartData(com.torodb.core.d2r.DocPartData) ArrayList(java.util.ArrayList) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob)

Example 2 with ImmutableMetaField

use of com.torodb.core.transaction.metainf.ImmutableMetaField in project torodb by torodb.

the class DefaultToBackendFunctionTest method testApply_newField.

@Test
public void testApply_newField() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart withNewFieldsDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(false).when(withNewFieldsDocPart).isCreatedOnCurrentBatch();
    doReturn(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.INTEGER))).when(withNewFieldsDocPart).getOnBatchModifiedMetaFields();
    doReturn(Collections.emptyList()).when(withNewFieldsDocPart).getOnBatchModifiedMetaScalars();
    DocPartData withNewData = mock(DocPartData.class);
    given(withNewData.getMetaDocPart()).willReturn(withNewFieldsDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(withNewData));
    //when
    Iterable<BackendTransactionJob> result = fun.apply(collectionData);
    ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
    //then
    assertEquals("Expected 2 jobs to do, but " + resultList.size() + " were recived", 2, resultList.size());
    {
        Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(withNewData)).findAny();
        assertTrue(insertJob.isPresent());
        Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddFieldDdlJob)) {
                return false;
            }
            AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
            return castedJob.getDocPart().equals(withNewFieldsDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
        }).findAny();
        assertTrue(addFieldJob.isPresent());
        int addFieldIndex = resultList.indexOf(addFieldJob.get());
        int insertIndex = resultList.indexOf(insertJob.get());
        assert addFieldIndex >= 0;
        assert insertIndex >= 0;
        assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) InsertBackendJob(com.torodb.core.dsl.backend.InsertBackendJob) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) MockSettings(org.mockito.MockSettings) AddFieldDdlJob(com.torodb.core.dsl.backend.AddFieldDdlJob) Test(org.junit.Test)

Example 3 with ImmutableMetaField

use of com.torodb.core.transaction.metainf.ImmutableMetaField in project torodb by torodb.

the class IdentifierFactoryImplTest method longForCounterWithCollisionCharacterFieldToIdentifierTest.

@Test
public void longForCounterWithCollisionCharacterFieldToIdentifierTest() {
    ImmutableMetaDocPart metaDocPart = new ImmutableMetaDocPart.Builder(createTableRef(), "docpart").put(new ImmutableMetaField("field_collider", "field____long_long_long_long_long_long_long_long_long_long_lonng_long_long_long_long_long_long_long_long_long_long_long_long_1_s", FieldType.STRING)).build();
    String identifier = identifierFactory.toFieldIdentifier(metaDocPart, "field____long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long_long", FieldType.STRING);
    Assert.assertEquals("field____long_long_long_long_long_long_long_long_long_long_lonng_long_long_long_long_long_long_long_long_long_long_long_long_2_s", identifier);
}
Also used : ImmutableMetaDocPart(com.torodb.core.transaction.metainf.ImmutableMetaDocPart) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) Test(org.junit.Test)

Example 4 with ImmutableMetaField

use of com.torodb.core.transaction.metainf.ImmutableMetaField in project torodb by torodb.

the class DefaultToBackendFunctionTest method testApply_newDocPart.

@Test
public void testApply_newDocPart() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart allNewDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(true).when(allNewDocPart).isCreatedOnCurrentBatch();
    doReturn(Lists.newArrayList(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.BOOLEAN))).stream()).when(allNewDocPart).streamFields();
    doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.BOOLEAN)).stream()).when(allNewDocPart).streamScalars();
    DocPartData allNewData = mock(DocPartData.class);
    given(allNewData.getMetaDocPart()).willReturn(allNewDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allNewData));
    //when
    Iterable<BackendTransactionJob> result = fun.apply(collectionData);
    ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
    //then
    assertEquals("Expected 4 jobs to do, but " + resultList.size() + " were recived", 4, resultList.size());
    {
        Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allNewData)).findAny();
        assertTrue(insertJob.isPresent());
        Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddFieldDdlJob)) {
                return false;
            }
            AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
        }).findAny();
        assertTrue(addFieldJob.isPresent());
        Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddScalarDddlJob)) {
                return false;
            }
            AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.BOOLEAN);
        }).findAny();
        assertTrue(addScalarJob.isPresent());
        Optional<BackendTransactionJob> createDocPartJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddDocPartDdlJob)) {
                return false;
            }
            AddDocPartDdlJob castedJob = (AddDocPartDdlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart);
        }).findAny();
        assertTrue(createDocPartJob.isPresent());
        int createDocPartIndex = resultList.indexOf(createDocPartJob.get());
        int addFieldIndex = resultList.indexOf(addFieldJob.get());
        int addScalarIndex = resultList.indexOf(addScalarJob.get());
        int insertIndex = resultList.indexOf(insertJob.get());
        assert createDocPartIndex >= 0;
        assert addFieldIndex >= 0;
        assert addScalarIndex >= 0;
        assert insertIndex >= 0;
        assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
        assertTrue("For a given doc part, all related add scalar jobs must be executed before insert " + "jobs, but in this case the add scalr job has index " + addScalarIndex + " and the insert job has index " + insertIndex, addScalarIndex < insertIndex);
        assertTrue("For a given doc part, all related create doc part jobs must be executed " + "before add field jobs, but in this case the create doc part job has index " + createDocPartIndex + " and " + "the add field job has index " + addFieldIndex, createDocPartIndex < addFieldIndex);
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) AddDocPartDdlJob(com.torodb.core.dsl.backend.AddDocPartDdlJob) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar) AddScalarDddlJob(com.torodb.core.dsl.backend.AddScalarDddlJob) InsertBackendJob(com.torodb.core.dsl.backend.InsertBackendJob) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) MockSettings(org.mockito.MockSettings) AddFieldDdlJob(com.torodb.core.dsl.backend.AddFieldDdlJob) Test(org.junit.Test)

Example 5 with ImmutableMetaField

use of com.torodb.core.transaction.metainf.ImmutableMetaField in project torodb by torodb.

the class BatchMetaDocPart method addMetaField.

@Override
public ImmutableMetaField addMetaField(String name, String identifier, FieldType type) throws IllegalArgumentException {
    ImmutableMetaField newMetaField = delegate.addMetaField(name, identifier, type);
    fieldsChangesOnBatch.add(newMetaField);
    changeConsumer.accept(this);
    return newMetaField;
}
Also used : ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField)

Aggregations

ImmutableMetaField (com.torodb.core.transaction.metainf.ImmutableMetaField)7 CollectionData (com.torodb.core.d2r.CollectionData)3 DocPartData (com.torodb.core.d2r.DocPartData)3 BackendTransactionJob (com.torodb.core.dsl.backend.BackendTransactionJob)3 ImmutableMetaScalar (com.torodb.core.transaction.metainf.ImmutableMetaScalar)3 Test (org.junit.Test)3 AddFieldDdlJob (com.torodb.core.dsl.backend.AddFieldDdlJob)2 InsertBackendJob (com.torodb.core.dsl.backend.InsertBackendJob)2 ImmutableMetaDocPart (com.torodb.core.transaction.metainf.ImmutableMetaDocPart)2 Optional (java.util.Optional)2 MockSettings (org.mockito.MockSettings)2 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)1 AddScalarDddlJob (com.torodb.core.dsl.backend.AddScalarDddlJob)1 BackendTransactionJobFactory (com.torodb.core.dsl.backend.BackendTransactionJobFactory)1 ImmutableMetaIdentifiedDocPartIndex (com.torodb.core.transaction.metainf.ImmutableMetaIdentifiedDocPartIndex)1 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)1 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)1 MetaElementState (com.torodb.core.transaction.metainf.MetaElementState)1 ArrayList (java.util.ArrayList)1