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

io.evitadb.utils.MemoryMeasuringConstants Maven / Gradle / Ivy

There is a newer version: 2024.9.3
Show newest version
/*
 *
 *                         _ _        ____  ____
 *               _____   _(_) |_ __ _|  _ \| __ )
 *              / _ \ \ / / | __/ _` | | | |  _ \
 *             |  __/\ V /| | || (_| | |_| | |_) |
 *              \___| \_/ |_|\__\__,_|____/|____/
 *
 *   Copyright (c) 2023
 *
 *   Licensed under the Business Source License, Version 1.1 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   https://github.com/FgForrest/evitaDB/blob/main/LICENSE
 *
 *   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.evitadb.utils;

import io.evitadb.dataType.EvitaDataTypes;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * This interface contains usual memory sizes (in Bytes) for base Java types. The data were taken from
 * StackExchange
 * post and are valid for 64-bit architecture.
 *
 * @author Jan Novotný ([email protected]), FG Forrest a.s. (c) 2022
 */
public interface MemoryMeasuringConstants {
	int OBJECT_HEADER_SIZE = 16;
	int REFERENCE_SIZE = 8;
	int ARRAY_BASE_SIZE = OBJECT_HEADER_SIZE + 8;
	int BYTE_SIZE = 1;
	int CHAR_SIZE = 2;
	int SMALL_SIZE = 2;
	int INT_SIZE = 4;
	int LONG_SIZE = 8;
	/**
	 * Source: with estimate for a large magnitude.
	 */
	int BIG_INTEGER = 20 + OBJECT_HEADER_SIZE + 4;
	int BIG_DECIMAL_SIZE = 16 + BIG_INTEGER;
	/**
	 * Source: of the data
	 */
	int LOCAL_DATE_TIME_SIZE = 48;
	int LOCAL_DATE_SIZE = 24;
	int LOCAL_TIME_SIZE = 24;

	/**
	 * Source of computational logic.
	 */
	static int computeStringSize(@Nonnull String string) {
		return 8 * ((((string.length()) * 2) + 45) / 8);
	}

	/**
	 * Source: for the base size
	 * and for element size.
	 */
	static int computeLinkedListSize(@Nonnull List list) {
		return 48 + 24 * list.size() +
			list.stream()
				.mapToInt(EvitaDataTypes::estimateSize)
				.sum();
	}

	/**
	 * Computes size of the array.
	 */
	static int computeArraySize(@Nonnull Serializable[] array) {
		return MemoryMeasuringConstants.ARRAY_BASE_SIZE + array.length * MemoryMeasuringConstants.REFERENCE_SIZE +
			Arrays.stream(array)
				.mapToInt(EvitaDataTypes::estimateSize)
				.sum();
	}

	/**
	 * Computes size of the array.
	 */
	static int computeArraySize(@Nonnull int[] array) {
		return MemoryMeasuringConstants.ARRAY_BASE_SIZE + array.length * MemoryMeasuringConstants.REFERENCE_SIZE +
			array.length * MemoryMeasuringConstants.INT_SIZE;
	}

	/**
	 * Source: for the base size
	 * and for element size.
	 */
	static int computeHashMapSize(@Nonnull Map map) {
		return 128 + 32 * map.size() +
			map.entrySet()
				.stream()
				.mapToInt(it -> EvitaDataTypes.estimateSize(it.getKey()) + EvitaDataTypes.estimateSize(it.getValue()))
				.sum();
	}

	/**
	 * Returns estimated size per component.
	 */
	static int getElementSize(Class componentType) {
		if (byte.class.isInstance(componentType)) {
			return BYTE_SIZE;
		} else if (short.class.isInstance(componentType)) {
			return SMALL_SIZE;
		} else if (int.class.isInstance(componentType)) {
			return INT_SIZE;
		} else if (long.class.isInstance(componentType)) {
			return LONG_SIZE;
		} else if (char.class.isInstance(componentType)) {
			return CHAR_SIZE;
		} else {
			return REFERENCE_SIZE;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy