From 4c3b919103432dee08d0a2ca32782d74fd8372c5 Mon Sep 17 00:00:00 2001 From: Ruslan Cheremin Date: Sat, 29 Jun 2024 13:41:27 +0200 Subject: [PATCH] [vfs] cleanup & refactoring + changed method names, added javadoc GitOrigin-RevId: 1277e85187b2fb8a6fdeccff8ed786806ba4d6db --- .../vfs/newvfs/persistent/FSRecordsImpl.java | 9 ++- .../PersistentFSContentAccessor.java | 9 ++- .../newvfs/persistent/PersistentFSImpl.java | 57 +++++++++---------- .../vfs/newvfs/persistent/VfsRecoveryUtils.kt | 2 +- .../util/io/storage/VFSContentStorage.java | 4 +- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecordsImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecordsImpl.java index dfec6e638550..a74aacc3e44c 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecordsImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/FSRecordsImpl.java @@ -1337,9 +1337,14 @@ public final class FSRecordsImpl implements Closeable { } } - int storeUnlinkedContent(@NotNull ByteArraySequence content) { + /** + * Stores content and return contentRecordId, by which content could be later retrieved. + * If the same content (bytes) was already stored -- method could return id of already existing record, without allocating + * & storing new record. + */ + int writeContentRecord(@NotNull ByteArraySequence content) { try { - return contentAccessor.allocateContentRecordAndStore(content); + return contentAccessor.writeContentRecord(content); } catch (IOException e) { throw handleError(e); diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSContentAccessor.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSContentAccessor.java index e1de602d95ae..2f197271fabc 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSContentAccessor.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSContentAccessor.java @@ -43,13 +43,18 @@ public final class PersistentFSContentAccessor { PersistentFSConnection.ensureIdIsValid(fileId); PersistentFSRecordsStorage records = connection.getRecords(); - int contentRecordId = allocateContentRecordAndStore(content); + int contentRecordId = writeContentRecord(content); records.setContentRecordId(fileId, contentRecordId); } - int allocateContentRecordAndStore(@NotNull ByteArraySequence content) throws IOException { + /** + * Stores content and return contentRecordId, by which content could be later retrieved. + * If the same content (bytes) was already stored -- method could return id of already existing record, without allocating + * & storing new record. + */ + int writeContentRecord(@NotNull ByteArraySequence content) throws IOException { return connection.getContents().storeRecord(content); } diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSImpl.java b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSImpl.java index d8648972cfd1..aa277331f0da 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/PersistentFSImpl.java @@ -488,7 +488,7 @@ public final class PersistentFSImpl extends PersistentFS implements Disposable { @Override public int storeUnlinkedContent(byte @NotNull [] bytes) { - return vfsPeer.storeUnlinkedContent(new ByteArraySequence(bytes)); + return vfsPeer.writeContentRecord(new ByteArraySequence(bytes)); } @SuppressWarnings("removal") @@ -828,15 +828,7 @@ public final class PersistentFSImpl extends PersistentFS implements Disposable { byte[] content = fs.contentsToByteArray(file); if (mayCacheContent && shouldCache(content.length)) { - //VFS content storage is append-only, hence storing could be done outside the lock: - int newContentRecordId = vfsPeer.storeUnlinkedContent(new ByteArraySequence(content)); - myInputLock.writeLock().lock(); - try { - updateContentId(fileId, newContentRecordId, content.length); - } - finally { - myInputLock.writeLock().unlock(); - } + updateContentForFile(fileId, new ByteArraySequence(content)); } else { myInputLock.writeLock().lock(); @@ -992,23 +984,24 @@ public final class PersistentFSImpl extends PersistentFS implements Disposable { if (byteLength == fileLength) { ByteArraySequence newContent = new ByteArraySequence(bytes, 0, byteLength); - int newContentId = vfsPeer.storeUnlinkedContent(newContent); - myInputLock.writeLock().lock(); - try { - updateContentId(fileId, newContentId, byteLength); - } - finally { - myInputLock.writeLock().unlock(); - } + updateContentForFile(fileId, newContent); } else { - myInputLock.writeLock().lock(); - try { - doCleanPersistedContent(fileId); - } - finally { - myInputLock.writeLock().unlock(); - } + doCleanPersistedContent(fileId); + } + } + + private void updateContentForFile(int fileId, + @NotNull ByteArraySequence newContent) throws IOException { + //VFS content storage is append-only, hence storing could be done outside the lock: + int newContentId = vfsPeer.writeContentRecord(newContent); + + myInputLock.writeLock().lock(); + try { + updateContentId(fileId, newContentId, newContent.length()); + } + finally { + myInputLock.writeLock().unlock(); } } @@ -2386,9 +2379,11 @@ public final class PersistentFSImpl extends PersistentFS implements Disposable { private void setFlag(int id, @Attributes int mask, boolean value) { int oldFlags = vfsPeer.getFlags(id); - int flags = value ? oldFlags | mask : oldFlags & ~mask; + int flags = value ? oldFlags | mask + : oldFlags & ~mask; if (oldFlags != flags) { + //noinspection MagicConstant vfsPeer.setFlags(id, flags); } } @@ -2458,9 +2453,13 @@ public final class PersistentFSImpl extends PersistentFS implements Disposable { } private void doCleanPersistedContent(int id) { - //TODO set in single call: setFlag(id, MUST_RELOAD_CONTENT | MUST_RELOAD_LENGTH, true); - setFlag(id, Flags.MUST_RELOAD_CONTENT, true); - setFlag(id, Flags.MUST_RELOAD_LENGTH, true); + myInputLock.writeLock().lock(); + try { + setFlag(id, Flags.MUST_RELOAD_CONTENT | Flags.MUST_RELOAD_LENGTH, true); + } + finally { + myInputLock.writeLock().unlock(); + } } @Override diff --git a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/VfsRecoveryUtils.kt b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/VfsRecoveryUtils.kt index fb7cc5f616e4..da3eb32a91af 100644 --- a/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/VfsRecoveryUtils.kt +++ b/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/persistent/VfsRecoveryUtils.kt @@ -393,7 +393,7 @@ object VfsRecoveryUtils { when (val nextContent = snapshot.getContent(lastRecoveredContentId + 1)) { is NotAvailable -> break is Ready -> { - val result = newFsRecords.contentAccessor().allocateContentRecordAndStore(ByteArraySequence(nextContent.value)) + val result = newFsRecords.contentAccessor().writeContentRecord(ByteArraySequence(nextContent.value)) check(result == lastRecoveredContentId + 1) { "assumption failed: got $result, expected ${lastRecoveredContentId + 1}" } lastRecoveredContentId++ } diff --git a/platform/util/src/com/intellij/util/io/storage/VFSContentStorage.java b/platform/util/src/com/intellij/util/io/storage/VFSContentStorage.java index 48f998c4c41f..dce3e56c5a89 100644 --- a/platform/util/src/com/intellij/util/io/storage/VFSContentStorage.java +++ b/platform/util/src/com/intellij/util/io/storage/VFSContentStorage.java @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.util.io.storage; import com.intellij.openapi.Forceable; @@ -25,7 +25,7 @@ public interface VFSContentStorage extends CleanableStorage, Closeable, Forceabl /** * Stores bytes and return contentRecordId, by which content could be later retrieved. - * If the same bytes was already stored -- method could return id of already existing record, without allocating + * If the same content (bytes) was already stored -- method could return id of already existing record, without allocating * & storing new record. */ int storeRecord(@NotNull ByteArraySequence bytes) throws IOException;