Skip to content

Commit e50c5b3

Browse files
Run search operations in batch
1 parent 1e9db02 commit e50c5b3

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

pkcs11/_pkcs11.pyx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,31 +657,39 @@ cdef class SearchIter(OperationContext):
657657
"""Iterate a search for objects on a session."""
658658

659659
cdef AttributeList template
660+
cdef CK_ULONG batch_size
660661

661-
def __init__(self, session, attrs):
662+
def __init__(self, session, attrs, batch_size):
662663
cdef AttributeList template = AttributeList(attrs)
663664
self.template = template
665+
self.batch_size = batch_size
664666
super().__init__(session)
665667

666668
def __iter__(self):
667669
return self
668670

671+
def _batch_iter(self, results, count):
672+
for ix in range(count):
673+
yield make_object(self.session, results[ix])
674+
669675
def __next__(self):
670-
"""Get the next object."""
676+
"""Get the next batch of objects."""
671677
cdef CK_SESSION_HANDLE handle = self.session.handle
672678
cdef CK_OBJECT_HANDLE obj
673679
cdef CK_ULONG count
674680
cdef CK_RV retval
675681

682+
cdef CK_OBJECT_HANDLE [:] results = CK_ULONG_buffer(self.batch_size)
683+
676684
with nogil:
677-
retval = self.session.funclist.C_FindObjects(handle, &obj, 1, &count)
685+
retval = self.session.funclist.C_FindObjects(handle, &results[0], self.batch_size, &count)
678686
assertRV(retval)
679687

680688
if count == 0:
681689
self._finalize()
682690
raise StopIteration()
683691
else:
684-
return make_object(self.session, obj)
692+
return self._batch_iter(results, count)
685693

686694
def _initiate(self):
687695
cdef CK_SESSION_HANDLE handle = self.session.handle
@@ -830,9 +838,10 @@ cdef class Session(HasFuncList, types.Session):
830838
retval = self.funclist.C_CloseSession(handle)
831839
assertRV(retval)
832840

833-
def get_objects(self, attrs=None):
834-
with SearchIter(self, attrs or {}) as op:
835-
yield from op
841+
def get_objects(self, attrs=None, batch_size=10):
842+
with SearchIter(self, attrs or {}, batch_size) as op:
843+
for batch in op:
844+
yield from batch
836845

837846
def reaffirm_credentials(self, pin):
838847
cdef CK_UTF8CHAR *pin_data

0 commit comments

Comments
 (0)