Search in sources :

Example 6 with Index

use of io.prestosql.spi.heuristicindex.Index in project hetu-core by openlookeng.

the class TestIndexCacheFetch method testIndexCacheGetIndices.

@Test
public void testIndexCacheGetIndices() throws Exception {
    synchronized (this) {
        IndexMetadata indexMetadata = mock(IndexMetadata.class);
        when(indexMetadata.getLastModifiedTime()).thenReturn(testLastModifiedTime);
        Index index = mock(Index.class);
        when(indexMetadata.getIndex()).then(new Returns(index));
        when(index.getMemoryUsage()).thenReturn(new DataSize(1, KILOBYTE).toBytes());
        List<IndexMetadata> expectedIndices = new LinkedList<>();
        expectedIndices.add(indexMetadata);
        IndexCacheLoader indexCacheLoader = mock(IndexCacheLoader.class);
        when(indexCacheLoader.load(any())).then(new Returns(expectedIndices));
        IndexCache indexCache = new IndexCache(indexCacheLoader, new NoOpIndexClient(), false);
        List<IndexMetadata> actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
        Thread.sleep(loadDelay + 2000);
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), numberOfIndexTypes);
        assertEquals(actualSplitIndex.get(0), expectedIndices.get(0));
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) DataSize(io.airlift.units.DataSize) Index(io.prestosql.spi.heuristicindex.Index) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) NoOpIndexClient(io.prestosql.testing.NoOpIndexClient) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 7 with Index

use of io.prestosql.spi.heuristicindex.Index in project hetu-core by openlookeng.

the class TestIndexCacheRemoval method testExpiredCacheIndices.

@Test
public void testExpiredCacheIndices() throws Exception {
    synchronized (this) {
        IndexMetadata indexMetadata = mock(IndexMetadata.class);
        when(indexMetadata.getLastModifiedTime()).thenReturn(testLastModifiedTime);
        Index index = mock(Index.class);
        when(indexMetadata.getIndex()).then(new Returns(index));
        when(index.getMemoryUsage()).thenReturn(new DataSize(1, KILOBYTE).toBytes());
        List<IndexMetadata> expectedIndices = new LinkedList<>();
        expectedIndices.add(indexMetadata);
        IndexCacheLoader indexCacheLoader = mock(IndexCacheLoader.class);
        when(indexCacheLoader.load(any())).then(new Returns(expectedIndices));
        IndexCache indexCache = new IndexCache(indexCacheLoader, new NoOpIndexClient(), false);
        List<IndexMetadata> actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
        Thread.sleep(loadDelay + 2000);
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), numberOfIndexTypes);
        // now the index is in the cache, but changing the lastmodified date of the split should invalidate it
        when(indexMetadata.getLastModifiedTime()).then(new Returns(testLastModifiedTime + 1));
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) DataSize(io.airlift.units.DataSize) Index(io.prestosql.spi.heuristicindex.Index) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) NoOpIndexClient(io.prestosql.testing.NoOpIndexClient) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 8 with Index

use of io.prestosql.spi.heuristicindex.Index in project hetu-core by openlookeng.

the class TestIndexCacheRemoval method testIndexCacheEviction.

@Test
public void testIndexCacheEviction() throws Exception {
    synchronized (this) {
        IndexCacheLoader indexCacheLoader = mock(IndexCacheLoader.class);
        IndexCache indexCache = new IndexCache(indexCacheLoader, new NoOpIndexClient(), false);
        // get index for split1
        IndexMetadata indexMetadata1 = mock(IndexMetadata.class);
        when(indexMetadata1.getLastModifiedTime()).thenReturn(testLastModifiedTime);
        Index index1 = mock(Index.class);
        when(indexMetadata1.getIndex()).thenReturn(index1);
        when(index1.getMemoryUsage()).thenReturn(new DataSize(2, KILOBYTE).toBytes());
        List<IndexMetadata> expectedIndices1 = new LinkedList<>();
        expectedIndices1.add(indexMetadata1);
        when(indexCacheLoader.load(any())).then(new Returns(expectedIndices1));
        // each index is has memory usage of 2, and limit is 2*types of idx, so all should be loaded
        List<IndexMetadata> actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
        Thread.sleep(loadDelay + 2000);
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), numberOfIndexTypes);
        assertEquals(actualSplitIndex.get(0), indexMetadata1);
        assertEquals(indexCache.getCacheSize(), numberOfIndexTypes);
        // get index for split2
        when(connectorSplit.getFilePath()).thenReturn(testPath2);
        IndexMetadata indexMetadata2 = mock(IndexMetadata.class);
        when(indexMetadata2.getLastModifiedTime()).thenReturn(testLastModifiedTime);
        Index index2 = mock(Index.class);
        when(indexMetadata2.getIndex()).thenReturn(index2);
        when(index2.getMemoryUsage()).thenReturn(new DataSize(2, KILOBYTE).toBytes());
        // previous indexes should be evicted bc cache was at max weight limit and new ones should be added
        List<IndexMetadata> expectedIndices2 = new LinkedList<>();
        expectedIndices2.add(indexMetadata2);
        when(indexCacheLoader.load(any())).then(new Returns(expectedIndices2));
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
        assertEquals(indexCache.getCacheSize(), numberOfIndexTypes);
        Thread.sleep(loadDelay + 2000);
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), numberOfIndexTypes);
        assertEquals(actualSplitIndex.get(0), indexMetadata2);
        assertEquals(indexCache.getCacheSize(), numberOfIndexTypes);
        // get index for split1
        when(connectorSplit.getFilePath()).thenReturn(testPath);
        actualSplitIndex = indexCache.getIndices(table, column, split);
        assertEquals(actualSplitIndex.size(), 0);
        assertEquals(indexCache.getCacheSize(), numberOfIndexTypes);
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) DataSize(io.airlift.units.DataSize) Index(io.prestosql.spi.heuristicindex.Index) NoOpIndexClient(io.prestosql.testing.NoOpIndexClient) IndexMetadata(io.prestosql.spi.heuristicindex.IndexMetadata) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 9 with Index

