【问题标题】:What is the most efficient way of storing multiple revisions of binary files in PostgreSQL?在 PostgreSQL 中存储多个二进制文件修订版的最有效方法是什么?
【发布时间】:2011-04-02 10:28:00
【问题描述】:

我在这里的数据库中寻找一种有限形式的版本控制:

  • 大小最重要:同一文件的多个修订版应占用尽可能小的空间(我不希望压缩,因为数据已经压缩)
  • 计算要求是次要的
  • 我应该能够尽快获取文档的当前修订版(获取旧版本不是时间关键的)

基本上答案应该至少包含两件事:

  • 您会使用哪种二进制差异算法?
  • 您将如何以特定于 PostreSQL 的方式构建该系统?

【问题讨论】:

    标签: database-design postgresql version-control binary diff


    【解决方案1】:

    “大小最重要”:例如使用PL/sh 的外部差异工具(如bsdiff?)怎么样。

    “我应该能够尽快获取文档的当前修订版”:在这种情况下,您将希望以“错误”的方式进行 diff,因此每个修订版都涉及:

    1. 用“新版本”和“上一个版本”之间的差异替换“上一个版本”
    2. 添加“新修订”

    要回到旧版本,需要反复应用以前的差异作为补丁,直到获得所需的版本。

    无论您做什么,我认为您都需要先解压缩数据,然后才能使用 diff 工具。原因如下:

    dd if=/dev/urandom of=myfile.1 bs=1024 count=10
    cp myfile.1 tmp; cat tmp >> myfile.1
    cp myfile.1 tmp; cat tmp >> myfile.1
    cp myfile.1 tmp; cat tmp >> myfile.1
    cp myfile.1 tmp; cat tmp >> myfile.1
    dd if=/dev/urandom of=myfile.2 bs=1024 count=10
    cp myfile.2 tmp; cat tmp >> myfile.2
    cp myfile.2 tmp; cat tmp >> myfile.2
    cp myfile.2 tmp; cat tmp >> myfile.2
    cp myfile.2 tmp; cat tmp >> myfile.2
    cat myfile.1 >> myfile.2
    bsdiff myfile.1 myfile.2 diff
    gzip -c myfile.1 > myfile.1.gz
    gzip -c myfile.2 > myfile.2.gz
    bsdiff myfile.1.gz myfile.2.gz gz.diff
    rm tmp
    ls -l
    
    -rw-r--r-- 1 root root  17115 2011-04-05 10:54 diff
    -rw-r--r-- 1 root root  21580 2011-04-05 10:54 gz.diff
    -rw-r--r-- 1 root root 163840 2011-04-05 10:54 myfile.1
    -rw-r--r-- 1 root root  11709 2011-04-05 10:54 myfile.1.gz
    -rw-r--r-- 1 root root 327680 2011-04-05 10:54 myfile.2
    -rw-r--r-- 1 root root  23399 2011-04-05 10:54 myfile.2.gz
    

    请注意,gz.diff 大于 diff - 如果您使用真实文件尝试此操作,我预计差异会更大。

    【讨论】:

    • 感谢您的回答:您能否详细说明为什么我需要在使用 diff 工具之前解压缩我的数据?
    【解决方案2】:

    我真的不喜欢重新发明轮子。在存储空间优化的情况下,人们已经比我聪明地想出了解决方案。如果可能的话,我更愿意利用这些真正聪明的人的辛勤工作。话虽如此,一旦我了解它们如何存储二进制数据,我可能会考虑将我的文件存储在诸如 Mercurial 或 Git 之类的修订控制系统中。一旦你确定了你想使用哪一个,你可以看看创建一些最有可能在 pl/perl 中的存储函数的方法,或者类似的可以与版本控制系统交互的方法,并弥合 PostgreSQL 中的关系数据和二进制文件之间的差距文件。

    我对这种方法的唯一问题是我不太喜欢我采用事务系统并在其中引入了外部系统(Mercurial/Git)。最重要的是,数据库的备份不会备份我的 Mercurial 或 Git 存储库。但是总会有一个权衡,所以只要弄清楚你可以接受哪些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 2012-05-26
      • 2014-11-03
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      相关资源
      最近更新 更多