Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Python iterator for RecordDatabase (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringgaard authored Dec 3, 2018
1 parent db39e09 commit 756bebf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sling/pyapi/pyrecordio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ void PyRecordDatabase::Define(PyObject *module) {

type.tp_init = method_cast<initproc>(&PyRecordDatabase::Init);
type.tp_dealloc = method_cast<destructor>(&PyRecordDatabase::Dealloc);
type.tp_iter = method_cast<getiterfunc>(&PyRecordDatabase::Self);
type.tp_iternext = method_cast<iternextfunc>(&PyRecordDatabase::Next);

methods.Add("close", &PyRecordDatabase::Close);
methods.AddO("lookup", &PyRecordDatabase::Lookup);
Expand Down Expand Up @@ -200,6 +202,35 @@ PyObject *PyRecordDatabase::Lookup(PyObject *obj) {
return PyString_FromStringAndSize(record.value.data(), record.value.size());
}

PyObject *PyRecordDatabase::Next() {
// Read next record.
Record record;
if (!db->Next(&record)) {
PyErr_SetNone(PyExc_StopIteration);
return nullptr;
}

// Create key and value tuple.
PyObject *k = Py_None;
PyObject *v = Py_None;
if (!record.key.empty()) {
k = PyString_FromStringAndSize(record.key.data(), record.key.size());
}
if (!record.value.empty()) {
v = PyString_FromStringAndSize(record.value.data(), record.value.size());
}
PyObject *pair = PyTuple_Pack(2, k, v);
if (k != Py_None) Py_DECREF(k);
if (v != Py_None) Py_DECREF(v);

return pair;
}

PyObject *PyRecordDatabase::Self() {
Py_INCREF(this);
return AsObject();
}

void PyRecordWriter::Define(PyObject *module) {
InitType(&type, "sling.RecordWriter", sizeof(PyRecordWriter), true);

Expand Down
6 changes: 6 additions & 0 deletions sling/pyapi/pyrecordio.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ struct PyRecordDatabase : public PyBase {
// Close record database.
PyObject *Close();

// Return next record as 2-tuple with key and value.
PyObject *Next();

// Return self as iterator.
PyObject *Self();

// Record reader.
RecordDatabase *db;

Expand Down

0 comments on commit 756bebf

Please sign in to comment.