Search in sources :

Example 1 with DatabaseWriteException

use of nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException in project timbuctoo by HuygensING.

the class BdbRmlDataSourceStore method onChangedSubject.

@Override
public void onChangedSubject(String subject, ChangeFetcher changeFetcher) throws RdfProcessingFailedException {
    try {
        boolean[] wasCollection = new boolean[] { false };
        StreamIterator.iterateAndCloseOrThrow(changeFetcher.getPredicates(subject, TIM_HAS_ROW, Direction.OUT, true, false, true), quad -> {
            wasCollection[0] = true;
            if (quad.getChangeType() == ChangeType.ASSERTED) {
                bdbWrapper.put(subject, quad.getObject());
            }
        });
        if (!wasCollection[0]) {
            try (Stream<CursorQuad> quads = changeFetcher.getPredicates(subject, TIM_HAS_ROW, Direction.IN, true, true, true)) {
                Optional<CursorQuad> isRawRow = quads.findFirst();
                if (isRawRow.isPresent()) {
                    final String collectionUri = isRawRow.get().getObject();
                    Map<String, Property> predicatesToStore = collectionProperties.computeIfAbsent(collectionUri, collection -> getPropertyNames(changeFetcher, collection));
                    switch(isRawRow.get().getChangeType()) {
                        case ASSERTED:
                            // add all unchanged and new predicates
                            StreamIterator.iterateAndCloseOrThrow(changeFetcher.getPredicates(subject, false, true, true), pred -> {
                                final Property property = predicatesToStore.get(pred.getPredicate());
                                if (property != null) {
                                    bdbWrapper.put(subject, property.newName + "\n" + pred.getObject());
                                }
                            });
                            break;
                        case RETRACTED:
                            // remove all unchanged and removed predicates
                            StreamIterator.iterateAndCloseOrThrow(changeFetcher.getPredicates(subject, true, true, false), pred -> {
                                final Property property = predicatesToStore.get(pred.getPredicate());
                                if (property != null) {
                                    bdbWrapper.delete(subject, property.oldName + "\n" + pred.getObject());
                                }
                            });
                            break;
                        case UNCHANGED:
                            // add all added predicates, remove all removed predicates
                            StreamIterator.iterateAndCloseOrThrow(changeFetcher.getPredicates(subject, true, false, true), pred -> {
                                final Property property = predicatesToStore.get(pred.getPredicate());
                                if (property != null) {
                                    if (pred.getChangeType() == ChangeType.RETRACTED) {
                                        bdbWrapper.delete(subject, property.oldName + "\n" + pred.getObject());
                                    } else {
                                        bdbWrapper.put(subject, property.oldName + "\n" + pred.getObject());
                                    }
                                }
                            });
                            break;
                        default:
                            throw new RuntimeException("Should not happen");
                    }
                }
            }
        }
    } catch (DatabaseWriteException e) {
        throw new RdfProcessingFailedException(e);
    }
}
Also used : CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) RdfProcessingFailedException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)

Example 2 with DatabaseWriteException

use of nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException in project timbuctoo by HuygensING.

the class BdbWrapper method replace.

public void replace(KeyT key, ValueT initialValue, Function<ValueT, ValueT> replacer) throws DatabaseWriteException {
    synchronized (keyEntry) {
        try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
            keyBinder.objectToEntry(key, keyEntry);
            OperationStatus searchResult = cursor.getSearchKey(keyEntry, valueEntry, LockMode.DEFAULT);
            ValueT newValue = initialValue;
            if (searchResult.equals(OperationStatus.SUCCESS)) {
                newValue = replacer.apply(valueBinder.entryToObject(valueEntry));
            }
            valueBinder.objectToEntry(newValue, valueEntry);
            cursor.putCurrent(valueEntry);
        } catch (Exception e) {
            throw new DatabaseWriteException(e);
        }
    }
}
Also used : DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) OperationStatus(com.sleepycat.je.OperationStatus) Cursor(com.sleepycat.je.Cursor) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)

Example 3 with DatabaseWriteException

use of nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException in project timbuctoo by HuygensING.

the class BdbWrapper method delete.

public boolean delete(KeyT key, ValueT value) throws DatabaseWriteException {
    boolean wasChange = false;
    synchronized (keyEntry) {
        try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
            keyBinder.objectToEntry(key, keyEntry);
            valueBinder.objectToEntry(value, valueEntry);
            OperationStatus searchResult = cursor.getSearchBoth(keyEntry, valueEntry, LockMode.DEFAULT);
            if (searchResult.equals(OperationStatus.SUCCESS)) {
                wasChange = cursor.delete() == OperationStatus.SUCCESS;
            }
        } catch (Exception e) {
            throw new DatabaseWriteException(e);
        }
    }
    return wasChange;
}
Also used : DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) OperationStatus(com.sleepycat.je.OperationStatus) Cursor(com.sleepycat.je.Cursor) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)

