Search in sources :

Example 11 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method search.

@Override
public <O> PagedResult<EntryData> search(String key, String objectClass, ConvertedExpression expression, SearchScope scope, String[] attributes, Sort[] orderBy, SpannerBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    PagedResult<EntryData> result = searchImpl(tableMapping, key, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: search, duration: {}, table: {}, key: {}, expression: {}, scope: {}, attributes: {}, orderBy: {}, batchOperationWraper: {}, returnDataType: {}, start: {}, count: {}, pageSize: {}", duration, tableMapping.getTableName(), key, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration)

Example 12 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method updateEntry.

@Override
public boolean updateEntry(String key, String objectClass, List<AttributeDataModification> mods) throws UnsupportedOperationException, PersistenceException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    boolean result = updateEntryImpl(tableMapping, key, mods);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: modify, duration: {}, table: {}, key: {}, mods: {}", duration, tableMapping.getTableName(), key, mods);
    return result;
}
Also used : Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration)

Example 13 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method addEntry.

@Override
public boolean addEntry(String key, String objectClass, Collection<AttributeData> attributes) throws DuplicateEntryException, PersistenceException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    boolean result = addEntryImpl(tableMapping, key, attributes);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: add, duration: {}, table: {}, key: {}, attributes: {}", duration, tableMapping.getTableName(), key, attributes);
    return result;
}
Also used : Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration)

Example 14 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method updateEntryImpl.

private boolean updateEntryImpl(TableMapping tableMapping, String key, List<AttributeDataModification> mods) throws PersistenceException {
    try {
        MessageDigest messageDigest = getMessageDigestInstance();
        Map<String, StructField> columTypes = tableMapping.getColumTypes();
        WriteBuilder mutationBuilder = Mutation.newInsertOrUpdateBuilder(tableMapping.getTableName()).set(SpannerOperationService.DOC_ID).to(key);
        List<Mutation> mutations = new LinkedList<>();
        for (AttributeDataModification attributeMod : mods) {
            AttributeData attribute = attributeMod.getAttribute();
            AttributeModificationType type = attributeMod.getModificationType();
            String attributeName = attribute.getName();
            StructField attributeType = columTypes.get(attributeName.toLowerCase());
            // If column not inside table we should check if there is child table
            if (attributeType == null) {
                TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
                if (childTableMapping == null) {
                    throw new PersistenceException(String.format("Failed to update entry. Column '%s' is undefined", attributeName));
                }
                Map<String, StructField> childColumTypes = childTableMapping.getColumTypes();
                StructField childAttributeType = childColumTypes.get(attributeName.toLowerCase());
                // Build Mutation for child table
                Map<String, Object> oldValues = null;
                if ((attributeMod.getOldAttribute() != null) && (attributeMod.getOldAttribute().getValues() != null)) {
                    oldValues = new HashMap<>();
                    for (Object oldValue : attributeMod.getOldAttribute().getValues()) {
                        String dictDocId = getStringUniqueKey(messageDigest, oldValue);
                        oldValues.put(dictDocId, oldValue);
                    }
                }
                if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type) || (AttributeModificationType.REPLACE == type)) {
                    for (Object value : attribute.getValues()) {
                        WriteBuilder childMutationBuilder = Mutation.newInsertOrUpdateBuilder(childTableMapping.getTableName());
                        String dictDocId = getStringUniqueKey(messageDigest, value);
                        childMutationBuilder.set(SpannerOperationService.DOC_ID).to(key).set(SpannerOperationService.DICT_DOC_ID).to(dictDocId);
                        setMutationBuilderValue(childMutationBuilder, childAttributeType, value);
                        mutations.add(childMutationBuilder.build());
                        if (oldValues != null) {
                            oldValues.remove(dictDocId);
                        }
                    }
                } else if (AttributeModificationType.REMOVE == type) {
                    // Build Mutation for child table
                    com.google.cloud.spanner.KeySet.Builder keySetBuilder = KeySet.newBuilder();
                    for (Object value : attribute.getValues()) {
                        String dictDocId = getStringUniqueKey(messageDigest, value);
                        keySetBuilder.addKey(Key.of(key, dictDocId));
                    }
                    Mutation childMutation = Mutation.delete(childTableMapping.getTableName(), keySetBuilder.build());
                    mutations.add(childMutation);
                } else {
                    throw new UnsupportedOperationException("Operation type '" + type + "' is not implemented");
                }
                if ((oldValues != null) && (oldValues.size() > 0)) {
                    com.google.cloud.spanner.KeySet.Builder keySetBuilder = KeySet.newBuilder();
                    for (String removeDictDocId : oldValues.keySet()) {
                        keySetBuilder.addKey(Key.of(key, removeDictDocId));
                    }
                    Mutation childMutation = Mutation.delete(childTableMapping.getTableName(), keySetBuilder.build());
                    mutations.add(childMutation);
                }
            } else {
                if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type) || (AttributeModificationType.REPLACE == type)) {
                    setMutationBuilderValue(mutationBuilder, attributeType, attribute.getValues());
                } else if (AttributeModificationType.REMOVE == type) {
                    removeMutationBuilderValue(mutationBuilder, attribute, attributeType);
                } else {
                    throw new UnsupportedOperationException("Operation type '" + type + "' is not implemented");
                }
            }
        }
        mutations.add(0, mutationBuilder.build());
        databaseClient.write(mutations);
        return true;
    } catch (SpannerException | IllegalStateException ex) {
        throw new PersistenceException("Failed to update entry", ex);
    }
}
Also used : AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Builder(com.google.cloud.spanner.Statement.Builder) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) MessageDigest(java.security.MessageDigest) KeySet(com.google.cloud.spanner.KeySet) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) LinkedList(java.util.LinkedList) AttributeDataModification(io.jans.orm.model.AttributeDataModification) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) PersistenceException(io.jans.orm.exception.operation.PersistenceException) Mutation(com.google.cloud.spanner.Mutation) SpannerException(com.google.cloud.spanner.SpannerException) AttributeData(io.jans.orm.model.AttributeData)

