io changes rolled back

This commit is contained in:
Dmitry Avdeev
2009-11-17 11:11:33 +03:00
parent ee42d07c13
commit e99d90130e
6 changed files with 83 additions and 87 deletions
@@ -30,7 +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.HeavyProcessLatch;
import com.intellij.util.io.storage.Storage;
import gnu.trove.TIntArrayList;
@@ -83,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;
@@ -434,7 +434,7 @@ public class FSRecords implements Disposable, Forceable {
}
public void connect() {
DbConnection.connect();
myConnection = DbConnection.connect();
}
private static ResizeableMappedFile getRecords() {
@@ -514,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) {
Storage 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);
}
}
@@ -898,15 +897,14 @@ public class FSRecords implements Disposable, Forceable {
}
int attrsRecord = getAttributeRecordId(fileId, attributeId);
Storage storage = getAttributes(attributeId);
if (attrsRecord == 0) {
if (!createIfNotFound) return 0;
attrsRecord = storage.createNewRecord(0);
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 {
while (attrRefs.available() > 0) {
final int attIdOnPage = attrRefs.readInt();
@@ -921,9 +919,9 @@ public class FSRecords implements Disposable, Forceable {
}
if (createIfNotFound) {
Storage.AppenderStream appender = storage.appendStream(attrsRecord);
Storage.AppenderStream appender = getAttributes(attributeId).appendStream(attrsRecord);
appender.writeInt(attributeId);
int attAddress = getAttributes(attributeId).createNewRecord(0);
int attAddress = getAttributes(attributeId).createNewRecord();
appender.writeInt(attAddress);
appender.close();
return attAddress;
@@ -956,8 +954,7 @@ public class FSRecords implements Disposable, Forceable {
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();
}
@@ -102,16 +102,6 @@ public class RandomAccessDataFile implements Forceable {
return Bits.getInt(myTypedIOBuffer, 0);
}
public void putShort(long addr, short value) {
Bits.putShort(myTypedIOBuffer, 0, value);
put(addr, myTypedIOBuffer, 0, 2);
}
public short getShort(long addr) {
get(addr, myTypedIOBuffer, 0, 2);
return Bits.getShort(myTypedIOBuffer, 0);
}
public void putLong(long addr, long value) {
Bits.putLong(myTypedIOBuffer, 0, value);
put(addr, myTypedIOBuffer, 0, 8);
@@ -129,7 +129,7 @@ public class CompactStorage extends AbstractStorage {
markDirty();
int record = (int)myData.length();
setSize(record, 0);
int capacity = Storage.calcCapacity(requiredLength);
int capacity = requiredLength;
setCapacity(record, capacity);
myData.put(record + DATA_OFFSET + capacity - 1, new byte[] {0}, 0, 1);
@@ -19,19 +19,20 @@
*/
package com.intellij.util.io.storage;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.Forceable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.io.PagePool;
import com.intellij.util.io.RecordDataOutput;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.*;
@SuppressWarnings({"HardCodedStringLiteral"})
public class Storage extends AbstractStorage {
public class Storage implements Disposable, Forceable {
private static final Logger LOG = Logger.getInstance("#com.intellij.util.io.storage.Storage");
private final Object lock = new Object();
@@ -201,21 +202,21 @@ public class Storage extends AbstractStorage {
}
}
public int createNewRecord(int requiredLength) throws IOException {
public int createNewRecord() throws IOException {
synchronized (lock) {
return myRecordsTable.createNewRecord();
}
}
public StorageDataOutput createStream() throws IOException {
return writeStream(createNewRecord(0));
return writeStream(createNewRecord());
}
protected int appendBytes(int record, byte[] bytes) {
private void appendBytes(int record, byte[] bytes) {
assert record > 0;
int delta = bytes.length;
if (delta == 0) return record;
if (delta == 0) return;
synchronized (lock) {
int capacity = myRecordsTable.getCapacity(record);
@@ -238,7 +239,6 @@ public class Storage extends AbstractStorage {
myRecordsTable.setSize(record, newSize);
}
}
return record;
}
public void writeBytes(int record, byte[] bytes) {
@@ -271,18 +271,13 @@ public class Storage extends AbstractStorage {
myDataTable.writeBytes(address, bytes);
myRecordsTable.setSize(record, requiredLength);
}
return;
}
public int ensureCapacity(int attAddress, int capacity) {
return attAddress;
}
static int calcCapacity(int requiredLength) {
private static int calcCapacity(int requiredLength) {
return Math.max(64, nearestPowerOfTwo(requiredLength * 3 / 2));
}
static int nearestPowerOfTwo(int n) {
private static int nearestPowerOfTwo(int n) {
int power = 1;
while (n != 0) {
power *= 2;
@@ -291,6 +286,19 @@ public class Storage extends AbstractStorage {
return power;
}
public StorageDataOutput writeStream(final int record) {
return new StorageDataOutput(this, record);
}
public AppenderStream appendStream(int record) {
return new AppenderStream(record);
}
public DataInputStream readStream(int record) {
final byte[] bytes = readBytes(record);
return new DataInputStream(new ByteArrayInputStream(bytes));
}
public byte[] readBytes(int record) {
assert record > 0;
@@ -332,4 +340,37 @@ public class Storage extends AbstractStorage {
}
}
public static class StorageDataOutput extends DataOutputStream implements RecordDataOutput {
private final Storage myStorage;
private final int myRecordId;
public StorageDataOutput(Storage storage, int recordId) {
super(new ByteArrayOutputStream());
myStorage = storage;
myRecordId = recordId;
}
public void close() throws IOException {
super.close();
myStorage.writeBytes(myRecordId, ((ByteArrayOutputStream)out).toByteArray());
}
public int getRecordId() {
return myRecordId;
}
}
public class AppenderStream extends DataOutputStream {
private final int myRecordId;
public AppenderStream(int recordId) {
super(new ByteArrayOutputStream());
myRecordId = recordId;
}
public void close() throws IOException {
super.close();
appendBytes(myRecordId, ((ByteArrayOutputStream)out).toByteArray());
}
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.io.storage;
/**
* @author Dmitry Avdeev
*/
public class CompactStorageTest extends StorageTest {
protected void setUp() throws Exception {
myStorage = new CompactStorage(getFileName());
}
protected void tearDown() throws Exception {
super.tearDown();
CompactStorage.deleteFiles(getFileName());
}
}
@@ -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);