下载并修剪aerospike udf列表

我尝试删除和修剪列表以build立分页系统

local function createMap(postId, paramDate) local m = map { id = postId, date = paramDate }; return m; end function get(rec, binName, from, to) if aerospike:exists(rec) then local l = rec[binName] if (l == nil) then return nil else local length = #l; if (length <= 10 and to <=10) then return l; elseif (to >= length) then local drop = list.drop(l, from); return drop; else list.trim(l, to);--Remove all elements at and beyond a specified position in the List. list.drop(l, from); --Select all elements except the first n elements of the List return l; end end else return nil;--end return empty array end--end else aerospike exists end 

我的列表有这样的结构:

 [{"date":"2016-01-02T19:45:00.806Z", "id":"568828bc49017f16659f6978"}, {"date":"2016-01-02T19:44:56.040Z", "id":"568828b849017f16659f6977"},...] 

看来,我不能修剪,然后删除一个列表。 例如有21个元素:它首先返回到元素21到元素13,然后元素21到元素4,然后元素3返回到元素1

我在node.js中的function很简单,用于将'from'和'to'更改为我从前端发送'page'到node.js并使用以下函数:

  var skip = 9 * (page -1); var lastIndexToReturn = skip + 9 + 1; 

所以在第一个请求是“0”和“10”,然后是“9”和“19”等使用list.trim和list.drop我认为我可以build立一个分页系统

 function get(rec, binName, from, to) if aerospike:exists(rec) then local l = rec[binName] if (l == nil) then return list()--return empty array else --first index in lua is 1 local length = list.size(l) local pagination = list() if (length < from) then return list() elseif (length >= from and length <=to) then for i=from,length,1 do list.append(pagination, l[i]) end--end for return pagination else for i=from,to,1 do list.append(pagination, l[i]) end--end for return pagination end end else return list();--end return empty array end--end else aerospike exists end --end function 

我编写了这个函数结束:确保不会得到列表索引的错误,我得到列表的大小,我比较这个大小,我想分页确定要返回的部分:我不迭代整个arrays。

当然,只有当logging是一个列表时才有效注意:确保来自客户端的第一个“from”是1,因为lua数组开始为1