简体   繁体   English

将任何哈希存储在GDBM中,我可以在其中搜索吗?

[英]Store any hash in GDBM and can I search in it?

Reading about GDBM in this book they only give simple examples of the data structure that can stored. 本书中阅读GDBM,他们只给出了可以存储的数据结构的简单示例。 Eg 例如

$dbm{'key'} = "value";

Background 背景

I would like to save many small text files in the database for local use only, and use nested hashes and arrays to represent the file paths. 我想在数据库中保存许多小文本文件仅供本地使用,并使用嵌套的哈希和数组来表示文件路径。 It doesn't have to be GDBM, but it seams to be the only key/value database library for Perl. 它不一定是GDBM,但它接缝是Perl唯一的键/值数据库库。

Question

Can I store any hash in GDBM no matter have many nested hashes and arrays it contains? 我可以在GDBM中存储任何哈希,无论它包含多少嵌套哈希和数组?

Does GDBM offer any search features, or I am left to implement my own in Perl? GDBM是否提供任何搜索功能,或者我在Perl中实现自己的功能?

DBM databases don't support arrays at all. DBM数据库根本不支持数组。 They are esssentially the same as a Perl hash, except that the value of an item can only be a simple string and may not be a number or a reference. 它们基本上与Perl哈希相同,只是项的值只能是一个简单的字符串,可能不是数字或引用。 The keys and values for each data item in a DBM database are simple byte sequences. DBM数据库中每个数据项的键和值都是简单的字节序列。 That is, the API represents them by a char pointer and an int size. 也就是说,API通过char指针和int大小来表示它们。

Within that constraint you can use the database however you like, but remember that, unlike SQL databases, every key must be unique. 在该约束中,您可以随意使用数据库,但请记住,与SQL数据库不同,每个键都必须是唯一的。

You could emulate nested hashes by using the data fetched by one access as a key for the next access but, bearing in mind the requirement for unique keys, that's far from ideal. 您可以通过使用一次访问提取的数据作为下一次访问的键来模拟嵌套哈希,但要记住对唯一键的要求,这远非理想。

Alternatively, the value fetched could be the name of another DBM database which you could go on to query further. 或者,获取的值可以是另一个DBM数据库的名称,您可以继续进行查询。

A final option is to concatenate all the keys into a single value, so that 最后一个选项是将所有键连接成一个值,这样就可以了

$dbm{aa}{bb}{cc}

would actually be implemented as something like 实际上会实现像

$dbm{aa_bb_cc}

Actually, you can store hashes of hashes, or lists of lists in perl. 实际上,您可以在perl中存储哈希值或列表列表。 You use the MLDBM module from CPAN, along with the dbm of your choice.. 您可以使用CPAN中的MLDBM模块以及您选择的dbm。

check out this online pdf book and go to chapter 13. [ https://www.perl.org/books/beginning-perl/][1] 查看这个在线pdf书并转到第13章。[ https://www.perl.org/books/beginning-perl/] [1 ]

The complex part is figuring out how to access the various levels of references. 复杂的部分是弄清楚如何访问各种级别的引用。 To search you would have to run through the keys and parse the values. 要搜索,您必须运行键并解析值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM