2024-02-28 11:40:11 +01:00
|
|
|
package nu.marginalia.linkgraph;
|
2024-01-08 15:53:13 +01:00
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
|
import io.grpc.stub.StreamObserver;
|
2024-02-28 11:40:11 +01:00
|
|
|
import nu.marginalia.api.linkgraph.*;
|
2024-09-27 13:22:26 +02:00
|
|
|
import nu.marginalia.service.server.DiscoverableService;
|
2024-01-08 15:53:13 +01:00
|
|
|
|
2024-02-28 11:40:11 +01:00
|
|
|
/** GRPC service for interrogating domain links for a single partition. For accessing the data
|
|
|
|
* in the application, the AggregateLinkGraphService should be used instead via the
|
|
|
|
* AggregateLinkGraphClient.
|
2024-01-08 15:53:13 +01:00
|
|
|
*/
|
2024-09-27 13:22:26 +02:00
|
|
|
public class PartitionLinkGraphService
|
|
|
|
extends LinkGraphApiGrpc.LinkGraphApiImplBase
|
|
|
|
implements DiscoverableService
|
|
|
|
{
|
2024-02-28 11:40:11 +01:00
|
|
|
private final DomainLinks domainLinks;
|
2024-01-08 15:53:13 +01:00
|
|
|
|
|
|
|
@Inject
|
2024-02-28 11:40:11 +01:00
|
|
|
public PartitionLinkGraphService(DomainLinks domainLinks) {
|
|
|
|
this.domainLinks = domainLinks;
|
2024-01-08 15:53:13 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 14:01:23 +01:00
|
|
|
public void getAllLinks(Empty request,
|
|
|
|
io.grpc.stub.StreamObserver<RpcDomainIdPairs> responseObserver) {
|
2024-01-08 15:53:13 +01:00
|
|
|
|
|
|
|
try (var idsConverter = new AllIdsResponseConverter(responseObserver)) {
|
2024-02-28 11:40:11 +01:00
|
|
|
domainLinks.forEach(idsConverter::accept);
|
2024-01-08 15:53:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
responseObserver.onCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class AllIdsResponseConverter implements AutoCloseable {
|
|
|
|
private RpcDomainIdPairs.Builder builder;
|
|
|
|
private final io.grpc.stub.StreamObserver<RpcDomainIdPairs> responseObserver;
|
|
|
|
private int n = 0;
|
|
|
|
|
|
|
|
private AllIdsResponseConverter(io.grpc.stub.StreamObserver<RpcDomainIdPairs> responseObserver) {
|
|
|
|
this.responseObserver = responseObserver;
|
|
|
|
this.builder = RpcDomainIdPairs.newBuilder();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void accept(int source, int dest) {
|
|
|
|
builder.addSourceIds(source);
|
|
|
|
builder.addDestIds(dest);
|
|
|
|
|
|
|
|
if (++n > 1000) {
|
|
|
|
responseObserver.onNext(builder.build());
|
|
|
|
builder = RpcDomainIdPairs.newBuilder();
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
if (n > 0) {
|
|
|
|
responseObserver.onNext(builder.build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getLinksFromDomain(RpcDomainId request,
|
|
|
|
StreamObserver<RpcDomainIdList> responseObserver) {
|
|
|
|
|
2024-02-28 11:40:11 +01:00
|
|
|
var links = domainLinks.findDestinations(request.getDomainId());
|
2024-01-08 15:53:13 +01:00
|
|
|
|
|
|
|
var rspBuilder = RpcDomainIdList.newBuilder();
|
|
|
|
for (int i = 0; i < links.size(); i++) {
|
|
|
|
rspBuilder.addDomainId(links.get(i));
|
|
|
|
}
|
|
|
|
responseObserver.onNext(rspBuilder.build());
|
|
|
|
|
|
|
|
responseObserver.onCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getLinksToDomain(RpcDomainId request,
|
|
|
|
StreamObserver<RpcDomainIdList> responseObserver) {
|
|
|
|
|
2024-02-28 11:40:11 +01:00
|
|
|
var links = domainLinks.findSources(request.getDomainId());
|
2024-01-08 15:53:13 +01:00
|
|
|
|
|
|
|
var rspBuilder = RpcDomainIdList.newBuilder();
|
|
|
|
for (int i = 0; i < links.size(); i++) {
|
|
|
|
rspBuilder.addDomainId(links.get(i));
|
|
|
|
}
|
|
|
|
responseObserver.onNext(rspBuilder.build());
|
|
|
|
|
|
|
|
responseObserver.onCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void countLinksFromDomain(RpcDomainId request,
|
|
|
|
StreamObserver<RpcDomainIdCount> responseObserver) {
|
|
|
|
responseObserver.onNext(RpcDomainIdCount.newBuilder()
|
2024-02-28 11:40:11 +01:00
|
|
|
.setIdCount(domainLinks.countDestinations(request.getDomainId()))
|
2024-01-08 15:53:13 +01:00
|
|
|
.build());
|
|
|
|
responseObserver.onCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void countLinksToDomain(RpcDomainId request,
|
|
|
|
StreamObserver<RpcDomainIdCount> responseObserver) {
|
|
|
|
responseObserver.onNext(RpcDomainIdCount.newBuilder()
|
2024-02-28 11:40:11 +01:00
|
|
|
.setIdCount(domainLinks.countSources(request.getDomainId()))
|
2024-01-08 15:53:13 +01:00
|
|
|
.build());
|
|
|
|
responseObserver.onCompleted();
|
|
|
|
}
|
|
|
|
}
|