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

com.infomaximum.database.utils.IndexUtils Maven / Gradle / Ivy

The newest version!
package com.infomaximum.database.utils;

import com.infomaximum.database.exception.SchemaException;
import com.infomaximum.database.schema.Field;
import com.infomaximum.database.schema.StructEntity;
import com.infomaximum.database.schema.dbstruct.DBField;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class IndexUtils {

    public static List buildIndexedFields(int[] indexedFields, StructEntity parent) {
        return Arrays.stream(indexedFields)
                .mapToObj(parent::getField)
                .sorted(Comparator.comparing(f -> f.getName().toLowerCase())) //Сортируем, что бы хеш не ломался из-за перестановки местами полей
                .collect(Collectors.toList());
    }

    public static byte[] buildFieldsHashCRC32(List indexedFields) {
        StringBuilder stringBuilder = new StringBuilder();
        indexedFields.forEach(field -> stringBuilder.append(field.getName()).append(':').append(field.getType().getName()).append('.'));
        return TypeConvert.packCRC32(stringBuilder.toString());
    }

    public static byte[] buildFieldsHashCRC32(DBField[] indexedFields) {
        StringBuilder stringBuilder = new StringBuilder();
        Arrays.stream(indexedFields).forEach(field -> stringBuilder.append(field.getName()).append(':').append(field.getType().getName()).append('.'));
        return TypeConvert.packCRC32(stringBuilder.toString());
    }

    public static DBField[] getFieldsByIds(List tableFields, int... destinationFields) {
        DBField[] result = new DBField[destinationFields.length];
        for (int i = 0; i < destinationFields.length; i++) {
            int destFieldId = destinationFields[i];
            result[i] = getFieldsByIds(tableFields, destFieldId);
        }
        return result;
    }

    public static DBField getFieldsByIds(List tableFields, int destinationField) {
        return tableFields.stream()
                .filter(field -> destinationField == field.getId())
                .findAny()
                .orElseThrow(() -> new SchemaException("Destination field id=" + destinationField + " doesn't found in table"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy