Example usage for com.mongodb.client.model BulkWriteOptions ordered

List of usage examples for com.mongodb.client.model BulkWriteOptions ordered

Introduction

In this page you can find the example usage for com.mongodb.client.model BulkWriteOptions ordered.

Prototype

boolean ordered

To view the source code for com.mongodb.client.model BulkWriteOptions ordered.

Click Source Link

Usage

From source file:net.es.netshell.mongodb.MongoDBProvider.java

License:Open Source License

@Override
public final void store(List<ResourceAnchor> anchors) throws IOException {
    if (!KernelThread.currentKernelThread().isPrivileged()) {
        throw new SecurityException("store db list - not authorized");
    }/* w  w  w.ja v a2 s  . c  o  m*/
    HashMap<String, ArrayList<WriteModel>> collectionRequests = new HashMap<String, ArrayList<WriteModel>>();
    // Build the bulk requests per collection
    for (ResourceAnchor anchor : anchors) {
        String user = anchor.getContainerOwner();
        String collection = anchor.getContainerName();
        String collectionName = user + "_" + collection;
        ArrayList<WriteModel> requests;
        if (!collectionRequests.containsKey(collectionName)) {
            requests = new ArrayList<WriteModel>();
            collectionRequests.put(collectionName, requests);
        } else {
            requests = collectionRequests.get(collectionName);
        }
        try {
            // Likely to be in the Resource cache. Otherwise replace by itself.
            Resource resource = Resource.findByName(user, collection, anchor.getResourceName());
            Document doc = Document.parse(resource.saveToJSON());
            Document query = new Document("resourceName", resource.getResourceName());
            ;
            ReplaceOneModel<Document> request = new ReplaceOneModel<Document>(query, doc);
            request.getOptions().upsert(true);
            requests.add(request);
        } catch (InstantiationException e) {
            throw new IOException(e);
        }
    }
    // Bulk write the collection's request
    for (Map.Entry<String, ArrayList<WriteModel>> entry : collectionRequests.entrySet()) {
        String[] name = entry.getKey().split("_");
        ArrayList<WriteModel> requests = entry.getValue();
        MongoCollection mongoCollection = this.getCollection(name[0], name[1]);
        if (mongoCollection == null) {
            throw new RuntimeErrorException(new Error("Could not store into collection " + entry.getKey()));
        }
        BulkWriteOptions options = new BulkWriteOptions();
        options.ordered(false);
        mongoCollection.bulkWrite(requests, options);
    }
}

From source file:net.es.netshell.mongodb.MongoDBProvider.java

License:Open Source License

@Override
public final void delete(List<ResourceAnchor> anchors) throws IOException {
    if (!KernelThread.currentKernelThread().isPrivileged()) {
        throw new SecurityException("delete db list - not authorized");
    }/*from   ww  w.jav  a  2s . c o  m*/
    HashMap<String, ArrayList<WriteModel>> collectionRequests = new HashMap<String, ArrayList<WriteModel>>();
    // Build the bulk requests per collection
    for (ResourceAnchor anchor : anchors) {
        String user = anchor.getContainerOwner();
        String collection = anchor.getContainerName();
        String collectionName = user + "_" + collection;
        ArrayList<WriteModel> requests;
        if (!collectionRequests.containsKey(collectionName)) {
            requests = new ArrayList<WriteModel>();
            collectionRequests.put(collectionName, requests);
        } else {
            requests = collectionRequests.get(collectionName);
        }
        Document query = new Document("resourceName", anchor.getResourceName());
        DeleteOneModel request = new DeleteOneModel(query);
        ;
        requests.add(request);
    }
    // Bulk delete the collection's request
    for (Map.Entry<String, ArrayList<WriteModel>> entry : collectionRequests.entrySet()) {
        String[] name = entry.getKey().split("_");
        ArrayList<WriteModel> requests = entry.getValue();
        MongoCollection mongoCollection = this.getCollection(name[0], name[1]);
        if (mongoCollection == null) {
            throw new RuntimeErrorException(new Error("Could not delete into collection " + entry.getKey()));
        }
        BulkWriteOptions options = new BulkWriteOptions();
        options.ordered(false);
        mongoCollection.bulkWrite(requests, options);
    }
}