[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io; |
---|
| 20 | |
---|
| 21 | import org.apache.hadoop.conf.Configuration; |
---|
| 22 | import org.apache.hadoop.fs.FileSystem; |
---|
| 23 | import org.apache.hadoop.fs.Path; |
---|
| 24 | |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | |
---|
| 27 | public class TestBloomMapFile extends TestCase { |
---|
| 28 | private static Configuration conf = new Configuration(); |
---|
| 29 | |
---|
| 30 | public void testMembershipTest() throws Exception { |
---|
| 31 | // write the file |
---|
| 32 | Path dirName = new Path(System.getProperty("test.build.data",".") + |
---|
| 33 | getName() + ".bloommapfile"); |
---|
| 34 | FileSystem fs = FileSystem.getLocal(conf); |
---|
| 35 | Path qualifiedDirName = fs.makeQualified(dirName); |
---|
| 36 | conf.setInt("io.mapfile.bloom.size", 2048); |
---|
| 37 | BloomMapFile.Writer writer = new BloomMapFile.Writer(conf, fs, |
---|
| 38 | qualifiedDirName.toString(), IntWritable.class, Text.class); |
---|
| 39 | IntWritable key = new IntWritable(); |
---|
| 40 | Text value = new Text(); |
---|
| 41 | for (int i = 0; i < 2000; i += 2) { |
---|
| 42 | key.set(i); |
---|
| 43 | value.set("00" + i); |
---|
| 44 | writer.append(key, value); |
---|
| 45 | } |
---|
| 46 | writer.close(); |
---|
| 47 | |
---|
| 48 | BloomMapFile.Reader reader = new BloomMapFile.Reader(fs, |
---|
| 49 | qualifiedDirName.toString(), conf); |
---|
| 50 | // check false positives rate |
---|
| 51 | int falsePos = 0; |
---|
| 52 | int falseNeg = 0; |
---|
| 53 | for (int i = 0; i < 2000; i++) { |
---|
| 54 | key.set(i); |
---|
| 55 | boolean exists = reader.probablyHasKey(key); |
---|
| 56 | if (i % 2 == 0) { |
---|
| 57 | if (!exists) falseNeg++; |
---|
| 58 | } else { |
---|
| 59 | if (exists) falsePos++; |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | reader.close(); |
---|
| 63 | fs.delete(qualifiedDirName, true); |
---|
| 64 | System.out.println("False negatives: " + falseNeg); |
---|
| 65 | assertEquals(0, falseNeg); |
---|
| 66 | System.out.println("False positives: " + falsePos); |
---|
| 67 | assertTrue(falsePos < 2); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | } |
---|