/
persistent_dictionary.py
55 lines (45 loc) · 1.5 KB
/
persistent_dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
#
# Copyright (c) 2011-2013, Shopkick Inc.
# All rights reserved.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# ---
# Author: John Egan <john@shopkick.com>
import os
import os.path
import pickle
import shutil
import threading
from flawless.lib.data_structures import ProxyContainerMethodsMetaClass
class PersistentDictionary(object):
''' Provides a persistent thread-safe dictionary that is backed by a file on disk '''
__metaclass__ = ProxyContainerMethodsMetaClass
def _proxyfunc_(attr, self, *args, **kwargs):
with self.lock:
return getattr(self.dict, attr)(*args, **kwargs)
def __init__(self, file_path):
self.lock = threading.RLock()
self.file_path = file_path
self.dict = None
def open(self):
with self.lock:
if os.path.isfile(self.file_path):
fh = open(self.file_path, "rb+")
self.dict = pickle.load(fh)
fh.close()
else:
self.dict = dict()
def sync(self):
with self.lock:
fh = open(self.file_path + ".tmp", "wb+")
pickle.dump(self.dict, fh, pickle.HIGHEST_PROTOCOL)
fh.close()
shutil.move(self.file_path + ".tmp", self.file_path)
def close(self):
pass
def get_path(self):
return self.file_path