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

org.metaeffekt.artifact.resolver.rpm.index.RpmIndex Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * 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 org.metaeffekt.artifact.resolver.rpm.index;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class RpmIndex {

    private static final Logger LOG = LoggerFactory.getLogger(RpmIndex.class);

    public RpmIndex(RpmIndexConfig rpmIndexConfig) {
        if (rpmIndexConfig != null && rpmIndexConfig.getRawIndexPath() != null) {
            final File indexFile = new File(rpmIndexConfig.getRawIndexPath());
            init(indexFile);
        }
    }

    private void init(File indexFile) {
        if (indexFile.exists()) {
            buildInternalMaps(indexFile);
        } else {
            LOG.warn("Configured rawIndexPath " + indexFile.getAbsolutePath() + " does not exit.");
        }
    }

    public RpmIndex(File indexFile) {
        init(indexFile);

    }

    private Map> filenameToPackageDataMap = new HashMap<>();

    private Map> packageDataUrlMap = new HashMap<>();

    private void buildInternalMaps(File indexFile) {
        try (BufferedReader reader = new BufferedReader(new FileReader(indexFile))) {
            String url = reader.readLine();

            while (url != null) {

                // fixes a crawler issue (introduced multiple slashes)
                url = url.replaceAll("([^:])\\/\\/+", "$1/");

                if (url.endsWith(".rpm")) {
                    RpmIndexPackageData rpmPackageData = new RpmIndexPackageData(url);
                    filenameToPackageDataMap.computeIfAbsent(rpmPackageData.getName(), (a) -> new HashSet<>()).add(rpmPackageData);
                    final Set urls = packageDataUrlMap.computeIfAbsent(rpmPackageData, a -> new HashSet<>());
                    urls.add(url);
                }
                url = reader.readLine();
            }
        } catch (IOException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }

    }


    public Set queryPackageData(String rpmFilename) {
        return filenameToPackageDataMap.get(rpmFilename);
    }

    public Set getRpmUrl(RpmIndexPackageData rpmPackageData) {
        return packageDataUrlMap.get(rpmPackageData);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy