保持番石榴图的方法 [英] Ways to persist Guava Graph

查看:76
本文介绍了保持番石榴图的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用版本21.0的Google Guava中的common.graph .没有一个方面,它非常适合我的用例:持久性.该图似乎仅在内存中.图类未实现Serializable,对此问题帖子进行了解释.

I'm using the common.graph from Google Guava in Version 21.0. It suits very well to my usecase without one aspect: Persistence. The graph seems to be in-memory only. The graph-classes does not implement Serializable, it was explained in this issue posts.

Google描述了三个用于存储拓扑的模型.第三个选项是:

Google describes three models to store the topology. The third option is:

单独的数据存储库(例如数据库)存储拓扑

a separate data repository (for example, a database) stores the topology

仅此而已.我没有在包中找到任何方法来应用单独的数据存储库.有什么办法吗?还是使用nodes()edges()方法获取节点的Set和边缘的Set的唯一方法?如果我在此类中实现Serializable并可以通过调用addNode(Node)addEdge(Source, Target, Edge)(没有addAll方法)来还原图形,则可以将它们持久保存在数据库中.但这似乎是一种解决方法.

But that's all. I didn't found any methods in the package to apply a separate data repository. Is there any way to do this? Or is the only way to use the nodes()and edges() method to get a Set of my nodes and a Set of my edges? I can persist them in a database if I implement Serializable in this classes and restore the graph by calling addNode(Node) and addEdge(Source, Target, Edge) (there are no addAll-methods). But this seems to be a workaround.

感谢您的支持!

推荐答案

简单回顾一下Guava的common.graph类不是Serializable的原因:Java序列化是脆弱的,因为它取决于实现的细节,并且可以随时更改,因此我们不支持图形类型.

To briefly recap the reason why Guava's common.graph classes aren't Serializable: Java serialization is fragile because it depends on the details of the implementation, and that can change at any time, so we don't support it for the graph types.

在短期内,建议的解决方法可能是最好的选择,尽管您需要小心地将边缘的端点(源和目标)与边缘对象一起存储,以便能够重建您所描述的图形.实际上,从长远来看,这也可能对您有用,如果您拥有一个满意的数据库,而不必担心与其他任何人的互操作.

In the short term, your proposed workaround is probably your best bet, although you'll need to be careful to store the endpoints (source and target) of the edges alongside the edge objects so that you'll be able to rebuild the graph as you describe. And in fact this may work for you in the longer term, too, if you've got a database that you're happy with and you don't need to worry about interoperation with anyone else.

正如我在 GitHub问题中提到的那样,另一种选择是将图形持久化为某种文件格式. (番石榴本身不提供执行此操作的机制,但是一旦我使用 JUNG 可以发布3.0,而我仍在努力.)请注意,大多数图形文件格式(至少是我熟悉的图形文件格式)对存储节点和边缘元数据的支持非常有限,因此您可能需要自己的格式文件格式(例如,基于协议缓冲区的文件格式).

As I mentioned in that GitHub issue, another option is to persist your graph to some kind of file format. (Guava itself does not provide a mechanism for doing this, but JUNG will for common.graph graphs once I can get 3.0 out the door, which I'm still working on.) Note that most graph file formats (at least the ones I'm familiar with) have fairly limited support for storing node and edge metadata, so you might want your own file format (say, something based on protocol buffers).

这篇关于保持番石榴图的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