Skip to content

Commit

Permalink
AOStorage去掉所有自动提交的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Oct 28, 2015
1 parent a2533d2 commit 8681d25
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 353 deletions.
Expand Up @@ -28,38 +28,6 @@ private StorageBuilder set(String key, Object value) {
return this; return this;
} }


/**
* Disable auto-commit, by setting the auto-commit delay and auto-commit
* buffer size to 0.
*
* @return this
*/
public StorageBuilder autoCommitDisabled() {
// we have a separate config option so that
// no thread is started if the write delay is 0
// (if we only had a setter in the AOStore,
// the thread would need to be started in any case)
set("autoCommitBufferSize", 0);
return set("autoCommitDelay", 0);
}

/**
* Set the size of the write buffer, in KB disk space (for file-based
* stores). Unless auto-commit is disabled, changes are automatically
* saved if there are more than this amount of changes.
* <p>
* The default is 1024 KB.
* <p>
* When the value is set to 0 or lower, data is not automatically
* stored.
*
* @param kb the write buffer size, in kilobytes
* @return this
*/
public StorageBuilder autoCommitBufferSize(int kb) {
return set("autoCommitBufferSize", kb);
}

/** /**
* Set the auto-compact target fill rate. If the average fill rate (the * Set the auto-compact target fill rate. If the average fill rate (the
* percentage of the storage space that contains active data) of the * percentage of the storage space that contains active data) of the
Expand Down
83 changes: 40 additions & 43 deletions lealone-storage/src/main/java/org/lealone/storage/AOStorage.java
Expand Up @@ -17,13 +17,13 @@
*/ */
package org.lealone.storage; package org.lealone.storage;


import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;


import org.lealone.common.util.DataUtils;
import org.lealone.db.Constants; import org.lealone.db.Constants;
import org.lealone.storage.btree.BTreeMap; import org.lealone.storage.btree.BTreeMap;
import org.lealone.storage.fs.FilePath; import org.lealone.storage.fs.FilePath;
Expand Down Expand Up @@ -131,47 +131,10 @@ public boolean isReadOnly() {
return config.containsKey("readOnly"); return config.containsKey("readOnly");
} }


public boolean isClosed() {
return closed;
}

@Override
public synchronized void close() {
close(true);
}

@Override
public void closeImmediately() {
close(false);
}

private void close(boolean closeMaps) {
closed = true;

for (StorageMap<?, ?> map : maps.values())
map.close();

maps.clear();
}

public synchronized void commit() {
for (StorageMap<?, ?> map : maps.values())
map.save();
}

public Set<String> getMapNames() { public Set<String> getMapNames() {
return new HashSet<String>(maps.keySet()); return new HashSet<String>(maps.keySet());
} }


public Collection<StorageMap<?, ?>> getMaps() {
return maps.values();
}

@Override
public boolean hasMap(String name) {
return maps.containsKey(name);
}

@Override @Override
public <K, V> StorageMap<K, V> openMap(String name, String mapType, DataType keyType, DataType valueType, public <K, V> StorageMap<K, V> openMap(String name, String mapType, DataType keyType, DataType valueType,
Map<String, String> parameters) { Map<String, String> parameters) {
Expand All @@ -184,27 +147,61 @@ public <K, V> StorageMap<K, V> openMap(String name, String mapType, DataType key
} else if (mapType.equalsIgnoreCase("MemoryMap)")) { } else if (mapType.equalsIgnoreCase("MemoryMap)")) {
return openMemoryMap(name, keyType, valueType); return openMemoryMap(name, keyType, valueType);
} else { } else {
return openAOMap(name, keyType, valueType); throw DataUtils.newIllegalArgumentException("Unknow map type: {0}", mapType);
} }
} }


@Override
public synchronized String nextTemporaryMapName() {
return TEMP_NAME_PREFIX + nextTemporaryMapId++;
}

@Override
public boolean hasMap(String name) {
return maps.containsKey(name);
}

@Override @Override
public void backupTo(String fileName) { public void backupTo(String fileName) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }


@Override @Override
public void flush() { public void flush() {
commit(); save();
} }


@Override @Override
public void sync() { public void sync() {
commit(); save();
}

public synchronized void save() {
for (StorageMap<?, ?> map : maps.values())
map.save();
}

public boolean isClosed() {
return closed;
} }


@Override @Override
public synchronized String nextTemporaryMapName() { public void close() {
return TEMP_NAME_PREFIX + nextTemporaryMapId++; close(true);
} }

@Override
public void closeImmediately() {
close(false);
}

