[vfs] cleanup & refactoring

+ changed method names, added javadoc

GitOrigin-RevId: 1277e85187b2fb8a6fdeccff8ed786806ba4d6db
This commit is contained in:
Ruslan Cheremin
2024-06-29 13:41:27 +02:00
committed by intellij-monorepo-bot
parent bd2766f742
commit 4c3b919103
5 changed files with 45 additions and 36 deletions

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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++
}

View File

@@ -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;