From 1a066f1c31dcaafd979c8bb3f17622d72580bd5b Mon Sep 17 00:00:00 2001 From: Sergey Patrikeev Date: Mon, 4 May 2020 19:00:17 +0300 Subject: [PATCH] shared-indexes: cleanup IndexedHashesSupport: keep only hash evaluation method with FileContent parameter. GitOrigin-RevId: 11aab86de8e48aac40f31c20ac21b5837f7f428e --- .../util/indexing/FileBasedIndexImpl.java | 2 +- .../snapshot/IndexedHashesSupport.java | 53 +++---------------- 2 files changed, 7 insertions(+), 48 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index a37bf6ec17ff..23b3761d10b2 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -1186,7 +1186,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx { initFileContent(fc, project == null ? ProjectUtil.guessProjectForFile(file) : project, psiFile); if (FileBasedIndex.ourSnapshotMappingsEnabled) { - IndexedHashesSupport.initIndexedHash(fc); + IndexedHashesSupport.getOrInitIndexedHash(fc); } ProgressManager.checkCanceled(); diff --git a/platform/lang-impl/src/com/intellij/util/indexing/snapshot/IndexedHashesSupport.java b/platform/lang-impl/src/com/intellij/util/indexing/snapshot/IndexedHashesSupport.java index 72faed64163b..845c889fa3e9 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/snapshot/IndexedHashesSupport.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/snapshot/IndexedHashesSupport.java @@ -1,9 +1,7 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.indexing.snapshot; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.ShutDownTracker; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.persistent.FlushingDaemon; import com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl; import com.intellij.util.ArrayUtilRt; @@ -15,7 +13,6 @@ import com.intellij.util.io.DigestUtil; import com.intellij.util.io.IOUtil; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; @@ -26,8 +23,6 @@ import java.security.MessageDigest; @ApiStatus.Internal public class IndexedHashesSupport { - private static final Logger LOG = Logger.getInstance(IndexedHashesSupport.class); - private static final MessageDigest TEXT_CONTENT_HASH_DIGEST = DigestUtil.sha1(); private static volatile ContentHashEnumerator ourTextContentHashes; @@ -63,61 +58,25 @@ public class IndexedHashesSupport { return ourTextContentHashes.enumerate(digest); } - public static void initIndexedHash(@NotNull FileContentImpl content) { - byte[] fileContentHash = calculateIndexedHashForFileContent(content); - content.setHashes(fileContentHash); - } - public static byte @NotNull [] getOrInitIndexedHash(@NotNull FileContentImpl content) { byte[] hash = content.getHash(); if (hash == null) { - initIndexedHash(content); - hash = content.getHash(); - LOG.assertTrue(hash != null); + hash = calculateIndexedHashForFileContent(content); + content.setHashes(hash); } return hash; } - /** - * Calculates hash for this virtual file. Does not load full file content into memory. - * - * Result is the same as if invoking: - * 1) fc = FileContentImpl(virtualFile) - * 2) getOrInitIndexedHash(fc, fromDocument = false) - */ - @ApiStatus.Experimental - @ApiStatus.Internal - public static byte @NotNull [] calculateHashForPhysicalVirtualFileNotCached(@NotNull VirtualFile virtualFile) { - boolean binary = virtualFile.getFileType().isBinary(); - - byte[] contentHash = PersistentFSImpl.getContentHashIfStored(virtualFile); - if (contentHash == null) { - contentHash = DigestUtil.calculateContentHash(TEXT_CONTENT_HASH_DIGEST, virtualFile); - } - - return mergeIndexedHash(contentHash, binary ? null : virtualFile.getCharset()); - } - private static byte @NotNull [] calculateIndexedHashForFileContent(@NotNull FileContentImpl content) { byte[] contentHash = PersistentFSImpl.getContentHashIfStored(content.getFile()); if (contentHash == null) { - contentHash = calculateContentHash(content); + contentHash = DigestUtil.calculateContentHash(TEXT_CONTENT_HASH_DIGEST, ((FileContent)content).getContent()); // todo store content hash in FS } boolean isBinary = content.getFileTypeWithoutSubstitution().isBinary(); - return mergeIndexedHash(contentHash, isBinary ? null : content.getCharset()); - } - - private static byte[] calculateContentHash(@NotNull FileContent content) { - return DigestUtil.calculateContentHash(TEXT_CONTENT_HASH_DIGEST, content.getContent()); - } - - private static byte @NotNull [] mergeIndexedHash(byte @NotNull [] binaryContentHash, - @Nullable Charset charsetOrNullForBinary) { - byte[] charsetBytes = charsetOrNullForBinary != null - ? charsetOrNullForBinary.name().getBytes(StandardCharsets.UTF_8) - : ArrayUtilRt.EMPTY_BYTE_ARRAY; - return DigestUtil.calculateMergedHash(TEXT_CONTENT_HASH_DIGEST, new byte[][]{binaryContentHash, charsetBytes}); + Charset charset = isBinary ? null : content.getCharset(); + byte[] charsetBytes = charset != null ? charset.name().getBytes(StandardCharsets.UTF_8) : ArrayUtilRt.EMPTY_BYTE_ARRAY; + return DigestUtil.calculateMergedHash(TEXT_CONTENT_HASH_DIGEST, new byte[][]{contentHash, charsetBytes}); } } \ No newline at end of file