MapDB是一个快速、易用的嵌入式Java数据库引擎,它提供了基于磁盘或者堆外(off-heap允许Java直接操作内存空间, 类似于C的malloc和free)存储的并发的Maps、Sets、Queues。
业务场景:
朋友公司需要根据坐标,在200m的地址库中寻找离该坐标最近的经纬度坐标,难点主要有以下两个:
1.快速把坐标落点到二维的平面上区域,假设(-1,-1),应该落点到xy二维的左下方,这里我采用KDTree的方式
2.因为考虑到tree构建成功后,不想每次都重新构建树,那就需要把树缓存起来,但是通过redis等分布式的cache觉得网络带宽是瓶颈,而且我们的地址库可能会频繁更新,如果用jvm等map的缓存,内存马上就被爆仓了,后来转用MapDB发现它提供多种缓存方式,而且对比后,不管速率以及占用空间都相对较小
3.计算点点之间的距离,在二维平面上其实并不难,通过向量,计算sin、cos等常用手段,马上计算所得结果
Spring中但配置
<bean id="dbFile" class="java.io.File"> <constructor-arg value="/usr/local/DB/monitor.DB"></constructor-arg> </bean> <bean id="dbFactory" class="org.mapdb.DBMaker" factory-method="newFileDB"> <constructor-arg ref="dbFile" /> </bean> <bean id="shutdownHook" factory-bean="dbFactory" factory-method="closeOnJvmShutdown"> </bean> <bean id="database" factory-bean="dbFactory" factory-method="make"> </bean>
Spring应用启动时加载
public class StartupListener implements ServletContextListener { private static final Logger LOG = LoggerFactory.getLogger(StartupListener.class); @Override public void contextInitialized(ServletContextEvent e) { ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(e.getServletContext()); // AddressInfoMapper addressInfoMapper = (AddressInfoMapper)ctx.getBean("addressInfoMapper"); DB db = (DB) ctx.getBean("database"); BTreeMap<String, String> monitorDataMap = db.getTreeMap("monitorDataMap"); // monitorDataMap.put("name", "Young"); //you can load address information to mapdb db.commit(); if (ctx == null) { LOG.error("app start fail!", e); throw new RuntimeException("WebApplicationContextUtils.getWebApplicationContext() Fail!"); } LOG.info("app start success."); } @Override public void contextDestroyed(ServletContextEvent sce) { } }
Service中使用
// Injected database the map are obtained from it. private DB database; private BTreeMap<String, String> monitorDataMap; public void setDatabase(DB database) { this.database = database; } @PostConstruct public void init() throws Exception { this.monitorDataMap = database.getTreeMap("monitorDataMap"); }
KDTree构建
public class KDTree { // prevent instantiation private KDTree() {} private KDTreeNode root; public static KDTree build(List<? extends Point> points) { KDTree tree = new KDTree(); tree.root = build(points, 0); return tree; } private static KDTreeNode build(List<? extends Point> points, int depth) { if (points.isEmpty()) return null; final int axis = depth % 2; Collections.sort(points, new Comparator<Point>() { public int compare(Point p1, Point p2) { double coord1 = p1.getCoords()[axis]; double coord2 = p2.getCoords()[axis]; return Double.compare(coord1, coord2); } }); int index = points.size() / 2; KDTreeNode leftChild = build(points.subList(0, index), depth + 1); KDTreeNode rightChild = build(points.subList(index + 1, points.size()), depth + 1); Point point = points.get(index); return new KDTreeNode(point, axis, leftChild, rightChild); } @SuppressWarnings({"unchecked"}) public <T extends Point> T findNearest(Point point) { return (T) findNearest(point, 1).get(0); } public List<? extends Point> findNearest(Point point, int amount) { return root.findNearest(point, amount); } @SuppressWarnings({"unchecked"}) public <T extends Point> T getRootPoint() { return (T) root.getPoint(); } }
个人结论:
在使用mapdb的使用后,本人并未去深入了解mapdb的底层原理,只是应急使用,后续肯定会有很多bug显现,但是在使用其框架后,确实性能不少,3-5ms内就能够很容易的找到点之间最近关联的,内存损耗40多m左右。
相关推荐
mapdb
赠送jar包:mapdb-3.0.5.jar; 赠送原API文档:mapdb-3.0.5-javadoc.jar; 赠送源代码:mapdb-3.0.5-sources.jar; 赠送Maven依赖信息文件:mapdb-3.0.5.pom; 包含翻译后的API文档:mapdb-3.0.5-javadoc-API文档-...
mapdb-1.0.8.jar 免费下载 mapdb
文本文档的使用对象是MapDB初学者,文档内容包括:MapDB简介、环境搭建和JavaAPI操作,欢迎下载使用。
MapDB: database engine MapDB combines embedded database engine and Java collections. It is free under Apache 2 license. MapDB is flexible and can be used in many roles: Drop-in replacement for ...
MapDB操作手册
MapDB,效率非常高,这是到上传时最新的源代码
MapDB is an open-source (Apache 2.0 licensed), embedded Java database engine and collection framework. It provides Maps, Sets, Lists, Queues, Bitmaps with range queries, expiration, compression, off-...
持久化存储的 Map MapDB ,MapDB是一个快速、易用的嵌入式Java数据库引擎,它提供了基于磁盘或者堆外(off-heap允许...
MapDB是灵活的,可以在许多角色中使用: 可以直接替换地图,列表,队列和其他集合。 不受垃圾收集器影响的堆外收集 具有到期和磁盘溢出的多级缓存。 用事务,MVCC,增量备份等替换RDBM ... 本地数据处理和过滤...
赠送jar包:mapdb-3.0.5.jar; 赠送原API文档:mapdb-3.0.5-javadoc.jar; 赠送源代码:mapdb-3.0.5-sources.jar; 赠送Maven依赖信息文件:mapdb-3.0.5.pom; 包含翻译后的API文档:mapdb-3.0.5-javadoc-API文档-...
MAPB结合了嵌入式数据库引擎和Java集合。
wcm-io-sling.zip,用于Sling Models的AEM对象注入器。用于Apache Sling功能的wcm.io扩展。
Tinkerpop 蓝图图形... MapDB 使用预写日志或仅追加存储来实现出色的写入持久性。 灵活 - MapDB 可用于从内存缓存到多 TB 数据库的任何地方。 它还具有许多选项,可以用持久性来换取写入性能。 这使得配置 MapDB 以
mapdb.sql
mapdb 由Kaldonis主持的mapdb主要受到善意的地图编辑人员的感动。 2021年4月3日-各种作品 更新了Cysaegir图形,新的Stables房间,超节点标签,固定的RP条目。 更新了Halcyon Hills图形,添加了新房间 所有丢失的信...
MapDB / BerkeleyDB数据库和Spring REST接口 该项目的目标是创建一个可移动的存储设备,用于移动多个文件,从而为使用REST接口的最轻量的数据库提供支持。 掌握 开发 支持的数据库 地图数据库 伯克利数据库 用法 ...
赫兹 该项目为HZ地图提供堆外存储。 这是非官方的扩展和Hazelcast公司... 在com.hazelcast.map.impl.AbstractRecordStore类中,它使用MapDB集合替换地图records值: private final ConcurrentMap< Data , Record
该项目旨在成为由 mapdb 支持的 redis 的替代品 服务器/解析是从这个项目分叉出来的: : 后端数据库: : 编译 ./gradlew fatJar 跑步 java -jar build/libs/redis-mapdb-all-1.0.jar -threads 5 选项 Usage: ...
!!!过时的!! Unsafe 现在是 MapDB 2.0 的一部分。 使用 DBMaker.newMemoryUnsafeDB().make()