mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
indexes: simplify PagedFileStorage
GitOrigin-RevId: 10f4a41c49408b8e6428d475926b2f83a41d0075
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b0e669ab69
commit
74ef421118
@@ -8,6 +8,7 @@ import com.intellij.util.SystemProperties;
|
||||
import com.intellij.util.containers.ConcurrentIntObjectMap;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.hash.LinkedHashMap;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -93,17 +94,13 @@ public class PagedFileStorage implements Forceable {
|
||||
protected final int myPageSize;
|
||||
protected final boolean myValuesAreBufferAligned;
|
||||
|
||||
public PagedFileStorage(Path file, StorageLock lock) {
|
||||
this(file, lock.myDefaultStorageLockContext, BUFFER_SIZE, false, false);
|
||||
}
|
||||
|
||||
public PagedFileStorage(Path file,
|
||||
@Nullable StorageLockContext storageLockContext,
|
||||
int pageSize,
|
||||
boolean valuesAreBufferAligned,
|
||||
boolean nativeBytesOrder) {
|
||||
myFile = file;
|
||||
myStorageLockContext = storageLockContext != null ? storageLockContext : ourLock.myDefaultStorageLockContext;
|
||||
myStorageLockContext = storageLockContext != null ? storageLockContext : ourLock.myDefaultContext;
|
||||
myPageSize = Math.max(pageSize > 0 ? pageSize : BUFFER_SIZE, Page.PAGE_SIZE);
|
||||
myValuesAreBufferAligned = valuesAreBufferAligned;
|
||||
myStorageIndex = myStorageLockContext.myStorageLock.registerPagedFileStorage(this);
|
||||
@@ -462,10 +459,11 @@ public class PagedFileStorage implements Forceable {
|
||||
return isDirty;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static class StorageLock {
|
||||
private static final int FILE_INDEX_MASK = 0xFFFF0000;
|
||||
private static final int FILE_INDEX_SHIFT = 16;
|
||||
public final StorageLockContext myDefaultStorageLockContext;
|
||||
public final StorageLockContext myDefaultContext;
|
||||
private final ConcurrentIntObjectMap<PagedFileStorage> myIndex2Storage = ContainerUtil.createConcurrentIntObjectMap();
|
||||
|
||||
private final LinkedHashMap<Integer, ByteBufferWrapper> mySegments;
|
||||
@@ -478,12 +476,8 @@ public class PagedFileStorage implements Forceable {
|
||||
private volatile long mySizeLimit;
|
||||
private volatile int myMappingChangeCount;
|
||||
|
||||
public StorageLock() {
|
||||
this(true);
|
||||
}
|
||||
|
||||
public StorageLock(boolean checkThreadAccess) {
|
||||
myDefaultStorageLockContext = new StorageLockContext(this, checkThreadAccess);
|
||||
private StorageLock() {
|
||||
myDefaultContext = new StorageLockContext(this, true);
|
||||
|
||||
mySizeLimit = UPPER_LIMIT;
|
||||
mySegments = new LinkedHashMap<Integer, ByteBufferWrapper>(10, 0.75f, true) {
|
||||
@@ -507,14 +501,6 @@ public class PagedFileStorage implements Forceable {
|
||||
};
|
||||
}
|
||||
|
||||
public void lock() {
|
||||
myDefaultStorageLockContext.lock();
|
||||
}
|
||||
|
||||
public void unlock() {
|
||||
myDefaultStorageLockContext.unlock();
|
||||
}
|
||||
|
||||
private int registerPagedFileStorage(@NotNull PagedFileStorage storage) {
|
||||
int registered = myIndex2Storage.size();
|
||||
assert registered <= MAX_LIVE_STORAGES_COUNT;
|
||||
|
||||
@@ -60,14 +60,6 @@ public class ResizeableMappedFile implements Forceable {
|
||||
myLastWrittenLogicalSize = myLogicalSize = readLength();
|
||||
}
|
||||
|
||||
public ResizeableMappedFile(Path file, int initialSize, PagedFileStorage.StorageLock lock, int pageSize, boolean valuesAreBufferAligned) throws IOException {
|
||||
this(file, initialSize, lock.myDefaultStorageLockContext, pageSize, valuesAreBufferAligned);
|
||||
}
|
||||
|
||||
public ResizeableMappedFile(Path file, int initialSize, PagedFileStorage.StorageLock lock) throws IOException {
|
||||
this(file, initialSize, lock, -1, false);
|
||||
}
|
||||
|
||||
public long length() {
|
||||
return myLogicalSize;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -25,7 +24,7 @@ public class PagedFileStorageTest {
|
||||
private static final Logger LOG = Logger.getInstance(PagedFileStorageTest.class);
|
||||
@Rule public TempDirectory tempDir = new TempDirectory();
|
||||
|
||||
private final PagedFileStorage.StorageLock lock = new PagedFileStorage.StorageLock();
|
||||
private final PagedFileStorage.StorageLockContext lock = new PagedFileStorage.StorageLockContext(true);
|
||||
private Path f;
|
||||
private PagedFileStorage s;
|
||||
|
||||
@@ -33,7 +32,7 @@ public class PagedFileStorageTest {
|
||||
public void setUp() throws IOException {
|
||||
withLock(lock, () -> {
|
||||
f = tempDir.newFile("storage").toPath();
|
||||
s = new PagedFileStorage(f, lock);
|
||||
s = new PagedFileStorage(f, lock, PagedFileStorage.BUFFER_SIZE, false, false);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,7 +73,7 @@ public class PagedFileStorageTest {
|
||||
@Test
|
||||
public void testResizeableMappedFile() throws IOException {
|
||||
withLock(lock, () -> {
|
||||
ResizeableMappedFile file = new ResizeableMappedFile(f, 2000000, lock);
|
||||
ResizeableMappedFile file = new ResizeableMappedFile(f, 2000000, lock, -1, false);
|
||||
|
||||
LOG.debug("writing...");
|
||||
long t = System.currentTimeMillis();
|
||||
@@ -110,7 +109,7 @@ public class PagedFileStorageTest {
|
||||
public void testResizeableMappedFile2() throws IOException {
|
||||
withLock(lock, () -> {
|
||||
int initialSize = 4096;
|
||||
ResizeableMappedFile file = new ResizeableMappedFile(f, initialSize, lock.myDefaultStorageLockContext, PagedFileStorage.MB, false);
|
||||
ResizeableMappedFile file = new ResizeableMappedFile(f, initialSize, lock, PagedFileStorage.MB, false);
|
||||
byte[] bytes = StringUtil.repeat("1", initialSize + 2).getBytes(Charsets.UTF_8);
|
||||
assertTrue(bytes.length > initialSize);
|
||||
|
||||
@@ -124,7 +123,7 @@ public class PagedFileStorageTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static void withLock(PagedFileStorage.StorageLock lock, ThrowableRunnable<IOException> block) throws IOException {
|
||||
private static void withLock(PagedFileStorage.StorageLockContext lock, ThrowableRunnable<IOException> block) throws IOException {
|
||||
lock.lock();
|
||||
try {
|
||||
block.run();
|
||||
|
||||
Reference in New Issue
Block a user