This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.py
178 lines (133 loc) · 5.39 KB
/
persistence.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
from abc import abstractmethod
import redis
import pymongo
import config
class Database:
def __init__(self, name: str, db_type):
self.name = name
self.type = db_type
@abstractmethod
def count(self):
pass
class RedisSet(Database):
def __init__(self, name: str):
super(RedisSet, self).__init__(name, 'Redis')
self.conn = redis.StrictRedis(host=config.Config.database.redis.host, port=config.Config.database.redis.port,
decode_responses=True)
def add(self, values):
return self.conn.sadd(self.name, values)
def count(self):
return self.conn.scard(self.name)
def pop(self):
return self.conn.spop(self.name)
def remove(self, values):
return self.conn.srem(self.name, values)
def rand(self, number=None):
if number:
return self.conn.srandmember(self.name, number)
else:
return self.conn.srandmember(self.name)
def is_member(self, value):
return self.conn.sismember(self.name, value)
def all(self):
return self.conn.smembers(self.name)
def flush_all(self):
return self.conn.delete(self.name)
class RedisHash(Database):
def __init__(self, name: str):
super(RedisHash, self).__init__(name, 'Redis')
self.conn = redis.StrictRedis(host=config.Config.database.redis.host, port=config.Config.database.redis.port,
decode_responses=True)
def add(self, key):
return self.conn.hsetnx(self.name, key, 0)
def count(self):
return self.conn.hlen(self.name)
def remove(self, keys):
return self.conn.hdel(self.name, keys)
def exists(self, key):
return self.conn.hexists(self.name, key)
def all(self):
return self.conn.hgetall(self.name)
def get(self, keys):
"""
:param keys: a single key or a list of keys
:return: a string, or a list of string correspondingly
"""
if type(keys) is list:
return self.conn.hmget(self.name, keys)
else:
return self.conn.hget(self.name, keys)
def set(self, mapping: dict):
if len(mapping) > 1:
return self.conn.hmset(self.name, mapping)
elif len(mapping) == 1:
(key, value), = mapping.items()
return self.conn.hset(self.name, key, value)
def increment(self, key, value: int = 1):
return self.conn.hincrby(self.name, key, value)
class MongoDB(Database):
def __init__(self, collection: str):
super(MongoDB, self).__init__(collection, 'MongoDB')
client = pymongo.MongoClient(host=config.Config.database.mongodb.host, port=config.Config.database.mongodb.port)
db = client[config.Config.database.mongodb.database]
self.conn = db[collection]
def insert(self, documents):
if type(documents) is list:
return self.conn.insert_many(documents)
else:
return self.conn.insert_one(documents)
def remove(self, filter, all=False):
if all:
return self.conn.delete_many(filter=filter)
else:
return self.conn.delete_one(filter=filter)
def update(self, filter, update, all=False):
if all:
return self.conn.update_many(filter=filter, update=update)
else:
return self.conn.update_one(filter=filter, update=update)
def replace(self, filter, replacement):
return self.conn.replace_one(filter=filter, replacement=replacement)
def find(self, filter, all=False, **kwargs):
if all:
return self.conn.find(filter=filter, **kwargs)
else:
return self.conn.find_one(filter=filter, **kwargs)
def all(self):
return self.conn.find()
def count(self, filter=None, **kwargs):
if filter is None:
return self.conn.count_documents(filter={}, **kwargs)
else:
return self.conn.count_documents(filter=filter, **kwargs)
def create_index(self, index):
"""
:param index: [('key', pymongo.HASHED)]
:return: Index Name
"""
return self.conn.create_index(index)
class LocalFile(Database):
def __init__(self, file_path: str, file_type=None):
super(LocalFile, self).__init__(os.path.basename(file_path), 'LocalFile')
self.file_path = file_path
self.file_type = file_type
def count(self):
if os.path.isdir(self.file_path):
if self.file_type:
return len([name for name in os.listdir(self.file_path) if
os.path.isfile(os.path.join(self.file_path, name)) and os.path.splitext(
name) == self.file_type])
else:
return len([name for name in os.listdir(self.file_path)
if os.path.isfile(os.path.join(self.file_path, name))])
else:
return 0
def test_redis():
conn = redis.StrictRedis(host=config.Config.database.redis.host, port=config.Config.database.redis.port,
decode_responses=True)
conn.client_list()
def test_mongodb():
client = pymongo.MongoClient(host=config.Config.database.mongodb.host, port=config.Config.database.mongodb.port,
serverSelectionTimeoutMS=3000, connectTimeoutMS=3000)
client.admin.command('ismaster')