All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.yawp.driver.appengine.pipes.reload.DrainSinkJob Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.driver.appengine.pipes.reload;

import com.google.appengine.tools.pipeline.FutureValue;
import com.google.appengine.tools.pipeline.Job3;
import com.google.appengine.tools.pipeline.Value;
import io.yawp.commons.utils.ReflectionUtils;
import io.yawp.repository.IdRef;
import io.yawp.repository.Repository;
import io.yawp.repository.pipes.Pipe;
import io.yawp.repository.pipes.SinkMarker;
import io.yawp.repository.query.QueryBuilder;

import java.util.List;

import static io.yawp.repository.Yawp.yawp;

public class DrainSinkJob extends Job3, String, String> {

    private static final int BATCH_SIZE = 100;

    private transient Repository r;

    private transient Class pipeClazz;

    private transient Class sourceClazz;

    private transient IdRef sinkId;

    private transient String cursor;

    @Override
    public Value run(Class pipeClazz, String sinkUri, String cursor) throws Exception {
        init(pipeClazz, sinkUri, cursor);
        return execute();
    }

    private void init(Class pipeClazz, String sinkUri, String cursor) {
        this.r = yawp();
        this.pipeClazz = pipeClazz;
        this.sourceClazz = ReflectionUtils.getFeatureEndpointClazz(pipeClazz);
        this.sinkId = IdRef.parse(r, sinkUri);
        this.cursor = cursor;
    }

    private Value execute() {
        List> markerIds = sinkMarkerIds();

        FutureValue waitForClearSink = null;
        if (cursor != null) {
            waitForClearSink = futureCall(new DrainSinkJob(), immediate(pipeClazz), immediate(sinkId.getUri()), immediate(cursor));
        } else {
            clearSink();
        }

        destroySinkMarkers(markerIds);

        return waitForClearSink;
    }

    private void destroySinkMarkers(List> markerIds) {
        for (IdRef id : markerIds) {
            if (!id.getParentId().getClazz().equals(sourceClazz)) {
                continue;
            }
            r.destroy(id);
        }
    }

    private void clearSink() {
        Pipe pipe = newPipeInstance();
        Object sink = sinkId.fetch();
        pipe.drain(sink);
        r.save(sink);
    }

    private Pipe newPipeInstance() {
        return Pipe.newInstance(r, pipeClazz);
    }

    private List> sinkMarkerIds() {
        QueryBuilder q = r.query(SinkMarker.class).from(sinkId).order("id").limit(BATCH_SIZE);
        if (cursor != null) {
            q.cursor(cursor);
        }
        List> ids = q.ids();
        if (ids.size() < BATCH_SIZE) {
            cursor = null;
        } else {
            cursor = q.getCursor();
        }
        return ids;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy