Example usage for com.mongodb.client.model Filters eq

List of usage examples for com.mongodb.client.model Filters eq

Introduction

In this page you can find the example usage for com.mongodb.client.model Filters eq.

Prototype

public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates a filter that matches all documents where the value of the field name equals the specified value.

Usage

From source file:io.mandrel.document.impl.MongoDocumentStore.java

License:Apache License

@Override
public void save(List<Document> data) {
    if (data != null) {
        List<ReplaceOneModel<org.bson.Document>> updates = data.stream().map(toBson)
                .map(doc -> new ReplaceOneModel<org.bson.Document>(Filters.eq("_id", doc.getString("_id")), doc,
                        new UpdateOptions().upsert(true)))
                .collect(Collectors.toList());
        collection.bulkWrite(updates);// w  ww  . j a va  2  s.c  o  m
    }
}

From source file:io.mandrel.metadata.impl.MongoMetadataStore.java

License:Apache License

@Override
public void addMetadata(Uri uri, BlobMetadata metadata) {
    org.bson.Document document = JsonBsonCodec.toBson(mapper, metadata);
    document.append("_id", uri.toString());
    collection.replaceOne(Filters.eq("_id", document.getString("_id")), document,
            new UpdateOptions().upsert(true));
}

From source file:io.mandrel.metadata.impl.MongoMetadataStore.java

License:Apache License

@Override
public void delete(Uri uri) {
    collection.deleteOne(Filters.eq("_id", uri.toString()));
}

From source file:io.mandrel.metadata.impl.MongoMetadataStore.java

License:Apache License

@Override
public BlobMetadata getMetadata(Uri uri) {
    Document doc = collection.find(Filters.eq("_id", uri.toString())).first();
    return JsonBsonCodec.fromBson(mapper, doc, BlobMetadata.class);
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

@Override
public void sync(Map<String, Long> accumulators) {

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime keytime = now.withMinute(0).withSecond(0).withNano(0);

    // {global.XXX=0, global.YYY=0, ...} to {global{XXX=O, YYY=0}, ...}
    Stream<Pair<String, Pair<String, Long>>> map = accumulators.entrySet().stream().map(e -> {
        Iterable<String> results = splitter.split(e.getKey());
        List<String> elts = Lists.newArrayList(results);
        return Pair.of(elts.get(0), Pair.of(elts.get(1), e.getValue()));
    });//from w  w  w .jav  a  2 s .  co m
    Map<String, List<Pair<String, Long>>> byKey = map.collect(Collectors.groupingBy(e -> e.getLeft(),
            Collectors.mapping(e -> e.getRight(), Collectors.toList())));

    List<? extends WriteModel<Document>> requests = byKey.entrySet().stream().map(e -> {
        Document updates = new Document();

        e.getValue().stream().forEach(i -> {
            Iterable<String> results = splitter.split(i.getKey());
            List<String> elts = Lists.newArrayList(results);
            if (elts.size() > 1) {
                updates.put(elts.get(0) + "." + JsonBsonCodec.toBson(elts.get(1)), i.getValue());
            } else {
                updates.put(i.getKey(), i.getValue());
            }
        });

        return new UpdateOneModel<Document>(Filters.eq("_id", e.getKey()), new Document("$inc", updates),
                new UpdateOptions().upsert(true));
    }).collect(Collectors.toList());

    counters.bulkWrite(requests);

    requests = byKey.entrySet().stream().map(e -> {
        List<UpdateOneModel<Document>> tsUpdates = Lists.newArrayList();

        e.getValue().stream().forEach(i -> {
            Iterable<String> results = splitter.split(i.getKey());
            List<String> elts = Lists.newArrayList(results);

            if (elts.size() == 1 && e.getKey().equalsIgnoreCase(MetricKeys.global())) {
                tsUpdates.add(new UpdateOneModel<Document>(
                        Filters.and(Filters.eq("type", e.getKey() + MetricKeys.METRIC_DELIM + i.getKey()),
                                Filters.eq("timestamp_hour", keytime)),
                        new Document("$inc",
                                new Document("values." + Integer.toString(now.getMinute()), i.getValue())),
                        new UpdateOptions().upsert(true)));
            }
        });

        return tsUpdates;
    }).flatMap(list -> list.stream()).collect(Collectors.toList());

    timeseries.bulkWrite(requests);

}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

public void prepareMinutes(LocalDateTime keytime) {
    List<? extends WriteModel<Document>> requests = Arrays
            .asList(MetricKeys.globalTotalSize(), MetricKeys.globalNbPages()).stream().map(el -> {
                Document document = new Document();
                document.append("type", el).append("timestamp_hour", keytime);
                document.append("values", IntStream.range(0, 60).collect(Document::new,
                        (doc, val) -> doc.put(Integer.toString(val), Long.valueOf(0)), Document::putAll));
                return document;
            })//from   w ww  .j  av a  2s.com
            .map(doc -> new UpdateOneModel<Document>(
                    Filters.and(Filters.eq("type", doc.getString("type")),
                            Filters.eq("timestamp_hour", keytime)),
                    new Document("$set", doc), new UpdateOptions().upsert(true)))
            .collect(Collectors.toList());

    timeseries.bulkWrite(requests);
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

@Override
public NodeMetrics node(String nodeId) {
    Document document = counters.find(Filters.eq("_id", MetricKeys.node(nodeId))).first();
    return document != null ? JsonBsonCodec.fromBson(mapper, document, NodeMetrics.class) : new NodeMetrics();
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

@Override
public GlobalMetrics global() {
    Document document = counters.find(Filters.eq("_id", MetricKeys.global())).first();
    return document != null ? JsonBsonCodec.fromBson(mapper, document, GlobalMetrics.class)
            : new GlobalMetrics();
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

@Override
public SpiderMetrics spider(long spiderId) {
    Document document = counters.find(Filters.eq("_id", MetricKeys.spider(spiderId))).first();
    return document != null ? JsonBsonCodec.fromBson(mapper, document, SpiderMetrics.class)
            : new SpiderMetrics();
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

License:Apache License

@Override
public void delete(long spiderId) {
    counters.deleteOne(Filters.eq("_id", spiderId));
}