tiramisu/tiramisu/storage/util.py

136 lines
4.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-01-20 14:53:08 +01:00
"utils used by storage"
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
from ..setting import owners
2013-09-22 20:57:52 +02:00
class SerializeObject(object):
def __getstate__(self):
ret = {}
for key in dir(self):
if not key.startswith('__'):
ret[key] = getattr(self, key)
return ret
class Cache(object):
2013-09-22 20:57:52 +02:00
__slots__ = ('_cache', '_storage')
key_is_path = False
def __init__(self, storage):
self._cache = {}
2013-09-22 20:57:52 +02:00
self._storage = storage
def __getstate__(self):
slots = set()
for subclass in self.__class__.__mro__:
if subclass is not object:
slots.update(subclass.__slots__)
slots -= frozenset(['__weakref__', '_storage'])
states = {}
for slot in slots:
try:
value = getattr(self, slot)
#value has owners object, need 'str()' it
if slot == '_values':
2015-11-19 22:25:00 +01:00
_value = []
_value.append(value[0])
_value.append(value[1])
str_owner = []
_value.append(value[2])
for owner in value[3]:
if isinstance(owner, list):
str_owners = []
for subowner in owner:
str_owners.append(str(subowner))
str_owner.append(str_owners)
else:
str_owner.append(str(owner))
_value.append(str_owner)
2013-09-22 20:57:52 +02:00
states[slot] = _value
else:
states[slot] = value
except AttributeError:
pass
return states
def __setstate__(self, states):
2015-11-19 22:25:00 +01:00
def convert_owner(owner):
try:
owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
owner = getattr(owners, owner)
return owner
2013-09-22 20:57:52 +02:00
for key, value in states.items():
#value has owners object, need to reconstruct it
if key == '_values':
2015-11-19 22:25:00 +01:00
_value = []
_value.append(value[0])
_value.append(value[1])
_value.append(value[2])
obj_owner = []
for owner in value[3]:
if isinstance(owner, list):
obj_owners = []
for subowner in owner:
obj_owners.append(convert_owner(subowner))
obj_owner.append(tuple(obj_owners))
else:
obj_owner.append(convert_owner(owner))
_value.append(tuple(obj_owner))
value = tuple(_value)
2013-09-22 20:57:52 +02:00
setattr(self, key, value)
def setcache(self, path, val, time, index):
self._cache.setdefault(path, {})[index] = (val, time)
def getcache(self, path, exp, index):
value, created = self._cache[path][index]
2013-09-07 22:37:13 +02:00
if created is None or exp <= created:
return True, value
return False, None
def hascache(self, path, index):
""" path is in the cache
:param path: the path's option
"""
return path in self._cache and index in self._cache[path]
2013-09-07 17:25:22 +02:00
def reset_expired_cache(self, exp):
2016-03-19 21:27:37 +01:00
cache_keys = list(self._cache.keys())
for key in cache_keys:
key_cache_keys = list(self._cache[key].keys())
for index in key_cache_keys:
val, created = self._cache[key][index]
if created is not None and exp > created:
del(self._cache[key][index])
if self._cache[key] == {}:
del(self._cache[key])
2013-09-07 17:25:22 +02:00
def reset_all_cache(self):
"empty the cache"
self._cache.clear()
2013-09-07 17:25:22 +02:00
def get_cached(self, context):
"""return all values in a dictionary
example: {'path1': {'index1': ('value1', 'time1')}, 'path2': {'index2': ('value2', 'time2', )}}
"""
return self._cache