use of io.prestosql.spi.heuristicindex.Index in project hetu-core by openlookeng.

the class TestBTreeIndex method testLessThanEqualTo.

@Test
public void testLessThanEqualTo() throws IOException, IndexLookUpException {
    BTreeIndex index = new BTreeIndex();
    for (int i = 0; i < 100; i++) {
        List<Pair> pairs = new ArrayList<>();
        Long key = Long.valueOf(100 + i);
        String value = "value" + i;
        pairs.add(new Pair(key, value));
        Pair pair = new Pair("dummyCol", pairs);
        index.addKeyValues(Collections.singletonList(pair));
    }
    File file = getFile();
    index.serialize(new FileOutputStream(file));
    BTreeIndex readIndex = new BTreeIndex();
    readIndex.deserialize(new FileInputStream(file));
    RowExpression comparisonExpression = simplePredicate(OperatorType.LESS_THAN_OR_EQUAL, "dummyCol", BIGINT, 120L);
    Iterator<String> result = readIndex.lookUp(comparisonExpression);
    assertNotNull(result, "Result shouldn't be null");
    assertTrue(result.hasNext());
    Object[] arr = IntStream.iterate(0, n -> n + 1).limit(21).mapToObj(i -> "value" + i).toArray();
    Arrays.sort(arr);
    for (int i = 0; i <= 20; i++) {
        assertEquals(arr[i], result.next());
    }
    assertFalse(result.hasNext());
    index.close();
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) ArrayList(java.util.ArrayList) VARCHAR(io.prestosql.spi.type.VarcharType.VARCHAR) OperatorType(io.prestosql.spi.function.OperatorType) BOOLEAN(io.prestosql.spi.type.BooleanType.BOOLEAN) SpecialForm(io.prestosql.spi.relation.SpecialForm) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Assert.assertFalse(org.testng.Assert.assertFalse) Iterator(java.util.Iterator) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) Assert.assertNotNull(org.testng.Assert.assertNotNull) Pair(io.prestosql.spi.heuristicindex.Pair) File(java.io.File) List(java.util.List) HeuristicIndexTestUtils.simplePredicate(io.hetu.core.HeuristicIndexTestUtils.simplePredicate) RowExpression(io.prestosql.spi.relation.RowExpression) Assert.assertTrue(org.testng.Assert.assertTrue) IndexLookUpException(io.prestosql.spi.heuristicindex.IndexLookUpException) Index(io.prestosql.spi.heuristicindex.Index) Collections(java.util.Collections) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Pair(io.prestosql.spi.heuristicindex.Pair) Test(org.testng.annotations.Test)

Example 10 with Index

use of io.prestosql.spi.heuristicindex.Index in project hetu-core by openlookeng.

the class TestBTreeIndex method testDeserialize.

@Test
public void testDeserialize() throws IOException, IndexLookUpException {
    BTreeIndex index = new BTreeIndex();
    String value = "foo bar";
    for (int i = 0; i < 1000; i++) {
        List<Pair> pairs = new ArrayList<>();
        Long key = Long.valueOf(100 + i);
        pairs.add(new Pair(key, value));
        Pair pair = new Pair("dummyCol", pairs);
        index.addKeyValues(Collections.singletonList(pair));
    }
    File file = File.createTempFile("test-serialize-", UUID.randomUUID().toString());
    index.serialize(new FileOutputStream(file));
    Index readindex = new BTreeIndex();
    readindex.deserialize(new FileInputStream(file));
    RowExpression comparisonExpression = simplePredicate(OperatorType.EQUAL, "column", BIGINT, 101L);
    Iterator<String> result = readindex.lookUp(comparisonExpression);
    assertNotNull(result, "Result shouldn't be null");
    assertTrue(result.hasNext());
    assertEquals(value, result.next());
    index.close();
}
Also used : ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) Index(io.prestosql.spi.heuristicindex.Index) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Pair(io.prestosql.spi.heuristicindex.Pair) Test(org.testng.annotations.Test)

Aggregations

Index (io.prestosql.spi.heuristicindex.Index)24 LinkedList (java.util.LinkedList)17 IndexMetadata (io.prestosql.spi.heuristicindex.IndexMetadata)15 Test (org.testng.annotations.Test)14 DataSize (io.airlift.units.DataSize)12 NoOpIndexClient (io.prestosql.testing.NoOpIndexClient)11 Returns (org.mockito.internal.stubbing.answers.Returns)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 List (java.util.List)9 HiveSplit (io.prestosql.plugin.hive.HiveSplit)8 Pair (io.prestosql.spi.heuristicindex.Pair)8 Collections (java.util.Collections)7 Map (java.util.Map)7 CreateIndexMetadata (io.prestosql.spi.connector.CreateIndexMetadata)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Logger (io.airlift.log.Logger)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5