Commit 78a635b8 by Christian Hudon

Add support for gzipped .dat files when in MEMORY_CACHE mode.

parent f9e69e66
...@@ -32,6 +32,7 @@ import os ...@@ -32,6 +32,7 @@ import os
import math import math
import socket import socket
import mmap import mmap
import gzip
from . import const from . import const
from .util import ip2long from .util import ip2long
...@@ -78,11 +79,13 @@ class GeoIP(GeoIPBase): ...@@ -78,11 +79,13 @@ class GeoIP(GeoIPBase):
""" """
Initialize the class. Initialize the class.
@param filename: path to a geoip database @param filename: path to a geoip database. If MEMORY_CACHE is used,
the file can be gzipped.
@type filename: str @type filename: str
@param flags: flags that affect how the database is processed. @param flags: flags that affect how the database is processed.
Currently the only supported flags are STANDARD, MEMORY_CACHE, and Currently the only supported flags are STANDARD (the default),
MMAP_CACHE. MEMORY_CACHE (preload the whole file into memory), and
MMAP_CACHE (access the file via mmap).
@type flags: int @type flags: int
""" """
self._filename = filename self._filename = filename
...@@ -92,12 +95,13 @@ class GeoIP(GeoIPBase): ...@@ -92,12 +95,13 @@ class GeoIP(GeoIPBase):
with open(filename, 'rb') as f: with open(filename, 'rb') as f:
self._filehandle = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) self._filehandle = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
elif self._flags & const.MEMORY_CACHE:
self._filehandle = gzip.open(filename, 'rb')
self._memoryBuffer = self._filehandle.read()
else: else:
self._filehandle = open(filename, 'rb') self._filehandle = open(filename, 'rb')
if self._flags & const.MEMORY_CACHE:
self._memoryBuffer = self._filehandle.read()
self._setup_segments() self._setup_segments()
def _setup_segments(self): def _setup_segments(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment