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

io.github.mfvanek.pg.model.index.utils.DuplicatedIndexesParser Maven / Gradle / Ivy

Go to download

pg-index-health-model is a set of common classes and interfaces for getting information about PostgreSQL database objects.

There is a newer version: 0.13.0
Show newest version
/*
 * Copyright (c) 2019-2024. Ivan Vakhrushev and others.
 * https://github.com/mfvanek/pg-index-health
 *
 * This file is a part of "pg-index-health" - a Java library for
 * analyzing and maintaining indexes health in PostgreSQL databases.
 *
 * Licensed under the Apache License 2.0
 */

package io.github.mfvanek.pg.model.index.utils;

import io.github.mfvanek.pg.model.DbObject;
import io.github.mfvanek.pg.model.table.TableNameAware;
import io.github.mfvanek.pg.model.validation.Validators;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

public final class DuplicatedIndexesParser {

    private DuplicatedIndexesParser() {
        throw new UnsupportedOperationException();
    }

    public static List> parseAsIndexNameAndSize(@Nonnull final String duplicatedAsString) {
        Validators.notBlank(duplicatedAsString, "duplicatedAsString");
        final String[] indexes = duplicatedAsString.split("; ");
        return Arrays.stream(indexes)
            .map(s -> s.split(","))
            .filter(a -> a[0].trim().startsWith("idx=") && a[1].trim().startsWith("size="))
            .map(a -> {
                final String indexName = a[0].trim().substring("idx=".length());
                final String sizeAsString = a[1].trim().substring("size=".length());
                return Map.entry(indexName, Long.valueOf(sizeAsString));
            })
            .collect(Collectors.toUnmodifiableList());
    }

    /**
     * Combines given database objects into list.
     *
     * @param firstObject  first database object; should be non-null.
     * @param secondObject second database object; should be non-null.
     * @param otherObjects other database objects.
     * @param           the type of the list elements
     * @return combined list of database objects
     */
    @SafeVarargs
    @Nonnull
    public static  List combine(@Nonnull final T firstObject,
                                                                        @Nonnull final T secondObject,
                                                                        @Nonnull final T... otherObjects) {
        Objects.requireNonNull(firstObject, "firstObject cannot be null");
        Objects.requireNonNull(secondObject, "secondObject cannot be null");
        if (Stream.of(otherObjects).anyMatch(Objects::isNull)) {
            throw new IllegalArgumentException("otherObjects cannot contain nulls");
        }
        final Stream basePart = Stream.of(firstObject, secondObject);
        return Stream.concat(basePart, Stream.of(otherObjects))
            .collect(Collectors.toUnmodifiableList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy