io.yawp.driver.appengine.pipes.reload.DrainSinkJob Maven / Gradle / Ivy
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 extends Pipe> pipeClazz;
private transient Class> sourceClazz;
private transient IdRef> sinkId;
private transient String cursor;
@Override
public Value run(Class extends Pipe> pipeClazz, String sinkUri, String cursor) throws Exception {
init(pipeClazz, sinkUri, cursor);
return execute();
}
private void init(Class extends Pipe> 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 extends IdRef>> 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 extends IdRef>> 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 extends IdRef>> sinkMarkerIds() {
QueryBuilder> q = r.query(SinkMarker.class).from(sinkId).order("id").limit(BATCH_SIZE);
if (cursor != null) {
q.cursor(cursor);
}
List extends IdRef>> ids = q.ids();
if (ids.size() < BATCH_SIZE) {
cursor = null;
} else {
cursor = q.getCursor();
}
return ids;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy