Search in sources :

Example 6 with JsonDBException

use of io.syndesis.server.jsondb.JsonDBException in project syndesis by syndesisio.

the class SqlJsonDB method update.

@Override
public void update(String path, InputStream is) {
    ArrayList<String> updatePaths = new ArrayList<>();
    withTransaction(dbi -> {
        try {
            BatchManager mb = new BatchManager(dbi);
            try (JsonParser jp = new JsonFactory().createParser(is)) {
                JsonToken nextToken = jp.nextToken();
                if (nextToken != JsonToken.START_OBJECT) {
                    throw new JsonParseException(jp, "Update did not contain a json object");
                }
                while (true) {
                    nextToken = jp.nextToken();
                    if (nextToken == JsonToken.END_OBJECT) {
                        break;
                    }
                    if (nextToken != JsonToken.FIELD_NAME) {
                        throw new JsonParseException(jp, "Expected a field name");
                    }
                    String key = suffix(path, "/") + jp.getCurrentName();
                    updatePaths.add(key);
                    String baseDBPath = JsonRecordSupport.convertToDBPath(key);
                    mb.deleteRecordsForSet(baseDBPath);
                    try {
                        JsonRecordSupport.jsonStreamToRecords(indexPaths, jp, baseDBPath, mb.createSetConsumer());
                    } catch (IOException e) {
                        throw new JsonDBException(e);
                    }
                }
                nextToken = jp.nextToken();
                if (nextToken != null) {
                    throw new JsonParseException(jp, "Document did not terminate as expected.");
                }
                mb.flush();
            }
        } catch (IOException e) {
            throw new JsonDBException(e);
        }
    });
    if (bus != null) {
        for (String updatePath : updatePaths) {
            bus.broadcast("jsondb-updated", prefix(trimSuffix(updatePath, "/"), "/"));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonDBException(io.syndesis.server.jsondb.JsonDBException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonDBException (io.syndesis.server.jsondb.JsonDBException)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 ActivityStep (io.syndesis.server.endpoint.v1.handler.activity.ActivityStep)1 GetOptions (io.syndesis.server.jsondb.GetOptions)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 DBI (org.skife.jdbi.v2.DBI)1