comparison roundup/hyperdb.py @ 5869:16e1255b16cf

Implement limit and offset for filter These map to the corresponding SQL parameters for the SQL backends.
author Ralf Schlatterbeck <rsc@runtux.com>
date Mon, 26 Aug 2019 20:23:53 +0200
parents ee2e8f8d6648
children acc4a128ab9b
comparison
equal deleted inserted replaced
5868:d2fac1069028 5869:16e1255b16cf
1397 if 'id' not in seen : 1397 if 'id' not in seen :
1398 sortattr.append(('+', 'id')) 1398 sortattr.append(('+', 'id'))
1399 return sortattr 1399 return sortattr
1400 1400
1401 def filter(self, search_matches, filterspec, sort=[], group=[], 1401 def filter(self, search_matches, filterspec, sort=[], group=[],
1402 retired=False, exact_match_spec={}): 1402 retired=False, exact_match_spec={}, limit=None, offset=None):
1403 """Return a list of the ids of the active nodes in this class that 1403 """Return a list of the ids of the active nodes in this class that
1404 match the 'filter' spec, sorted by the group spec and then the 1404 match the 'filter' spec, sorted by the group spec and then the
1405 sort spec. 1405 sort spec.
1406 1406
1407 "search_matches" is a container type which by default is None 1407 "search_matches" is a container type which by default is None
1424 (un-retired) results. When setting it to True, only retired 1424 (un-retired) results. When setting it to True, only retired
1425 items are returned. If None, both retired and unretired items 1425 items are returned. If None, both retired and unretired items
1426 are returned. The default is False, i.e. only live items are 1426 are returned. The default is False, i.e. only live items are
1427 returned by default. 1427 returned by default.
1428 1428
1429 The "limit" and "offset" parameters define a limit on the number
1430 of results returned and an offset before returning any results,
1431 respectively. These can be used when displaying a number of
1432 items in a pagination application or similar. A common use-case
1433 is returning the first item of a sorted search by specifying
1434 limit=1 (i.e. the maximum or minimum depending on sort order).
1435
1429 The filter must match all properties specificed. If the property 1436 The filter must match all properties specificed. If the property
1430 value to match is a list: 1437 value to match is a list:
1431 1438
1432 1. String properties must match all elements in the list, and 1439 1. String properties must match all elements in the list, and
1433 2. Other properties must match any of the elements in the list. 1440 2. Other properties must match any of the elements in the list.
1451 override the filter method instead of implementing _filter. 1458 override the filter method instead of implementing _filter.
1452 """ 1459 """
1453 sortattr = self._sortattr(sort = sort, group = group) 1460 sortattr = self._sortattr(sort = sort, group = group)
1454 proptree = self._proptree(exact_match_spec, filterspec, sortattr) 1461 proptree = self._proptree(exact_match_spec, filterspec, sortattr)
1455 proptree.search(search_matches, retired=retired) 1462 proptree.search(search_matches, retired=retired)
1463 if offset is not None or limit is not None:
1464 items = proptree.sort()
1465 if limit and offset:
1466 return items[offset:offset+limit]
1467 elif offset is not None:
1468 return items[offset:]
1469 else:
1470 return items[:limit]
1456 return proptree.sort() 1471 return proptree.sort()
1472
1457 1473
1458 # non-optimized filter_iter, a backend may chose to implement a 1474 # non-optimized filter_iter, a backend may chose to implement a
1459 # better version that provides a real iterator that pre-fills the 1475 # better version that provides a real iterator that pre-fills the
1460 # cache for each id returned. Note that the filter_iter doesn't 1476 # cache for each id returned. Note that the filter_iter doesn't
1461 # promise to correctly sort by multilink (which isn't sane to do 1477 # promise to correctly sort by multilink (which isn't sane to do

Roundup Issue Tracker: http://roundup-tracker.org/