key/data pair与索引文件

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

前段时间浏览 swish 2.6 版本的代码,其中的索引存储采用了 Berkeley DB ,直接通过 key data pair 方式进行存储( Berkeley DB 本身就是轻量级的,通过 key-data pair 进行数据的存储)。

Swish 2.6 中对于 ENTRY 词条的存储基于 key/data pair 方法。词条的 word 作为 key ,位置信息等作为 data ,通过数据库的方式进行存储。

 

例如:对于词条 china ,出现在 filenum1 的标题中和 filenum2 中的文章内容中。此时,可以将词条作为 key ,位置等信息作为 data ,数据设计为:

<metaID><datasize>< 所在文件 ><position><metaID><datasize>< 所在文件 ><position>

其中, metaID 相当与 lucene 中的 field 域。

在查询的时候,通过 key china )获得 data ,直接分析其中的位置信息即可。但是 key/pair 存储仅支持完全匹配查询。(当然,可以增加一个查询处理层,对于通配符查询等进行处理)。

swish2.4 原先的索引文件设计中,将词条 ENTRY 通过 hash 方法映射到 hash 表中,然后直接按照 binary file 的方式写入文件。应该说,效率方面比不了 key/pair 访问方式。

 

打算在桌面搜索的程序中,用 kerkeley DB 进行处理。

在C++中,`std::string`并不是标准库中的线性查找数据结构的键类型,因为它是可变长度的字符序列,不适合直接作为数组或容器的一级索引。然而,你可以通过一些间接方式来实现类似的功能,例如: 1. 使用`std::unordered_map`或`std::unordered_set`: 这些是哈希表,虽然它们的标准键类型通常是基本类型(如`int`、`char`),但通过模板参数可以接受自定义类型的键,包括`std::string`。这样你就可以以字符串作为键,存储其他相关的数据。 ```cpp #include <unordered_map> class StringIndexContainer { private: std::unordered_map<std::string, YourDataType> indexedData; public: void insert(const std::string& key, YourDataType data) { indexedData[key] = data; } YourDataType& get(const std::string& key) { auto it = indexedData.find(key); if (it != indexedData.end()) { return it->second; } else { throw std::runtime_error("Key not found"); } } }; ``` 在这里,`YourDataType`是你想要关联的具体类型。 2. 字符串数组+辅助数据结构:如果你只是需要对字符串进行顺序访问,可以创建一个二维数组,其中第一维是索引(可能需要映射到实际的字符串),第二维保存数据。然后,你可以用一个辅助数据结构(如`std::vector<std::pair<std::string, YourDataType>>`)来管理这种映射关系,便于查找。 请注意,这种方式的性能通常不如哈希表好,尤其是对于频繁查找的情况,因为它依赖于线性搜索的时间复杂度(O(n))。哈希表则提供了平均常数时间复杂度(O(1))的查找。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值