Search in sources :

Example 1 with CollectionData

use of com.torodb.core.d2r.CollectionData 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 CollectionData

use of com.torodb.core.d2r.CollectionData in project torodb by torodb.

the class D2RTranslationBatchFunctionTest method testApply.

@Test
public void testApply() {
    D2RTranslator translator = mock(D2RTranslator.class);
    CollectionData colData = mock(CollectionData.class);
    KvDocument doc1 = mock(KvDocument.class);
    KvDocument doc2 = mock(KvDocument.class);
    given(translator.getCollectionDataAccumulator()).willReturn(colData);
    given(translatorFactory.createTranslator(metaDb, batchMetaCol)).willReturn(translator);
    List<KvDocument> docs = Lists.newArrayList(doc1, doc2);
    //when
    CollectionData result = fun.apply(docs);
    //then
    verify(batchMetaCol).newBatch();
    verify(translator).translate(doc1);
    verify(translator).translate(doc2);
    verify(translator).getCollectionDataAccumulator();
    verifyNoMoreInteractions(translator);
    verify(translatorFactory).createTranslator(metaDb, batchMetaCol);
    verifyNoMoreInteractions(translatorFactory);
    assertEquals(colData, result);
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) KvDocument(com.torodb.kvdocument.values.KvDocument) D2RTranslator(com.torodb.core.d2r.D2RTranslator) Test(org.junit.Test)

Example 3 with CollectionData

use of com.torodb.core.d2r.CollectionData 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 4 with CollectionData

use of com.torodb.core.d2r.CollectionData in project torodb by torodb.

the class SameThreadInsertPipeline method insert.

@Override
public void insert(Stream<KvDocument> docs) throws RollbackException, UserException {
    D2RTranslationBatchFunction d2rFun = new D2RTranslationBatchFunction(translatorFactory, metaDb, mutableMetaCollection);
    DefaultToBackendFunction r2BackendFun = new DefaultToBackendFunction(jobFactory, metaDb, mutableMetaCollection);
    try {
        Iterators.partition(docs.iterator(), docBatchSize).forEachRemaining(list -> {
            CollectionData collData = d2rFun.apply(list);
            Iterable<BackendTransactionJob> jobs = r2BackendFun.apply(collData);
            jobs.forEach(Unchecked.consumer(job -> job.execute(backendConnection)));
        });
    } catch (UncheckedException ex) {
        Throwable cause = ex.getCause();
        if (cause != null && cause instanceof UserException) {
            throw (UserException) cause;
        }
        throw ex;
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) Unchecked(org.jooq.lambda.Unchecked) KvDocument(com.torodb.kvdocument.values.KvDocument) UserException(com.torodb.core.exceptions.user.UserException) BackendTransactionJobFactory(com.torodb.core.dsl.backend.BackendTransactionJobFactory) Iterators(com.google.common.collect.Iterators) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) RollbackException(com.torodb.core.transaction.RollbackException) D2RTranslatorFactory(com.torodb.core.d2r.D2RTranslatorFactory) Assisted(com.google.inject.assistedinject.Assisted) Inject(javax.inject.Inject) MutableMetaCollection(com.torodb.core.transaction.metainf.MutableMetaCollection) UncheckedException(org.jooq.lambda.UncheckedException) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Stream(java.util.stream.Stream) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction) WriteBackendTransaction(com.torodb.core.backend.WriteBackendTransaction) Preconditions(com.google.common.base.Preconditions) CollectionData(com.torodb.core.d2r.CollectionData) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) InsertPipeline(com.torodb.torod.pipeline.InsertPipeline) UncheckedException(org.jooq.lambda.UncheckedException) D2RTranslationBatchFunction(com.torodb.torod.pipeline.D2RTranslationBatchFunction) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) UserException(com.torodb.core.exceptions.user.UserException) DefaultToBackendFunction(com.torodb.torod.pipeline.DefaultToBackendFunction)

Example 5 with CollectionData

use of com.torodb.core.d2r.CollectionData in project torodb by torodb.

the class DefaultToBackendFunctionTest method testNoBatchMetaDocPart.