Example 4 with DatabaseWriteException

use of nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException in project timbuctoo by HuygensING.

the class BdbWrapper method put.

public boolean put(KeyT key, ValueT value) throws DatabaseWriteException {
    synchronized (keyEntry) {
        try {
            keyBinder.objectToEntry(key, keyEntry);
            if (databaseConfig.getSortedDuplicates()) {
                valueBinder.objectToEntry(value, valueEntry);
                OperationStatus operationStatus = database.putNoDupData(transaction, keyEntry, valueEntry);
                // operation status is only SUCCESS if the data was not in the database before
                return operationStatus.equals(OperationStatus.SUCCESS);
            } else {
                try (Cursor cursor = database.openCursor(transaction, CursorConfig.DEFAULT)) {
                    OperationStatus searchResult = cursor.getSearchKey(keyEntry, valueEntry, LockMode.DEFAULT);
                    if (searchResult == SUCCESS && Objects.equals(value, valueBinder.entryToObject(valueEntry))) {
                        return false;
                    } else {
                        valueBinder.objectToEntry(value, valueEntry);
                        // returns OperationStatus.SUCCESS or throws an exception
                        database.put(transaction, keyEntry, valueEntry);
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            throw new DatabaseWriteException(e);
        }
    }
}
Also used : DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) OperationStatus(com.sleepycat.je.OperationStatus) Cursor(com.sleepycat.je.Cursor) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)

Example 5 with DatabaseWriteException

use of nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException in project timbuctoo by HuygensING.

the class BdbSchemaStore method finish.

@Override
public void finish() {
    LOG.info("Finished processing entities");
    importStatus.setStatus("Finished processing entities");
    // Step 3: Add type information to inverse predicates
    for (Map.Entry<String, Type> typeEntry : types.entrySet()) {
        Type type = typeEntry.getValue();
        String typeName = typeEntry.getKey();
        for (Predicate predicate : type.getPredicates()) {
            predicate.finish();
            if (predicate.getDirection() == Direction.IN) {
                continue;
            }
            for (String referenceType : predicate.getReferenceTypes().keySet()) {
                try {
                    types.get(referenceType).getPredicate(predicate.getName(), // There must be an inverse for each outward predicate
                    Direction.IN).incReferenceType(type.getName(), 1);
                } catch (Exception e) {
                    String cause = "Referenced type " + referenceType + " not found";
                    try {
                        if (types.containsKey(referenceType)) {
                            cause = "type does not have the inverse predicate " + predicate.getName();
                            if (types.get(referenceType).getPredicate(predicate.getName(), Direction.IN) != null) {
                                cause = "Something failed during addreferencetype(" + typeName + ")";
                            }
                        }
                        LOG.error("Error during inverse generation (ignored): " + cause, e);
                        importStatus.addError("Error during inverse generation (ignored): " + cause, e);
                    } catch (Exception e2) {
                        LOG.error("Error during inverse generation " + cause, e);
                        importStatus.addError("Error during inverse generation " + cause, e);
                        LOG.error("Error during recovery generation ", e2);
                        importStatus.addError("Error during recovery generation ", e2);
                    }
                }
            }
        }
    }
    try {
        try {
            String serializedValue = objectMapper.writeValueAsString(types);
            dataStore.setValue(serializedValue);
            dataStore.commit();
            stableTypes = readTypes(serializedValue);
        } catch (IOException | DatabaseWriteException e) {
            throw new SchemaUpdateException(e);
        }
    } catch (SchemaUpdateException e) {
        e.printStackTrace();
    }
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) ChangeType(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) IOException(java.io.IOException) SchemaUpdateException(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaUpdateException) HashMap(java.util.HashMap) Map(java.util.Map) DatabaseWriteException(nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException) IOException(java.io.IOException) SchemaUpdateException(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaUpdateException) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Aggregations

DatabaseWriteException (nl.knaw.huygens.timbuctoo.v5.berkeleydb.exceptions.DatabaseWriteException)8 RdfProcessingFailedException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.RdfProcessingFailedException)4 Cursor (com.sleepycat.je.Cursor)3 OperationStatus (com.sleepycat.je.OperationStatus)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ChangeType (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType)1 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)1 SchemaUpdateException (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaUpdateException)1 Predicate (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)1 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)1