avoid mixing languages in API

GitOrigin-RevId: 2dece4bbc5653092c1491d22ce4e9670f6cc56bb
This commit is contained in:
Eugene Zhuravlev
2024-09-04 14:57:55 +02:00
committed by intellij-monorepo-bot
parent becce181ac
commit e71458add8
8 changed files with 89 additions and 99 deletions

View File

@@ -0,0 +1,66 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.incremental;
import com.dynatrace.hash4j.hashing.HashSink;
import com.dynatrace.hash4j.hashing.Hashing;
import com.intellij.openapi.util.SystemInfoRt;
import com.intellij.openapi.util.text.StringUtilRt;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.incremental.storage.ProjectStamps;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
@ApiStatus.Internal
public final class FileHashUtil {
/**
* @param path a normalized system-independent path, possibly relative, but without "." and ".." relative references
* @param hash hash sink to be updated
*/
public static void computePathHashCode(@Nullable String path, HashSink hash) {
int length = path == null? 0 : path.length();
if (length == 0) {
hash.putInt(0);
}
else if (ProjectStamps.PORTABLE_CACHES || SystemInfoRt.isFileSystemCaseSensitive) {
hash.putString(path);
}
else {
for (int idx = 0; idx < length; idx++) {
hash.putChar(StringUtilRt.toLowerCase(path.charAt(idx)));
}
hash.putInt(length);
}
}
public static long getFileHash(Path file) throws IOException {
var hash = Hashing.komihash5_0().hashStream();
getFileHash(file, hash);
return hash.getAsLong();
}
public static void getFileHash(Path file, HashSink hash) throws IOException {
try (FileChannel channel = FileChannel.open(file, StandardOpenOption.READ)) {
long fileSize = channel.size();
ByteBuffer buffer = ByteBuffer.allocate(256 * 1024);
long offset = 0L;
while (offset < fileSize) {
buffer.clear();
int readBytes = channel.read(buffer, offset);
if (readBytes > 0) {
hash.putBytes(buffer.array(), 0, readBytes);
offset += readBytes;
}
else {
break;
}
}
hash.putLong(fileSize);
}
}
}

View File

@@ -1,65 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.jps.incremental
import com.dynatrace.hash4j.hashing.HashSink
import com.dynatrace.hash4j.hashing.Hashing
import com.intellij.openapi.util.SystemInfoRt
import com.intellij.openapi.util.text.StringUtilRt
import org.jetbrains.annotations.ApiStatus.Internal
import org.jetbrains.jps.incremental.storage.ProjectStamps
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.nio.file.Path
import java.nio.file.StandardOpenOption
@Internal
@Throws(IOException::class)
fun getFileHash(file: Path): Long {
val hash = Hashing.komihash5_0().hashStream()
getFileHash(file, hash)
return hash.asLong
}
@Internal
@Throws(IOException::class)
fun getFileHash(file: Path, hash: HashSink) {
FileChannel.open(file, StandardOpenOption.READ).use { channel ->
val fileSize = channel.size()
val buffer = ByteBuffer.allocate(256 * 1024)
var offset = 0L
var readBytes: Int
while (offset < fileSize) {
buffer.clear()
readBytes = channel.read(buffer, offset)
if (readBytes <= 0) {
break
}
hash.putBytes(buffer.array(), 0, readBytes)
offset += readBytes
}
hash.putLong(fileSize)
}
}
/** path must be absolute ([Path.toAbsolutePath]), normalized ([Path.normalize]) and system-independent */
@Internal
fun normalizedPathHashCode(path: String, hash: HashSink) {
if (path.isEmpty()) {
hash.putInt(0)
return
}
val length = path.length
if (ProjectStamps.PORTABLE_CACHES || SystemInfoRt.isFileSystemCaseSensitive) {
hash.putChars(path)
}
else {
for (offset in 0 until length) {
hash.putChar(StringUtilRt.toLowerCase(path[offset]))
}
}
hash.putInt(length)
}

View File

@@ -43,9 +43,6 @@ import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.jetbrains.jps.incremental.FileHashUtilKt.getFileHash;
import static org.jetbrains.jps.incremental.FileHashUtilKt.normalizedPathHashCode;
/**
* Describes a step of compilation process which produces JVM *.class files from files in production/test source roots of a Java module.
* These targets are built by {@link ModuleLevelBuilder} and they are the only targets that can have circular dependencies on each other.
@@ -189,7 +186,7 @@ public final class ModuleBuildTarget extends JVMModuleBuildTarget<JavaSourceRoot
if (logBuilder != null) {
logBuilder.append(path).append('\n');
}
normalizedPathHashCode(path, hash);
FileHashUtil.computePathHashCode(path, hash);
}
hash.putInt(roots.size());
@@ -256,9 +253,7 @@ public final class ModuleBuildTarget extends JVMModuleBuildTarget<JavaSourceRoot
}
}
private void getDependenciesFingerprint(@Nullable StringBuilder logBuilder,
@NotNull PathRelativizerService relativizer,
@NotNull HashSink hash) {
private void getDependenciesFingerprint(@Nullable StringBuilder logBuilder, @NotNull PathRelativizerService relativizer, @NotNull HashSink hash) {
if (!REBUILD_ON_DEPENDENCY_CHANGE) {
hash.putInt(0);
return;
@@ -285,27 +280,24 @@ public final class ModuleBuildTarget extends JVMModuleBuildTarget<JavaSourceRoot
}
logBuilder.append("\n");
}
normalizedPathHashCode(path, hash);
FileHashUtil.computePathHashCode(path, hash);
}
hash.putInt(roots.size());
}
private static void getContentHash(Path file, HashSink hash) {
if (!ProjectStamps.TRACK_LIBRARY_CONTENT) {
hash.putInt(0);
return;
}
try {
if (Files.isRegularFile(file) && file.getFileName().endsWith(".jar")) {
getFileHash(file, hash);
if (ProjectStamps.TRACK_LIBRARY_CONTENT) {
try {
if (Files.isRegularFile(file) && file.getFileName().endsWith(".jar")) {
FileHashUtil.getFileHash(file, hash);
}
else {
hash.putInt(0);
}
}
else {
hash.putInt(0);
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@@ -32,8 +32,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.jps.incremental.FileHashUtilKt.normalizedPathHashCode;
/**
* Describes a step of compilation process which copies resource's files from source and resource roots of a Java module.
*/
@@ -122,7 +120,7 @@ public final class ResourcesTarget extends JVMModuleBuildTarget<ResourceRootDesc
List<ResourceRootDescriptor> roots = projectDescriptor.getBuildRootIndex().getTargetRoots(this, null);
for (ResourceRootDescriptor root : roots) {
String path = relativizer.toRelative(root.rootFile.toString());
normalizedPathHashCode(path, hash);
FileHashUtil.computePathHashCode(path, hash);
hash.putString(root.getPackagePrefix());
}
hash.putInt(roots.size());

