doutuoji8418 2014-03-22 04:00
浏览 48
已采纳

在GoLang中编写可靠的数据存储

I have been building a simple data store as part of my module for a document database system which I'm going to build for educational purposes.

In order to store data reliably, I have to abide the ACID property. Shown below is my save method.

func (document Document) Save() (hash string, err error) {
    if err := os.MkdirAll(document.FileDirectory(), 0600); err != nil {
        return "", err
    }

    file, err := os.Create(document.TmpFile())
    if err != nil {
        return "", err
    }

    file.Write(document.Data)
    if err := file.Sync(); err != nil {
        return "", err
    }

    file.Close()

    if err := os.Rename(document.TmpFile(), document.File()); err != nil {
        return "", err
    }

    return document.Hash(), nil
}

First the data (in []byte) is saved to a temporary file. The file is then synced with file.Sync() to ensure the data is written to the persistent storage. Then the temporary file is renamed into the new file.

Note: The way i chose to store the data file is in spoolDir format. Meaning the first two character of the hash generated from the data is used as parent directory name. The following two character of the hash is used as the subsequent directory name. The filename will be the 36 character left over. The temporary file only has a suffix .tmp with the file path and file name the same. This design is inspired by how git store data.

Question: Is the way I implement the data storing algorithm sufficient to ensure the data is reliably persisted.

Answer so far: Something about directory syncing to ensure data durability (I'm not sure)

Thanks in advance


Updated code as per suggested by rightfold:

func (document Document) Save() (hash string, err error) {
    if err := os.MkdirAll(document.FileDirectory(), 0600); err != nil {
        return "", err
    }

    file, err := os.Create(document.TmpFile())
    if err != nil {
        return "", err
    }

    file.Write(document.Data)
    if err := file.Sync(); err != nil {
        return "", err
    }

    file.Close()

    if err := os.Rename(document.TmpFile(), document.File()); err != nil {
        os.Remove(document.TmpFile())
        return "", err
    }

    return document.Hash(), nil
}
  • 写回答

1条回答 默认 最新

  • duanjiwei1283 2014-03-22 09:42
    关注

    What you are doing guarantees durability to the degree the OS and hardware guarantee it (which is the best you can get).

    It is also atomic; incomplete writes don’t leave incomplete data, even when the CPU catches fire.

    You may want to delete the temporary file when renaming fails:

    if err := os.Rename(document.TmpFile(), document.File()); err != nil {
        os.Remove(document.TmpFile()) // ignore errors
        return "", err
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站