|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.processors.performancestatistics; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.nio.ByteOrder; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.UUID; |
| 26 | +import org.apache.ignite.internal.processors.cache.persistence.file.FileIO; |
| 27 | +import org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory; |
| 28 | +import org.apache.ignite.internal.processors.cache.query.GridCacheQueryType; |
| 29 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static java.util.Collections.singletonList; |
| 33 | +import static java.util.UUID.randomUUID; |
| 34 | +import static org.apache.ignite.internal.processors.performancestatistics.FilePerformanceStatisticsWriter.PERF_STAT_DIR; |
| 35 | +import static org.apache.ignite.internal.processors.performancestatistics.FilePerformanceStatisticsWriter.writeString; |
| 36 | +import static org.apache.ignite.internal.processors.performancestatistics.FilePerformanceStatisticsWriter.writeUuid; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests forward read mode for {@link OperationType#QUERY_PROPERTY} records. |
| 40 | + */ |
| 41 | +public class ForwardReadQueryPropertyTest extends AbstractPerformanceStatisticsTest { |
| 42 | + /** Read buffer size. */ |
| 43 | + private static final int BUFFER_SIZE = 100; |
| 44 | + |
| 45 | + /** @throws Exception If failed. */ |
| 46 | + @Test |
| 47 | + public void testStringForwardRead() throws Exception { |
| 48 | + File dir = U.resolveWorkDirectory(U.defaultWorkDirectory(), PERF_STAT_DIR, false); |
| 49 | + |
| 50 | + Map<String, String> expProps = createStatistics(dir); |
| 51 | + Map<String, String> actualProps = new HashMap<>(); |
| 52 | + |
| 53 | + new FilePerformanceStatisticsReader(BUFFER_SIZE, new TestHandler() { |
| 54 | + @Override public void queryProperty(UUID nodeId, GridCacheQueryType type, UUID qryNodeId, long id, String name, |
| 55 | + String val) { |
| 56 | + assertNotNull(name); |
| 57 | + assertNotNull(val); |
| 58 | + |
| 59 | + actualProps.put(name, val); |
| 60 | + } |
| 61 | + }).read(singletonList(dir)); |
| 62 | + |
| 63 | + assertEquals(expProps, actualProps); |
| 64 | + } |
| 65 | + |
| 66 | + /** Creates test performance statistics file. */ |
| 67 | + private Map<String, String> createStatistics(File dir) throws Exception { |
| 68 | + File file = new File(dir, "node-" + randomUUID() + ".prf"); |
| 69 | + |
| 70 | + try (FileIO fileIo = new RandomAccessFileIOFactory().create(file)) { |
| 71 | + ByteBuffer buf = ByteBuffer.allocate(1024).order(ByteOrder.nativeOrder()); |
| 72 | + |
| 73 | + buf.put(OperationType.VERSION.id()); |
| 74 | + buf.putShort(FilePerformanceStatisticsWriter.FILE_FORMAT_VERSION); |
| 75 | + |
| 76 | + writeQueryProperty(buf, "property", true, "val", true); |
| 77 | + writeQueryProperty(buf, "property", false, "val", false); |
| 78 | + writeQueryProperty(buf, "propertyNan", true, "valNan", true); |
| 79 | + |
| 80 | + buf.flip(); |
| 81 | + |
| 82 | + fileIo.write(buf); |
| 83 | + |
| 84 | + fileIo.force(); |
| 85 | + } |
| 86 | + |
| 87 | + return Map.of("property", "val"); |
| 88 | + } |
| 89 | + |
| 90 | + /** */ |
| 91 | + private static void writeQueryProperty(ByteBuffer buf, String name, boolean nameCached, String val, boolean valCached) { |
| 92 | + buf.put(OperationType.QUERY_PROPERTY.id()); |
| 93 | + |
| 94 | + writeString(buf, name, nameCached); |
| 95 | + writeString(buf, val, valCached); |
| 96 | + |
| 97 | + buf.put((byte)GridCacheQueryType.SQL_FIELDS.ordinal()); |
| 98 | + |
| 99 | + writeUuid(buf, randomUUID()); |
| 100 | + |
| 101 | + buf.putLong(0); |
| 102 | + } |
| 103 | +} |
0 commit comments