Java 内存数据库类对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19866064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:51:53  来源:igfitidea点击:

Java in-memory database-like object

javadatabasemapin-memory

提问by user1017413

I'm looking at a project that will benefit from an in-memory database-like object. I will have perhaps one or two thousand objects with the same structure, all inheriting from an abstract class. There will be a couple of string fields, an int, perhaps an enum or two (or maybe even a set of enums), and then a set of Strings. There would need to be a transient boolean field as well, but that likely wouldn't be a concern.

我正在研究一个将从类似内存数据库的对象中受益的项目。我可能会有一两千个具有相同结构的对象,它们都继承自一个抽象类。将有几个字符串字段,一个 int,可能是一两个枚举(甚至可能是一组枚举),然后是一组字符串。还需要一个瞬态布尔字段,但这可能不是问题。

These objects would be instantiated and constructed from preset data, although additional ones can be created beyond that if need be. They may be stored in an XML file or something similar. I'd rather not hardwire the entire thing, of course, and using a local database like SQLite feels like overkill.

这些对象将根据预设数据进行实例化和构造,但如果需要,可以在此之外创建其他对象。它们可能存储在 XML 文件或类似文件中。当然,我宁愿不硬连线整个事情,而且使用像 SQLite 这样的本地数据库感觉有点矫枉过正。

Storing these objects would be relatively simple if not for one thing: I want the user to easily be able to find the object they want from ANY of the values, most of which would be unique. This rules out a HashMap unless I want to wrap a massive bunch of them, which is hardly ideal. That leaves me looking for a sort of indexed, in-memory database-like object that supports retrieval via any field of the object. It may not have to store the objects directly, but could assemble them upon retrieval, or retrieve a "row" based on one field, get another field from the same "row" that serves as a sort of key, and then retrieve the object from a single HashMap based on that key.

如果不是为了一件事,存储这些对象将相对简单:我希望用户能够轻松地从任何值中找到他们想要的对象,其中大多数是唯一的。这排除了 HashMap 除非我想包装一大堆它们,这并不理想。这让我需要寻找一种支持通过对象的任何字段进行检索的索引的、类似内存中的数据库对象。它可能不必直接存储对象,但可以在检索时组合它们,或者根据一个字段检索“行”,从同一“行”中获取另一个字段作为一种键,然后检索对象来自基于该键的单个 HashMap。

In short, the idea is to easily and quickly retrieve objects with the same fields based on any field they contain. I've seen a variety of different libraries and such that may do this sort of thing, but there's a real myriad of these things out there. Whatever may work would need to be free and compatible with a variety of open licenses.

简而言之,其想法是根据对象包含的任何字段轻松快速地检索具有相同字段的对象。我见过各种不同的库,它们可能会做这种事情,但是那里有无数这样的东西。任何可能的工作都需要是免费的,并且与各种开放许可证兼容。

回答by scottb

For an "in memory database-like object" please don't re-invent the wheel. However simple or complex your needs are, using a library that has already been through years (or decades) of debugging and optimization is absolutely the right thing to do if it can be made to suit your needs.

对于“类似内存数据库的对象”,请不要重新发明轮子。无论您的需求是简单还是复杂,如果可以满足您的需求,使用已经经过数年(或数十年)调试和优化的库绝对是正确的做法。

SQLite is a fine choice. It is not Java, however, and requires a separate JDBC driver (no JDBC driver for SQLite is maintained by the SQlite project).

SQLite 是一个不错的选择。然而,它不是 Java,并且需要单独的 JDBC 驱动程序(SQlite 项目没有维护 SQLite 的 JDBC 驱动程序)。

Another RDBMS which is small footprint, reasonably lightweight, and robust is HSQLDB which includes a version 4.1 JDBC driver as part of the project. It is 100% Java. It provides native support for in memory tables. Any time I use the word "database" and "thousands of objects", my thoughts immediately go to HSQLDB.

另一个占用空间小、相当轻量且健壮的 RDBMS 是 HSQLDB,它包含一个 4.1 版 JDBC 驱动程序作为项目的一部分。它是 100% 的 Java。它为内存表提供本机支持。每当我使用“数据库”和“数千个对象”这个词时,我的想法都会立即转到 HSQLDB。

The project is managed by the HSQLDB development group and is available here: http://www.hsqldb.org

该项目由 HSQLDB 开发组管理,可在此处获得:http: //www.hsqldb.org

回答by satishns

I suggest going with the Caching solutions provided by Guava framework https://code.google.com/p/guava-libraries/wiki/CachesExplained

我建议使用 Guava 框架提供的缓存解决方案 https://code.google.com/p/guava-libraries/wiki/CachesExplained

回答by Doua Beri

A little late but you could use: http://www.mapdb.org

有点晚了,但你可以使用:http: //www.mapdb.org

From their website:

从他们的网站:

MapDB is an embedded database engine for Java. It provides Maps and other collections backed by disk or off-heap memory storage. MapDB is free under Apache License.

MapDB 是 Java 的嵌入式数据库引擎。它提供由磁盘或堆外内存存储支持的 Maps 和其他集合。MapDB 在 Apache 许可下是免费的。