Debian Codegraph @

python/examples/db_common.py

raw
1 #!/usr/bin/env python3
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21
22 import sqlite3
23 import queue
24 import threading
25
26
27 class Db(object):
28 def __init__(self, db, injector):
29 self.db = db
30 self.injector = injector
31 self.tasks = queue.Queue()
32 self.position = None
33 self.pending_events = []
34 self.running = True
35 self.thread = threading.Thread(target=self._process)
36 self.thread.daemon = True
37 self.thread.start()
38
39 def close(self):
40 self.tasks.put(lambda conn: self._close())
41
42 def reset(self):
43 self.tasks.put(lambda conn: self._reset())
44
45 def load(self, records, event=None):
46 self.tasks.put(lambda conn: self._load(conn, records, event))
47
48 def get_id(self, event):
49 self.tasks.put(lambda conn: self._get_id(conn, event))
50
51 def insert(self, id, data, event=None):
52 self.tasks.put(lambda conn: self._insert(conn, id, data, event))
53
54 def delete(self, id, event=None):
55 self.tasks.put(lambda conn: self._delete(conn, id, event))
56
57 def _reset(self, ignored=None):
58 self.position = None
59
60 def _close(self, ignored=None):
61 self.running = False
62
63 def _get_id(self, conn, event):
64 cursor = conn.execute("SELECT * FROM records ORDER BY id DESC")
65 row = cursor.fetchone()
66 if event:
67 if row:
68 event.id = row['id']
69 else:
70 event.id = 0
71 self.injector.trigger(event)
72
73 def _load(self, conn, records, event):
74 if self.position:
75 cursor = conn.execute("SELECT * FROM records WHERE id > ? ORDER BY id", (self.position,))
76 else:
77 cursor = conn.execute("SELECT * FROM records ORDER BY id")
78 while not records.full():
79 row = cursor.fetchone()
80 if row:
81 self.position = row['id']
82 records.put(dict(row))
83 else:
84 break
85 if event:
86 self.injector.trigger(event)
87
88 def _insert(self, conn, id, data, event):
89 if id:
90 conn.execute("INSERT INTO records(id, description) VALUES (?, ?)", (id, data))
91 else:
92 conn.execute("INSERT INTO records(description) VALUES (?)", (data,))
93 if event:
94 self.pending_events.append(event)
95
96 def _delete(self, conn, id, event):
97 conn.execute("DELETE FROM records WHERE id=?", (id,))
98 if event:
99 self.pending_events.append(event)
100
101 def _process(self):
102 conn = sqlite3.connect(self.db)
103 conn.row_factory = sqlite3.Row
104 with conn:
105 while self.running:
106 f = self.tasks.get(True)
107 try:
108 while True:
109 f(conn)
110 f = self.tasks.get(False)
111 except queue.Empty:
112 pass
113 conn.commit()
114 for event in self.pending_events:
115 self.injector.trigger(event)
116 self.pending_events = []
117 self.injector.close()