drop and trim a list in aerospike udf

匿名 (未验证) 提交于 2019-12-03 09:52:54

问题:

I try to drop and trim a list to build a pagination system

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

my list has this structure :

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

It seems that I can't trim and then drop a list. with 21 elements for example : it first return to element 21 to element 13 , then element 21 to element 4 , then element 3 to element 1

my function in node.js is simple for change 'from' and 'to' I send the 'page' fromm the frontend to node.js and use this function :

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

so in first request from and to are '0' and '10', then '9' and '19', etc by using list.trim and list.drop I thought that I can build a pagination system

回答1:

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

I ended by coding this function : to be sure to don't get errors for list index , I get the size of the list and I compare this size to the portion I want to paginate to determine what to return: I don't iterate over the whole array.

Of course it works only if the record is a list Note : ensure that the first 'from' coming from the client is 1 because lua arrays begin to 1



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!