|
8 | 8 |
|
9 | 9 | package org.opensearch.common.metadata; |
10 | 10 |
|
11 | | -import org.apache.lucene.store.IndexInput; |
12 | | -import org.apache.lucene.store.IndexOutput; |
13 | | - |
14 | 11 | import java.io.IOException; |
15 | 12 | import java.util.Map; |
16 | 13 |
|
| 14 | +import org.apache.lucene.codecs.CodecUtil; |
| 15 | +import org.apache.lucene.store.BufferedChecksumIndexInput; |
| 16 | +import org.apache.lucene.store.ChecksumIndexInput; |
| 17 | +import org.apache.lucene.store.IndexInput; |
| 18 | +import org.apache.lucene.store.IndexOutput; |
| 19 | + |
17 | 20 | /** |
18 | | - * Parser interface for Metadata. Holds methods to convert to/from file content streams to metadata object holder - {@link T} |
19 | | - * @param <T> The type of metadata to be parsed |
| 21 | + * Manages Metadata by adding version, codec in header and checksum in footer. |
| 22 | + * @param <T> The type of metadata to be read/written |
20 | 23 | */ |
21 | | -public interface MetadataParser<T> { |
| 24 | +public class MetadataParser<T> { |
| 25 | + // This can be updated to hold a parserFactory and get relevant parsers based on the metadata versions |
| 26 | + private final MetadataContentParser<T> parser; |
| 27 | + private final int currentVersion; |
| 28 | + private final String codec; |
| 29 | + |
22 | 30 | /** |
23 | | - * Implements logic to read metadata content from metadata file input stream {@code indexInput} and parse into {@link T} |
24 | | - * @param indexInput metadata file input stream |
25 | | - * @return metadata content parsed to {@link T} |
| 31 | + * @param parser parser to read/write metadata from T |
| 32 | + * @param currentVersion latest supported version of the metadata |
| 33 | + * @param codec: metadata codec |
26 | 34 | */ |
27 | | - T readContent(IndexInput indexInput) throws IOException; |
| 35 | + public MetadataParser(MetadataContentParser<T> parser, int currentVersion, String codec) { |
| 36 | + this.parser = parser; |
| 37 | + this.currentVersion = currentVersion; |
| 38 | + this.codec = codec; |
| 39 | + } |
28 | 40 |
|
29 | 41 | /** |
30 | | - * Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput} |
31 | | - * @param indexOutput metadata file input stream |
| 42 | + * Reads metadata content from {@code indexInput} and parses the read content to {@link T}. |
| 43 | + * Before reading actual content, verifies the header with relevant codec and version. |
| 44 | + * After reading the actual content, verifies the checksum as well |
| 45 | + * @param indexInput metadata file input stream |
| 46 | + * @return metadata content parsed into {@link T} |
32 | 47 | */ |
33 | | - void writeContent(IndexOutput indexOutput, T content) throws IOException; |
| 48 | + public T readMetadata(IndexInput indexInput) throws IOException { |
| 49 | + ChecksumIndexInput checksumIndexInput = new BufferedChecksumIndexInput(indexInput); |
| 50 | + checkHeader(checksumIndexInput); |
| 51 | + T metadata = this.parser.readContent(checksumIndexInput); |
| 52 | + checkFooter(checksumIndexInput); |
| 53 | + return metadata; |
| 54 | + } |
34 | 55 |
|
35 | 56 | /** |
36 | | - * Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput} |
37 | | - * This method only supports metadata content in the form {@code Map<String, String>} to support RemoteSegment store metadata content. |
38 | | - * This will removed in future releases and only {@link #writeContent(IndexOutput, Object)} )} should be used going fwd. |
39 | | - * @param indexOutput metadata file input stream |
40 | | - * @param content metadata content |
| 57 | + * Writes metadata to file output stream {@code indexOutput} |
| 58 | + * @param indexOutput file output stream which will store metadata content |
| 59 | + * @param metadata metadata content. |
| 60 | + * |
| 61 | + * TODO - This will removed in future releases and only {@link #writeMetadata(IndexOutput, Object)} should be used. |
41 | 62 | */ |
42 | 63 | @Deprecated |
43 | | - void writeContent(IndexOutput indexOutput, Map<String, String> content) throws IOException; |
| 64 | + public void writeMetadata(IndexOutput indexOutput, Map<String, String> metadata) throws IOException { |
| 65 | + this.writeHeader(indexOutput); |
| 66 | + this.parser.writeContent(indexOutput, metadata); |
| 67 | + this.writeFooter(indexOutput); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Writes metadata to file output stream {@code indexOutput} |
| 72 | + * @param indexOutput file output stream which will store metadata content |
| 73 | + * @param metadata metadata content. |
| 74 | + */ |
| 75 | + public void writeMetadata(IndexOutput indexOutput, T metadata) throws IOException { |
| 76 | + this.writeHeader(indexOutput); |
| 77 | + this.parser.writeContent(indexOutput, metadata); |
| 78 | + this.writeFooter(indexOutput); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Reads header from metadata file input stream containing {@code this.codec} and {@code this.currentVersion}. |
| 83 | + * @param indexInput metadata file input stream |
| 84 | + * @return header version found in the metadata file |
| 85 | + */ |
| 86 | + private int checkHeader(IndexInput indexInput) throws IOException { |
| 87 | + return CodecUtil.checkHeader(indexInput, this.codec, this.currentVersion, this.currentVersion); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Reads footer from metadata file input stream containing checksum. |
| 92 | + * The {@link IndexInput#getFilePointer()} should be at the footer start position. |
| 93 | + * @param indexInput metadata file input stream |
| 94 | + */ |
| 95 | + private void checkFooter(ChecksumIndexInput indexInput) throws IOException { |
| 96 | + CodecUtil.checkFooter(indexInput); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Writes header with {@code this.codec} and {@code this.currentVersion} to the metadata file output stream |
| 101 | + * @param indexOutput metadata file output stream |
| 102 | + */ |
| 103 | + private void writeHeader(IndexOutput indexOutput) throws IOException { |
| 104 | + CodecUtil.writeHeader(indexOutput, this.codec, this.currentVersion); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Writes footer with checksum of contents of metadata file output stream |
| 109 | + * @param indexOutput metadata file output stream |
| 110 | + */ |
| 111 | + private void writeFooter(IndexOutput indexOutput) throws IOException { |
| 112 | + CodecUtil.writeFooter(indexOutput); |
| 113 | + } |
44 | 114 | } |
0 commit comments