这应该是一个有趣的挑战。 我正在寻找一种尚不存在的算法(据我所知)
getFromDatabase(int page, int size)
。getRecords(int offset, int limit)
。我们必须使用给定的offset
和limit
来检索匹配的数据库记录,这些记录只能由page
和size
访问。显然,偏移/限制并不总是映射到单个页面/大小。挑战在于找到一个算法,使getFromDatabase
次调用的“理想”数量能够检索所有记录。该算法应考虑几个因素:
getFromDatabase
都会产生一定的管理费用;把电话保持在最低限度。我提出了以下算法:http://jsfiddle.net/mwvdlee/A7J9C/ (JS代码,但该算法与语言无关)。基本上它是以下伪代码:
do {
do {
try to convert (offset,limit) to (page,size)
if too much waste
lower limit by some amount
else
call `getDatabaseRecords()`
filter out waste records
increase offset to first record not yet retrieved
lower limit to last records not yet retrieved
} until some records were retrieved
} until all records are retrieved from database
此算法的关键在于确定too much waste
和some amount
。但是这个算法并不是最优的,也不是完整的保证(很可能,我无法证明)。
有没有更好的(已知?)算法,或者我可以做出哪些改进? 有没有人对如何解决这个问题有好的想法?
答案 0 :(得分:1)
正如@usr所指出的,在这个问题的大多数变体中(无论是查询数据库,API还是其他一些实体),最好尽可能减少调用次数,因为返回一些额外的行是几乎总是比发出单独的电话便宜。以下PageSizeConversion算法将始终找到一个返回最少记录的调用(这正是它执行搜索的方式)。在数据集的开头(headWaste
)或结束(tailWaste
)可能会返回一些额外的记录,这些记录是在单个页面中拟合数据集所必需的。该算法在Javascript中实现,但很容易移植到任何语言。
function PageSizeConversion(offset, limit) {
var window, leftShift;
for (window = limit; window <= offset + limit; window++) {
for (leftShift = 0; leftShift <= window - limit; leftShift++) {
if ((offset - leftShift) % window == 0) {
this.pageSize = window;
this.page = (offset - leftShift) / this.pageSize;
this.headWaste = leftShift;
this.tailWaste = ((this.page + 1) * this.pageSize) - (offset + limit);
return;
}
}
}
}
var testData = [
{"offset": 0,"limit": 10,"expectedPage": 0,"expectedSize": 10,"expectedHeadWaste": 0,"expectedTailWaste": 0},
{"offset": 2,"limit": 1,"expectedPage": 2,"expectedSize": 1,"expectedHeadWaste": 0,"expectedTailWaste": 0},
{"offset": 2,"limit": 2,"expectedPage": 1,"expectedSize": 2,"expectedHeadWaste": 0,"expectedTailWaste": 0},
{"offset": 5,"limit": 3,"expectedPage": 1,"expectedSize": 4,"expectedHeadWaste": 1,"expectedTailWaste": 0},
{"offset": 3,"limit": 5,"expectedPage": 0,"expectedSize": 8,"expectedHeadWaste": 3,"expectedTailWaste": 0},
{"offset": 7,"limit": 3,"expectedPage": 1,"expectedSize": 5,"expectedHeadWaste": 2,"expectedTailWaste": 0},
{"offset": 1030,"limit": 135,"expectedPage": 7,"expectedSize": 146,"expectedHeadWaste": 8,"expectedTailWaste": 3},
];
describe("PageSizeConversion Tests", function() {
testData.forEach(function(testItem) {
it("should return correct conversion for offset " + testItem.offset + " limit " + testItem.limit, function() {
conversion = new PageSizeConversion(testItem.offset, testItem.limit);
expect(conversion.page).toEqual(testItem.expectedPage);
expect(conversion.pageSize).toEqual(testItem.expectedSize);
expect(conversion.headWaste).toEqual(testItem.expectedHeadWaste);
expect(conversion.tailWaste).toEqual(testItem.expectedTailWaste);
});
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet" />
<title>Jasmine Spec Runner</title>
这可能不是完全 @Martijn正在寻找的东西,因为它偶尔会产生大量浪费的结果。但在大多数情况下,它似乎是解决一般问题的好方法。
答案 1 :(得分:1)
偏移量/限制页面/页面大小分页的原理(精确,无浪费)
如果页面大小==限制或(偏移百分比限制)== 0,则页面==(整数) 其他页面==(浮动)。
要运行,请在命令行中输入“ node convertOffsetToPage.js”
function convertOffsetToPage(offset, limit) {
const precision = 1000000;
const pageSize = limit;
let page = (offset + limit) / limit;
page = Math.round(page * precision) / precision;
return { page, pageSize };
}
function dbServiceSimulation(page, pageSize, items) {
const start = Math.round((page - 1) * pageSize);
const end = Math.round(page * pageSize);
return items.slice(start, end);
}
function getDataItems(itemCount) {
return Array.from(Array(itemCount), (_, x) => x);
}
const dataItems = getDataItems(1000000);
console.log('\ndata items: ', dataItems);
let offset = parseInt(process.argv[2], 10) || 0;
let limit = parseInt(process.argv[3], 10) || 1;
console.log('\ninput offset: ', offset);
console.log('\ninput limit: ', limit);
const { page, pageSize } = convertOffsetToPage(offset, limit);
console.log('\npage = ', page);
console.log('\npageSize = ', pageSize);
const result = dbServiceSimulation(page, pageSize, dataItems);
console.log('\nresult after core Service call');
console.log('\nresult: ', result)
console.log('\n');
答案 2 :(得分:0)
我也遇到了这个问题。我的解决方案是(在 Java 中)简而言之。首先是琐碎的案件被关闭,然后是棘手的部分:)。它尝试找到最佳页面大小、页面编号并计算从页面开头的新偏移量。保留原始限制:
public static PageSizeOffsetLimit toPageSize(int offset, int limit) {
if (offset < limit) {
return new PageSizeOffsetLimit(0, offset + limit, offset, limit);
}
if (offset == limit) {
return new PageSizeOffsetLimit(1, limit, 0, limit);
}
for (int size = limit; size < 2 * limit; size++) {
int newOffset = offset % size;
if ((size - limit) >= newOffset) {
return new PageSizeOffsetLimit(offset / size, size, newOffset, limit);
}
}
throw new RuntimeException(String.format(
"Cannot determinate page and size from offset and limit (offset: %s, limit: %s)",
offset,
limit));
}
玩得开心:)