Search in sources :

Example 6 with Sequence

use of io.lumeer.api.model.Sequence in project engine by Lumeer.

the class MongoSequenceDao method updateSequence.

private Sequence updateSequence(final Sequence sequence, final Bson filter) {
    FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
    try {
        Bson update = new Document("$set", sequence);
        final Sequence returnedSequence = databaseCollection().findOneAndUpdate(filter, update, options);
        if (returnedSequence == null) {
            throw new StorageException("Sequence '" + sequence.getId() + "' has not been updated.");
        }
        if (createOrUpdateSequenceEvent != null) {
            createOrUpdateSequenceEvent.fire(new CreateOrUpdateSequence(returnedSequence));
        }
        return returnedSequence;
    } catch (MongoException ex) {
        throw new StorageException("Cannot update sequence " + sequence, ex);
    }
}
Also used : MongoException(com.mongodb.MongoException) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Sequence(io.lumeer.api.model.Sequence) RemoveSequence(io.lumeer.engine.api.event.RemoveSequence) CreateOrUpdateSequence(io.lumeer.engine.api.event.CreateOrUpdateSequence) CreateOrUpdateSequence(io.lumeer.engine.api.event.CreateOrUpdateSequence) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) StorageException(io.lumeer.storage.api.exception.StorageException) Bson(org.bson.conversions.Bson)

Example 7 with Sequence

use of io.lumeer.api.model.Sequence in project engine by Lumeer.

the class MongoSequenceDao method getNextSequenceNo.

@Override
public synchronized int getNextSequenceNo(final String indexName) {
    final FindOneAndUpdateOptions options = new FindOneAndUpdateOptions();
    options.returnDocument(ReturnDocument.AFTER);
    final Sequence seq = databaseCollection().findOneAndUpdate(eq(SequenceCodec.NAME, indexName), inc(SequenceCodec.SEQ, 1), options);
    if (seq == null) {
        // the sequence did not exist
        resetSequence(indexName);
        return 0;
    } else {
        return seq.getSeq();
    }
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Sequence(io.lumeer.api.model.Sequence) RemoveSequence(io.lumeer.engine.api.event.RemoveSequence) CreateOrUpdateSequence(io.lumeer.engine.api.event.CreateOrUpdateSequence)

Aggregations

Sequence (io.lumeer.api.model.Sequence)7 CreateOrUpdateSequence (io.lumeer.engine.api.event.CreateOrUpdateSequence)4 RemoveSequence (io.lumeer.engine.api.event.RemoveSequence)4 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 MongoException (com.mongodb.MongoException)1 SequenceFacade (io.lumeer.core.facade.SequenceFacade)1 StorageException (io.lumeer.storage.api.exception.StorageException)1 ArrayList (java.util.ArrayList)1 Optional (java.util.Optional)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 BackupDataEvent (org.marvec.pusher.data.BackupDataEvent)1 Event (org.marvec.pusher.data.Event)1