|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2018, Chris Fraire <[email protected]>. |
| 22 | + */ |
| 23 | + |
| 24 | +package org.opensolaris.opengrok.index; |
| 25 | + |
| 26 | +import java.io.ByteArrayInputStream; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.ObjectInputStream; |
| 30 | +import java.io.ObjectOutputStream; |
| 31 | +import java.io.Serializable; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +/** |
| 37 | + * Represents a serializable gathering of some top-level metadata concerning the |
| 38 | + * operation of {@link IndexDatabase} -- and persisted therein too -- which are |
| 39 | + * re-compared upon each indexing run since changes to them might require |
| 40 | + * re-indexing particular files or in certain cases all files. |
| 41 | + */ |
| 42 | +public final class IndexAnalysisSettings2 implements Serializable { |
| 43 | + |
| 44 | + private static final long serialVersionUID = 1172403004716059609L; |
| 45 | + |
| 46 | + private String projectName; |
| 47 | + |
| 48 | + /** |
| 49 | + * (nullable to allow easing this object into existing OpenGrok indexes |
| 50 | + * without forcing a re-indexing) |
| 51 | + * @serial |
| 52 | + */ |
| 53 | + private Integer tabSize; |
| 54 | + |
| 55 | + /** |
| 56 | + * (nullable to allow easing this object into existing OpenGrok indexes |
| 57 | + * without forcing a re-indexing) |
| 58 | + * @serial |
| 59 | + */ |
| 60 | + private Long analyzerGuruVersion; |
| 61 | + |
| 62 | + /** |
| 63 | + * (nullable because otherwise custom de-serialization does not work, as a |
| 64 | + * {@code final} initialized value may not actually happen because Java |
| 65 | + * de-serialization circumvents normal construction.) |
| 66 | + * @serial |
| 67 | + */ |
| 68 | + private Map<String, Long> analyzersVersions = new HashMap<>(); |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets the project name to be used to distinguish different instances of |
| 72 | + * {@link IndexAnalysisSettings} that might be returned by a Lucene |
| 73 | + * {@code MultiReader} search across projects. |
| 74 | + * @return projectName |
| 75 | + */ |
| 76 | + public String getProjectName() { |
| 77 | + return projectName; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the project name to be used to distinguish different instances of |
| 82 | + * {@link IndexAnalysisSettings} that might be returned by a Lucene |
| 83 | + * {@code MultiReader} search across projects. |
| 84 | + * @param value |
| 85 | + */ |
| 86 | + public void setProjectName(String value) { |
| 87 | + this.projectName = value; |
| 88 | + } |
| 89 | + |
| 90 | + public Integer getTabSize() { |
| 91 | + return tabSize; |
| 92 | + } |
| 93 | + |
| 94 | + public void setTabSize(Integer value) { |
| 95 | + this.tabSize = value; |
| 96 | + } |
| 97 | + |
| 98 | + public Long getAnalyzerGuruVersion() { |
| 99 | + return analyzerGuruVersion; |
| 100 | + } |
| 101 | + |
| 102 | + public void setAnalyzerGuruVersion(Long value) { |
| 103 | + this.analyzerGuruVersion = value; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets the version number for the specified file type name if it exists |
| 108 | + * @return a defined value or {@code null} if unknown |
| 109 | + */ |
| 110 | + public Long getAnalyzerVersion(String fileTypeName) { |
| 111 | + return analyzersVersions.get(fileTypeName); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets an unmodifiable view of the map of file type names to version |
| 116 | + * numbers. |
| 117 | + * @return a defined instance |
| 118 | + */ |
| 119 | + public Map<String, Long> getAnalyzersVersions() { |
| 120 | + return Collections.unmodifiableMap(analyzersVersions); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Replaces the contents of the instance's map with the {@code values}. |
| 125 | + * @param values a defined instance |
| 126 | + */ |
| 127 | + public void setAnalyzersVersions(Map<String, Long> values) { |
| 128 | + analyzersVersions.clear(); |
| 129 | + analyzersVersions.putAll(values); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Creates a binary representation of this object. |
| 134 | + * @return a byte array representing this object |
| 135 | + * @throws IOException Any exception thrown by the underlying |
| 136 | + * OutputStream. |
| 137 | + */ |
| 138 | + public byte[] serialize() throws IOException { |
| 139 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 140 | + new ObjectOutputStream(bytes).writeObject(this); |
| 141 | + return bytes.toByteArray(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * De-serializes a binary representation of an {@link IndexAnalysisSettings} |
| 146 | + * object. |
| 147 | + * @param bytes a byte array containing the serialization |
| 148 | + * @return a defined instance |
| 149 | + * @throws IOException Any of the usual Input/Output related exceptions. |
| 150 | + * @throws ClassNotFoundException Class of a serialized object cannot be |
| 151 | + * found. |
| 152 | + * @throws ClassCastException if the array contains an object of another |
| 153 | + * type than {@code IndexAnalysisSettings} |
| 154 | + */ |
| 155 | + public static IndexAnalysisSettings2 deserialize(byte[] bytes) |
| 156 | + throws IOException, ClassNotFoundException { |
| 157 | + ObjectInputStream in = new ObjectInputStream( |
| 158 | + new ByteArrayInputStream(bytes)); |
| 159 | + return (IndexAnalysisSettings2)in.readObject(); |
| 160 | + } |
| 161 | + |
| 162 | + private void readObject(ObjectInputStream in) throws ClassNotFoundException, |
| 163 | + IOException { |
| 164 | + |
| 165 | + boolean hasValue = in.readBoolean(); |
| 166 | + String vstring = in.readUTF(); |
| 167 | + projectName = hasValue ? vstring : null; |
| 168 | + |
| 169 | + hasValue = in.readBoolean(); |
| 170 | + int vint = in.readInt(); |
| 171 | + tabSize = hasValue ? vint : null; |
| 172 | + |
| 173 | + hasValue = in.readBoolean(); |
| 174 | + long vlong = in.readLong(); |
| 175 | + analyzerGuruVersion = hasValue ? vlong : null; |
| 176 | + |
| 177 | + /** |
| 178 | + * De-serialization circumvents normal construction, so the following |
| 179 | + * field could be null. |
| 180 | + */ |
| 181 | + if (analyzersVersions == null) { |
| 182 | + analyzersVersions = new HashMap<>(); |
| 183 | + } |
| 184 | + int analyzerCount = in.readInt(); |
| 185 | + for (int i = 0; i < analyzerCount; ++i) { |
| 186 | + vstring = in.readUTF(); |
| 187 | + vlong = in.readLong(); |
| 188 | + analyzersVersions.put(vstring, vlong); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private void writeObject(ObjectOutputStream out) throws IOException { |
| 193 | + out.writeBoolean(projectName != null); // hasValue |
| 194 | + out.writeUTF(projectName == null ? "" : projectName); |
| 195 | + |
| 196 | + out.writeBoolean(tabSize != null); // hasValue |
| 197 | + out.writeInt(tabSize == null ? 0 : tabSize); |
| 198 | + |
| 199 | + out.writeBoolean(analyzerGuruVersion != null); // hasValue |
| 200 | + out.writeLong(analyzerGuruVersion == null ? 0 : analyzerGuruVersion); |
| 201 | + |
| 202 | + int analyzerCount = analyzersVersions.size(); |
| 203 | + out.writeInt(analyzerCount); |
| 204 | + for (Map.Entry<String, Long> entry : analyzersVersions.entrySet()) { |
| 205 | + out.writeUTF(entry.getKey()); |
| 206 | + out.writeLong(entry.getValue()); |
| 207 | + --analyzerCount; |
| 208 | + } |
| 209 | + if (analyzerCount != 0) { |
| 210 | + throw new IllegalStateException("analyzersVersions were modified"); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments