保存和重新加载番石榴布隆过滤器时出错 - 需要帮助找到代码中的任何错误

时间:2016-02-19 12:19:13

标签: java guava bloom-filter

我最近在生产中使用它之前测试了经典布隆过滤器的谷歌实现。我正在使用番石榴库的第18版。当我运行以下程序时,我在sysout中获得了超过200的不同计数。我不知道这里可能出现什么问题,有人可以提供第二双眼睛吗?

import com.google.common.collect.Lists;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.RandomStringUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * http://code.google.com/p/guava-libraries/wiki/HashingExplained
 * stackoverflow.com/questions/12319560/how-should-i-use-guavas-hashingconsistenthash
 */
public class GuavaHashing {
    private static final int N = 2500;

    public static void main(String[] args) throws IOException {
        List<String> ids = generateStoryIds(N);
        Set<String> testIds = generateTest(ids);
        bloomfiltertime(ids, testIds);
    }

    private static List<String> generateStoryIds(int size) {
        List<String> stories = new ArrayList<>();
        for (int i=0; i<size; ++i) {
            stories.add(RandomStringUtils.randomAlphanumeric(16));
        }
        return stories;
    }

    private static Set<String> generateTest(List<String> presList) {
        Set<String> test = new HashSet<>();
        Random rand = new Random(System.currentTimeMillis());
        for (int i=0; i<200; ++i) {
            test.add(presList.get(Math.abs(rand.nextInt()%N)));
        }
        for (int i=0; i<250; ++i) {
            test.add(RandomStringUtils.randomAlphanumeric(16));
        }
        return test;
    }

    public static void bloomfiltertime(List<String> storyIds, Set<String> testPresent) throws IOException {
        BloomFilter<String> stories = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), N, 0.05);
        long startTime = System.currentTimeMillis();
        for(String story : storyIds) {
            stories.put(story);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("bloom put time " + (endTime - startTime));

        FileOutputStream fos = new FileOutputStream("testfile.dat");
        stories.writeTo(fos);
        fos.close();

        FileInputStream fis = new FileInputStream("testfile.dat");
        BloomFilter<String> readStories = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), N, 0.05);
        startTime = System.currentTimeMillis();
        readStories.readFrom(fis, Funnels.stringFunnel(Charset.defaultCharset()));
        endTime = System.currentTimeMillis();
        System.out.println("bloom read file time " + (endTime - startTime));

        startTime = System.currentTimeMillis();
        int count = 0;
        for(String story : testPresent) {
            if(stories.mightContain(story) != readStories.mightContain(story)) {
                ++count;
            }
        }
        endTime = System.currentTimeMillis();
        System.out.println("bloom check time " + (endTime - startTime));
        System.out.println("varying : " + count);

    }
}

1 个答案:

答案 0 :(得分:2)

BloomFilter#readFrom方法是静态方法,它返回 BloomFilter对象。你忽略了这个返回值(显然假设这个方法“填充”它被调用的对象)。

所以改变

BloomFilter<String> readStories = 
    BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), N, 0.05);
readStories.readFrom(fis, Funnels.stringFunnel(Charset.defaultCharset()));

BloomFilter<CharSequence> readStories = 
    BloomFilter.readFrom(fis, Funnels.stringFunnel(Charset.defaultCharset()));

它应该有效。

(顺便说一下:当你在一个实例上调用静态方法时,现代IDE会发出警告。例如,Eclipse:Window - &gt; Preferences - &gt; Java - &gt; Compiler - &gt; Errors / Warnings - &gt ;代码样式 - &gt;将“对静态成员的非静态访问”设置为“警告”)