config and config builder is ready

This commit is contained in:
lionsoul2014 2025-11-20 15:40:54 +08:00
parent bd30b77d54
commit a6cb5161ff
3 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,70 @@
// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
package org.lionsoul.ip2region;
import java.io.IOException;
import org.lionsoul.ip2region.xdb.Header;
import org.lionsoul.ip2region.xdb.Searcher;
import org.lionsoul.ip2region.xdb.Version;
import org.lionsoul.ip2region.xdb.XdbException;
/**
* ip2region config class
* @Author Lion <chenxin619315@gmail.com>
* @Date 2025/11/20
*/
public class Config {
// cache policy consts
public static final int NoCache = 0;
public static final int VIndexCache = 1;
public static final int BufferCache = 2;
// search cache policy
public final int cachePolicy;
// xdb file path
public final String xdbPath;
public final Header header;
public final Version ipVersion;
// search limitation for NoCache or VIndexCache Only
public final int minSearchers;
public final int maxSearchers;
// config builder
public static ConfigBuilder custom() {
return new ConfigBuilder();
}
public Config(int cachePolicy, String xdbPath) throws IOException, XdbException {
this(cachePolicy, xdbPath, 10, 30);
}
public Config(int cachePolicy, String xdbPath, int minSearchers, int maxSearchers) throws IOException, XdbException {
this.cachePolicy = cachePolicy;
this.xdbPath = xdbPath;
// load the header from the xdb path
final Header header = Searcher.loadHeaderFromFile(xdbPath);
this.header = header;
this.ipVersion = Version.fromHeader(header);
this.minSearchers = minSearchers;
this.maxSearchers = maxSearchers;
}
@Override public String toString() {
final StringBuffer sb = new StringBuffer();
sb.append('{');
sb.append("cache_policy:").append(cachePolicy).append(',');
sb.append("xdb_path:").append(xdbPath).append(',');
sb.append("header:").append(header.toString()).append(',');
sb.append("version:").append(ipVersion.toString()).append(',');
sb.append("min_searchers:").append(minSearchers).append(',');
sb.append("max_searchers:").append(maxSearchers);
sb.append('}');
return sb.toString();
}
}

View File

@ -0,0 +1,60 @@
// Copyright 2022 The Ip2Region Authors. All rights reserved.
// Use of this source code is governed by a Apache2.0-style
// license that can be found in the LICENSE file.
package org.lionsoul.ip2region;
import java.io.IOException;
import org.lionsoul.ip2region.xdb.XdbException;
/**
* ip2region config builder
* @Author Lion <chenxin619315@gmail.com>
* @Date 2025/11/20
*/
public class ConfigBuilder {
// cache policy
private int cachePolicy = Config.VIndexCache;
// xdb file path
private String xdbPath = null;
// min searchers
private int minSearchers = 10;
// max searchers
private int maxSearchers = 30;
public ConfigBuilder() {}
public ConfigBuilder(String xdbPath) {
this.xdbPath = xdbPath;
}
public ConfigBuilder setCachePolicy(int cachePolicy) {
this.cachePolicy = cachePolicy;
return this;
}
public ConfigBuilder setXdbPath(String xdbPath) {
this.xdbPath = xdbPath;
return this;
}
public ConfigBuilder setMinSearchers(int minSearchers) {
this.minSearchers = minSearchers;
return this;
}
public ConfigBuilder setMaxSearchers(int maxSearchers) {
this.maxSearchers = maxSearchers;
return this;
}
// build the final #Config instance with the current config items
public Config build() throws IOException, XdbException {
return new Config(cachePolicy, xdbPath, minSearchers, maxSearchers);
}
}

View File

@ -0,0 +1,39 @@
package org.lionsoul.ip2region;
import java.io.IOException;
import java.security.CodeSource;
import org.junit.Test;
import org.lionsoul.ip2region.xdb.Log;
import org.lionsoul.ip2region.xdb.XdbException;
public class ConfigTest {
private static final Log log = Log.getLogger(ConfigTest.class).setLevel(Log.DEBUG);
public String getDataPath(String xdbFile) {
final CodeSource cs = this.getClass().getProtectionDomain().getCodeSource();
if (cs != null) {
// log.debugf("code path: %s", cs.getLocation().getPath().concat("../../../../data/"));
return cs.getLocation().getPath().concat("../../../../data/").concat(xdbFile);
} else {
return "../../../../data/".concat(xdbFile);
}
}
@Test
public void testConfig() throws IOException, XdbException {
final Config config = new Config(Config.VIndexCache, getDataPath("ip2region_v4.xdb"), 5, 10);
log.debugf("config: %s", config);
}
@Test
public void testBuildConfig() throws IOException, XdbException {
final Config config = Config.custom()
.setCachePolicy(Config.BufferCache)
.setXdbPath(getDataPath("ip2region_v6.xdb"))
.setMinSearchers(10)
.setMaxSearchers(30)
.build();
log.debugf("builded config: %s", config);
}
}