<commit_before>package org.zeropage.wiki_path_find;

import org.zeropage.CacheLinkSource;
import org.zeropage.LinkSource;
import org.zeropage.MemCacheStorage;
import org.zeropage.PathFinder;
import org.zeropage.log.Logger;
import org.zeropage.path.RedirectableNode;
import org.zeropage.path.RedirectablePath;
import org.zeropage.wiki_api.RedirectedException;
import org.zeropage.wiki_api.WikipediaApi;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class WikipediaPathFinder implements PathFinder {
    private LinkSource linkSource;
    private LinkSource backlinkSource;
    private WikipediaApi api;

    private Logger logger;

    public WikipediaPathFinder(WikipediaApi api) {
        this.api = api;
        linkSource = new CacheLinkSource(new WikipediaLinkSource(api), new MemCacheStorage());
        backlinkSource = new CacheLinkSource(new WikipediaBacklinkSource(api), new MemCacheStorage());

        logger = Logger.getInstance();
    }

    @Override
    public RedirectablePath getPath(String from, String to) throws Exception {
        ConcurrentMap<String, String> frontParents = new ConcurrentHashMap<>();
        ConcurrentMap<String, String> backParents = new ConcurrentHashMap<>();
        Queue<String> frontNextQueue = new LinkedList<>();
        Queue<String> backNextQueue = new LinkedList<>();
<commit_msg>perf(WikipediaPathFinder): link sources could be cached by sqlite<commit_after>package org.zeropage.wiki_path_find;

import org.zeropage.*;
import org.zeropage.log.Logger;
import org.zeropage.path.RedirectableNode;
import org.zeropage.path.RedirectablePath;
import org.zeropage.wiki_api.RedirectedException;
import org.zeropage.wiki_api.WikipediaApi;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class WikipediaPathFinder implements PathFinder {
    public static final String FRONTLINK_CACHE_NAME = "front.db";
    public static final String BACKLINK_CACHE_NAME = "back.db";

    private LinkSource linkSource;
    private LinkSource backlinkSource;
    private WikipediaApi api;

    private Logger logger;

    public WikipediaPathFinder(WikipediaApi api) throws SQLException, ClassNotFoundException {
        this(api, null);
    }

    public WikipediaPathFinder(WikipediaApi api, File cacheDir) throws SQLException, ClassNotFoundException {
        this.api = api;
        linkSource = new WikipediaLinkSource(api);
        backlinkSource = new WikipediaBacklinkSource(api);

        if (cacheDir != null) {
            SqliteCacheStorage sqlite = new SqliteCacheStorage(new File(cacheDir, FRONTLINK_CACHE_NAME));
            linkSource = new CacheLinkSource(linkSource, sqlite);

            sqlite = new SqliteCacheStorage(new File(cacheDir, BACKLINK_CACHE_NAME));
            backlinkSource = new CacheLinkSource(backlinkSource, sqlite);
        }

        linkSource = new CacheLinkSource(linkSource, new MemCacheStorage());
        backlinkSource = new CacheLinkSource(backlinkSource, new MemCacheStorage());

        logger = Logger.getInstance();
    }

    @Override
    public RedirectablePath getPath(String from, String to) throws Exception {
        ConcurrentMap<String, String> frontParents = new ConcurrentHashMap<>();
        ConcurrentMap<String, String> backParents = new ConcurrentHashMap<>();
        Queue<String> frontNextQueue = new LinkedList<>();
        Queue<String> backNextQueue = new LinkedList<>();

{'fraction_non_alphanumeric': 0.08558807288790723, 'fraction_numerical': 0.0, 'mean_word_length': 4.44812030075188, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2561362', 'n_tokens_mistral': 994, 'n_tokens_neox': 979, 'n_words': 238}