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

io.github.honhimw.ms.internal.reactive.ReactiveDocumentsImpl Maven / Gradle / Ivy

There is a newer version: 1.11.0.0
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.honhimw.ms.internal.reactive;

import io.github.honhimw.ms.api.reactive.ReactiveDocuments;
import io.github.honhimw.ms.json.ComplexTypeRef;
import io.github.honhimw.ms.json.TypeRef;
import io.github.honhimw.ms.model.*;
import io.github.honhimw.ms.support.CollectionUtils;
import io.github.honhimw.ms.support.StringUtils;
import io.github.honhimw.ms.support.TypeRefs;
import jakarta.annotation.Nullable;
import reactor.core.publisher.Mono;

import java.util.*;

/**
 * @author hon_him
 * @since 2024-01-04
 */

class ReactiveDocumentsImpl extends AbstractReactiveImpl implements ReactiveDocuments {

    private final ReactiveIndexesImpl indexes;
    private final String indexUid;
    private final String _path;

    protected ReactiveDocumentsImpl(ReactiveIndexesImpl indexes, String indexUid) {
        super(indexes._client);
        this.indexes = indexes;
        this.indexUid = indexUid;
        this._path = String.format("/indexes/%s/documents", indexUid);
    }

    @Override
    public Mono>> list(GetDocumentRequest page) {
        return list(page, TypeRefs.StringObjectMapRef.INSTANCE);
    }

    @Override
    public  Mono> list(@Nullable Integer offset, @Nullable Integer limit, TypeRef typeRef) {
        String _offset = Optional.ofNullable(offset).map(String::valueOf).orElse("0");
        String _limit = Optional.ofNullable(limit).map(String::valueOf).orElse("20");
        return get(_path, configurer -> configurer
                .param("offset", _offset)
                .param("limit", _limit),
            new ComplexTypeRef>(typeRef) {
            });
    }

    @Override
    public  Mono> list(GetDocumentRequest page, TypeRef typeRef) {
        return get(_path, configurer -> {
            List fields = page.getFields();
            if (CollectionUtils.isNotEmpty(fields)) {
                configurer.param("fields", String.join(",", fields));
            }
            String filter = page.getFilter();
            if (StringUtils.isNotEmpty(filter)) {
                configurer.param("filter", filter);
            }
            configurer
                .param("offset", String.valueOf(page.toOffset()))
                .param("limit", String.valueOf(page.toLimit()));
        }, new ComplexTypeRef>(typeRef) {
        });
    }

    @Override
    public Mono save(String json) {
        return post(_path, configurer -> json(configurer, json), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono save(Collection collection) {
        return post(_path, configurer -> json(configurer, collection), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono saveVectorized(Collection collection) {
        return post(_path, configurer -> json(configurer, collection), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono update(String json) {
        return put(_path, configurer -> json(configurer, json), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono update(Collection collection) {
        return put(_path, configurer -> json(configurer, collection), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono edit(EditRequest edit) {
        return post(String.format("%s/edit", _path), configurer -> json(configurer, edit), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono deleteAll() {
        return delete(_path, TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono>> batchGet(BatchGetDocumentsRequest fetch) {
        return post(String.format("%s/fetch", _path), configurer -> json(configurer, fetch), TypeRefs.PageStringObjectMapRef.INSTANCE);
    }

    @Override
    public  Mono> batchGet(BatchGetDocumentsRequest fetch, TypeRef typeRef) {
        return post(String.format("%s/fetch", _path), configurer -> json(configurer, fetch),
            new ComplexTypeRef>(typeRef) {
            });
    }

    @Override
    public Mono batchDelete(List ids) {
        return post(String.format("%s/delete-batch", _path), configurer -> json(configurer, ids), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono delete(FilterableAttributesRequest filter) {
        return post(String.format("%s/delete", _path), configurer -> json(configurer, filter), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono> get(String id, @Nullable String... fields) {
        return get(id, TypeRefs.StringObjectMapRef.INSTANCE, fields);
    }

    @Override
    public  Mono get(String id, TypeRef typeRef, @Nullable String... fields) {
        String _fields;
        if (Objects.isNull(fields) || fields.length == 0) {
            _fields = "*";
        } else {
            _fields = String.join(",", fields);
        }
        return get(String.format("%s/%s", _path, id), configurer -> configurer
            .param("fields", _fields), typeRef);
    }

    @Override
    public Mono delete(String id) {
        return delete(String.format("%s/%s", _path, id), TypeRefs.TaskInfoRef.INSTANCE);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy