Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 1 | # Copyright (C) 2018-2019 Garmin Ltd. |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 2 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0-only |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 4 | # |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 5 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 6 | from contextlib import closing |
| 7 | import re |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 8 | import sqlite3 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 9 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 10 | UNIX_PREFIX = "unix://" |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 11 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 12 | ADDR_TYPE_UNIX = 0 |
| 13 | ADDR_TYPE_TCP = 1 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 14 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 15 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 16 | def setup_database(database, sync=True): |
| 17 | db = sqlite3.connect(database) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 18 | db.row_factory = sqlite3.Row |
| 19 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 20 | with closing(db.cursor()) as cursor: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 21 | cursor.execute(''' |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 22 | CREATE TABLE IF NOT EXISTS tasks_v2 ( |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 24 | method TEXT NOT NULL, |
| 25 | outhash TEXT NOT NULL, |
| 26 | taskhash TEXT NOT NULL, |
| 27 | unihash TEXT NOT NULL, |
| 28 | created DATETIME, |
| 29 | |
| 30 | -- Optional fields |
| 31 | owner TEXT, |
| 32 | PN TEXT, |
| 33 | PV TEXT, |
| 34 | PR TEXT, |
| 35 | task TEXT, |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 36 | outhash_siginfo TEXT, |
| 37 | |
| 38 | UNIQUE(method, outhash, taskhash) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 39 | ) |
| 40 | ''') |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 41 | cursor.execute('PRAGMA journal_mode = WAL') |
| 42 | cursor.execute('PRAGMA synchronous = %s' % ('NORMAL' if sync else 'OFF')) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 43 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 44 | # Drop old indexes |
| 45 | cursor.execute('DROP INDEX IF EXISTS taskhash_lookup') |
| 46 | cursor.execute('DROP INDEX IF EXISTS outhash_lookup') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 47 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 48 | # Create new indexes |
| 49 | cursor.execute('CREATE INDEX IF NOT EXISTS taskhash_lookup_v2 ON tasks_v2 (method, taskhash, created)') |
| 50 | cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup_v2 ON tasks_v2 (method, outhash)') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 51 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 52 | return db |
| 53 | |
| 54 | |
| 55 | def parse_address(addr): |
| 56 | if addr.startswith(UNIX_PREFIX): |
| 57 | return (ADDR_TYPE_UNIX, (addr[len(UNIX_PREFIX):],)) |
| 58 | else: |
| 59 | m = re.match(r'\[(?P<host>[^\]]*)\]:(?P<port>\d+)$', addr) |
| 60 | if m is not None: |
| 61 | host = m.group('host') |
| 62 | port = m.group('port') |
| 63 | else: |
| 64 | host, port = addr.split(':') |
| 65 | |
| 66 | return (ADDR_TYPE_TCP, (host, int(port))) |
| 67 | |
| 68 | |
| 69 | def create_server(addr, dbname, *, sync=True): |
| 70 | from . import server |
| 71 | db = setup_database(dbname, sync=sync) |
| 72 | s = server.Server(db) |
| 73 | |
| 74 | (typ, a) = parse_address(addr) |
| 75 | if typ == ADDR_TYPE_UNIX: |
| 76 | s.start_unix_server(*a) |
| 77 | else: |
| 78 | s.start_tcp_server(*a) |
| 79 | |
| 80 | return s |
| 81 | |
| 82 | |
| 83 | def create_client(addr): |
| 84 | from . import client |
| 85 | c = client.Client() |
| 86 | |
| 87 | (typ, a) = parse_address(addr) |
| 88 | if typ == ADDR_TYPE_UNIX: |
| 89 | c.connect_unix(*a) |
| 90 | else: |
| 91 | c.connect_tcp(*a) |
| 92 | |
| 93 | return c |