-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperQueue.py
executable file
·117 lines (96 loc) · 3.83 KB
/
SuperQueue.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
# SuperQueue.py
#
# Non-Deterministic Processor (NDP) - efficient parallel SAT-solver
# Copyright (c) 2023 GridSAT Stiftung
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# GridSAT Stiftung - Georgstr. 11 - 30159 Hannover - Germany - ipfs: gridsat.eth/ - info@gridsat.io
#
import hashlib
import re
import time
import Set
from DbAdaptor import DbAdapter
from configs import PROBLEM_ID
from collections import deque
from collections import OrderedDict
from ordered_set import OrderedSet
''' Queue that uses both memory and database to hold big number of object efficiently '''
# will save ids in memory, while the objects will be saved in DB
# tip: a nice command to get the size of a table in bytes: SELECT pg_size_pretty(pg_relation_size('foo'));
class SuperQueue:
db = None
use_runtime_db = False
def __init__(self, name="", unique_queue=False, use_runtime_db=False, problem_id=PROBLEM_ID):
self.unique_queue = unique_queue
if unique_queue:
self.objqueue = OrderedSet()
self.idsqueue = OrderedSet() # queue of objects ids
else:
self.objqueue = deque() # queue of objects
self.idsqueue = deque() # queue of objects ids
self.table_name = "queue_" + hashlib.sha224("{}_{}_{}".format(re.sub(r'[\(\)\{\}# ]', '', name), problem_id, str(time.time()).replace(".", "")).encode()).hexdigest()
self.use_runtime_db = use_runtime_db
if use_runtime_db:
self.db = DbAdapter()
self.db.rtq_create_table(self.table_name)
def __del__(self):
#drop table
if self.use_runtime_db and self.db is not None:
self.db.rtq_cleanup(self.table_name)
def insert(self, item):
will_add_new_item = True
# in case of unique queue, make sure to add to the database only when new item is added
if self.unique_queue and len(self.idsqueue) > self.idsqueue.append(item):
will_add_new_item = False
if will_add_new_item:
if self.use_runtime_db:
self.idsqueue.append(item.id)
self.db.rtq_insert_set(self.table_name, item.id, item.to_string(pretty=False), item.serialize_properties())
else:
self.objqueue.append(item)
return True
def pop(self):
item = None
if self.use_runtime_db:
objid = None
if self.unique_queue:
objid = self.idsqueue[0]
self.idsqueue.remove(objid)
else:
objid = self.idsqueue.popleft()
id, body, properties = self.db.rtq_get_set(self.table_name, objid)
item = Set.Set(body, properties=properties)
item.id = id
elif self.unique_queue:
item = self.objqueue[0]
self.objqueue.remove(item)
else:
item = self.objqueue.popleft()
return item
def size(self):
size = 0
if self.use_runtime_db:
size = len(self.idsqueue)
else:
size = len(self.objqueue)
return size
def is_empty(self):
return not bool(self.size())
def relink_db(self):
if self.use_runtime_db:
self.db = DbAdapter()
def unlink_db(self):
self.db = None