Skip to content

Commit

Permalink
fix issue #66 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
gijzelaerr authored Sep 11, 2020
1 parent 729d582 commit cd1c125
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
46 changes: 26 additions & 20 deletions monetdbe/_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,21 @@ class MonetEmbedded:
in_memory_active: bool = False
_connection: Optional[ffi.CData] = None

def __init__(self, dbdir: Optional[Path] = None):
def __init__(
self,
dbdir: Optional[Path] = None,
memorylimit: int = 0,
querytimeout: int = 0,
sessiontimeout: int = 0,
nr_threads: int = 0,
have_hge: bool = False
):
self.dbdir = dbdir
self.memorylimit = memorylimit
self.querytimeout = querytimeout
self.sessiontimeout = sessiontimeout
self.nr_threads = nr_threads
self.have_hge = have_hge
self._switch()

@classmethod
Expand Down Expand Up @@ -157,7 +170,7 @@ def _switch(self):
return

self.close()
self.set_connection(self.open(self.dbdir))
self.set_connection(self.open())
self.set_active_context(self)

if not self.dbdir:
Expand All @@ -168,28 +181,20 @@ def cleanup_result(self, result: ffi.CData):
if result and self._connection:
check_error(lib.monetdbe_cleanup_result(self._connection, result))

@staticmethod
def open(
dbdir: Optional[Path] = None,
memorylimit: int = 0,
querytimeout: int = 0,
sessiontimeout: int = 0,
nr_threads: int = 0,
have_hge: bool = False
):
def open(self):

if not dbdir:
if not self.dbdir:
url = ffi.NULL
else:
url = str(dbdir).encode()
url = str(self.dbdir).encode()

p_connection = ffi.new("monetdbe_database *")

p_options = ffi.new("monetdbe_options *")
p_options.memorylimit = memorylimit
p_options.querytimeout = querytimeout
p_options.sessiontimeout = sessiontimeout
p_options.nr_threads = nr_threads
p_options.memorylimit = self.memorylimit
p_options.querytimeout = self.querytimeout
p_options.sessiontimeout = self.sessiontimeout
p_options.nr_threads = self.nr_threads

result_code = lib.monetdbe_open(p_connection, url, p_options)
connection = p_connection[0]
Expand Down Expand Up @@ -226,9 +231,10 @@ def query(self, query: str, make_result: bool = False) -> Tuple[Optional[Any], i
"""
Execute a query.
query: the query
make_results: Create and return a result object. If enabled, you need to call cleanup_result on the
result afterwards
Args:
query: the query
make_results: Create and return a result object. If enabled, you need to call cleanup_result on the
result afterwards
returns:
result, affected_rows
Expand Down
32 changes: 26 additions & 6 deletions monetdbe/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,40 @@ class Connection:
def __init__(self,
database: Optional[Union[str, Path]] = None,
uri: bool = False,
timeout: float = 5.0,
timeout: int = 0,
detect_types: int = 0,
check_same_thread: bool = True,
autocommit: bool = False,
nr_threads: int = 0,
memorylimit: int = 0,
querytimeout: int = 0,
logging: Optional[Path] = None,
username: Optional[str] = None,
password: Optional[str] = None,
port: Optional[int] = None,
):
"""
Args:
database: The path to you database. Leave empty or use the `:memory:` string to start an in-memory database.
uri: if true, database is interpreted as a URI. This allows you to specify options.
timeout: The connection timeout, which is unused and meaningless in the case of MonetDBe and exists for
compatibility reasons.
timeout: The session / connection timeout in seconds, 0 = no limit (default)
detect_types: defaults to 0 (i. e. off, no type detection), you can set it to any combination of
PARSE_DECLTYPES and PARSE_COLNAMES to turn type detection on.
check_same_thread: By default, check_same_thread is True and only the creating thread may use the
connection. If set False, the returned connection may be shared across multiple threads.
When using multiple threads with the same connection writing operations should be
serialized by the user to avoid data corruption.
autocommit: Enable autocommit mode
nr_threads: to control the level of parallelism, 0 = all cores (default)
memorylimit: to control the memory footprint allowed in :memory: mode in MB, 0 = no limit (default)
querytimeout: The query timeout in seconds, 0 = no limit (default)
logging: the file location for the tracer files (not used yet)
username: used to connect to a remote server (not used yet)
password: credentials to reach the remote server (not used yet)
port: TCP/IP port to listen for connections (not used yet)
"""
if uri:
if uri or port or username or password or logging:
raise NotImplemented # todo

if not check_same_thread:
Expand All @@ -55,7 +69,13 @@ def __init__(self,
raise TypeError

from monetdbe._cffi import MonetEmbedded
self.lowlevel: Optional[MonetEmbedded] = MonetEmbedded(dbdir=database)
self.lowlevel: Optional[MonetEmbedded] = MonetEmbedded(
dbdir=database,
memorylimit=memorylimit,
nr_threads=nr_threads,
querytimeout=querytimeout,
sessiontimeout=timeout
)

self.result = None
self.row_factory: Optional[Type[Row]] = None
Expand Down Expand Up @@ -210,7 +230,7 @@ def set_autocommit(self, value: bool) -> None:
value: a boolean value
"""
self._check()
return self.lowlevel.set_autocommit(value) # type: ignore
return self.lowlevel.set_autocommit(value) # type: ignore

# these are required by the python DBAPI
Warning = exceptions.Warning
Expand Down
4 changes: 2 additions & 2 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def setUp(self):
except OSError:
pass

self.con1 = monetdbe.connect(self.db_path, timeout=0.1)
self.con1 = monetdbe.connect(self.db_path, timeout=1)
self.cur1 = self.con1.cursor()

self.con2 = monetdbe.connect(self.db_path, timeout=0.1)
self.con2 = monetdbe.connect(self.db_path, timeout=1)
self.cur2 = self.con2.cursor()

def tearDown(self):
Expand Down

0 comments on commit cd1c125

Please sign in to comment.