Example 15 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerConnectionProvider method getChildTablesMapping.

public Map<String, TableMapping> getChildTablesMapping(String key, TableMapping tableMapping) {
    Set<String> childAttributes = tableChildAttributesMap.get(tableMapping.getObjectClass());
    if (childAttributes == null) {
        return null;
    }
    Map<String, TableMapping> childTableMapping = new HashMap<>();
    for (String childAttribute : childAttributes) {
        TableMapping childColumTypes = getChildTableMappingByKey(key, tableMapping, childAttribute);
        childTableMapping.put(childAttribute.toLowerCase(), childColumTypes);
    }
    return childTableMapping;
}
Also used : HashMap(java.util.HashMap) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping)

Aggregations

TableMapping (io.jans.orm.cloud.spanner.model.TableMapping)17 Duration (java.time.Duration)8 Instant (java.time.Instant)8 StructField (com.google.cloud.spanner.Type.StructField)5 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)4 AttributeData (io.jans.orm.model.AttributeData)4 SearchException (io.jans.orm.exception.operation.SearchException)3 LinkedList (java.util.LinkedList)3 Mutation (com.google.cloud.spanner.Mutation)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 PersistenceException (io.jans.orm.exception.operation.PersistenceException)2 MessageDigest (java.security.MessageDigest)2 Column (net.sf.jsqlparser.schema.Column)2 KeySet (com.google.cloud.spanner.KeySet)1 Builder (com.google.cloud.spanner.Statement.Builder)1 KeyConversionException (io.jans.orm.exception.KeyConversionException)1 MappingException (io.jans.orm.exception.MappingException)1 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)1 AttributeDataModification (io.jans.orm.model.AttributeDataModification)1