private synchronized void close(boolean closeMaps) {
closed = true;

for (StorageMap<?, ?> map : maps.values())
map.close();

maps.clear();
}

} }
Expand Up @@ -23,6 +23,8 @@
*/ */
public class BTreeChunk { public class BTreeChunk {


public static final int MAX_SIZE = 1 << 31 - BTreeStorage.CHUNK_HEADER_SIZE;

private static final int FORMAT_VERSION = 1; private static final int FORMAT_VERSION = 1;


/** /**
Expand Down Expand Up @@ -87,6 +89,9 @@ public class BTreeChunk {
*/ */
public ArrayList<Long> pagePositions; public ArrayList<Long> pagePositions;
public int pagePositionsOffset; public int pagePositionsOffset;
public ArrayList<Long> leafPagePositions;
public int leafPagePositionsOffset;
public int leafPageCount;


/** /**
* The garbage collection priority. Priority 0 means it needs to be * The garbage collection priority. Priority 0 means it needs to be
Expand Down Expand Up @@ -162,6 +167,8 @@ public StringBuilder asStringBuilder() {
} }


DataUtils.appendMap(buff, "pagePositionsOffset", pagePositionsOffset); DataUtils.appendMap(buff, "pagePositionsOffset", pagePositionsOffset);
DataUtils.appendMap(buff, "leafPagePositionsOffset", leafPagePositionsOffset);
DataUtils.appendMap(buff, "leafPageCount", leafPageCount);


DataUtils.appendMap(buff, "blockSize", BTreeStorage.BLOCK_SIZE); DataUtils.appendMap(buff, "blockSize", BTreeStorage.BLOCK_SIZE);
DataUtils.appendMap(buff, "format", FORMAT_VERSION); DataUtils.appendMap(buff, "format", FORMAT_VERSION);
Expand Down Expand Up @@ -194,6 +201,8 @@ public static BTreeChunk fromString(String s) {
c.unused = DataUtils.readHexLong(map, "unused", 0); c.unused = DataUtils.readHexLong(map, "unused", 0);


c.pagePositionsOffset = DataUtils.readHexInt(map, "pagePositionsOffset", 0); c.pagePositionsOffset = DataUtils.readHexInt(map, "pagePositionsOffset", 0);
c.leafPagePositionsOffset = DataUtils.readHexInt(map, "leafPagePositionsOffset", 0);
c.leafPageCount = DataUtils.readHexInt(map, "leafPageCount", 0);


long format = DataUtils.readHexLong(map, "format", FORMAT_VERSION); long format = DataUtils.readHexLong(map, "format", FORMAT_VERSION);
if (format > FORMAT_VERSION) { if (format > FORMAT_VERSION) {
Expand Down
Expand Up @@ -201,7 +201,6 @@ protected void beforeWrite() {
if (readOnly) { if (readOnly) {
throw DataUtils.newUnsupportedOperationException("This map is read-only"); throw DataUtils.newUnsupportedOperationException("This map is read-only");
} }
storage.beforeWrite();
} }


/** /**
Expand Down Expand Up @@ -900,51 +899,6 @@ private int rewrite(BTreePage p, Set<Integer> set) {
return writtenPageCount; return writtenPageCount;
} }


// /**
// * Copy a map. All pages are copied.
// *
// * @param sourceMap the source map
// */
// public void copyFrom(BTreeMap<K, V> sourceMap) {
// beforeWrite();
// newRoot(copy(sourceMap.root, null));
// }
//
// private BTreePage copy(BTreePage source, CursorPos parent) {
// BTreePage target = BTreePage.create(this, storage.getCurrentVersion(), source);
// if (source.isLeaf()) {
// BTreePage child = target;
// for (CursorPos p = parent; p != null; p = p.parent) {
// p.page.setChild(p.index, child);
// p.page = p.page.copy(storage.getCurrentVersion());
// child = p.page;
// if (p.parent == null) {
// newRoot(p.page);
// beforeWrite();
// }
// }
// } else {
// // temporarily, replace child pages with empty pages,
// // to ensure there are no links to the old storage
// for (int i = 0; i < getChildPageCount(target); i++) {
// target.setChild(i, null);
// }
// CursorPos pos = new CursorPos(target, 0, parent);
// for (int i = 0; i < getChildPageCount(target); i++) {
// pos.index = i;
// long p = source.getChildPagePos(i);
// if (p != 0) {
// // p == 0 means no child
// // (for example the last entry of an r-tree node)
// // (the MVMap is also used for r-trees for compacting)
// copy(source.getChildPage(i), pos);
// }
// }
// target = pos.page;
// }
// return target;
// }

@Override @Override
public int hashCode() { public int hashCode() {
return name.hashCode(); return name.hashCode();
Expand Down Expand Up @@ -1001,10 +955,6 @@ public void rollbackTo(long version) {
storage.rollbackTo(version); storage.rollbackTo(version);
} }


void writeInBackground(int autoCommitDelay) {
storage.writeInBackground(autoCommitDelay);
}

@Override @Override
public void save() { public void save() {
commit(); commit();
Expand Down

0 comments on commit 8681d25

Please sign in to comment.