使用最低共同技术分母的 UTF-8 元组存储,仅附加

发布于 2024-09-07 20:13:30 字数 927 浏览 2 评论 0原文

编辑:请注意,由于硬盘驱动器实际写入数据的方式,此列表中的方案都无法可靠工作。不要使用它们。只需使用数据库即可。 SQLite 是一个很好、简单的工具。

在磁盘上存储 UTF-8 字符串元组的技术含量最低但最可靠的方法是什么?为了可靠性,存储应该是仅附加的。

作为我正在试验的文档存储系统的一部分,我必须将 UTF-8 元组数据存储在磁盘上。显然,对于成熟的实施,我想使用 Amazon S3、Project Voldemort 或 CouchDB 之类的东西。

然而,目前我正在尝试,甚至还没有坚定地选择一种编程语言。我一直在使用 CSV,但是当您尝试存储奇怪的 unicode 和意外的空白(例如垂直制表符)时,CSV 往往会变得脆弱。

我可以使用 XML 或 JSON 进行存储,但它们不能很好地处理仅附加文件。到目前为止,我最好的猜测是一种相当特殊的格式,其中每个字符串前面都有一个 4 字节有符号整数,指示它包含的字节数,整数值 -1 指示该元组是完整的 - 相当于 CSV 换行符。令人头痛的主要根源是必须决定磁盘上整数的字节顺序。

编辑:实际上,这行不通。如果程序在写入字符串时退出,则数据将变得不可挽回地错位。需要某种带外信令来确保在中止元组后可以重新获得对齐。

编辑 2:事实证明,在附加到文本文件时保证原子性是可能的,但解析器相当不平凡的。现在编写所述解析器。

编辑 3:您可以在 http://github.com/MetalBeetle/Fruitbat/tree/master/src/com/metalbeetle/fruitbat/atrio/

EDIT: Note that due to the way hard drives actually write data, none of the schemes in this list work reliably. Do not use them. Just use a database. SQLite is a good simple one.

What's the most low-tech but reliable way of storing tuples of UTF-8 strings on disk? Storage should be append-only for reliability.

As part of a document storage system I'm experimenting with I have to store UTF-8 tuple data on disk. Obviously, for a full-blown implementation, I want to use something like Amazon S3, Project Voldemort, or CouchDB.

However, at the moment, I'm experimenting and haven't even firmly settled on a programming language yet. I have been using CSV, but CSV tend to become brittle when you try to store outlandish unicode and unexpected whitespace (eg vertical tabs).

I could use XML or JSON for storage, but they don't play nice with append-only files. My best guess so far is a rather idiosyncratic format where each string is preceded by a 4-byte signed integer indicating the number of bytes it contains, and an integer value of -1 indicates that this tuple is complete - the equivalent of a CSV newline. The main source of headaches there is having to decide on the endianness of the integer on disk.

Edit: actually, this won't work. If the program exits while writing a string, the data becomes irrevocably misaligned. Some sort of out-of-band signalling is needed to ensure alignment can be regained after an aborted tuple.

Edit 2: Turns out that guaranteeing atomicity when appending to text files is possible, but the parser is quite non-trivial. Writing said parser now.

Edit 3: You can view the end result at http://github.com/MetalBeetle/Fruitbat/tree/master/src/com/metalbeetle/fruitbat/atrio/ .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴梦长久 2024-09-14 20:13:35

主要是在这里大声思考......

真正低技术的方法是使用(例如)空字节作为分隔符,然后用附加的空值“引用”输出中出现的所有空字节。

也许人们可以同时使用 SCSU

或者可能值得查看 gzip 格式,如果不使用它,也许可以模仿它:

gzip 文件由一系列“成员”(压缩数据集)组成。

[...]

成员只是一个接一个地出现在文件中,在它们之前、之间或之后没有任何附加信息。

每个成员都可以有一个可选的“文件名”、注释等,我相信您可以继续添加成员。

或者您可以使用 bencode,用于 torrent 文件。或者BSON

另请参阅维基百科的数据序列化格式比较

否则我认为你在每个字符串前面加上它的长度的想法可能是最简单的。

Mostly thinking out loud here...

Really low tech would be to use (for example) null bytes as separators, and just "quote" all null bytes appearing in the output with an additional null.

Perhaps one could use SCSU along with that.

Or it might be worth to look at the gzip format, and maybe ape it, if not using it:

A gzip file consists of a series of "members" (compressed data sets).

[...]

The members simply appear one after another in the file, with no additional information before, between, or after them.

Each of these members can have an optional "filename", comment, or the like, and i believe you can just keep appending members.

Or you could use bencode, used in torrent-files. Or BSON.

See also Wikipedia's Comparison of data serialization formats.

Otherwise i think your idea of preceding each string with its length is probably the simplest one.

情归归情 2024-09-14 20:13:33

我建议用制表符分隔每个字段,用回车符分隔每个记录。

在每个字符串中,替换所有会影响字段并记录解释和呈现的字符。这将包括控制字符(U+0000–U+001F、U+007F–U+009F)、非图形行和段落分隔符(U+2028、U=2029)、方向控制字符(U+202A–U+ 202E)和字节顺序标记(U+FEFF)。

它们应该被替换为恒定长度的转义序列。转义序列应以罕见的(对于您的应用程序而言)字符开头。转义字符本身也应该被转义。

这将允许您轻松附加新记录。它的另一个优点是能够将文件加载到任何电子表格或文字处理程序中进行目视检查和修改,这对于调试目的可能很有用。

这也很容易编码,因为该文件将是有效的 UTF-8 文档,因此可以使用标准文本读写例程。如果需要,这还允许您轻松转换为 UTF-16BE 或 UTF-16LE,而不会出现任何复杂情况。

示例:

U+0009 CHARACTER TABULATION becomes ~TB
U+000A LINE FEED            becomes ~LF
U+000D CARRIAGE RETURN      becomes ~CR
U+007E TILDE                becomes ~~~
etc.

制表符作为字段分隔符比逗号更好有几个原因。逗号在普通文本字符串(例如英文文本)中更常见,因此必须更频繁地替换。电子表格程序(例如 Microsoft Excel)往往更自然地处理制表符分隔的文件。

I would recommend tab delimiting each field and carriage-return delimiting each record.

Within each string, Replace all characters that would affect the field and record interpretation and rendering. This would include control characters (U+0000–U+001F, U+007F–U+009F), non-graphical line and paragraph separators (U+2028, U=2029), directional control characters (U+202A–U+202E), and the byte order mark (U+FEFF).

They should be replaced with escape sequences of constant length. The escape sequences should begin with a rare (for your application) character. The escape character itself should also be escaped.

This would allow you to append new records easily. It has the additional advantage of being able to load the file for visual inspection and modification into any spreadsheet or word processing program, which could be useful for debugging purposes.

This would also be easy to code, since the file will be a valid UTF-8 document, so standard text reading and writing routines may be used. This also allows you to convert easily to UTF-16BE or UTF-16LE if desired, without complications.

Example:

U+0009 CHARACTER TABULATION becomes ~TB
U+000A LINE FEED            becomes ~LF
U+000D CARRIAGE RETURN      becomes ~CR
U+007E TILDE                becomes ~~~
etc.

There are a couple of reasons why tabs would be better than commas as field delimiters. Commas appear more commonly within normal text strings (such as English text), and would have to be replaced more frequently. And spreadsheet programs (such as Microsoft Excel) tend to handle tab-delimited files much more naturally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文