|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.fs; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +/** |
| 25 | + * Immutable record describing a file or directory in a storage system. |
| 26 | + * <p> |
| 27 | + * Replaces {@link org.apache.doris.fs.remote.RemoteFile} with zero Hadoop dependency. |
| 28 | + * Block location information uses {@link BlockInfo} instead of {@code BlockLocation[]}. |
| 29 | + */ |
| 30 | +public final class FileEntry { |
| 31 | + |
| 32 | + private final Location location; |
| 33 | + private final boolean directory; |
| 34 | + private final long length; |
| 35 | + private final long blockSize; |
| 36 | + private final long modificationTime; |
| 37 | + private final List<BlockInfo> blocks; |
| 38 | + |
| 39 | + private FileEntry(Builder builder) { |
| 40 | + this.location = Objects.requireNonNull(builder.location, "location"); |
| 41 | + this.directory = builder.directory; |
| 42 | + this.length = builder.length; |
| 43 | + this.blockSize = builder.blockSize; |
| 44 | + this.modificationTime = builder.modificationTime; |
| 45 | + this.blocks = builder.blocks == null |
| 46 | + ? Collections.emptyList() |
| 47 | + : Collections.unmodifiableList(builder.blocks); |
| 48 | + } |
| 49 | + |
| 50 | + /** Full location (URI) of this file or directory. */ |
| 51 | + public Location location() { |
| 52 | + return location; |
| 53 | + } |
| 54 | + |
| 55 | + /** Returns the last component of the path (file or directory name). */ |
| 56 | + public String name() { |
| 57 | + return location.name(); |
| 58 | + } |
| 59 | + |
| 60 | + public boolean isDirectory() { |
| 61 | + return directory; |
| 62 | + } |
| 63 | + |
| 64 | + public boolean isFile() { |
| 65 | + return !directory; |
| 66 | + } |
| 67 | + |
| 68 | + /** File size in bytes. -1 if unknown. */ |
| 69 | + public long length() { |
| 70 | + return length; |
| 71 | + } |
| 72 | + |
| 73 | + /** Underlying block size of the storage (e.g., HDFS 128 MB). 0 for object storage. */ |
| 74 | + public long blockSize() { |
| 75 | + return blockSize; |
| 76 | + } |
| 77 | + |
| 78 | + /** Last-modified time in milliseconds since epoch. */ |
| 79 | + public long modificationTime() { |
| 80 | + return modificationTime; |
| 81 | + } |
| 82 | + |
| 83 | + /** Block location hints for split planning. Empty list for object storage. */ |
| 84 | + public List<BlockInfo> blocks() { |
| 85 | + return blocks; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String toString() { |
| 90 | + return "FileEntry{location=" + location |
| 91 | + + ", directory=" + directory |
| 92 | + + ", length=" + length |
| 93 | + + ", modificationTime=" + modificationTime + "}"; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean equals(Object o) { |
| 98 | + if (this == o) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + if (!(o instanceof FileEntry)) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + FileEntry that = (FileEntry) o; |
| 105 | + return directory == that.directory |
| 106 | + && length == that.length |
| 107 | + && modificationTime == that.modificationTime |
| 108 | + && location.equals(that.location); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hash(location, directory, length, modificationTime); |
| 114 | + } |
| 115 | + |
| 116 | + public static Builder builder(Location location) { |
| 117 | + return new Builder(location); |
| 118 | + } |
| 119 | + |
| 120 | + /** Convenience factory for simple file entries (no block info). */ |
| 121 | + public static FileEntry file(Location location, long length, long modificationTime) { |
| 122 | + return builder(location).length(length).modificationTime(modificationTime).build(); |
| 123 | + } |
| 124 | + |
| 125 | + /** Convenience factory for directory entries. */ |
| 126 | + public static FileEntry directory(Location location, long modificationTime) { |
| 127 | + return builder(location).directory(true).modificationTime(modificationTime).build(); |
| 128 | + } |
| 129 | + |
| 130 | + public static final class Builder { |
| 131 | + private final Location location; |
| 132 | + private boolean directory = false; |
| 133 | + private long length = -1L; |
| 134 | + private long blockSize = 0L; |
| 135 | + private long modificationTime = 0L; |
| 136 | + private List<BlockInfo> blocks; |
| 137 | + |
| 138 | + private Builder(Location location) { |
| 139 | + this.location = Objects.requireNonNull(location); |
| 140 | + } |
| 141 | + |
| 142 | + public Builder directory(boolean isDirectory) { |
| 143 | + this.directory = isDirectory; |
| 144 | + return this; |
| 145 | + } |
| 146 | + |
| 147 | + public Builder length(long fileLength) { |
| 148 | + this.length = fileLength; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + public Builder blockSize(long bSize) { |
| 153 | + this.blockSize = bSize; |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + public Builder modificationTime(long modTime) { |
| 158 | + this.modificationTime = modTime; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + public Builder blocks(List<BlockInfo> blockList) { |
| 163 | + this.blocks = blockList; |
| 164 | + return this; |
| 165 | + } |
| 166 | + |
| 167 | + public Builder addBlock(BlockInfo block) { |
| 168 | + if (this.blocks == null) { |
| 169 | + this.blocks = new java.util.ArrayList<>(); |
| 170 | + } |
| 171 | + this.blocks.add(block); |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + public FileEntry build() { |
| 176 | + return new FileEntry(this); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Lightweight replacement for {@code org.apache.hadoop.fs.BlockLocation}. |
| 182 | + * Contains only the data needed by Doris split planning. |
| 183 | + */ |
| 184 | + public static final class BlockInfo { |
| 185 | + private final long offset; |
| 186 | + private final long length; |
| 187 | + private final List<String> hosts; |
| 188 | + |
| 189 | + public BlockInfo(long offset, long length, List<String> hosts) { |
| 190 | + this.offset = offset; |
| 191 | + this.length = length; |
| 192 | + this.hosts = hosts == null ? Collections.emptyList() : Collections.unmodifiableList(hosts); |
| 193 | + } |
| 194 | + |
| 195 | + public long offset() { |
| 196 | + return offset; |
| 197 | + } |
| 198 | + |
| 199 | + public long length() { |
| 200 | + return length; |
| 201 | + } |
| 202 | + |
| 203 | + /** Data-local host names for this block (for scheduling affinity). */ |
| 204 | + public List<String> hosts() { |
| 205 | + return hosts; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public String toString() { |
| 210 | + return "BlockInfo{offset=" + offset + ", length=" + length + ", hosts=" + hosts + "}"; |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments