将映射键/值存储在持久文件中
我将创建一个或多或少的形式的结构:
type FileState struct {
LastModified int64
Hash string
Path string
}
我想将这些值写入文件并在后续调用中读入它们。我最初的计划是将它们读入映射并使用键(路径)查找值(哈希和 LastModified)。 Go 中有一个巧妙的方法可以做到这一点吗?
如果不是,您可以推荐什么文件格式?我在之前的项目中阅读并尝试过一些键/值文件存储,但没有使用 Go。现在,我的要求可能相当简单,因此大型数据库服务器系统就显得有些过分了。我只想要一些可以快速、轻松、便携地写入和读取的东西(Windows、Mac、Linux)。因为我必须在多个平台上部署,所以我试图将非 go 依赖关系保持在最低限度。
我考虑过 XML、CSV、JSON。我简单地查看了 Go 中的 gob 包,并注意到 Go 包仪表板上有一个 BSON 包,但我不确定这些是否适用。
我的主要目标是快速启动和运行,这意味着我需要编写的代码量最少并且易于部署。
I will be creating a structure more or less of the form:
type FileState struct {
LastModified int64
Hash string
Path string
}
I want to write these values to a file and read them in on subsequent calls. My initial plan is to read them into a map and lookup values (Hash and LastModified) using the key (Path). Is there a slick way of doing this in Go?
If not, what file format can you recommend? I have read about and experimented with with some key/value file stores in previous projects, but not using Go. Right now, my requirements are probably fairly simple so a big database server system would be overkill. I just want something I can write to and read from quickly, easily, and portably (Windows, Mac, Linux). Because I have to deploy on multiple platforms I am trying to keep my non-go dependencies to a minimum.
I've considered XML, CSV, JSON. I've briefly looked at the gob package in Go and noticed a BSON package on the Go package dashboard, but I'm not sure if those apply.
My primary goal here is to get up and running quickly, which means the least amount of code I need to write along with ease of deployment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您的整个数据适合内存,就不会有问题。使用内存中的映射并定期将快照写入磁盘(例如通过使用
gob
包)是一个好主意。 Andrew Gerrand 的实用 Go 编程演讲使用这项技术。如果您需要使用不同的程序访问这些文件,那么使用流行的编码(例如 json 或 csv)可能是一个好主意。如果您只需从 Go 中访问这些文件,我会使用优秀的 gob 包,它具有 很多不错的功能。
一旦数据变得越来越大,每次更改时都将整个数据库写入磁盘就不是一个好主意。此外,您的数据可能不再适合 RAM。在这种情况下,您可能需要查看 leveldb 键值数据库由另一位 Go 开发者 Nigel Tao 提供的软件包。它目前正在积极开发中(但尚未可用),但它还将提供一些高级功能,例如事务和自动压缩。此外,由于 leveldb 的设计,读/写吞吐量应该相当好。
As long as your entiere data fits in memory, you should't have a problem. Using an in-memory map and writing snapshots to disk regularly (e.g. by using the
gob
package) is a good idea. The Practical Go Programming talk by Andrew Gerrand uses this technique.If you need to access those files with different programs, using a popular encoding like json or csv is probably a good idea. If you just have to access those file from within Go, I would use the excellent gob package, which has a lot of nice features.
As soon as your data becomes bigger, it's not a good idea to always write the whole database to disk on every change. Also, your data might not fit into the RAM anymore. In that case, you might want to take a look at the leveldb key-value database package by Nigel Tao, another Go developer. It's currently under active development (but not yet usable), but it will also offer some advanced features like transactions and automatic compression. Also, the read/write throughput should be quite good because of the leveldb design.
我编写了一个名为 gkvlite 的有序键值持久性库 -
https://github.com/steveyen/gkvlite
There's an ordered, key-value persistence library for the go that I wrote called gkvlite -
https://github.com/steveyen/gkvlite
JSON 非常简单,但由于变量名称重复,会产生更大的文件。 XML没有优势。您应该使用 CSV,这也非常简单。您的程序将少于一页。
但事实上,这取决于您的修改。如果您进行了大量修改并且必须将它们同步存储在磁盘上,那么您可能需要比单个文件更复杂的东西。如果您的地图主要是只读的,或者您有能力很少(不是每秒)将其转储到文件中,则内存中地图上的单个 csv 文件将使事情变得简单而高效。
顺便说一句,使用 go 的 csv 包来做到这一点。
JSON is very simple but makes bigger files because of the repeated variable names. XML has no advantage. You should go with CSV, which is really simple too. Your program will make less than one page.
But it depends, in fact, upon your modifications. If you make a lot of modifications and must have them stored synchronously on disk, you may need something a little more complex that a single file. If your map is mainly read-only or if you can afford to dump it on file rarely (not every second) a single csv file along an in-memory map will keep things simple and efficient.
BTW, use the csv package of go to do this.