Skip to content

Commit

Permalink
fix bug: 对无主键的表进行append操作时可能产生错乱的row id(之前是因为lealone 6的代码未移植到lealone 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
codefollower committed Mar 14, 2023
1 parent 16c854e commit d76b280
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.lealone.common.util.DataUtils;
import org.lealone.db.async.AsyncHandler;
import org.lealone.db.async.AsyncResult;
import org.lealone.db.value.ValueLong;
import org.lealone.storage.CursorParameters;
import org.lealone.storage.StorageMapBase;
import org.lealone.storage.StorageMapCursor;
Expand Down Expand Up @@ -540,14 +539,10 @@ public K append(V value, AsyncHandler<AsyncResult<K>> handler) {
return append0(value, handler);
}

@SuppressWarnings("unchecked")
private K append0(V value, AsyncHandler<AsyncResult<K>> handler) {
checkWrite(value);
// 先得到一个long类型的key
K key = (K) ValueLong.get(maxKey.incrementAndGet());
Append<K, V> append = new Append<>(this, key, value, handler);
runPageOperation(append);
return key;
Append<K, V> append = new Append<>(this, value, handler);
return runPageOperation(append);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.lealone.common.exceptions.DbException;
import org.lealone.db.async.AsyncHandler;
import org.lealone.db.async.AsyncResult;
import org.lealone.db.value.ValueLong;
import org.lealone.storage.aose.btree.BTreeMap;
import org.lealone.storage.page.PageOperation;
import org.lealone.storage.page.PageOperationHandler;
Expand All @@ -20,7 +21,7 @@ private PageOperations() {
// 只针对单Key的写操作,包括: Put、PutIfAbsent、Replace、Remove、Append
public static abstract class SingleWrite<K, V, R> implements PageOperation {
final BTreeMap<K, V> map;
final K key;
K key; // 允许append操作设置
AsyncHandler<AsyncResult<R>> resultHandler;

Page p; // 最终要操作的leaf page
Expand Down Expand Up @@ -192,8 +193,8 @@ protected Object writeLocal(int index) {

public static class Append<K, V> extends Put<K, V, K> {

public Append(BTreeMap<K, V> map, K key, V value, AsyncHandler<AsyncResult<K>> resultHandler) {
super(map, key, value, resultHandler);
public Append(BTreeMap<K, V> map, V value, AsyncHandler<AsyncResult<K>> resultHandler) {
super(map, null, value, resultHandler);
}

@Override
Expand All @@ -219,7 +220,9 @@ protected int getKeyIndex() {
}

@Override
@SuppressWarnings("unchecked")
protected Object writeLocal(int index) {
key = (K) ValueLong.get(map.incrementAndGetMaxKey());
p.markDirty(true);
insertLeaf(index, value);
return key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public long getMaxKey() {
return maxKey.get();
}

public long incrementAndGetMaxKey() {
return maxKey.incrementAndGet();
}

@Override
public long getDiskSpaceUsed() {
return 0;
Expand Down

0 comments on commit d76b280

Please sign in to comment.