<commit_before>//============================================================================//
//                                                                            //
//                Copyright © 2015 - 2020 Subterranean Security               //
//                                                                            //
//  This source file is subject to the terms of the Mozilla Public License    //
//  version 2. You may not use this file except in compliance with the MPL    //
//  as published by the Mozilla Foundation at:                                //
//                                                                            //
//    https://mozilla.org/MPL/2.0                                             //
//                                                                            //
//=========================================================S A N D P O L I S==//
package com.sandpolis.server.vanilla.store.location;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.sandpolis.core.instance.store.StoreBase;
import com.sandpolis.core.proto.util.LocationOuterClass;
import com.sandpolis.core.util.ValidationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sandpolis.server.vanilla.store.location.LocationStore.LocationStoreConfig;

import java.time.Duration;
import java.util.concurrent.*;
import java.util.function.Consumer;

public class LocationStore extends StoreBase<LocationStoreConfig> {

	private static final Logger log = LoggerFactory.getLogger(LocationStore.class);

	private Cache<String, LocationOuterClass.Location> cache;

	private AbstractGeolocationService service;

	public LocationStore() {
		super(log);
	}

	public Future<LocationOuterClass.Location> queryAsync(String ip) {
		// Private IPs should not be resolved
		if (ValidationUtil.privateIP(ip)) {
			return CompletableFuture.completedFuture(null);
		}

		// Check cache
		synchronized (cache) {
			var location = cache.getIfPresent(ip);
			if (location != null)
				return CompletableFuture.completedFuture(location);
		}

		return service.query(ip, service.attrMap.keySet()).thenApply(location -> {
			if (location != null) {
				synchronized (cache) {
					cache.put(ip, location);
				}
			}
			return location;
		});
	}

	public LocationOuterClass.Location query(String ip, long timeout) {
		try {
			return queryAsync(ip).get(timeout, TimeUnit.MILLISECONDS);
		} catch (Exception e) {
			log.error("Failed to query location service", e);
			return null;
		}<commit_msg>[skip ci] Suppress TimeoutException in location query<commit_after>//============================================================================//
//                                                                            //
//                Copyright © 2015 - 2020 Subterranean Security               //
//                                                                            //
//  This source file is subject to the terms of the Mozilla Public License    //
//  version 2. You may not use this file except in compliance with the MPL    //
//  as published by the Mozilla Foundation at:                                //
//                                                                            //
//    https://mozilla.org/MPL/2.0                                             //
//                                                                            //
//=========================================================S A N D P O L I S==//
package com.sandpolis.server.vanilla.store.location;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.sandpolis.core.instance.store.StoreBase;
import com.sandpolis.core.proto.util.LocationOuterClass.Location;
import com.sandpolis.core.util.ValidationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sandpolis.server.vanilla.store.location.LocationStore.LocationStoreConfig;

import java.time.Duration;
import java.util.concurrent.*;
import java.util.function.Consumer;

public class LocationStore extends StoreBase<LocationStoreConfig> {

	private static final Logger log = LoggerFactory.getLogger(LocationStore.class);

	private Cache<String, Location> cache;

	private AbstractGeolocationService service;

	public LocationStore() {
		super(log);
	}

	public Future<Location> queryAsync(String ip) {
		// Private IPs should not be resolved
		if (ValidationUtil.privateIP(ip)) {
			return CompletableFuture.completedFuture(null);
		}

		// Check cache
		synchronized (cache) {
			var location = cache.getIfPresent(ip);
			if (location != null)
				return CompletableFuture.completedFuture(location);
		}

		return service.query(ip, service.attrMap.keySet()).thenApply(location -> {
			if (location != null) {
				synchronized (cache) {
					cache.put(ip, location);
				}
			}
			return location;
		});
	}

	public Location query(String ip, long timeout) {
		try {
			return queryAsync(ip).get(timeout, TimeUnit.MILLISECONDS);
		} catch (TimeoutException e) {
			log.error("Failed to query location service: timed out");
			return null;
		} catch (Exception e) {
			log.error("Failed to query location service", e);
			return null;
		}
{'fraction_non_alphanumeric': 0.1446613088404133, 'fraction_numerical': 0.004975124378109453, 'mean_word_length': 2.1224611708482675, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1700596', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1262, 'n_words': 328}