Search in sources :

Example 6 with ZuliaIndexManager

use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.

the class IndexController method get.

@Get
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public HttpResponse<?> get(@QueryValue(ZuliaConstants.INDEX) String index, @QueryValue(value = ZuliaConstants.PRETTY, defaultValue = "true") Boolean pretty) {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    try {
        StringBuilder responseBuilder = new StringBuilder();
        GetIndexSettingsResponse getIndexSettingsResponse = indexManager.getIndexSettings(ZuliaServiceOuterClass.GetIndexSettingsRequest.newBuilder().setIndexName(index).build());
        responseBuilder.append("{");
        responseBuilder.append("\"indexSettings\": ");
        JsonFormat.Printer printer = JsonFormat.printer();
        responseBuilder.append(printer.print(getIndexSettingsResponse.getIndexSettings()));
        responseBuilder.append("}");
        String docString = responseBuilder.toString();
        if (pretty) {
            docString = JsonWriter.formatJson(docString);
        }
        return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS);
    } catch (Exception e) {
        return HttpResponse.serverError("Failed to get index names: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
    }
}
Also used : JsonFormat(com.google.protobuf.util.JsonFormat) GetIndexSettingsResponse(io.zulia.message.ZuliaServiceOuterClass.GetIndexSettingsResponse) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 7 with ZuliaIndexManager

use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.

the class NodesController method get.

@Get
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public HttpResponse<?> get(@QueryValue(value = ZuliaConstants.PRETTY, defaultValue = "true") Boolean pretty, @QueryValue(value = ZuliaConstants.ACTIVE, defaultValue = "false") Boolean active) {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    try {
        GetNodesResponse getNodesResponse = indexManager.getNodes(GetNodesRequest.newBuilder().setActiveOnly(active).build());
        org.bson.Document mongoDocument = new org.bson.Document();
        List<Document> memberObjList = new ArrayList<>();
        for (Node node : getNodesResponse.getNodeList()) {
            Document memberObj = new Document();
            memberObj.put("serverAddress", node.getServerAddress());
            memberObj.put("servicePort", node.getServicePort());
            memberObj.put("restPort", node.getRestPort());
            memberObj.put("heartbeat", node.getHeartbeat());
            List<Document> indexMappingList = new ArrayList<>();
            for (IndexMapping indexMapping : getNodesResponse.getIndexMappingList()) {
                TreeSet<Integer> primaryShards = new TreeSet<>();
                TreeSet<Integer> replicaShards = new TreeSet<>();
                for (ShardMapping shardMapping : indexMapping.getShardMappingList()) {
                    if (ZuliaNode.isEqual(shardMapping.getPrimaryNode(), node)) {
                        primaryShards.add(shardMapping.getShardNumber());
                    }
                    for (Node replica : shardMapping.getReplicaNodeList()) {
                        if (ZuliaNode.isEqual(replica, node)) {
                            replicaShards.add(shardMapping.getShardNumber());
                        }
                    }
                }
                Document shards = new Document();
                shards.put("name", indexMapping.getIndexName());
                shards.put("size", indexMapping.getSerializedSize());
                shards.put("primary", primaryShards);
                shards.put("replica", replicaShards);
                indexMappingList.add(shards);
            }
            memberObj.put("indexMappings", indexMappingList);
            memberObjList.add(memberObj);
        }
        mongoDocument.put("members", memberObjList);
        String docString = mongoDocument.toJson();
        if (pretty) {
            docString = JsonWriter.formatJson(docString);
        }
        return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS);
    } catch (Exception e) {
        return HttpResponse.serverError("Failed to get cluster membership: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
    }
}
Also used : Document(org.bson.Document) ZuliaNode(io.zulia.server.node.ZuliaNode) Node(io.zulia.message.ZuliaBase.Node) ArrayList(java.util.ArrayList) GetNodesResponse(io.zulia.message.ZuliaServiceOuterClass.GetNodesResponse) Document(org.bson.Document) IndexMapping(io.zulia.message.ZuliaIndex.IndexMapping) TreeSet(java.util.TreeSet) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) ShardMapping(io.zulia.message.ZuliaIndex.ShardMapping) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 8 with ZuliaIndexManager

use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.

the class AssociatedController method post.

@Post(consumes = MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Publisher<HttpResponse<?>> post(StreamingFileUpload file, Map<String, Object> metadata) {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    String id = metadata.get("id").toString();
    String fileName = metadata.get("fileName").toString();
    String indexName = metadata.get("indexName").toString();
    if (id != null && fileName != null && indexName != null) {
        Document metaDoc;
        if (metadata.containsKey("metaJson")) {
            metaDoc = Document.parse(metadata.get("metaJson").toString());
        } else {
            metaDoc = new Document();
        }
        OutputStream associatedDocumentOutputStream;
        try {
            associatedDocumentOutputStream = indexManager.getAssociatedDocumentOutputStream(indexName, id, fileName, metaDoc);
            Publisher<Boolean> uploadPublisher = transferToStream(ioExecutor, file, associatedDocumentOutputStream);
            return Flux.from(uploadPublisher).map(success -> {
                if (success) {
                    try {
                        associatedDocumentOutputStream.close();
                    } catch (IOException e) {
                        LOG.log(Level.SEVERE, "Failed to close stream: " + e.getMessage(), e);
                    }
                    return HttpResponse.ok("Stored associated document with uniqueId <" + id + "> and fileName <" + fileName + ">").status(ZuliaConstants.SUCCESS);
                } else {
                    try {
                        associatedDocumentOutputStream.close();
                    } catch (IOException e) {
                        LOG.log(Level.SEVERE, "Failed to close stream: " + e.getMessage(), e);
                    }
                    return HttpResponse.serverError("Failed to store associated document with uniqueId <" + id + "> and filename <" + fileName + ">");
                }
            });
        } catch (Exception e) {
            LOG.log(Level.SEVERE, e.getMessage(), e);
            return Mono.just(HttpResponse.serverError("Failed to store <" + id + "> in index <" + indexName + "> for file <" + fileName + ">"));
        }
    } else {
        return Mono.just(HttpResponse.serverError(ZuliaConstants.ID + " and " + ZuliaConstants.FILE_NAME + " are required"));
    }
}
Also used : OutputStream(java.io.OutputStream) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) IOException(java.io.IOException) Document(org.bson.Document) MultipartException(io.micronaut.http.multipart.MultipartException) IOException(java.io.IOException) Produces(io.micronaut.http.annotation.Produces) Post(io.micronaut.http.annotation.Post)

Example 9 with ZuliaIndexManager

use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.

the class AssociatedController method getAll.

@Get("/all")
@Produces(MediaType.APPLICATION_JSON)
public HttpResponse<?> getAll(@QueryValue(ZuliaConstants.INDEX) final String indexName, @Nullable @QueryValue(ZuliaConstants.QUERY) String query) {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    Writable writable = out -> {
        Document filter;
        if (query != null) {
            filter = Document.parse(query);
        } else {
            filter = new Document();
        }
        try {
            indexManager.getAssociatedFilenames(indexName, out, filter);
        } catch (Exception e) {
            LOG.log(Level.SEVERE, e.getMessage(), e);
            HttpResponse.serverError(e.getMessage());
        }
    };
    return HttpResponse.ok(writable).status(ZuliaConstants.SUCCESS);
}
Also used : Document(org.bson.Document) JsonObject(com.google.gson.JsonObject) StreamingFileUpload(io.micronaut.http.multipart.StreamingFileUpload) StreamedFile(io.micronaut.http.server.types.files.StreamedFile) Produces(io.micronaut.http.annotation.Produces) Level(java.util.logging.Level) TaskExecutors(io.micronaut.scheduling.TaskExecutors) PartData(io.micronaut.http.multipart.PartData) ByteArrayInputStream(java.io.ByteArrayInputStream) MediaType(io.micronaut.http.MediaType) Nullable(io.micronaut.core.annotation.Nullable) Map(java.util.Map) HttpResponse(io.micronaut.http.HttpResponse) Schedulers(reactor.core.scheduler.Schedulers) ZuliaNodeProvider(io.zulia.server.util.ZuliaNodeProvider) MultipartException(io.micronaut.http.multipart.MultipartException) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) Subscriber(org.reactivestreams.Subscriber) ExecutorService(java.util.concurrent.ExecutorService) Get(io.micronaut.http.annotation.Get) ZuliaConstants(io.zulia.ZuliaConstants) ZuliaBase(io.zulia.message.ZuliaBase) OutputStream(java.io.OutputStream) Controller(io.micronaut.http.annotation.Controller) Publisher(org.reactivestreams.Publisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) QueryValue(io.micronaut.http.annotation.QueryValue) Mono(reactor.core.publisher.Mono) IOException(java.io.IOException) Logger(java.util.logging.Logger) Post(io.micronaut.http.annotation.Post) Flux(reactor.core.publisher.Flux) JsonArray(com.google.gson.JsonArray) List(java.util.List) Writable(io.micronaut.core.io.Writable) Subscription(org.reactivestreams.Subscription) Inject(jakarta.inject.Inject) Named(jakarta.inject.Named) InputStream(java.io.InputStream) ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) Writable(io.micronaut.core.io.Writable) Document(org.bson.Document) MultipartException(io.micronaut.http.multipart.MultipartException) IOException(java.io.IOException) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Example 10 with ZuliaIndexManager

use of io.zulia.server.index.ZuliaIndexManager in project zuliasearch by zuliaio.

the class IndexesController method get.

@Get
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public HttpResponse<?> get() {
    ZuliaIndexManager indexManager = ZuliaNodeProvider.getZuliaNode().getIndexManager();
    try {
        GetIndexesResponse getIndexesResponse = indexManager.getIndexes(GetIndexesRequest.newBuilder().build());
        Document mongoDocument = new org.bson.Document();
        ProtocolStringList indexNameList = getIndexesResponse.getIndexNameList();
        List<String> sorted = new ArrayList<>(indexNameList);
        Collections.sort(sorted);
        mongoDocument.put("indexes", sorted);
        String docString = mongoDocument.toJson();
        docString = JsonWriter.formatJson(docString);
        return HttpResponse.ok(docString).status(ZuliaConstants.SUCCESS);
    } catch (Exception e) {
        return HttpResponse.serverError("Failed to get index names: " + e.getMessage()).status(ZuliaConstants.INTERNAL_ERROR);
    }
}
Also used : ZuliaIndexManager(io.zulia.server.index.ZuliaIndexManager) ArrayList(java.util.ArrayList) GetIndexesResponse(io.zulia.message.ZuliaServiceOuterClass.GetIndexesResponse) Document(org.bson.Document) ProtocolStringList(com.google.protobuf.ProtocolStringList) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get)

Aggregations

Produces (io.micronaut.http.annotation.Produces)11 ZuliaIndexManager (io.zulia.server.index.ZuliaIndexManager)11 Get (io.micronaut.http.annotation.Get)10 Document (org.bson.Document)8 MultipartException (io.micronaut.http.multipart.MultipartException)4 IOException (java.io.IOException)4 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 JsonFormat (com.google.protobuf.util.JsonFormat)2 Nullable (io.micronaut.core.annotation.Nullable)2 Writable (io.micronaut.core.io.Writable)2 HttpResponse (io.micronaut.http.HttpResponse)2 MediaType (io.micronaut.http.MediaType)2 Controller (io.micronaut.http.annotation.Controller)2 Post (io.micronaut.http.annotation.Post)2 QueryValue (io.micronaut.http.annotation.QueryValue)2 StreamedFile (io.micronaut.http.server.types.files.StreamedFile)2 ZuliaConstants (io.zulia.ZuliaConstants)2 ZuliaBase (io.zulia.message.ZuliaBase)2