Added mwindow_mapped_limit, cached_memory, enable_caching, cache_max_size, cache_object_limit options

This commit is contained in:
Szucs Krisztian 2016-12-03 13:52:19 +01:00
parent 66280af83a
commit 77f0585645
4 changed files with 221 additions and 0 deletions

View File

@ -28,6 +28,11 @@
from _pygit2 import option
from _pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
from _pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
from _pygit2 import GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
from _pygit2 import GIT_OPT_SET_CACHE_OBJECT_LIMIT
from _pygit2 import GIT_OPT_GET_CACHED_MEMORY
from _pygit2 import GIT_OPT_ENABLE_CACHING
from _pygit2 import GIT_OPT_SET_CACHE_MAX_SIZE
class SearchPathList(object):
@ -64,3 +69,36 @@ class Settings(object):
@mwindow_size.setter
def mwindow_size(self, value):
option(GIT_OPT_SET_MWINDOW_SIZE, value)
@property
def mwindow_mapped_limit(self):
"""Mwindow mapped limit"""
return option(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT)
@mwindow_mapped_limit.setter
def mwindow_mapped_limit(self, value):
"""Mwindow mapped limit"""
return option(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, value)
@property
def cached_memory(self):
"""Maximum mmap window size"""
return option(GIT_OPT_GET_CACHED_MEMORY)
def enable_caching(self, value=True):
return option(GIT_OPT_ENABLE_CACHING, value)
def cache_max_size(self, value):
return option(GIT_OPT_SET_CACHE_MAX_SIZE, value)
def cache_object_limit(self, object_type, value):
"""Set the maximum data size for the given type of object to be
considered eligible for caching in memory.
Setting to value to zero means that that type of object will not
be cached. Defaults to 0 for GIT_OBJ_BLOB (i.e. won't cache
blobs) and 4k for GIT_OBJ_COMMIT, GIT_OBJ_TREE, and GIT_OBJ_TAG.
"""
return option(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, value)

View File

@ -156,6 +156,138 @@ option(PyObject *self, PyObject *args)
Py_RETURN_NONE;
break;
}
case GIT_OPT_GET_MWINDOW_MAPPED_LIMIT:
{
size_t limit;
error = git_libgit2_opts(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, &limit);
if (error < 0) {
Error_set(error);
return NULL;
}
return PyLong_FromSize_t(limit);
break;
}
case GIT_OPT_SET_MWINDOW_MAPPED_LIMIT:
{
size_t limit;
PyObject *py_limit;
py_limit = PyTuple_GetItem(args, 1);
if (!py_limit)
return NULL;
if (!PyLong_Check(py_limit))
goto on_non_integer;
limit = PyLong_AsSize_t(py_limit);
error = git_libgit2_opts(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, limit);
if (error < 0) {
Error_set(error);
return NULL;
}
Py_RETURN_NONE;
break;
}
case GIT_OPT_SET_CACHE_OBJECT_LIMIT:
{
size_t limit;
int object_type;
PyObject *py_object_type, *py_limit;
py_object_type = PyTuple_GetItem(args, 1);
if (!py_object_type)
return NULL;
py_limit = PyTuple_GetItem(args, 2);
if (!py_limit)
return NULL;
if (!PyLong_Check(py_limit))
goto on_non_integer;
object_type = PyLong_AsLong(py_object_type);
limit = PyLong_AsSize_t(py_limit);
error = git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, limit);
if (error < 0) {
Error_set(error);
return NULL;
}
Py_RETURN_NONE;
break;
}
case GIT_OPT_SET_CACHE_MAX_SIZE:
{
size_t max_size;
PyObject *py_max_size;
py_max_size = PyTuple_GetItem(args, 1);
if (!py_max_size)
return NULL;
if (!PyLong_Check(py_max_size))
goto on_non_integer;
max_size = PyLong_AsSize_t(py_max_size);
error = git_libgit2_opts(GIT_OPT_SET_CACHE_MAX_SIZE, max_size);
if (error < 0) {
Error_set(error);
return NULL;
}
Py_RETURN_NONE;
break;
}
case GIT_OPT_ENABLE_CACHING:
{
int flag;
PyObject *py_flag;
py_flag = PyTuple_GetItem(args, 1);
if (!PyLong_Check(py_flag))
goto on_non_integer;
flag = PyLong_AsSize_t(py_flag);
error = git_libgit2_opts(GIT_OPT_ENABLE_CACHING, flag);
if (error < 0) {
Error_set(error);
return NULL;
}
Py_RETURN_NONE;
break;
}
case GIT_OPT_GET_CACHED_MEMORY:
{
size_t current;
size_t allowed;
PyObject* tup = PyTuple_New(2);
error = git_libgit2_opts(GIT_OPT_GET_CACHED_MEMORY, &current, &allowed);
if (error < 0) {
Error_set(error);
return NULL;
}
PyTuple_SetItem(tup, 0, PyLong_FromLong(current));
PyTuple_SetItem(tup, 1, PyLong_FromLong(allowed));
return tup;
break;
}
}
PyErr_SetString(PyExc_ValueError, "unknown/unsupported option value");

