提交 cb37d125 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Merge and optimize applyOffset() and applyLimit()

上级 43e247ee
...@@ -386,11 +386,43 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -386,11 +386,43 @@ public class LocalResult implements ResultInterface, ResultTarget {
} }
} }
} }
applyOffset(); applyOffsetAndLimit();
applyLimit();
reset(); reset();
} }
private void applyOffsetAndLimit() {
int offset = Math.max(this.offset, 0);
int limit = this.limit;
if (offset == 0 && limit < 0 || rowCount == 0) {
return;
}
boolean clearAll = offset >= rowCount || limit == 0;
if (!clearAll) {
int remaining = rowCount - offset;
limit = limit < 0 ? remaining : Math.min(remaining, limit);
if (offset == 0 && remaining <= limit) {
return;
}
} else {
limit = 0;
}
distinctRows = null;
rowCount = limit;
if (external == null) {
if (clearAll) {
rows.clear();
return;
}
// avoid copying the whole array for each row
rows = new ArrayList<>(rows.subList(offset, offset + limit));
} else {
if (clearAll) {
return;
}
diskOffset = offset;
}
}
@Override @Override
public int getRowCount() { public int getRowCount() {
return rowCount; return rowCount;
...@@ -410,24 +442,6 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -410,24 +442,6 @@ public class LocalResult implements ResultInterface, ResultTarget {
this.limit = limit; this.limit = limit;
} }
private void applyLimit() {
if (limit < 0) {
return;
}
if (external == null) {
if (rows.size() > limit) {
rows = new ArrayList<>(rows.subList(0, limit));
rowCount = limit;
distinctRows = null;
}
} else {
if (limit < rowCount) {
rowCount = limit;
distinctRows = null;
}
}
}
@Override @Override
public boolean needToClose() { public boolean needToClose() {
return external != null; return external != null;
...@@ -501,31 +515,6 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -501,31 +515,6 @@ public class LocalResult implements ResultInterface, ResultTarget {
this.offset = offset; this.offset = offset;
} }
private void applyOffset() {
if (offset <= 0) {
return;
}
if (external == null) {
if (offset >= rows.size()) {
rows.clear();
rowCount = 0;
} else {
// avoid copying the whole array for each row
int remove = Math.min(offset, rows.size());
rows = new ArrayList<>(rows.subList(remove, rows.size()));
rowCount -= remove;
}
} else {
if (offset >= rowCount) {
rowCount = 0;
} else {
diskOffset = offset;
rowCount -= offset;
}
}
distinctRows = null;
}
@Override @Override
public String toString() { public String toString() {
return super.toString() + " columns: " + visibleColumnCount + return super.toString() + " columns: " + visibleColumnCount +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论