Skip to content

Commit

Permalink
增加新的AOStorageEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Sep 25, 2015
1 parent 75556de commit 7ea0b28
Show file tree
Hide file tree
Showing 10 changed files with 903 additions and 119 deletions.
2 changes: 1 addition & 1 deletion lealone-common/src/main/java/org/lealone/db/Constants.java
Expand Up @@ -23,7 +23,7 @@ public class Constants {
*/ */
public static final String PROJECT_NAME_PREFIX = PROJECT_NAME + "."; public static final String PROJECT_NAME_PREFIX = PROJECT_NAME + ".";


public static final String DEFAULT_STORAGE_ENGINE_NAME = "MVStore"; public static final String DEFAULT_STORAGE_ENGINE_NAME = "AOStore";


public static final String DEFAULT_SQL_ENGINE_NAME = PROJECT_NAME; public static final String DEFAULT_SQL_ENGINE_NAME = PROJECT_NAME;


Expand Down
26 changes: 12 additions & 14 deletions lealone-db/src/main/java/org/lealone/db/table/MVTable.java
Expand Up @@ -40,9 +40,7 @@
import org.lealone.db.result.SortOrder; import org.lealone.db.result.SortOrder;
import org.lealone.db.schema.SchemaObject; import org.lealone.db.schema.SchemaObject;
import org.lealone.storage.TransactionStorageEngine; import org.lealone.storage.TransactionStorageEngine;
import org.lealone.storage.type.ObjectDataType;
import org.lealone.transaction.Transaction; import org.lealone.transaction.Transaction;
import org.lealone.transaction.TransactionMap;


/** /**
* A table stored in a MVStore. * A table stored in a MVStore.
Expand Down Expand Up @@ -71,7 +69,7 @@ public class MVTable extends TableBase {
private final TransactionStorageEngine storageEngine; private final TransactionStorageEngine storageEngine;
private boolean containsGlobalUniqueIndex; private boolean containsGlobalUniqueIndex;


private final TransactionMap<Long, Long> rowVersionMap; // private final TransactionMap<Long, Long> rowVersionMap;


public MVTable(CreateTableData data, TransactionStorageEngine storageEngine) { public MVTable(CreateTableData data, TransactionStorageEngine storageEngine) {
super(data); super(data);
Expand All @@ -85,8 +83,8 @@ public MVTable(CreateTableData data, TransactionStorageEngine storageEngine) {
} }
traceLock = database.getTrace(Trace.LOCK); traceLock = database.getTrace(Trace.LOCK);


rowVersionMap = storageEngine.openMap(data.session, getName() + "_row_version", new ObjectDataType(), // rowVersionMap = storageEngine.openMap(data.session, getName() + "_row_version", new ObjectDataType(),
new ObjectDataType()); // new ObjectDataType());
} }


/** /**
Expand Down Expand Up @@ -801,13 +799,13 @@ public boolean containsGlobalUniqueIndex() {
return containsGlobalUniqueIndex; return containsGlobalUniqueIndex;
} }


@Override // @Override
public long getRowVersion(long rowKey) { // public long getRowVersion(long rowKey) {
return rowVersionMap.get(rowKey); // return rowVersionMap.get(rowKey);
} // }

//
@Override // @Override
public TransactionMap<Long, Long> getRowVersionMap() { // public TransactionMap<Long, Long> getRowVersionMap() {
return rowVersionMap; // return rowVersionMap;
} // }
} }
Expand Up @@ -128,7 +128,7 @@ private void initManagementDb() {
managementDb = conn; managementDb = conn;
SERVERS.put(port, this); SERVERS.put(port, this);
} catch (SQLException e) { } catch (SQLException e) {
DbException.convert(e); throw DbException.convert(e);
} finally { } finally {
JdbcUtils.closeSilently(stat); JdbcUtils.closeSilently(stat);
} }
Expand Down
148 changes: 137 additions & 11 deletions lealone-storage/aostore/src/main/java/org/lealone/aostore/AOStore.java
Expand Up @@ -13,15 +13,16 @@
*/ */
package org.lealone.aostore; package org.lealone.aostore;


import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;


import org.lealone.aostore.btree.BTreeMap; import org.lealone.aostore.btree.BTreeMap;
import org.lealone.aostore.btree.BTreeStore; import org.lealone.aostore.btree.BTreeStore;
import org.lealone.common.util.DataUtils; import org.lealone.common.util.DataUtils;
import org.lealone.common.util.New; import org.lealone.common.util.New;
import org.lealone.storage.fs.FilePath;
import org.lealone.storage.fs.FileUtils; import org.lealone.storage.fs.FileUtils;


// adaptive optimization store // adaptive optimization store
Expand All @@ -43,7 +44,7 @@ public class AOStore {
public static final String SUFFIX_AO_STORE_TEMP_FILE = ".tempFile"; public static final String SUFFIX_AO_STORE_TEMP_FILE = ".tempFile";


private final HashMap<String, Object> config; private final HashMap<String, Object> config;
private final HashMap<String, BTreeMap<?, ?>> maps = new HashMap<String, BTreeMap<?, ?>>(); private final ConcurrentSkipListMap<String, BTreeMap<?, ?>> maps = new ConcurrentSkipListMap<String, BTreeMap<?, ?>>();


private int lastMapId; private int lastMapId;


Expand All @@ -53,14 +54,20 @@ public class AOStore {
if (storeName != null) { if (storeName != null) {
String sn = storeName.toString(); String sn = storeName.toString();
if (!FileUtils.exists(sn)) if (!FileUtils.exists(sn))
FileUtils.createDirectory(sn); FileUtils.createDirectories(sn);


FilePath dir = FilePath.get(sn); // FilePath dir = FilePath.get(sn);
for (FilePath file : dir.newDirectoryStream()) { // for (FilePath file : dir.newDirectoryStream()) {
String name = file.getName(); // String name = file.getName();
maps.put(name.substring(0, name.length() - SUFFIX_AO_FILE.length()), null); // maps.put(name.substring(0, name.length() - SUFFIX_AO_FILE.length()), null);
} // }
} }

// setAutoCommitDelay starts the thread, but only if
// the parameter is different from the old value
Object delayObject = config.get("autoCommitDelay");
int delay = delayObject == null ? 1000 : (Integer) delayObject;
setAutoCommitDelay(delay);
} }


public HashMap<String, Object> getConfig() { public HashMap<String, Object> getConfig() {
Expand All @@ -76,19 +83,22 @@ public synchronized <M extends BTreeMap<K, V>, K, V> M openMap(String name, BTre
c.put("id", id); c.put("id", id);
c.put("createVersion", 0L); c.put("createVersion", 0L);
map = builder.create(); map = builder.create();
map.setName(name);


BTreeStore store = new BTreeStore(name, config); BTreeStore store = new BTreeStore(name, config);
map.init(store, c); map.init(store, c);


if (!maps.containsKey(name)) // map不存在时标数据改变 // if (!maps.containsKey(name)) // map不存在时标数据改变
store.markMetaChanged(); store.markMetaChanged();
maps.put(name, map); maps.put(name, map);
} }


return map; return map;
} }


public synchronized void close() { public synchronized void close() {
stopBackgroundThread();

for (BTreeMap<?, ?> map : maps.values()) for (BTreeMap<?, ?> map : maps.values())
map.close(); map.close();
} }
Expand All @@ -98,10 +108,18 @@ public synchronized void commit() {
map.commit(); map.commit();
} }


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


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

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

/** /**
* Open a store in exclusive mode. For a file-based store, the parent * Open a store in exclusive mode. For a file-based store, the parent
* directory must already exist. * directory must already exist.
Expand Down Expand Up @@ -329,4 +347,112 @@ public static Builder fromString(String s) {
} }


} }

/**
* The background thread, if any.
*/
volatile BackgroundWriterThread backgroundWriterThread;

/**
* The delay in milliseconds to automatically commit and write changes.
*/
private int autoCommitDelay;

/**
* Set the maximum delay in milliseconds to auto-commit changes.
* <p>
* To disable auto-commit, set the value to 0. In this case, changes are
* only committed when explicitly calling commit.
* <p>
* The default is 1000, meaning all changes are committed after at most one
* second.
*
* @param millis the maximum delay
*/

public void setAutoCommitDelay(int millis) {
if (autoCommitDelay == millis) {
return;
}
autoCommitDelay = millis;
if (config.get("storeName") == null) // 内存存储不需要BackgroundWriterThread
return;
stopBackgroundThread();
// start the background thread if needed
if (millis > 0) {
int sleep = Math.max(1, millis / 10);
BackgroundWriterThread t = new BackgroundWriterThread(this, sleep, config.get("storeName").toString());
t.start();
backgroundWriterThread = t;
}
}

private void stopBackgroundThread() {
BackgroundWriterThread t = backgroundWriterThread;
if (t == null) {
return;
}
backgroundWriterThread = null;
if (Thread.currentThread() == t) {
// within the thread itself - can not join
return;
}
synchronized (t.sync) {
t.sync.notifyAll();
}
if (Thread.holdsLock(this)) {
// called from storeNow: can not join,
// because that could result in a deadlock
return;
}
try {
t.join();
} catch (Exception e) {
// ignore
}
}

/**
* A background writer thread to automatically store changes from time to
* time.
*/
private static class BackgroundWriterThread extends Thread {

public final Object sync = new Object();
private final AOStore store;
private final int sleep;

BackgroundWriterThread(AOStore store, int sleep, String fileStoreName) {
super("AOStore background writer " + fileStoreName);
this.store = store;
this.sleep = sleep;
setDaemon(true);
}

@Override
public void run() {
while (true) {
Thread t = store.backgroundWriterThread;
if (t == null) {
break;
}
synchronized (sync) {
try {
sync.wait(sleep);
} catch (InterruptedException e) {
continue;
}
}
for (BTreeMap<?, ?> map : store.getMaps()) {
BTreeStore btreeStore = map.getStore();
FileStore fileStore = btreeStore.getFileStore();
if (fileStore == null || fileStore.isReadOnly()) {
return;
}
btreeStore.writeInBackground();
}
}
}

}
} }
Expand Up @@ -54,6 +54,7 @@ public class BTreeMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K
protected volatile long writeVersion; protected volatile long writeVersion;


private int id; private int id;
private String name;
private long createVersion; private long createVersion;
private final DataType keyType; private final DataType keyType;
private final DataType valueType; private final DataType valueType;
Expand Down Expand Up @@ -953,7 +954,11 @@ public BTreePage getRoot() {
*/ */
@Override @Override
public String getName() { public String getName() {
return store.getMapName(id); return name; // store.getMapName(id);
}

public void setName(String name) {
this.name = name;
} }


public BTreeStore getStore() { public BTreeStore getStore() {
Expand Down

0 comments on commit 7ea0b28

Please sign in to comment.