【问题标题】:How to add strings to cuckoo filter?如何将字符串添加到杜鹃过滤器?
【发布时间】:2017-03-25 00:22:49
【问题描述】:

我正在运行 Cuckoo 过滤器存储库中提供的示例的修改版本:https://github.com/efficient/cuckoofilter/blob/master/example/test.cc

我想给 cuckoo 过滤器添加字符串。虽然添加了字符串,但是当我检查它是否存在于过滤器中时,它总是返回 false。谁能指出我的方法有什么问题?

size_t total_items  = 1000000;
CuckooFilter<string, 12> filter(total_items);

// Insert items to this cuckoo filter
string temp1 = "sample";
if (filter.Add(temp1) != cuckoofilter::Ok) {
        cout<<"not added"<<endl;
}    

// Check if previously inserted items are in the filter
string temp2 = "sample";
assert(filter.Contain(temp2) == cuckoofilter::Ok);

断言应该是真的,但在这种情况下它是假的。为什么?

【问题讨论】:

    标签: c++ hash hashmap


    【解决方案1】:

    快速浏览https://github.com/efficient/cuckoofilter/blob/master/src/cuckoofilter.h#L65 的来源,发现它使用了一个函数

    inline void GenerateIndexTagHash(const ItemType &item, size_t* index, uint32_t* tag) const
    {
        std::string hashed_key = HashUtil::SHA1Hash(
            (const char*) &item,
            sizeof(item)
        );
    
    // ... rest is skipped for brevity
    
    }
    

    生成项目的初始索引和指纹(标签)。问题是它散列了一个实际的对象。为了简化,它这样做:

    // Your filter.Add(temp1) inside does this
    HashUtil::SHA1Hash((const char*) &temp1, sizeof(temp1));
    
    // Your filter.Contain(temp2) inside does this
    HashUtil::SHA1Hash((const char*) &temp2, sizeof(temp2));
    

    基本上,它散列两个完全不同的对象,正如预期的那样,生成不同的散列并映射到不同的桶。

    为了在您的情况下工作,它需要以散列实际字符串数据的方式调用 HashUtil::SHA1Hash(),即:

    // It should do something like this
    HashUtil::SHA1Hash(
        temp1.c_str(), // <-- notice we pass a pointer to an actual character data rather than a pointer to an instance of a std::string()
        temp1.length()
    );
    
    
    HashUtil::SHA1Hash(
        temp2.c_str(), // <-- notice we pass a pointer to an actual character data rather than a pointer to an instance of a std::string()
        temp2.length()
    );
    

    这应该回答您的为什么? 问题。至于

    谁能指出我的方法有什么问题?

    您的方法本身并没有什么问题,它只是不能像您预期的那样工作,因为库不支持这样的用例。

    【讨论】:

    • 感谢您的回答。我修改了源代码以使用 SuperFastHash 而不是 SHA1/MD5 并且它有效。
    【解决方案2】:

    我正在尝试将字符串添加到 cuckoofilter 库 https://github.com/efficient/cuckoofilter/blob/master/src/cuckoofilter.h

    我的代码

    cuckoofilter::CuckooFilter&lt;string, 12&gt; cuckoo_mempool(total_items);

    但每次我运行代码时,我都会在线收到此错误 [https://github.com/efficient/cuckoofilter/blob/master/src/cuckoofilter.h#L68]

    错误:不匹配调用‘(const cuckoofilter::TwoIndependentMultiplyShift) (const std::_cxx11::basic_string&)’ 68 | const uint64_t hash = hasher(item); | ^~~~

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2023-03-14
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多