/** the test */ @Test @SuppressWarnings("unchecked") public void testSortedMapWritable() { Text[] keys = { new Text("key1"), new Text("key2"), new Text("key3"), }; BytesWritable[] values = { new BytesWritable("value1".getBytes()), new BytesWritable("value2".getBytes()), new BytesWritable("value3".getBytes()) }; SortedMapWritable inMap = new SortedMapWritable(); for (int i = 0; i < keys.length; i++) { inMap.put(keys[i], values[i]); } assertEquals(0, inMap.firstKey().compareTo(keys[0])); assertEquals(0, inMap.lastKey().compareTo(keys[2])); SortedMapWritable outMap = new SortedMapWritable(inMap); assertEquals(inMap.size(), outMap.size()); for (Map.Entry<WritableComparable, Writable> e : inMap.entrySet()) { assertTrue(outMap.containsKey(e.getKey())); assertEquals(0, ((WritableComparable) outMap.get(e.getKey())).compareTo(e.getValue())); } // Now for something a little harder... Text[] maps = {new Text("map1"), new Text("map2")}; SortedMapWritable mapOfMaps = new SortedMapWritable(); mapOfMaps.put(maps[0], inMap); mapOfMaps.put(maps[1], outMap); SortedMapWritable copyOfMapOfMaps = new SortedMapWritable(mapOfMaps); for (int i = 0; i < maps.length; i++) { assertTrue(copyOfMapOfMaps.containsKey(maps[i])); SortedMapWritable a = (SortedMapWritable) mapOfMaps.get(maps[i]); SortedMapWritable b = (SortedMapWritable) copyOfMapOfMaps.get(maps[i]); assertEquals(a.size(), b.size()); for (Writable key : a.keySet()) { assertTrue(b.containsKey(key)); // This will work because we know what we put into each set WritableComparable aValue = (WritableComparable) a.get(key); WritableComparable bValue = (WritableComparable) b.get(key); assertEquals(0, aValue.compareTo(bValue)); } } }
/** * Optimization hook. Override this to make SequenceFile.Sorter's scream. * * <p>The default implementation reads the data into two {@link WritableComparable}s (using {@link * Writable#readFields(DataInput)}, then calls {@link * #compare(WritableComparable,WritableComparable)}. */ public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { try { buffer.reset(b1, s1, l1); // parse key1 key1.readFields(buffer); buffer.reset(b2, s2, l2); // parse key2 key2.readFields(buffer); } catch (IOException e) { throw new RuntimeException(e); } return compare(key1, key2); // compare them }
@Override public synchronized void append(WritableComparable key, Writable val) throws IOException { super.append(key, val); buf.reset(); key.write(buf); bloomKey.set(byteArrayForBloomKey(buf), 1.0); bloomFilter.add(bloomKey); }
/** * Checks if this MapFile has the indicated key. The membership test is performed using a Bloom * filter, so the result has always non-zero probability of false positives. * * @param key key to check * @return false iff key doesn't exist, true if key probably exists. * @throws IOException */ public boolean probablyHasKey(WritableComparable key) throws IOException { if (bloomFilter == null) { return true; } buf.reset(); key.write(buf); bloomKey.set(byteArrayForBloomKey(buf), 1.0); return bloomFilter.membershipTest(bloomKey); }
/** * Compare two WritableComparables. * * <p>The default implementation uses the natural ordering, calling {@link * Comparable#compareTo(Object)}. */ @SuppressWarnings("unchecked") public int compare(WritableComparable a, WritableComparable b) { return a.compareTo(b); }