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

com.neko233.toolchain.memoryDatabase.indexer.ColumnIndexer Maven / Gradle / Ivy

package com.neko233.toolchain.memoryDatabase.indexer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 列索引
 *
 * @author SolarisNeko
 */
public class ColumnIndexer {

    private final Map>> index = new HashMap<>();

    // 进行索引
    public void buildIndex(List> rows) {
        for (int i = 0; i < rows.size(); i++) {
            Map row = rows.get(i);
            for (Map.Entry entry : row.entrySet()) {
                String columnName = entry.getKey();
                Object value = entry.getValue();

                Map> valueMap = index.computeIfAbsent(columnName, k -> new HashMap<>());
                List rowList = valueMap.computeIfAbsent(value, k -> new ArrayList<>());
                rowList.add(i);
            }
        }
    }

    // 查询
    public List query(String columnName, Object value) {
        Map> valueMap = index.get(columnName);
        if (valueMap == null) {
            return new ArrayList<>();
        }

        List rowList = valueMap.get(value);
        if (rowList == null) {
            return new ArrayList<>();
        }

        return new ArrayList<>(rowList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy