Store map key/values in a persistent file
Asked Answered
W

3

12

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.

Wendolyn answered 13/10, 2011 at 5:47 Comment(0)
S
13

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.

Staurolite answered 13/10, 2011 at 15:45 Comment(1)
gob turned out to be a great solution. Reminds me of serialization in C#. It's not portable (across languages), but it doesn't need to be right now. Thanks for the leveldb tip, I will keep an eye on that project.Wendolyn
M
2

There's an ordered, key-value persistence library for the go that I wrote called gkvlite -
https://github.com/steveyen/gkvlite

Mast answered 3/5, 2013 at 6:59 Comment(1)
gkvlite seems nice however I have the impression that it is no longer maintainedLevitical
W
1

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.

Willner answered 13/10, 2011 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.