View File

@@ -23,7 +23,7 @@ import org.jetbrains.jps.cache.model.BuildTargetState;
import org.jetbrains.jps.cmdline.ProjectDescriptor;
import org.jetbrains.jps.incremental.BuildListener;
import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.FileHashUtilKt;
import org.jetbrains.jps.incremental.FileHashUtil;
import org.jetbrains.jps.incremental.messages.FileDeletedEvent;
import org.jetbrains.jps.incremental.messages.FileGeneratedEvent;
import org.jetbrains.jps.incremental.relativizer.PathRelativizerService;
@@ -334,7 +334,7 @@ public final class BuildTargetSourcesState implements BuildListener {
private static long getOutputFileHash(@NotNull Path file, @NotNull Path rootPath, @NotNull HashStream64 hashToReuse) throws IOException {
// reduce GC - reuse hashToReuse - do not inline fileHash variable
FileHashUtilKt.getFileHash(file, hashToReuse.reset());
FileHashUtil.getFileHash(file, hashToReuse.reset());
long fileHash = hashToReuse.getAsLong();
return hashToReuse
.reset()

View File

@@ -5,6 +5,7 @@ import com.intellij.util.ArrayUtil;
import com.intellij.util.io.DataExternalizer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.builders.BuildTarget;
import org.jetbrains.jps.incremental.FileHashUtil;
import org.jetbrains.jps.incremental.relativizer.PathRelativizerService;
import java.io.DataInput;
@@ -14,7 +15,6 @@ import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import static org.jetbrains.jps.incremental.FileHashUtilKt.getFileHash;
import static org.jetbrains.jps.incremental.storage.FileTimestampStorage.FileTimestamp;
import static org.jetbrains.jps.incremental.storage.HashStampStorage.HashStampPerTarget;
@@ -122,7 +122,7 @@ final class HashStampStorage extends AbstractStateStorage<String, HashStampPerTa
@Override
public HashStamp getCurrentStamp(Path file) throws IOException {
FileTimestamp currentTimestamp = timestampStorage.getCurrentStamp(file);
return new HashStamp(getFileHash(file), currentTimestamp.asLong());
return new HashStamp(FileHashUtil.getFileHash(file), currentTimestamp.asLong());
}
@Override
@@ -141,7 +141,7 @@ final class HashStampStorage extends AbstractStateStorage<String, HashStampPerTa
return true;
}
return hash != getFileHash(file.toPath());
return hash != FileHashUtil.getFileHash(file.toPath());
}
@Override
@@ -160,7 +160,7 @@ final class HashStampStorage extends AbstractStateStorage<String, HashStampPerTa
return true;
}
return hash != getFileHash(file.toPath());
return hash != FileHashUtil.getFileHash(file.toPath());
}
@Override

View File

@@ -23,6 +23,6 @@ class FileHashUtilTest {
private fun doTest(dir: Path, data: ByteArray) {
val file = dir.resolve("test.txt")
Files.write(file, data)
assertThat(getFileHash(file)).isEqualTo(Hashing.komihash5_0().hashStream().putBytes(data).putLong(data.size.toLong()).asLong)
assertThat(FileHashUtil.getFileHash(file)).isEqualTo(Hashing.komihash5_0().hashStream().putBytes(data).putLong(data.size.toLong()).asLong)
}
}

View File

@@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.builders.storage.BuildDataPaths;
import org.jetbrains.jps.incremental.CompileContext;
import org.jetbrains.jps.incremental.FileHashUtilKt;
import org.jetbrains.jps.incremental.FileHashUtil;
import org.jetbrains.jps.incremental.artifacts.instructions.ArtifactRootCopyingHandlerProvider;
import org.jetbrains.jps.incremental.artifacts.instructions.FileCopyingHandler;
import org.jetbrains.jps.incremental.artifacts.instructions.FilterCopyHandler;
@@ -183,8 +183,7 @@ public final class MavenWebArtifactRootCopyingHandlerProvider extends ArtifactRo
protected void configurationHash(@NotNull HashSink hash) {
super.configurationHash(hash);
FileHashUtilKt.normalizedPathHashCode(myTargetDir.toPath().toAbsolutePath().normalize().toString(), hash);
FileHashUtil.computePathHashCode(myTargetDir.toPath().toAbsolutePath().normalize().toString(), hash);
for (ResourceRootConfiguration webResource : myWebResources) {
hash.putUnorderedIterable(webResource.includes, HashFunnel.forString(), Hashing.komihash5_0());
hash.putUnorderedIterable(webResource.excludes, HashFunnel.forString(), Hashing.komihash5_0());