Java Code Examples for org.apache.flink.runtime.operators.testutils.types.IntPair

The following examples show how to use org.apache.flink.runtime.operators.testutils.types.IntPair. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Flink-CEPplus   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildAndRetrieve() throws Exception {
	final int NUM_MEM_PAGES = 32 * NUM_PAIRS / PAGE_SIZE;
	AbstractMutableHashTable<IntPair> table = getHashTable(intPairSerializer, intPairComparator, getMemory(NUM_MEM_PAGES));

	final Random rnd = new Random(RANDOM_SEED);
	final IntPair[] pairs = getRandomizedIntPairs(NUM_PAIRS, rnd);

	table.open();

	for (int i = 0; i < NUM_PAIRS; i++) {
		table.insert(pairs[i]);
	}

	AbstractHashTableProber<IntPair, IntPair> prober = table.getProber(intPairComparator, pairComparator);
	IntPair target = new IntPair();

	for (int i = 0; i < NUM_PAIRS; i++) {
		assertNotNull(prober.getMatchFor(pairs[i], target));
		assertEquals(pairs[i].getValue(), target.getValue());
	}


	table.close();
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 2
Source Project: Flink-CEPplus   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected static IntPair[] getRandomizedIntPairs(int num, Random rnd) {
	IntPair[] pairs = new IntPair[num];
	
	// create all the pairs, dense
	for (int i = 0; i < num; i++) {
		pairs[i] = new IntPair(i, i + KEY_VALUE_DIFF);
	}
	
	// randomly swap them
	for (int i = 0; i < 2 * num; i++) {
		int pos1 = rnd.nextInt(num);
		int pos2 = rnd.nextInt(num);
		
		IntPair tmp = pairs[pos1];
		pairs[pos1] = pairs[pos2];
		pairs[pos2] = tmp;
	}
	
	return pairs;
}
 
Example 3
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildAndRetrieve() throws Exception {
	final int NUM_MEM_PAGES = 32 * NUM_PAIRS / PAGE_SIZE;
	AbstractMutableHashTable<IntPair> table = getHashTable(intPairSerializer, intPairComparator, getMemory(NUM_MEM_PAGES));

	final Random rnd = new Random(RANDOM_SEED);
	final IntPair[] pairs = getRandomizedIntPairs(NUM_PAIRS, rnd);

	table.open();

	for (int i = 0; i < NUM_PAIRS; i++) {
		table.insert(pairs[i]);
	}

	AbstractHashTableProber<IntPair, IntPair> prober = table.getProber(intPairComparator, pairComparator);
	IntPair target = new IntPair();

	for (int i = 0; i < NUM_PAIRS; i++) {
		assertNotNull(prober.getMatchFor(pairs[i], target));
		assertEquals(pairs[i].getValue(), target.getValue());
	}


	table.close();
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 4
Source Project: flink   Source File: TestData.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putNormalizedKey(IntPair record, MemorySegment target, int offset, int len) {
	// see IntValue for a documentation of the logic
	final int value = record.getKey() - Integer.MIN_VALUE;

	if (len == 4) {
		target.putIntBigEndian(offset, value);
	} else if (len <= 0) {
	} else if (len < 4) {
		for (int i = 0; len > 0; len--, i++) {
			target.put(offset + i, (byte) ((value >>> ((3 - i) << 3)) & 0xff));
		}
	} else {
		target.putIntBigEndian(offset, value);
		for (int i = 4; i < len; i++) {
			target.put(offset + i, (byte) 0);
		}
	}
}
 
Example 5
Source Project: flink   Source File: NonReusingHashJoinIteratorITCase.java    License: Apache License 2.0 6 votes vote down vote up
public static Map<Integer, Collection<Integer>> collectIntPairData(MutableObjectIterator<IntPair> iter)
throws Exception
{
	Map<Integer, Collection<Integer>> map = new HashMap<>();
	IntPair pair = new IntPair();
	
	while ((pair = iter.next(pair)) != null) {

		final int key = pair.getKey();
		final int value = pair.getValue();
		if (!map.containsKey(key)) {
			map.put(key, new ArrayList<Integer>());
		}

		Collection<Integer> values = map.get(key);
		values.add(value);
	}

	return map;
}
 
Example 6
public static Map<Integer, Collection<Integer>> collectIntPairData(MutableObjectIterator<IntPair> iter)
throws Exception
{
	Map<Integer, Collection<Integer>> map = new HashMap<>();
	IntPair pair = new IntPair();
	
	while ((pair = iter.next(pair)) != null) {

		final int key = pair.getKey();
		final int value = pair.getValue();
		if (!map.containsKey(key)) {
			map.put(key, new ArrayList<Integer>());
		}

		Collection<Integer> values = map.get(key);
		values.add(value);
	}

	return map;
}
 
Example 7
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected static IntPair[] getRandomizedIntPairs(int num, Random rnd) {
	IntPair[] pairs = new IntPair[num];
	
	// create all the pairs, dense
	for (int i = 0; i < num; i++) {
		pairs[i] = new IntPair(i, i + KEY_VALUE_DIFF);
	}
	
	// randomly swap them
	for (int i = 0; i < 2 * num; i++) {
		int pos1 = rnd.nextInt(num);
		int pos2 = rnd.nextInt(num);
		
		IntPair tmp = pairs[pos1];
		pairs[pos1] = pairs[pos2];
		pairs[pos2] = tmp;
	}
	
	return pairs;
}
 
Example 8
Source Project: Flink-CEPplus   Source File: TestData.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void putNormalizedKey(IntPair record, MemorySegment target, int offset, int len) {
	// see IntValue for a documentation of the logic
	final int value = record.getKey() - Integer.MIN_VALUE;

	if (len == 4) {
		target.putIntBigEndian(offset, value);
	} else if (len <= 0) {
	} else if (len < 4) {
		for (int i = 0; len > 0; len--, i++) {
			target.put(offset + i, (byte) ((value >>> ((3 - i) << 3)) & 0xff));
		}
	} else {
		target.putIntBigEndian(offset, value);
		for (int i = 4; i < len; i++) {
			target.put(offset + i, (byte) 0);
		}
	}
}
 
Example 9
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 6 votes vote down vote up
protected static IntPair[] getRandomizedIntPairs(int num, Random rnd) {
	IntPair[] pairs = new IntPair[num];
	
	// create all the pairs, dense
	for (int i = 0; i < num; i++) {
		pairs[i] = new IntPair(i, i + KEY_VALUE_DIFF);
	}
	
	// randomly swap them
	for (int i = 0; i < 2 * num; i++) {
		int pos1 = rnd.nextInt(num);
		int pos2 = rnd.nextInt(num);
		
		IntPair tmp = pairs[pos1];
		pairs[pos1] = pairs[pos2];
		pairs[pos2] = tmp;
	}
	
	return pairs;
}
 
Example 10
Source Project: flink   Source File: ReusingHashJoinIteratorITCase.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void join(IntPair rec1, Tuple2<Integer, String> rec2, Collector<Tuple2<Integer, String>> out) throws Exception
{
	final int k = rec1.getKey();
	final int v = rec1.getValue(); 
	
	final Integer key = rec2.f0;
	final String value = rec2.f1;
	
	Assert.assertTrue("Key does not match for matching IntPair Tuple combination.", k == key); 
	
	Collection<TupleIntPairMatch> matches = this.toRemoveFrom.get(key);
	if (matches == null) {
		Assert.fail("Match " + key + " - " + v + ":" + value + " is unexpected.");
	}
	
	Assert.assertTrue("Produced match was not contained: " + key + " - " + v + ":" + value,
		matches.remove(new TupleIntPairMatch(v, value)));
	
	if (matches.isEmpty()) {
		this.toRemoveFrom.remove(key);
	}
}
 
Example 11
Source Project: Flink-CEPplus   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentProbers() {
	final int NUM_MEM_PAGES = 32 * NUM_PAIRS / PAGE_SIZE;
	AbstractMutableHashTable<IntPair> table = getHashTable(intPairSerializer, intPairComparator, getMemory(NUM_MEM_PAGES));

	AbstractHashTableProber<IntPair, IntPair> prober1 = table.getProber(intPairComparator, pairComparator);
	AbstractHashTableProber<IntPair, IntPair> prober2 = table.getProber(intPairComparator, pairComparator);

	assertFalse(prober1 == prober2);

	table.close(); // (This also tests calling close without calling open first.)
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 12
Source Project: Flink-CEPplus   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleProbers() throws Exception {
	final int NUM_MEM_PAGES = SIZE * NUM_LISTS / PAGE_SIZE;
	AbstractMutableHashTable<IntList> table = getHashTable(serializerV, comparatorV, getMemory(NUM_MEM_PAGES));

	final Random rnd = new Random(RANDOM_SEED);
	final IntList[] lists = getRandomizedIntLists(NUM_LISTS, rnd);
	final IntPair[] pairs = getRandomizedIntPairs(NUM_LISTS, rnd);

	table.open();
	for (int i = 0; i < NUM_LISTS; i++) {
		table.insert(lists[i]);
	}

	AbstractHashTableProber<IntList, IntList> listProber = table.getProber(comparatorV, pairComparatorV);

	AbstractHashTableProber<IntPair, IntList> pairProber = table.getProber(intPairComparator, pairComparatorPL);

	IntList target = new IntList();
	for (int i = 0; i < NUM_LISTS; i++) {
		assertNotNull(pairProber.getMatchFor(pairs[i], target));
		assertNotNull(listProber.getMatchFor(lists[i], target));
		assertArrayEquals(lists[i].getValue(), target.getValue());
	}
	table.close();
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 13
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentProbers() {
	final int NUM_MEM_PAGES = 32 * NUM_PAIRS / PAGE_SIZE;
	AbstractMutableHashTable<IntPair> table = getHashTable(intPairSerializer, intPairComparator, getMemory(NUM_MEM_PAGES));

	AbstractHashTableProber<IntPair, IntPair> prober1 = table.getProber(intPairComparator, pairComparator);
	AbstractHashTableProber<IntPair, IntPair> prober2 = table.getProber(intPairComparator, pairComparator);

	assertFalse(prober1 == prober2);

	table.close(); // (This also tests calling close without calling open first.)
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 14
Source Project: Flink-CEPplus   Source File: HashTableITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair reuse) {
	if (this.numLeft > 0) {
		this.numLeft--;
		reuse.setKey(this.key);
		reuse.setValue(this.value);
		return reuse;
	}
	else {
		return null;
	}
}
 
Example 15
Source Project: flink   Source File: RandomIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next() {
	if (this.count++ < this.numRecords) {
		return new IntPair(this.rnd.nextInt(), this.rnd.nextInt());
	} else {
		return null;
	}
}
 
Example 16
Source Project: flink   Source File: RandomIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair reuse) {
	if (this.count++ < this.numRecords) {
		reuse.setKey(this.rnd.nextInt());
		reuse.setValue(this.rnd.nextInt());
		return reuse;
	} else {
		return null;
	}
}
 
Example 17
Source Project: Flink-CEPplus   Source File: UniformIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair target) {
	if(!repeatKey) {
		if(valCnt >= numVals) {
			return null;
		}
		
		target.setKey(keyCnt++);
		target.setValue(valCnt);
		
		if(keyCnt == numKeys) {
			keyCnt = 0;
			valCnt++;
		}
	} else {
		if(keyCnt >= numKeys) {
			return null;
		}
		
		target.setKey(keyCnt);
		target.setValue(valCnt++);
		
		if(valCnt == numVals) {
			valCnt = 0;
			keyCnt++;
		}
	}
	
	return target;
}
 
Example 18
Source Project: Flink-CEPplus   Source File: RandomIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next() {
	if (this.count++ < this.numRecords) {
		return new IntPair(this.rnd.nextInt(), this.rnd.nextInt());
	} else {
		return null;
	}
}
 
Example 19
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentProbers() {
	final int NUM_MEM_PAGES = 32 * NUM_PAIRS / PAGE_SIZE;
	AbstractMutableHashTable<IntPair> table = getHashTable(intPairSerializer, intPairComparator, getMemory(NUM_MEM_PAGES));

	AbstractHashTableProber<IntPair, IntPair> prober1 = table.getProber(intPairComparator, pairComparator);
	AbstractHashTableProber<IntPair, IntPair> prober2 = table.getProber(intPairComparator, pairComparator);

	assertFalse(prober1 == prober2);

	table.close(); // (This also tests calling close without calling open first.)
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 20
Source Project: flink   Source File: RandomIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next() {
	if (this.count++ < this.numRecords) {
		return new IntPair(this.rnd.nextInt(), this.rnd.nextInt());
	} else {
		return null;
	}
}
 
Example 21
Source Project: flink   Source File: HashTableITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair reuse) {
	if (this.numLeft > 0) {
		this.numLeft--;
		reuse.setKey(this.key);
		reuse.setValue(this.value);
		return reuse;
	}
	else {
		return null;
	}
}
 
Example 22
Source Project: flink   Source File: HashTableITCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair reuse) {
	if (this.numLeft > 0) {
		this.numLeft--;
		reuse.setKey(this.key);
		reuse.setValue(this.value);
		return reuse;
	}
	else {
		return null;
	}
}
 
Example 23
Source Project: flink   Source File: UniformIntPairGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IntPair next(IntPair target) {
	if(!repeatKey) {
		if(valCnt >= numVals) {
			return null;
		}
		
		target.setKey(keyCnt++);
		target.setValue(valCnt);
		
		if(keyCnt == numKeys) {
			keyCnt = 0;
			valCnt++;
		}
	} else {
		if(keyCnt >= numKeys) {
			return null;
		}
		
		target.setKey(keyCnt);
		target.setValue(valCnt++);
		
		if(valCnt == numVals) {
			valCnt = 0;
			keyCnt++;
		}
	}
	
	return target;
}
 
Example 24
Source Project: flink   Source File: MutableHashTableTestBase.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleProbers() throws Exception {
	final int NUM_MEM_PAGES = SIZE * NUM_LISTS / PAGE_SIZE;
	AbstractMutableHashTable<IntList> table = getHashTable(serializerV, comparatorV, getMemory(NUM_MEM_PAGES));

	final Random rnd = new Random(RANDOM_SEED);
	final IntList[] lists = getRandomizedIntLists(NUM_LISTS, rnd);
	final IntPair[] pairs = getRandomizedIntPairs(NUM_LISTS, rnd);

	table.open();
	for (int i = 0; i < NUM_LISTS; i++) {
		table.insert(lists[i]);
	}

	AbstractHashTableProber<IntList, IntList> listProber = table.getProber(comparatorV, pairComparatorV);

	AbstractHashTableProber<IntPair, IntList> pairProber = table.getProber(intPairComparator, pairComparatorPL);

	IntList target = new IntList();
	for (int i = 0; i < NUM_LISTS; i++) {
		assertNotNull(pairProber.getMatchFor(pairs[i], target));
		assertNotNull(listProber.getMatchFor(lists[i], target));
		assertArrayEquals(lists[i].getValue(), target.getValue());
	}
	table.close();
	assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
}
 
Example 25
Source Project: flink   Source File: ReusingHashJoinIteratorITCase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int compareToReference(IntPair candidate) {
	return candidate.getKey() - this.reference;
}
 
Example 26
Source Project: flink   Source File: TestData.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public IntPair readWithKeyDenormalization(IntPair reuse, DataInputView source) throws IOException {
	reuse.setKey(source.readInt() + Integer.MIN_VALUE);
	reuse.setValue(source.readInt());
	return reuse;
}
 
Example 27
Source Project: flink   Source File: HashTablePerformanceComparison.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testCompactingHashMapPerformance() {
	
	try {
		final int NUM_MEM_PAGES = SIZE * NUM_PAIRS / PAGE_SIZE;
		
		MutableObjectIterator<IntPair> buildInput = new UniformIntPairGenerator(NUM_PAIRS, 1, false);
		
		MutableObjectIterator<IntPair> probeTester = new UniformIntPairGenerator(NUM_PAIRS, 1, false);
		
		MutableObjectIterator<IntPair> updater = new UniformIntPairGenerator(NUM_PAIRS, 1, false);

		MutableObjectIterator<IntPair> updateTester = new UniformIntPairGenerator(NUM_PAIRS, 1, false);
		
		long start;
		long end;
		
		long first = System.currentTimeMillis();
		
		System.out.println("Creating and filling CompactingHashMap...");
		start = System.currentTimeMillis();
		AbstractMutableHashTable<IntPair> table = new CompactingHashTable<IntPair>(serializer, comparator, getMemory(NUM_MEM_PAGES, PAGE_SIZE));
		table.open();
		
		IntPair target = new IntPair();
		while(buildInput.next(target) != null) {
			table.insert(target);
		}
		end = System.currentTimeMillis();
		System.out.println("HashMap ready. Time: " + (end-start) + " ms");
		
		System.out.println("Starting first probing run...");
		start = System.currentTimeMillis();
		
		AbstractHashTableProber<IntPair, IntPair> prober = table.getProber(comparator, pairComparator);
		IntPair temp = new IntPair();
		while(probeTester.next(target) != null) {
			assertNotNull(prober.getMatchFor(target, temp));
			assertEquals(temp.getValue(), target.getValue());
		}
		end = System.currentTimeMillis();
		System.out.println("Probing done. Time: " + (end-start) + " ms");
		
		System.out.println("Starting update...");
		start = System.currentTimeMillis();
		while(updater.next(target) != null) {
			target.setValue(target.getValue() + 1);
			table.insertOrReplaceRecord(target);
		}
		end = System.currentTimeMillis();
		System.out.println("Update done. Time: " + (end-start) + " ms");
		
		System.out.println("Starting second probing run...");
		start = System.currentTimeMillis();
		while (updateTester.next(target) != null) {
			assertNotNull(prober.getMatchFor(target, temp));
			assertEquals(target.getValue() + 1, temp.getValue());
		}
		end = System.currentTimeMillis();
		System.out.println("Probing done. Time: " + (end-start) + " ms");
		
		table.close();
		
		end = System.currentTimeMillis();
		System.out.println("Overall time: " + (end-first) + " ms");
		
		assertEquals("Memory lost", NUM_MEM_PAGES, table.getFreeMemory().size());
	}
	catch (Exception e) {
		e.printStackTrace();
		fail("Error: " + e.getMessage());
	}
}
 
Example 28
Source Project: flink   Source File: NonReusingHashJoinIteratorITCase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean equalToReference(IntPair candidate) {
	return this.reference == candidate.getKey();
}
 
Example 29
@Override
public void setReference(IntPair reference) {
	this.reference = reference.getKey();	
}
 
Example 30
@Override
public boolean equalToReference(IntPair candidate) {
	return this.reference == candidate.getKey();
}