View File

@ -170,6 +170,12 @@ moduleinit(PyObject* m)
ADD_CONSTANT_INT(m, GIT_OPT_SET_SEARCH_PATH);
ADD_CONSTANT_INT(m, GIT_OPT_GET_MWINDOW_SIZE);
ADD_CONSTANT_INT(m, GIT_OPT_SET_MWINDOW_SIZE);
ADD_CONSTANT_INT(m, GIT_OPT_GET_MWINDOW_MAPPED_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_SET_CACHE_OBJECT_LIMIT);
ADD_CONSTANT_INT(m, GIT_OPT_GET_CACHED_MEMORY);
ADD_CONSTANT_INT(m, GIT_OPT_ENABLE_CACHING);
ADD_CONSTANT_INT(m, GIT_OPT_SET_CACHE_MAX_SIZE);
/* Errors */
GitError = PyErr_NewException("_pygit2.GitError", NULL, NULL);

View File

@ -31,9 +31,14 @@ from __future__ import absolute_import
from __future__ import unicode_literals
import unittest
import pygit2
from pygit2 import GIT_OBJ_BLOB
from pygit2 import GIT_OPT_GET_MWINDOW_SIZE, GIT_OPT_SET_MWINDOW_SIZE
from pygit2 import GIT_OPT_GET_SEARCH_PATH, GIT_OPT_SET_SEARCH_PATH
from pygit2 import GIT_OPT_GET_MWINDOW_MAPPED_LIMIT, GIT_OPT_SET_MWINDOW_MAPPED_LIMIT
from pygit2 import GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_GLOBAL
from pygit2 import GIT_OPT_SET_CACHE_OBJECT_LIMIT
from pygit2 import GIT_OPT_GET_CACHED_MEMORY
from pygit2 import GIT_OPT_ENABLE_CACHING
from pygit2 import option
from . import utils
@ -50,6 +55,46 @@ class OptionsTest(utils.NoRepoTestCase):
self.assertEqual(new_size, pygit2.settings.mwindow_size)
def test_mwindow_mapped_limit(self):
new_limit = 200 * 1024
option(GIT_OPT_SET_MWINDOW_MAPPED_LIMIT, new_limit)
self.assertEqual(new_limit, option(GIT_OPT_GET_MWINDOW_MAPPED_LIMIT))
def test_mwindow_mapped_limit(self):
new_limit = 300 * 1024
pygit2.settings.mwindow_mapped_limit = new_limit
self.assertEqual(new_limit, pygit2.settings.mwindow_mapped_limit)
def test_cache_object_limit(self):
new_limit = 2 * 1024
option(GIT_OPT_SET_CACHE_OBJECT_LIMIT, GIT_OBJ_BLOB, new_limit)
def test_cache_object_limit_proxy(self):
new_limit = 4 * 1024
pygit2.settings.cache_object_limit(GIT_OBJ_BLOB, new_limit)
def test_cached_memory(self):
value = option(GIT_OPT_GET_CACHED_MEMORY)
self.assertEqual(value, (0, 256 * 1024**2))
def test_cached_memory_proxy(self):
self.assertEqual(pygit2.settings.cached_memory, (0, 256 * 1024**2))
def test_enable_cache(self):
option(GIT_OPT_ENABLE_CACHING, False)
option(GIT_OPT_ENABLE_CACHING, True)
def test_enable_cache_proxy(self):
pygit2.settings.enable_caching(False)
pygit2.settings.enable_caching(True)
def test_cache_max_size_proxy(self):
pygit2.settings.cache_max_size(128 * 1024**2)
self.assertEqual(pygit2.settings.cached_memory, (0, 128 * 1024**2))
pygit2.settings.cache_max_size(256 * 1024**2)
self.assertEqual(pygit2.settings.cached_memory, (0, 256 * 1024**2))
def test_search_path(self):
paths = [(GIT_CONFIG_LEVEL_GLOBAL, '/tmp/global'),
(GIT_CONFIG_LEVEL_XDG, '/tmp/xdg'),