@Test
public void testNoBatchMetaDocPart() {
    CollectionData collectionData = mock(CollectionData.class);
    DocPartData data1 = mock(DocPartData.class);
    given(data1.getMetaDocPart()).willReturn(new WrapperMutableMetaDocPart(new ImmutableMetaDocPart(tableRefFactory.createRoot(), "aDocPartName"), (o) -> {
    }));
    given(collectionData.orderedDocPartData()).willReturn(Collections.singleton(data1));
    //when
    try {
        fun.apply(collectionData);
        //then
        fail("An exception was expected when a metadoc part which is not a " + BatchMetaDocPart.class + " is used");
    } catch (AssertionError | ClassCastException ex) {
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) Iterables(com.google.common.collect.Iterables) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar) Mockito.spy(org.mockito.Mockito.spy) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) BackendTransactionJobFactory(com.torodb.core.dsl.backend.BackendTransactionJobFactory) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) RollbackException(com.torodb.core.transaction.RollbackException) ArrayList(java.util.ArrayList) TableRefFactory(com.torodb.core.TableRefFactory) ImmutableMetaDocPart(com.torodb.core.transaction.metainf.ImmutableMetaDocPart) Lists(com.google.common.collect.Lists) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) BDDMockito.given(org.mockito.BDDMockito.given) MetaScalar(com.torodb.core.transaction.metainf.MetaScalar) WriteBackendTransaction(com.torodb.core.backend.WriteBackendTransaction) Assert.fail(org.junit.Assert.fail) CollectionData(com.torodb.core.d2r.CollectionData) Mockito.doReturn(org.mockito.Mockito.doReturn) TableRefFactoryImpl(com.torodb.core.impl.TableRefFactoryImpl) AddCollectionDdlJob(com.torodb.core.dsl.backend.AddCollectionDdlJob) Before(org.junit.Before) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) WrapperMutableMetaDocPart(com.torodb.core.transaction.metainf.WrapperMutableMetaDocPart) MockSettings(org.mockito.MockSettings) Assert.assertTrue(org.junit.Assert.assertTrue) FieldType(com.torodb.core.transaction.metainf.FieldType) ImmutableMetaCollection(com.torodb.core.transaction.metainf.ImmutableMetaCollection) Test(org.junit.Test) UserException(com.torodb.core.exceptions.user.UserException) MutableMetaDocPart(com.torodb.core.transaction.metainf.MutableMetaDocPart) AddFieldDdlJob(com.torodb.core.dsl.backend.AddFieldDdlJob) AddDocPartDdlJob(com.torodb.core.dsl.backend.AddDocPartDdlJob) MetaField(com.torodb.core.transaction.metainf.MetaField) AddScalarDddlJob(com.torodb.core.dsl.backend.AddScalarDddlJob) DocPartData(com.torodb.core.d2r.DocPartData) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) Optional(java.util.Optional) ImmutableMetaDatabase(com.torodb.core.transaction.metainf.ImmutableMetaDatabase) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) InsertBackendJob(com.torodb.core.dsl.backend.InsertBackendJob) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) Mockito.mock(org.mockito.Mockito.mock) AddDatabaseDdlJob(com.torodb.core.dsl.backend.AddDatabaseDdlJob) DocPartData(com.torodb.core.d2r.DocPartData) WrapperMutableMetaDocPart(com.torodb.core.transaction.metainf.WrapperMutableMetaDocPart) ImmutableMetaDocPart(com.torodb.core.transaction.metainf.ImmutableMetaDocPart) Test(org.junit.Test)

Aggregations

CollectionData (com.torodb.core.d2r.CollectionData)9 BackendTransactionJob (com.torodb.core.dsl.backend.BackendTransactionJob)8 Test (org.junit.Test)7 DocPartData (com.torodb.core.d2r.DocPartData)6 InsertBackendJob (com.torodb.core.dsl.backend.InsertBackendJob)5 Optional (java.util.Optional)5 MockSettings (org.mockito.MockSettings)5 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)5 ImmutableMetaField (com.torodb.core.transaction.metainf.ImmutableMetaField)4 ImmutableMetaScalar (com.torodb.core.transaction.metainf.ImmutableMetaScalar)4 AddFieldDdlJob (com.torodb.core.dsl.backend.AddFieldDdlJob)3 AddScalarDddlJob (com.torodb.core.dsl.backend.AddScalarDddlJob)3 BackendTransactionJobFactory (com.torodb.core.dsl.backend.BackendTransactionJobFactory)3 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)3 WriteBackendTransaction (com.torodb.core.backend.WriteBackendTransaction)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)2 UserException (com.torodb.core.exceptions.user.UserException)2 RollbackException (com.torodb.core.transaction.RollbackException)2 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)2 KvDocument (com.torodb.kvdocument.values.KvDocument)2