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

io.github.honhimw.ms.internal.reactive.ReactiveIndexesImpl 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.
 */

/*
 * 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.*;
import io.github.honhimw.ms.json.TypeRef;
import io.github.honhimw.ms.model.*;
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-03
 */

class ReactiveIndexesImpl extends AbstractReactiveImpl implements ReactiveIndexes {

    protected ReactiveIndexesImpl(ReactiveMSearchClientImpl client) {
        super(client);
    }

    @Override
    public Mono> list(@Nullable Integer offset, @Nullable Integer limit) {
        String _offset = Optional.ofNullable(offset).map(String::valueOf).orElse("0");
        String _limit = Optional.ofNullable(limit).map(String::valueOf).orElse("20");
        return get("/indexes", configurer -> configurer
                .param("offset", _offset)
                .param("limit", _limit),
            TypeRefs.PageIndexRef.INSTANCE);
    }

    @Override
    public Mono get(String uid) {
        return get(String.format("/indexes/%s", uid), configurer -> {
        }, TypeRefs.of(Index.class));
    }

    @Override
    public Mono create(String uid, @Nullable String primaryKey) {
        return post("/indexes", configurer -> configurer
            .body(payload -> payload
                .raw(raw -> {
                    Map obj = new HashMap<>();
                    obj.put("uid", uid);
                    Optional.ofNullable(primaryKey)
                        .ifPresent(pk -> obj.put("primaryKey", pk));
                    raw.json(jsonHandler.toJson(obj));
                })), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono update(String uid, String primaryKey) {
        return patch(String.format("/indexes/%s", uid), configurer -> configurer
            .body(payload -> payload
                .raw(raw -> {
                    Map obj = new HashMap<>();
                    obj.put("primaryKey", primaryKey);
                    raw.json(jsonHandler.toJson(obj));
                })), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono delete(String uid) {
        return delete(String.format("/indexes/%s", uid), configurer -> {
        }, TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public Mono swap(List> uids) {
        return post("/swap-indexes", configurer -> configurer
            .body(payload -> payload.raw(raw -> {
                List>> list = new ArrayList<>();
                for (Map.Entry uid : uids) {
                    Map> swap = new HashMap<>();
                    List indexes = new ArrayList<>(2);
                    indexes.add(uid.getKey());
                    indexes.add(uid.getValue());
                    swap.put("indexes", indexes);
                    list.add(swap);
                }
                raw.json(jsonHandler.toJson(list));
            })), TypeRefs.TaskInfoRef.INSTANCE);
    }

    @Override
    public ReactiveSingleIndex single(String uid) {
        return new ReactiveSingleIndexImpl(uid, this);
    }

    @Override
    public ReactiveDocuments documents(String uid) {
        return new ReactiveDocumentsImpl(this, uid);
    }

    @Override
    public  ReactiveTypedDocuments documents(String uid, TypeRef typeRef) {
        return new ReactiveTypedDocumentsImpl<>(this, uid, typeRef);
    }

    @Override
    public ReactiveSearch search(String uid) {
        return new ReactiveSearchImpl(this, uid);
    }

    @Override
    public  ReactiveTypedSearch search(String uid, TypeRef typeRef) {
        return new ReactiveTypedSearchImpl<>(this, uid, typeRef);
    }

    @Override
    public  ReactiveTypedDetailsSearch searchWithDetails(String uid, TypeRef typeRef) {
        return new ReactiveTypedDetailsSearchImpl<>(this, uid, typeRef);
    }

    @Override
    public ReactiveSettings settings(String uid) {
        return new ReactiveSettingsImpl(this, uid);
    }

    @Override
    public Mono stats() {
        return get("/stats", TypeRefs.of(Stats.class));
    }

    @Override
    public Mono stats(String uid) {
        return get(String.format("/indexes/%s/stats", uid), TypeRefs.of(IndexStats.class));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy