简体   繁体   English

文件支持的Java地图

[英]File backed Java map

Is there a simple way of having a file backed Map? 是否有一种简单的方法来备份文件?

The contents of the map are updated regularly, with some being deleted, as well as some being added. 地图的内容会定期更新,其中一些内容会被删除,还会添加一些内容。 To keep the data that is in the map safe, persistence is needed. 为了保证地图中的数据安全,需要持久性。 I understand a database would be ideal, but sadly due to constraints a database can't be used. 我理解数据库是理想的,但遗憾的是由于数据库无法使用数据库。

I have tried: 我试过了:

Writing the whole contents of the map to file each time it gets updated. 每次更新时都将地图的全部内容写入文件。 This worked, but obviously has the drawback that the whole file is rewritten each time, the contents of the map are expected to be anywhere from a couple of entries to ~2000. 这有效,但显然有一个缺点,即每次重写整个文件,地图的内容预计会从几个条目到〜2000。 There are also some concurrency issues (ie writing out of order results in loss of data). 还存在一些并发问题(即,无序写入会导致数据丢失)。

Using a RandomAccessFile and keeping a pointer to each file's start byte so that each entry can be looked up using seek(). 使用RandomAccessFile并保持指向每个文件的起始字节的指针,以便可以使用seek()查找每个条目。 Again this had a similar issue as before, changing an entry would involve updating all of the references after it. 同样,这个问题与以前类似,更改条目将涉及更新其后的所有引用。

Ideally, the solution would involve some sort of caching, so that only the most recently accessed entries are kept in memory. 理想情况下,解决方案将涉及某种缓存,因此只有最近访问的条目才会保留在内存中。

Is there such a thing? 有这样的事吗? Or is it available via a third party jar? 或者可以通过第三方罐子获得? Someone suggested Oracle Coherence, but I can't seem to find much on how to implement that, and it seems a bit like using a sledgehammer to crack a nut. 有人建议使用Oracle Coherence,但我似乎无法找到如何实现这一点,而且似乎有点像使用大锤来破解坚果。

You could look into MapDB which was created with this purpose in mind. 您可以查看为此目的而创建的MapDB

MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap-memory. MapDB提供由磁盘存储或堆外内存支持的并发映射,集和队列。 It is a fast and easy to use embedded Java database engine. 它是一种快速且易于使用的嵌入式Java数据库引擎。

jdbm2 looks promising, never used it but it seems to be a candidate to meet your requirements: jdbm2看起来很有前途,从未使用它,但似乎是满足您要求的候选者:

JDBM2 provides HashMap and TreeMap which are backed by disk storage. JDBM2提供由磁盘存储支持的HashMap和TreeMap。 It is very easy and fast way to persist your data. 保存数据非常简单快捷。 JDBM2 also have minimal hardware requirements and is highly embeddable (jar have only 145 KB). JDBM2也具有最低的硬件要求,并且具有高度可嵌入性(jar只有145 KB)。

You'll find many more solutions if you look for key/value stores. 如果您寻找键/值存储,您将找到更多解决方案。

Yes, Oracle Coherence can do all of that, but it may be overkill if that's all you're doing. 是的,Oracle Coherence可以做到这一切,但如果这就是你所做的一切可能会有点过头了。

One way to do this is to "overflow" from RAM to disk: 一种方法是从RAM“溢出”到磁盘:

    BinaryStore diskstore = new BerkeleyDBBinaryStore("mydb", ...);
    SimpleSerializationMap mapDisk = SimpleSerializationMap(diskstore);
    LocalCache mapRAM = new LocalCache(100 * 1024 * 1024); // 100MB in RAM
    OverflowMap cache = new OverflowMap(mapRAM, mapDisk);

Starting in version 3.7, you can also transparently overflow from RAM journal to flash journal. 从版本3.7开始,您还可以透明地从RAM日志溢出到flash日志。 While you can configure it in code (as per above), it's generally just a line or two of config and then you ask for the cache to be configured on your behalf, eg 虽然您可以在代码中配置它(如上所述),但它通常只是一行或两行配置,然后您要求代表您配置缓存,例如

    // simplest example; you'd probably use a builder pattern or a configurable cache factory
    NamedCache cache = CacheFactory.getCache("mycache");

For more information, see the doc available from http://coherence.oracle.com/ 有关详细信息,请参阅http://coherence.oracle.com/上提供的文档。

For the sake of full disclosure, I work at Oracle. 为了充分披露,我在Oracle工作。 The opinions and views expressed in this post are my own, and do not necessarily reflect the opinions or views of my employer. 这篇文章中表达的观点和观点是我自己的,不一定反映我的雇主的意见或观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM