|
| 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 | +package org.apache.carbondata.core.metadata; |
| 18 | + |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.BufferedWriter; |
| 21 | +import java.io.DataInputStream; |
| 22 | +import java.io.DataOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.OutputStreamWriter; |
| 26 | +import java.io.Serializable; |
| 27 | +import java.nio.charset.Charset; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import org.apache.carbondata.core.constants.CarbonCommonConstants; |
| 33 | +import org.apache.carbondata.core.datastore.filesystem.CarbonFile; |
| 34 | +import org.apache.carbondata.core.datastore.filesystem.CarbonFileFilter; |
| 35 | +import org.apache.carbondata.core.datastore.impl.FileFactory; |
| 36 | +import org.apache.carbondata.core.fileoperations.AtomicFileOperations; |
| 37 | +import org.apache.carbondata.core.fileoperations.AtomicFileOperationsImpl; |
| 38 | +import org.apache.carbondata.core.fileoperations.FileWriteOperation; |
| 39 | +import org.apache.carbondata.core.util.CarbonUtil; |
| 40 | +import org.apache.carbondata.core.util.path.CarbonTablePath; |
| 41 | + |
| 42 | +import com.google.gson.Gson; |
| 43 | + |
| 44 | +/** |
| 45 | + * Provide read and write support for partition mapping file in each segment |
| 46 | + */ |
| 47 | +public class PartitionMapFileStore { |
| 48 | + |
| 49 | + /** |
| 50 | + * Write partitionmapp file to the segment folder with indexfilename and corresponding partitions. |
| 51 | + * |
| 52 | + * @param segmentPath |
| 53 | + * @param taskNo |
| 54 | + * @param partionNames |
| 55 | + * @throws IOException |
| 56 | + */ |
| 57 | + public void writePartitionMapFile(String segmentPath, final String taskNo, |
| 58 | + List<String> partionNames) throws IOException { |
| 59 | + CarbonFile carbonFile = FileFactory.getCarbonFile(segmentPath); |
| 60 | + // write partition info to new file. |
| 61 | + if (carbonFile.exists() && partionNames.size() > 0) { |
| 62 | + CarbonFile[] carbonFiles = carbonFile.listFiles(new CarbonFileFilter() { |
| 63 | + @Override public boolean accept(CarbonFile file) { |
| 64 | + return file.getName().startsWith(taskNo) && file.getName() |
| 65 | + .endsWith(CarbonTablePath.INDEX_FILE_EXT); |
| 66 | + } |
| 67 | + }); |
| 68 | + if (carbonFiles != null && carbonFiles.length > 0) { |
| 69 | + PartitionMapper partitionMapper = new PartitionMapper(); |
| 70 | + Map<String, List<String>> partitionMap = new HashMap<>(); |
| 71 | + partitionMap.put(carbonFiles[0].getName(), partionNames); |
| 72 | + partitionMapper.setPartitionMap(partitionMap); |
| 73 | + String path = segmentPath + "/" + taskNo + CarbonTablePath.PARTITION_MAP_EXT; |
| 74 | + writePartitionFile(partitionMapper, path); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void writePartitionFile(PartitionMapper partitionMapper, String path) throws IOException { |
| 80 | + AtomicFileOperations fileWrite = |
| 81 | + new AtomicFileOperationsImpl(path, FileFactory.getFileType(path)); |
| 82 | + BufferedWriter brWriter = null; |
| 83 | + DataOutputStream dataOutputStream = null; |
| 84 | + Gson gsonObjectToWrite = new Gson(); |
| 85 | + try { |
| 86 | + dataOutputStream = fileWrite.openForWrite(FileWriteOperation.OVERWRITE); |
| 87 | + brWriter = new BufferedWriter(new OutputStreamWriter(dataOutputStream, |
| 88 | + Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET))); |
| 89 | + |
| 90 | + String metadataInstance = gsonObjectToWrite.toJson(partitionMapper); |
| 91 | + brWriter.write(metadataInstance); |
| 92 | + } finally { |
| 93 | + if (null != brWriter) { |
| 94 | + brWriter.flush(); |
| 95 | + } |
| 96 | + CarbonUtil.closeStreams(brWriter); |
| 97 | + fileWrite.close(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Merge all partition files in a segment to single file. |
| 103 | + * |
| 104 | + * @param segmentPath |
| 105 | + * @throws IOException |
| 106 | + */ |
| 107 | + public void mergePartitionMapFiles(String segmentPath) throws IOException { |
| 108 | + CarbonFile carbonFile = FileFactory.getCarbonFile(segmentPath); |
| 109 | + if (carbonFile.exists()) { |
| 110 | + CarbonFile[] carbonFiles = carbonFile.listFiles(new CarbonFileFilter() { |
| 111 | + @Override public boolean accept(CarbonFile file) { |
| 112 | + return file.getName().endsWith(CarbonTablePath.PARTITION_MAP_EXT); |
| 113 | + } |
| 114 | + }); |
| 115 | + if (carbonFiles != null && carbonFiles.length > 1) { |
| 116 | + PartitionMapper partitionMapper = null; |
| 117 | + for (CarbonFile file : carbonFiles) { |
| 118 | + PartitionMapper localMapper = readPartitionMap(file.getAbsolutePath()); |
| 119 | + if (partitionMapper == null && localMapper != null) { |
| 120 | + partitionMapper = localMapper; |
| 121 | + } |
| 122 | + if (localMapper != null) { |
| 123 | + partitionMapper = partitionMapper.merge(localMapper); |
| 124 | + } |
| 125 | + } |
| 126 | + if (partitionMapper != null) { |
| 127 | + String path = segmentPath + "/" + "mergedpartitions" + CarbonTablePath.PARTITION_MAP_EXT; |
| 128 | + writePartitionFile(partitionMapper, path); |
| 129 | + for (CarbonFile file : carbonFiles) { |
| 130 | + FileFactory.deleteAllCarbonFilesOfDir(file); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * This method reads the partition file |
| 139 | + * |
| 140 | + * @param partitionMapPath |
| 141 | + * @return |
| 142 | + */ |
| 143 | + public PartitionMapper readPartitionMap(String partitionMapPath) { |
| 144 | + Gson gsonObjectToRead = new Gson(); |
| 145 | + DataInputStream dataInputStream = null; |
| 146 | + BufferedReader buffReader = null; |
| 147 | + InputStreamReader inStream = null; |
| 148 | + PartitionMapper partitionMapper; |
| 149 | + AtomicFileOperations fileOperation = |
| 150 | + new AtomicFileOperationsImpl(partitionMapPath, FileFactory.getFileType(partitionMapPath)); |
| 151 | + |
| 152 | + try { |
| 153 | + if (!FileFactory.isFileExist(partitionMapPath, FileFactory.getFileType(partitionMapPath))) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + dataInputStream = fileOperation.openForRead(); |
| 157 | + inStream = new InputStreamReader(dataInputStream, |
| 158 | + Charset.forName(CarbonCommonConstants.DEFAULT_CHARSET)); |
| 159 | + buffReader = new BufferedReader(inStream); |
| 160 | + partitionMapper = gsonObjectToRead.fromJson(buffReader, PartitionMapper.class); |
| 161 | + } catch (IOException e) { |
| 162 | + return null; |
| 163 | + } finally { |
| 164 | + CarbonUtil.closeStreams(buffReader, inStream, dataInputStream); |
| 165 | + } |
| 166 | + |
| 167 | + return partitionMapper; |
| 168 | + } |
| 169 | + |
| 170 | + public static class PartitionMapper implements Serializable { |
| 171 | + |
| 172 | + private static final long serialVersionUID = 3582245668420401089L; |
| 173 | + |
| 174 | + private Map<String, List<String>> partitionMap; |
| 175 | + |
| 176 | + public PartitionMapper merge(PartitionMapper mapper) { |
| 177 | + if (this == mapper) { |
| 178 | + return this; |
| 179 | + } |
| 180 | + if (partitionMap != null && mapper.partitionMap != null) { |
| 181 | + partitionMap.putAll(mapper.partitionMap); |
| 182 | + } |
| 183 | + if (partitionMap == null) { |
| 184 | + partitionMap = mapper.partitionMap; |
| 185 | + } |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + public Map<String, List<String>> getPartitionMap() { |
| 190 | + return partitionMap; |
| 191 | + } |
| 192 | + |
| 193 | + public void setPartitionMap(Map<String, List<String>> partitionMap) { |
| 194 | + this.partitionMap = partitionMap; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | +} |
0 commit comments