mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-23 21:18:58 +00:00

Look, this will make the git history look funny, but trimming unnecessary depth from the source tree is a very necessary sanity-preserving measure when dealing with a super-modularized codebase like this one. While it makes the project configuration a bit less conventional, it will save you several clicks every time you jump between modules. Which you'll do a lot, because it's *modul*ar. The src/main/java convention makes a lot of sense for a non-modular project though. This ain't that.
62 lines
1.9 KiB
Java
62 lines
1.9 KiB
Java
package nu.marginalia.browse;
|
|
|
|
import com.google.inject.Inject;
|
|
import com.google.inject.Singleton;
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
import nu.marginalia.browse.model.BrowseResult;
|
|
import nu.marginalia.model.EdgeDomain;
|
|
import nu.marginalia.db.DomainBlacklist;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.sql.SQLException;
|
|
import java.util.*;
|
|
|
|
@Singleton
|
|
public class DbBrowseDomainsRandom {
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
private final HikariDataSource dataSource;
|
|
|
|
@Inject
|
|
public DbBrowseDomainsRandom(HikariDataSource dataSource) {
|
|
this.dataSource = dataSource;
|
|
}
|
|
|
|
public List<BrowseResult> getRandomDomains(int count, DomainBlacklist blacklist, int set) {
|
|
|
|
final String q = """
|
|
SELECT DOMAIN_ID, DOMAIN_NAME, INDEXED
|
|
FROM EC_RANDOM_DOMAINS
|
|
INNER JOIN EC_DOMAIN ON EC_DOMAIN.ID=DOMAIN_ID
|
|
WHERE STATE<2
|
|
AND DOMAIN_SET=?
|
|
AND DOMAIN_ALIAS IS NULL
|
|
ORDER BY RAND()
|
|
LIMIT ?
|
|
""";
|
|
List<BrowseResult> domains = new ArrayList<>(count);
|
|
try (var conn = dataSource.getConnection()) {
|
|
try (var stmt = conn.prepareStatement(q)) {
|
|
stmt.setInt(1, set);
|
|
stmt.setInt(2, count);
|
|
var rsp = stmt.executeQuery();
|
|
while (rsp.next()) {
|
|
int id = rsp.getInt(1);
|
|
String domain = rsp.getString(2);
|
|
boolean indexed = rsp.getBoolean("INDEXED");
|
|
|
|
if (!blacklist.isBlacklisted(id)) {
|
|
domains.add(new BrowseResult(new EdgeDomain(domain).toRootUrl(), id, 0, indexed));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (SQLException ex) {
|
|
logger.error("SQL error", ex);
|
|
}
|
|
return domains;
|
|
}
|
|
|
|
}
|