Search in sources :

Example 6 with MetaField

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

the class DerbyWriteInterface method getInsertDocPartDataStatement.

@Override
protected String getInsertDocPartDataStatement(String schemaName, MetaDocPart metaDocPart, Iterator<MetaField> metaFieldIterator, Iterator<MetaScalar> metaScalarIterator, Collection<InternalField<?>> internalFields, List<FieldType> fieldTypeList) {
    final StringBuilder insertStatementBuilder = new StringBuilder(2048);
    final StringBuilder insertStatementValuesBuilder = new StringBuilder(1024);
    insertStatementBuilder.append("INSERT INTO \"").append(schemaName).append("\".\"").append(metaDocPart.getIdentifier()).append("\" (");
    insertStatementValuesBuilder.append(" VALUES (");
    for (InternalField<?> internalField : internalFields) {
        insertStatementBuilder.append("\"").append(internalField.getName()).append("\",");
        insertStatementValuesBuilder.append("?,");
    }
    while (metaScalarIterator.hasNext()) {
        MetaScalar metaScalar = metaScalarIterator.next();
        insertStatementBuilder.append("\"").append(metaScalar.getIdentifier()).append("\",");
        insertStatementValuesBuilder.append("?,");
        fieldTypeList.add(metaScalar.getType());
    }
    while (metaFieldIterator.hasNext()) {
        MetaField metaField = metaFieldIterator.next();
        insertStatementBuilder.append("\"").append(metaField.getIdentifier()).append("\",");
        insertStatementValuesBuilder.append("?,");
        fieldTypeList.add(metaField.getType());
    }
    insertStatementBuilder.setCharAt(insertStatementBuilder.length() - 1, ')');
    insertStatementValuesBuilder.setCharAt(insertStatementValuesBuilder.length() - 1, ')');
    insertStatementBuilder.append(insertStatementValuesBuilder);
    String statement = insertStatementBuilder.toString();
    return statement;
}
Also used : MetaField(com.torodb.core.transaction.metainf.MetaField) MetaScalar(com.torodb.core.transaction.metainf.MetaScalar)

Example 7 with MetaField

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

the class PostgreSqlWriteInterface method getCopyInsertDocPartDataStatement.

protected String getCopyInsertDocPartDataStatement(String schemaName, DocPartData docPartData, final MetaDocPart metaDocPart, Collection<InternalField<?>> internalFields) {
    final StringBuilder copyStatementBuilder = new StringBuilder();
    copyStatementBuilder.append("COPY \"").append(schemaName).append("\".\"").append(metaDocPart.getIdentifier()).append("\"").append(" (");
    for (InternalField<?> internalField : internalFields) {
        copyStatementBuilder.append("\"").append(internalField.getName()).append("\",");
    }
    Iterator<MetaScalar> metaScalarIterator = docPartData.orderedMetaScalarIterator();
    while (metaScalarIterator.hasNext()) {
        MetaScalar metaScalar = metaScalarIterator.next();
        copyStatementBuilder.append("\"").append(metaScalar.getIdentifier()).append("\",");
    }
    Iterator<MetaField> metaFieldIterator = docPartData.orderedMetaFieldIterator();
    while (metaFieldIterator.hasNext()) {
        MetaField metaField = metaFieldIterator.next();
        copyStatementBuilder.append("\"").append(metaField.getIdentifier()).append("\",");
    }
    copyStatementBuilder.setCharAt(copyStatementBuilder.length() - 1, ')');
    copyStatementBuilder.append(" FROM STDIN");
    final String copyStatement = copyStatementBuilder.toString();
    return copyStatement;
}
Also used : MetaField(com.torodb.core.transaction.metainf.MetaField) MetaScalar(com.torodb.core.transaction.metainf.MetaScalar)

Example 8 with MetaField

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

the class R2DTranslatorImpl method readResult.

