This commit is contained in:
Dmitry Avdeev
2009-11-13 16:43:06 +03:00
parent 911e0440cf
commit 81c874deda
2 changed files with 38 additions and 89 deletions
@@ -30,8 +30,6 @@ import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.IntArrayList;
import com.intellij.util.io.PersistentStringEnumerator;
import com.intellij.util.io.ResizeableMappedFile;
import com.intellij.util.io.storage.AbstractStorage;
import com.intellij.util.io.storage.CompactStorage;
import com.intellij.util.io.storage.HeavyProcessLatch;
import com.intellij.util.io.storage.Storage;
import gnu.trove.TIntArrayList;
@@ -49,7 +47,7 @@ import java.util.concurrent.TimeUnit;
public class FSRecords implements Disposable, Forceable {
private static final Logger LOG = Logger.getInstance("#com.intellij.vfs.persistent.FSRecords");
private static final int VERSION = 10;
private static final int VERSION = 9;
private static final int PARENT_OFFSET = 0;
private static final int PARENT_SIZE = 4;
@@ -84,6 +82,7 @@ public class FSRecords implements Disposable, Forceable {
private static final String CHILDREN_ATT = "FsRecords.DIRECTORY_CHILDREN";
private static final Object lock = new Object();
private DbConnection myConnection;
private volatile static int ourLocalModificationCount = 0;
@@ -102,7 +101,7 @@ public class FSRecords implements Disposable, Forceable {
private static int CONTENT_ID;
private static PersistentStringEnumerator myNames;
private static AbstractStorage myAttributes;
private static Storage myAttributes;
private static Storage myContents;
private static ResizeableMappedFile myRecords;
private static final TIntArrayList myFreeRecords = new TIntArrayList();
@@ -169,7 +168,7 @@ public class FSRecords implements Disposable, Forceable {
if (!namesFile.exists()) {
invalidateIndex();
}
boolean compact = true;
try {
if (getCorruptionMarkerFile().exists()) {
invalidateIndex();
@@ -177,7 +176,7 @@ public class FSRecords implements Disposable, Forceable {
}
myNames = new PersistentStringEnumerator(namesFile);
myAttributes = compact ? new CompactStorage(attributesFile.getCanonicalPath()) : Storage.create(attributesFile.getCanonicalPath());
myAttributes = Storage.create(attributesFile.getCanonicalPath());
myContents = Storage.create(contentsFile.getCanonicalPath());
myRecords = new ResizeableMappedFile(recordsFile, 20 * 1024);
@@ -204,7 +203,7 @@ public class FSRecords implements Disposable, Forceable {
boolean deleted = true;
deleted &= FileUtil.delete(getCorruptionMarkerFile());
deleted &= deleteWithSubordinates(namesFile);
deleted &= compact ? CompactStorage.deleteFiles(attributesFile.getCanonicalPath()) : Storage.deleteFiles(attributesFile.getCanonicalPath());
deleted &= Storage.deleteFiles(attributesFile.getCanonicalPath());
deleted &= Storage.deleteFiles(contentsFile.getCanonicalPath());
deleted &= deleteWithSubordinates(recordsFile);
@@ -350,7 +349,7 @@ public class FSRecords implements Disposable, Forceable {
return myNames;
}
public static AbstractStorage getAttributes(int attId) {
public static Storage getAttributes(int attId) {
return attId == CONTENT_ID ? myContents : myAttributes;
}
@@ -435,14 +434,14 @@ public class FSRecords implements Disposable, Forceable {
}
public void connect() {
DbConnection.connect();
myConnection = DbConnection.connect();
}
private static ResizeableMappedFile getRecords() {
return DbConnection.getRecords();
}
private static AbstractStorage getAttributes(int attId) {
private static Storage getAttributes(int attId) {
return DbConnection.getAttributes(attId);
}
@@ -515,15 +514,14 @@ public class FSRecords implements Disposable, Forceable {
private void deleteAttribute(int id, int isContent) throws IOException {
int att_page = getAttributeRecordId(id, isContent);
if (att_page != 0) {
AbstractStorage storage = getAttributes(isContent);
final DataInputStream attStream = storage.readStream(att_page);
final DataInputStream attStream = getAttributes(isContent).readStream(att_page);
while (attStream.available() > 0) {
attStream.readInt(); // Attribute ID;
int attAddress = attStream.readInt();
storage.deleteRecord(attAddress);
getAttributes(isContent).deleteRecord(attAddress);
}
attStream.close();
storage.deleteRecord(att_page);
getAttributes(isContent).deleteRecord(att_page);
}
}
@@ -679,7 +677,7 @@ public class FSRecords implements Disposable, Forceable {
try {
synchronized (lock) {
int encodedAttId = DbConnection.getAttributeId(CHILDREN_ATT);
final int att = findAttributePage(id, encodedAttId, false, 0);
final int att = findAttributePage(id, encodedAttId, false);
return att != 0;
}
}
@@ -862,9 +860,7 @@ public class FSRecords implements Disposable, Forceable {
}
private static int getAttributeRecordId(final int id, int attributeId) throws IOException {
final int record = getRecords().getInt(getAttrOffset(id, attributeId));
assert record >= 0;
return record;
return getRecords().getInt(getAttrOffset(id, attributeId));
}
private static int getAttrOffset(int id, int attributeId) {
@@ -879,7 +875,7 @@ public class FSRecords implements Disposable, Forceable {
int encodedAttId;
synchronized (lock) {
encodedAttId = DbConnection.getAttributeId(attId);
page = findAttributePage(id, encodedAttId, false, 0);
page = findAttributePage(id, encodedAttId, false);
if (page == 0) return null;
}
@@ -891,7 +887,7 @@ public class FSRecords implements Disposable, Forceable {
}
}
private int findAttributePage(int fileId, int attributeId, boolean createIfNotFound, int capacity) throws IOException {
private int findAttributePage(int fileId, int attributeId, boolean createIfNotFound) throws IOException {
if (fileId <= 0) {
throw DbConnection.handleError(new AssertionError("assert fileId > 0 failed"));
}
@@ -899,58 +895,35 @@ public class FSRecords implements Disposable, Forceable {
if ((getFlags(fileId) & FREE_RECORD_FLAG) != 0) { // TODO: This assertion is a bit timey, will remove when bug is caught.
throw DbConnection.handleError(new AssertionError("Trying to find an attribute of deleted page"));
}
int attrsRecord = getAttributeRecordId(fileId, attributeId);
AbstractStorage storage = getAttributes(attributeId);
if (attrsRecord == 0) {
if (!createIfNotFound) return 0;
attrsRecord = storage.createNewRecord(16 * 8);
attrsRecord = getAttributes(attributeId).createNewRecord();
getRecords().putInt(getAttrOffset(fileId, attributeId), attrsRecord);
}
else {
final DataInputStream attrRefs = storage.readStream(attrsRecord);
final DataInputStream attrRefs = getAttributes(attributeId).readStream(attrsRecord);
try {
int offset = 4;
while (attrRefs.available() > 0) {
final int attIdOnPage = attrRefs.readInt();
if (attrRefs.available() < 4) {
LOG.error("bad");
}
final int attAddress = attrRefs.readInt();
if (attIdOnPage == attributeId) {
if (capacity > 0) {
int newAddress = storage.ensureCapacity(attAddress, capacity);
if (newAddress == attAddress) {
return attAddress;
}
((CompactStorage)storage).replaceIntInData(attrsRecord + offset, newAddress, attAddress);
return newAddress;
} else {
return attAddress;
}
}
offset += 8;
if (attIdOnPage == attributeId) return attAddress;
}
}
finally {
attrRefs.close();
}
}
if (createIfNotFound) {
Storage.AppenderStream appender = storage.appendStream(attrsRecord);
Storage.AppenderStream appender = getAttributes(attributeId).appendStream(attrsRecord);
appender.writeInt(attributeId);
int attAddress = storage.createNewRecord(capacity);
int attAddress = getAttributes(attributeId).createNewRecord();
appender.writeInt(attAddress);
appender.close();
if (appender.getRecordId() != attrsRecord) {
getRecords().putInt(getAttrOffset(fileId, attributeId), appender.getRecordId());
}
return attAddress;
}
@@ -978,35 +951,12 @@ public class FSRecords implements Disposable, Forceable {
DbConnection.markDirty();
incModCount(myFileId);
encodedAttId = DbConnection.getAttributeId(myAttributeId);
page = findAttributePage(myFileId, encodedAttId, true, ((ByteArrayOutputStream)out).size());
page = findAttributePage(myFileId, encodedAttId, true);
}
AbstractStorage storage = getAttributes(encodedAttId);
AbstractStorage.StorageDataOutput sinkStream = storage.writeStream(page);
final DataOutputStream sinkStream = getAttributes(encodedAttId).writeStream(page);
sinkStream.write(((ByteArrayOutputStream)out).toByteArray());
sinkStream.close();
final int recordId = sinkStream.getRecordId();
if (recordId != page) {
int attrsRecord = getAttributeRecordId(myFileId, encodedAttId);
DataInputStream stream = storage.readStream(attrsRecord);
try {
while (stream.available() > 0) {
if (encodedAttId == stream.readInt()) {
((CompactStorage)storage).replaceIntInData(attrsRecord + 4, recordId, page);
break;
}
if (stream.available() < 4) {
LOG.error("wtf");
}
stream.readInt();
attrsRecord += 8;
}
}
finally {
stream.close();
}
}
}
}
catch (Throwable e) {
@@ -29,14 +29,14 @@ import java.io.File;
import java.io.IOException;
public class StorageTest extends TestCase {
protected AbstractStorage myStorage;
private Storage myStorage;
protected void setUp() throws Exception {
super.setUp();
myStorage = Storage.create(getFileName());
}
protected String getFileName() {
private String getFileName() {
return FileUtil.getTempDirectory() + File.separatorChar + getName();
}
@@ -47,7 +47,7 @@ public class StorageTest extends TestCase {
}
public void testSmoke() throws Exception {
int record = myStorage.createNewRecord(0);
final int record = myStorage.createNewRecord();
myStorage.writeBytes(record, "Hello".getBytes());
assertEquals("Hello", new String(myStorage.readBytes(record)));
}
@@ -64,15 +64,13 @@ public class StorageTest extends TestCase {
int[] records = new int[count];
for (int i = 0; i < count; i++) {
byte[] bytes = hello.getBytes();
int record = myStorage.createNewRecord(bytes.length);
myStorage.writeBytes(record, bytes);
final int record = myStorage.createNewRecord();
myStorage.writeBytes(record, hello.getBytes());
records[i] = record;
}
for (int record : records) {
byte[] bytes = myStorage.readBytes(record);
assertEquals(hello, new String(bytes));
assertEquals(hello, new String(myStorage.readBytes(record)));
}
long timedelta = System.currentTimeMillis() - start;
@@ -80,22 +78,23 @@ public class StorageTest extends TestCase {
}
public void testAppender() throws Exception {
final int count = 1000;
int r = myStorage.createNewRecord(count * 4);
final int r = myStorage.createNewRecord();
AbstractStorage.AppenderStream out = myStorage.appendStream(r);
for (int i = 0; i < count; i++) {
DataOutputStream out = new DataOutputStream(myStorage.appendStream(r));
for (int i = 0; i < 10000; i++) {
out.writeInt(i);
if (i % 100 == 0) {
myStorage.readStream(r); // Drop the appenders cache
out.close();
out = myStorage.appendStream(r);
out = new DataOutputStream(myStorage.appendStream(r));
}
}
out.close();
DataInputStream in = myStorage.readStream(r);
for (int i = 0; i < count; i++) {
DataInputStream in = new DataInputStream(myStorage.readStream(r));
for (int i = 0; i < 10000; i++) {
assertEquals(i, in.readInt());
}
@@ -103,7 +102,7 @@ public class StorageTest extends TestCase {
}
public void testAppender2() throws Exception {
int r = myStorage.createNewRecord(0);
int r = myStorage.createNewRecord();
appendNBytes(r, 64);
appendNBytes(r, 256);
appendNBytes(r, 512);