-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathsqlite.py
More file actions
266 lines (234 loc) · 10.4 KB
/
Copy pathsqlite.py
File metadata and controls
266 lines (234 loc) · 10.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from typing import Optional, List
from datetime import datetime, timezone, timedelta
import json
import os
import logging
import sqlite3
from aw_core.models import Event
from aw_core.dirs import get_data_dir
from .abstract import AbstractStorage
logger = logging.getLogger(__name__)
LATEST_VERSION=1
# The max integer value in SQLite is signed 8 Bytes / 64 bits
MAX_TIMESTAMP = 2**63 - 1
CREATE_BUCKETS_TABLE = """
CREATE TABLE IF NOT EXISTS buckets (
rowid INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT UNIQUE NOT NULL,
name TEXT,
type TEXT NOT NULL,
client TEXT NOT NULL,
hostname TEXT NOT NULL,
created TEXT NOT NULL,
datastr TEXT NOT NULL
)
"""
CREATE_EVENTS_TABLE = """
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bucketrow INTEGER NOT NULL,
starttime INTEGER NOT NULL,
endtime INTEGER NOT NULL,
datastr TEXT NOT NULL,
FOREIGN KEY (bucketrow) REFERENCES buckets(rowid)
)
"""
INDEX_BUCKETS_TABLE_ID = """
CREATE INDEX IF NOT EXISTS event_index_id ON events(id);
"""
INDEX_EVENTS_TABLE_STARTTIME = """
CREATE INDEX IF NOT EXISTS event_index_starttime ON events(bucketrow, starttime);
"""
INDEX_EVENTS_TABLE_ENDTIME = """
CREATE INDEX IF NOT EXISTS event_index_endtime ON events(bucketrow, endtime);
"""
class SqliteStorage(AbstractStorage):
sid = "sqlite"
def __init__(self, testing, filepath: str = None, enable_lazy_commit=True) -> None:
self.testing = testing
self.enable_lazy_commit = enable_lazy_commit
# Ignore the migration check if custom filepath is set
ignore_migration_check = filepath is not None
ds_name = self.sid + ('-testing' if testing else '')
if not filepath:
data_dir = get_data_dir("aw-server")
filename = ds_name + ".v{}".format(LATEST_VERSION) + '.db'
filepath = os.path.join(data_dir, filename)
new_db_file = not os.path.exists(filepath)
self.conn = sqlite3.connect(filepath)
logger.info("Using database file: {}".format(filepath))
# Create tables
self.conn.execute(CREATE_BUCKETS_TABLE)
self.conn.execute(CREATE_EVENTS_TABLE)
self.conn.execute(INDEX_BUCKETS_TABLE_ID)
self.conn.execute(INDEX_EVENTS_TABLE_STARTTIME)
self.conn.execute(INDEX_EVENTS_TABLE_ENDTIME)
self.conn.execute("PRAGMA journal_mode=WAL;")
self.commit()
if new_db_file and not ignore_migration_check:
logger.info("Created new SQlite db file")
from aw_datastore import check_for_migration
check_for_migration(self)
self.last_commit = datetime.now()
self.num_uncommited_statements = 0
def commit(self):
"""
Useful for debugging and trying to lower the amount of
unnecessary commits
"""
self.conn.commit()
self.last_commit = datetime.now()
self.num_uncommited_statements = 0
def conditional_commit(self, num_statements):
"""
Only commit transactions if:
- We have a lot of statements in our transaction
- Was a while ago since last commit
This is because sqlite is very slow with small inserts, this
is a way to batch them together and lower CPU+disk usage
"""
if self.enable_lazy_commit:
self.num_uncommited_statements += num_statements
if self.num_uncommited_statements > 10:
self.commit()
if (self.last_commit - datetime.now()) > timedelta(seconds=10):
self.commit()
else:
self.commit()
def buckets(self):
buckets = {}
c = self.conn.cursor()
for row in c.execute("SELECT id, name, type, client, hostname, created FROM buckets"):
buckets[row[0]] = {
"id": row[0],
"name": row[1],
"type": row[2],
"client": row[3],
"hostname": row[4],
"created": row[5],
}
return buckets
def create_bucket(self, bucket_id: str, type_id: str, client: str,
hostname: str, created: str, name: Optional[str] = None):
self.conn.execute("INSERT INTO buckets(id, name, type, client, hostname, created, datastr) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)",
[bucket_id, name, type_id, client, hostname, created, str({})])
self.commit()
return self.get_metadata(bucket_id)
def delete_bucket(self, bucket_id: str):
self.conn.execute("DELETE FROM events WHERE bucketrow IN (SELECT rowid FROM buckets WHERE id = ?)", [bucket_id])
cursor = self.conn.execute("DELETE FROM buckets WHERE id = ?", [bucket_id])
self.commit()
if cursor.rowcount != 1:
raise Exception('Bucket did not exist, could not delete')
def get_metadata(self, bucket_id: str):
c = self.conn.cursor()
res = c.execute("SELECT id, name, type, client, hostname, created FROM buckets WHERE id = ?", [bucket_id])
row = res.fetchone()
if row is not None:
return {
"id": row[0],
"name": row[1],
"type": row[2],
"client": row[3],
"hostname": row[4],
"created": row[5],
}
else:
raise Exception('Bucket did not exist, could not get metadata')
def insert_one(self, bucket_id: str, event: Event) -> Event:
c = self.conn.cursor()
starttime = event.timestamp.timestamp() * 1000000
endtime = starttime + (event.duration.total_seconds() * 1000000)
datastr = json.dumps(event.data)
c.execute("INSERT INTO events(bucketrow, starttime, endtime, datastr) " +
"VALUES ((SELECT rowid FROM buckets WHERE id = ?), ?, ?, ?)",
[bucket_id, starttime, endtime, datastr])
event.id = c.lastrowid
self.conditional_commit(1)
return event
def insert_many(self, bucket_id, events: List[Event], fast=False) -> None:
# FIXME: Is this true not only for peewee but sqlite aswell?
# Chunking into lists of length 100 is needed here due to SQLITE_MAX_COMPOUND_SELECT
# and SQLITE_LIMIT_VARIABLE_NUMBER under Windows.
# See: https://github.com/coleifer/peewee/issues/948
event_rows = []
for event in events:
starttime = event.timestamp.timestamp() * 1000000
endtime = starttime + (event.duration.total_seconds() * 1000000)
datastr = json.dumps(event.data)
event_rows.append((bucket_id, starttime, endtime, datastr))
query = "INSERT INTO events(bucketrow, starttime, endtime, datastr) " + \
"VALUES ((SELECT rowid FROM buckets WHERE id = ?), ?, ?, ?)"
self.conn.executemany(query, event_rows)
self.conditional_commit(len(event_rows))
def replace_last(self, bucket_id, event):
starttime = event.timestamp.timestamp() * 1000000
endtime = starttime + (event.duration.total_seconds() * 1000000)
datastr = json.dumps(event.data)
query = """UPDATE events
SET starttime = ?, endtime = ?, datastr = ?
WHERE id = (
SELECT id FROM events WHERE endtime =
(SELECT max(endtime) FROM events WHERE bucketrow =
(SELECT rowid FROM buckets WHERE id = ?) LIMIT 1))"""
self.conn.execute(query, [starttime, endtime, datastr, bucket_id])
self.conditional_commit(1)
return True
def delete(self, bucket_id, event_id):
query = "DELETE FROM events " + \
"WHERE id = ? AND bucketrow = (SELECT b.rowid FROM buckets b WHERE b.id = ?)"
cursor = self.conn.execute(query, [event_id, bucket_id])
return cursor.rowcount == 1
def replace(self, bucket_id, event_id, event) -> bool:
starttime = event.timestamp.timestamp() * 1000000
endtime = starttime + (event.duration.total_seconds() * 1000000)
datastr = json.dumps(event.data)
query = """UPDATE events
SET bucketrow = (SELECT rowid FROM buckets WHERE id = ?),
starttime = ?,
endtime = ?,
datastr = ?
WHERE id = ?"""
self.conn.execute(query, [bucket_id, starttime, endtime, datastr, event_id])
self.conditional_commit(1)
return True
def get_events(self, bucket_id: str, limit: int,
starttime: Optional[datetime] = None, endtime: Optional[datetime] = None):
if limit == 0:
return []
elif limit < 0:
limit = -1
self.commit()
c = self.conn.cursor()
starttime_i = starttime.timestamp() * 1000000 if starttime else 0
endtime_i = endtime.timestamp() * 1000000 if endtime else MAX_TIMESTAMP
query = "SELECT id, starttime, endtime, datastr " + \
"FROM events " + \
"WHERE bucketrow = (SELECT rowid FROM buckets WHERE id = ?) " + \
"AND starttime >= ? AND endtime <= ? " + \
"ORDER BY endtime DESC LIMIT ?"
rows = c.execute(query, [bucket_id, starttime_i, endtime_i, limit])
events = []
for row in rows:
eid = row[0]
starttime = datetime.fromtimestamp(row[1] / 1000000, timezone.utc)
endtime = datetime.fromtimestamp(row[2] / 1000000, timezone.utc)
duration = endtime - starttime
data = json.loads(row[3])
events.append(Event(id=eid, timestamp=starttime, duration=duration, data=data))
return events
def get_eventcount(self, bucket_id: str,
starttime: Optional[datetime] = None, endtime: Optional[datetime] = None):
self.commit()
c = self.conn.cursor()
starttime_i = starttime.timestamp() * 1000000 if starttime else 0
endtime_i = endtime.timestamp() * 1000000 if endtime else MAX_TIMESTAMP
query = "SELECT count(*) " + \
"FROM events " + \
"WHERE bucketrow = (SELECT rowid FROM buckets WHERE id = ?) " + \
"AND endtime >= ? AND starttime <= ?"
rows = c.execute(query, [bucket_id, starttime_i, endtime_i])
row = rows.fetchone()
eventcount = row[0]
return eventcount