private void readResult(MetaDocPart metaDocPart, TableRef tableRef, DocPartResult docPartResult, Map<Integer, Map<String, List<KvValue<?>>>> currentFieldDocPartRow, Map<Integer, Map<String, List<KvValue<?>>>> childFieldDocPartRow, ImmutableList.Builder<ToroDocument> readedDocuments) {
    while (docPartResult.hasNext()) {
        KvDocument.Builder documentBuilder = new KvDocument.Builder();
        DocPartResultRow row = docPartResult.next();
        Integer did = row.getDid();
        Integer rid = row.getRid();
        Integer pid = row.getPid();
        Integer seq = row.getSeq();
        Map<String, List<KvValue<?>>> childFieldDocPartCell = childFieldDocPartRow.get(rid);
        //TODO: ensure MetaField order using ResultSet meta data
        Iterator<? extends MetaScalar> metaScalarIterator = metaDocPart.streamScalars().iterator();
        boolean wasScalar = false;
        int fieldIndex = 0;
        while (metaScalarIterator.hasNext() && !wasScalar) {
            assert seq != null : "found scalar value outside of an array";
            MetaScalar metaScalar = metaScalarIterator.next();
            KvValue<?> value = row.getUserValue(fieldIndex, metaScalar.getType());
            fieldIndex++;
            if (value != null) {
                if (metaScalar.getType() == FieldType.CHILD) {
                    value = getChildValue(value, getDocPartCellName(tableRef), childFieldDocPartCell);
                }
                addValueToDocPartRow(currentFieldDocPartRow, tableRef, pid, seq, value);
                wasScalar = true;
            }
        }
        if (wasScalar) {
            continue;
        }
        Iterator<? extends MetaField> metaFieldIterator = metaDocPart.streamFields().iterator();
        while (metaFieldIterator.hasNext()) {
            MetaField metaField = metaFieldIterator.next();
            KvValue<?> value = row.getUserValue(fieldIndex, metaField.getType());
            fieldIndex++;
            if (value != null) {
                if (metaField.getType() == FieldType.CHILD) {
                    value = getChildValue(value, metaField.getName(), childFieldDocPartCell);
                }
                documentBuilder.putValue(metaField.getName(), value);
            }
        }
        if (tableRef.isRoot()) {
            readedDocuments.add(new ToroDocument(did, documentBuilder.build()));
        } else {
            addValueToDocPartRow(currentFieldDocPartRow, tableRef, pid, seq, documentBuilder.build());
        }
    }
}
Also used : KvDocument(com.torodb.kvdocument.values.KvDocument) MetaField(com.torodb.core.transaction.metainf.MetaField) MetaScalar(com.torodb.core.transaction.metainf.MetaScalar) DocPartResultRow(com.torodb.core.d2r.DocPartResultRow) ToroDocument(com.torodb.core.document.ToroDocument) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with MetaField

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

the class TableMetadata method findMetaField.

private MetaField findMetaField(String key, FieldType type) {
    MetaField metaField = metaDocPart.getMetaFieldByNameAndType(key, type);
    if (metaField == null) {
        String identifier = collectionMetaInfo.getFieldIdentifier(tableRef, type, key);
        metaField = metaDocPart.addMetaField(key, identifier, type);
    }
    return metaField;
}
Also used : MetaField(com.torodb.core.transaction.metainf.MetaField)

Example 10 with MetaField

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

the class SqlTorodTransaction method findByAttRefIn.

@Override
public TorodCursor findByAttRefIn(String dbName, String colName, AttributeReference attRef, Collection<KvValue<?>> values) {
    MetaDatabase db = getInternalTransaction().getMetaSnapshot().getMetaDatabaseByName(dbName);
    if (db == null) {
        LOGGER.trace("Db with name " + dbName + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    MetaCollection col = db.getMetaCollectionByName(colName);
    if (col == null) {
        LOGGER.trace("Collection " + dbName + '.' + colName + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    if (values.isEmpty()) {
        LOGGER.trace("An empty list of values have been given as in condition. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    TableRef ref = extractTableRef(attRef);
    String lastKey = extractKeyName(attRef.getKeys().get(attRef.getKeys().size() - 1));
    MetaDocPart docPart = col.getMetaDocPartByTableRef(ref);
    if (docPart == null) {
        LOGGER.trace("DocPart " + dbName + '.' + colName + '.' + ref + " does not exist. An empty cursor is returned");
        return new EmptyTorodCursor();
    }
    Multimap<MetaField, KvValue<?>> valuesMap = ArrayListMultimap.create();
    for (KvValue<?> value : values) {
        MetaField field = docPart.getMetaFieldByNameAndType(lastKey, FieldType.from(value.getType()));
        if (field != null) {
            valuesMap.put(field, value);
        }
    }
    return toToroCursor(getInternalTransaction().getBackendTransaction().findByFieldIn(db, col, docPart, valuesMap));
}
Also used : EmptyTorodCursor(com.torodb.torod.cursors.EmptyTorodCursor) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) MetaField(com.torodb.core.transaction.metainf.MetaField) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) TableRef(com.torodb.core.TableRef) KvValue(com.torodb.kvdocument.values.KvValue)

Aggregations

MetaField (com.torodb.core.transaction.metainf.MetaField)12 MetaDocPart (com.torodb.core.transaction.metainf.MetaDocPart)6 TableRef (com.torodb.core.TableRef)5 MetaCollection (com.torodb.core.transaction.metainf.MetaCollection)5 MetaDatabase (com.torodb.core.transaction.metainf.MetaDatabase)5 MetaScalar (com.torodb.core.transaction.metainf.MetaScalar)5 KvValue (com.torodb.kvdocument.values.KvValue)3 EmptyCursor (com.torodb.core.cursors.EmptyCursor)2 EmptyTorodCursor (com.torodb.torod.cursors.EmptyTorodCursor)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1 Context (com.torodb.backend.ErrorHandler.Context)1 ResultSetDocPartResult (com.torodb.backend.d2r.ResultSetDocPartResult)1 DocPartTableFields (com.torodb.backend.tables.MetaDocPartTable.DocPartTableFields)1 TableRefFactory (com.torodb.core.TableRefFactory)1 Cursor (com.torodb.core.cursors.Cursor)1 IteratorCursor (com.torodb.core.cursors.IteratorCursor)1