将地图键/值存储在持久性文件中 [英] Store map key/values in a persistent file

查看:118
本文介绍了将地图键/值存储在持久性文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将创建一个或多或少的结构:

I will be creating a structure more or less of the form:

type FileState struct {
    LastModified int64
    Hash string
    Path string
}

我想写这些值到一个文件,并在随后的调用中读取它们。我的初始计划是使用键(Path)将它们读入地图并查找值(Hash和LastModified)。在Go中是否有光滑的方式?

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?

如果没有,你可以推荐什么文件格式?在以前的项目中,我已经阅读并尝试了一些关键/值文件存储,但不使用Go。现在,我的要求可能相当简单,所以一个大型的数据库服务器系统将会过度使用。我只想要一些可以快速,轻松,便携(Windows,Mac,Linux)写入和阅读的东西。因为我必须在多个平台上进行部署,所以我试图将非依赖关系保持在最低限度。

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.

我考虑过XML,CSV和JSON。我简要地看了一下Go中的gob软件包,并注意到Go软件包信息板上有一个BSON软件包,但我不确定是否应用。

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.

推荐答案

gob 包)是一个好主意。 Andrew Gerrand的实用Go Programming这种技术。

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.

如果您需要使用不同的程序访问这些文件,使用流行的编码,如json或csv可能是一个好主意。如果你只需要从Go内部访问这些文件,我将使用优秀的gob包,其中包含很多不错的功能

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.

一旦你的数据变大,总是写整个数据库不是一个好主意磁盘上的每一个变化。此外,您的数据可能不适合RAM。在这种情况下,您可能需要查看 leveldb 键值数据库另一个Go开发者Nigel Tao打包。它目前正在积极开发(但尚不可用),但它还将提供一些高级功能,如交易和自动压缩。此外,读/写吞吐量应该相当好,因为leveldb设计。

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.

这篇关于将地图键/值存储在持久性文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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