How to use the maxminddb.const.MODE_MEMORY function in maxminddb

To help you get started, we’ve selected a few maxminddb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maxmind / MaxMind-DB-Reader-python / maxminddb / reader.py View on Github external
* MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
            * MODE_FD - the param passed via database is a file descriptor, not
                        a path. This mode implies MODE_MEMORY.
        """
        if (mode == MODE_AUTO and mmap) or mode == MODE_MMAP:
            with open(database, 'rb') as db_file:
                self._buffer = mmap.mmap(db_file.fileno(),
                                         0,
                                         access=mmap.ACCESS_READ)
                self._buffer_size = self._buffer.size()
            filename = database
        elif mode in (MODE_AUTO, MODE_FILE):
            self._buffer = FileBuffer(database)
            self._buffer_size = self._buffer.size()
            filename = database
        elif mode == MODE_MEMORY:
            with open(database, 'rb') as db_file:
                self._buffer = db_file.read()
                self._buffer_size = len(self._buffer)
            filename = database
        elif mode == MODE_FD:
            self._buffer = database.read()
            self._buffer_size = len(self._buffer)
            filename = database.name
        else:
            raise ValueError(
                'Unsupported open mode ({0}). Only MODE_AUTO, MODE_FILE, '
                'MODE_MEMORY and MODE_FD are supported by the pure Python '
                'Reader'.format(mode))

        metadata_start = self._buffer.rfind(
            self._METADATA_START_MARKER, max(0,
github m-lab / mlab-ns / server / mlabns / third_party / geoip2 / maxminddb / reader.py View on Github external
* MODE_MEMORY - load database into memory.
            * MODE_AUTO - tries MODE_MMAP and then MODE_FILE. Default.
            * MODE_FD - the param passed via database is a file descriptor, not
                        a path. This mode implies MODE_MEMORY.
        """
        if (mode == MODE_AUTO and mmap) or mode == MODE_MMAP:
            with open(database, 'rb') as db_file:
                self._buffer = mmap.mmap(
                    db_file.fileno(), 0, access=mmap.ACCESS_READ)
                self._buffer_size = self._buffer.size()
            filename = database
        elif mode in (MODE_AUTO, MODE_FILE):
            self._buffer = FileBuffer(database)
            self._buffer_size = self._buffer.size()
            filename = database
        elif mode == MODE_MEMORY:
            with open(database, 'rb') as db_file:
                self._buffer = db_file.read()
                self._buffer_size = len(self._buffer)
            filename = database
        elif mode == MODE_FD:
            self._buffer = database.read()
            self._buffer_size = len(self._buffer)
            filename = database.name
        else:
            raise ValueError(
                'Unsupported open mode ({0}). Only MODE_AUTO, MODE_FILE, '
                'MODE_MEMORY and MODE_FD are supported by the pure Python '
                'Reader'.format(mode))

        metadata_start = self._buffer.rfind(
            self._METADATA_START_MARKER, max(0,