|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.insights.core.service.categorizer; |
| 10 | + |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.stream.StreamSupport; |
| 13 | +import org.opensearch.common.settings.Settings; |
| 14 | +import org.opensearch.core.index.Index; |
| 15 | +import org.opensearch.test.OpenSearchTestCase; |
| 16 | + |
| 17 | +public final class IndicesFieldTypeCacheTests extends OpenSearchTestCase { |
| 18 | + final Index index1 = new Index("index1", UUID.randomUUID().toString()); |
| 19 | + final Index index2 = new Index("index2", UUID.randomUUID().toString()); |
| 20 | + |
| 21 | + public IndicesFieldTypeCacheTests() {} |
| 22 | + |
| 23 | + public void testCache() { |
| 24 | + final IndicesFieldTypeCache indicesFieldTypeCache = new IndicesFieldTypeCache(Settings.EMPTY); |
| 25 | + |
| 26 | + // Assert cache is empty |
| 27 | + assertEquals(0, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); |
| 28 | + assertEquals(0, (long) indicesFieldTypeCache.getEntryCount()); |
| 29 | + assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount()); |
| 30 | + assertEquals(0, (long) indicesFieldTypeCache.getWeight()); |
| 31 | + |
| 32 | + // Test invalidate on empty cache |
| 33 | + indicesFieldTypeCache.invalidate(index1); |
| 34 | + indicesFieldTypeCache.invalidate(index2); |
| 35 | + |
| 36 | + // Insert some items in the cache |
| 37 | + if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field1", "keyword")) { |
| 38 | + indicesFieldTypeCache.incrementCountAndWeight("field1", "keyword"); |
| 39 | + } |
| 40 | + if (indicesFieldTypeCache.getOrInitialize(index1).putIfAbsent("field2", "boolean")) { |
| 41 | + indicesFieldTypeCache.incrementCountAndWeight("field2", "boolean"); |
| 42 | + } |
| 43 | + if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field3", "float_range")) { |
| 44 | + indicesFieldTypeCache.incrementCountAndWeight("field3", "float_range"); |
| 45 | + } |
| 46 | + if (indicesFieldTypeCache.getOrInitialize(index2).putIfAbsent("field4", "date")) { |
| 47 | + indicesFieldTypeCache.incrementCountAndWeight("field4", "date"); |
| 48 | + } |
| 49 | + |
| 50 | + assertEquals(2, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); |
| 51 | + assertEquals(4, (long) indicesFieldTypeCache.getEntryCount()); |
| 52 | + assertEquals(0, (long) indicesFieldTypeCache.getEvictionCount()); |
| 53 | + assertTrue(0 < indicesFieldTypeCache.getWeight()); |
| 54 | + |
| 55 | + // Invalidate index1 |
| 56 | + indicesFieldTypeCache.invalidate(index1); |
| 57 | + |
| 58 | + assertEquals(1, StreamSupport.stream(indicesFieldTypeCache.keySet().spliterator(), false).count()); |
| 59 | + assertEquals(2, (long) indicesFieldTypeCache.getEntryCount()); |
| 60 | + assertEquals(2, (long) indicesFieldTypeCache.getEvictionCount()); |
| 61 | + assertTrue(0 < indicesFieldTypeCache.getWeight()); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments