org.dcache.sf.FlushTask.java Source code

Java tutorial

Introduction

Here is the source code for org.dcache.sf.FlushTask.java

Source

/* dCache - http://www.dcache.org/
 *
 * Copyright (C) 2014 Deutsches Elektronen-Synchrotron
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dcache.sf;

import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;

import diskCacheV111.util.PnfsId;

import org.dcache.pool.nearline.spi.FlushRequest;

class FlushTask implements PollingTask<Set<URI>> {
    private final PnfsId pnfsId;
    private final MongoCollection<Document> files;

    public FlushTask(FlushRequest request, MongoCollection<Document> collection) {
        this.files = collection;
        this.pnfsId = request.getFileAttributes().getPnfsId();
    }

    @Override
    public Set<URI> start() throws IOException {
        files.find(Filters.eq("pnfsid", pnfsId.getId()));
        files.insertOne(new Document("pnfsid", pnfsId.getId()));

        return null;
    }

    @Override
    public Set<URI> poll() throws URISyntaxException {
        FindIterable<Document> result = files
                .find(Filters.and(Filters.eq("pnfsid", pnfsId.getId()), Filters.exists("bfid")));
        final Set<URI> uris = new HashSet<>();
        result.forEach(new Block<Document>() {
            @Override
            public void apply(Document document) {
                try {
                    String uriString = document.get("bfid").toString();
                    uris.add(new URI(uriString));
                } catch (URISyntaxException e) {
                    System.out.println(e.getMessage());
                }
            }
        });
        return uris;
    }

    @Override
    public boolean abort() throws IOException {
        return files.findOneAndDelete(Filters.eq("pnfsid", pnfsId.getId())) != null;
    }
}