]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/database.c
database: Unmap all mapped sections when freeing database
[people/ms/libloc.git] / src / python / database.c
index c29d912b00de4c10de1a82fb12b0e5303b31d537..931b54d4040e3650cec27b265b8ff61ac065d24e 100644 (file)
 #include <loc/libloc.h>
 #include <loc/database.h>
 
+#include "locationmodule.h"
+#include "as.h"
 #include "database.h"
+#include "network.h"
 
 static PyObject* Database_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
-       // Create libloc context
-       struct loc_ctx* ctx;
-       int r = loc_new(&ctx);
-       if (r)
-               return NULL;
-
        DatabaseObject* self = (DatabaseObject*)type->tp_alloc(type, 0);
-       if (self) {
-               self->ctx = ctx;
-       }
 
        return (PyObject*)self;
 }
@@ -40,9 +34,6 @@ static void Database_dealloc(DatabaseObject* self) {
        if (self->db)
                loc_database_unref(self->db);
 
-       if (self->ctx)
-               loc_unref(self->ctx);
-
        Py_TYPE(self)->tp_free((PyObject* )self);
 }
 
@@ -58,7 +49,7 @@ static int Database_init(DatabaseObject* self, PyObject* args, PyObject* kwargs)
                return -1;
 
        // Load the database
-       int r = loc_database_new(self->ctx, &self->db, f);
+       int r = loc_database_new(loc_ctx, &self->db, f);
        fclose(f);
 
        // Return on any errors
@@ -86,7 +77,69 @@ static PyObject* Database_get_created_at(DatabaseObject* self) {
        return PyLong_FromLong(created_at);
 }
 
+static PyObject* Database_get_as(DatabaseObject* self, PyObject* args) {
+       struct loc_as* as = NULL;
+       uint32_t number = 0;
+
+       if (!PyArg_ParseTuple(args, "i", &number))
+               return NULL;
+
+       // Try to retrieve the AS
+       int r = loc_database_get_as(self->db, &as, number);
+       if (r)
+               return NULL;
+
+       // Create an AS object
+       if (as) {
+               PyObject* obj = new_as(&ASType, as);
+               loc_as_unref(as);
+
+               return obj;
+       }
+
+       // Nothing found
+       Py_RETURN_NONE;
+}
+
+static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
+       struct loc_network* network = NULL;
+       const char* address = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &address))
+               return NULL;
+
+       // Try to retrieve a matching network
+       int r = loc_database_lookup_from_string(self->db, address, &network);
+
+       // We got a network
+       if (r == 0) {
+               PyObject* obj = new_network(&NetworkType, network);
+               loc_network_unref(network);
+
+               return obj;
+
+       // Nothing found
+       } else if (r == 1) {
+               Py_RETURN_NONE;
+       }
+
+       // Unexpected error
+       return NULL;
+}
+
 static struct PyMethodDef Database_methods[] = {
+       {
+               "get_as",
+               (PyCFunction)Database_get_as,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "lookup",
+               (PyCFunction)Database_lookup,
+               METH_VARARGS,
+               NULL,
+       },
        { NULL },
 };