Compress large source revision snapshots before PostgreSQL writes
Metadata integrity checks / metadata (push) Successful in 7m33s
Metadata integrity checks / metadata (push) Successful in 7m33s
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
"""Versioned publication-source scopes and guards against stale source refreshes."""
|
||||
import base64
|
||||
import gzip
|
||||
import json
|
||||
import re
|
||||
|
||||
@@ -6,6 +8,24 @@ TABLE = 'source_scope_revisions'
|
||||
MARKER = 'source_scope_revisions_v1'
|
||||
COLUMNS = ('version_sha256', 'source_id', 'venue_id', 'year', 'event_key', 'list_url',
|
||||
'parent_sha256', 'records_json', 'plan_json', 'applied_at')
|
||||
COMPRESSED_PREFIX = 'gzip+base64:v1:'
|
||||
COMPRESSION_THRESHOLD = 1024 * 1024
|
||||
|
||||
|
||||
def encode_payload(value):
|
||||
"""Compress large revision payloads before sending them to the database."""
|
||||
text = json.dumps(value, ensure_ascii=False, separators=(',', ':'))
|
||||
raw = text.encode('utf-8')
|
||||
if len(raw) < COMPRESSION_THRESHOLD:
|
||||
return text
|
||||
return COMPRESSED_PREFIX + base64.b64encode(gzip.compress(raw, compresslevel=1, mtime=0)).decode('ascii')
|
||||
|
||||
|
||||
def decode_payload(text):
|
||||
if text.startswith(COMPRESSED_PREFIX):
|
||||
packed = base64.b64decode(text[len(COMPRESSED_PREFIX):], validate=True)
|
||||
return json.loads(gzip.decompress(packed))
|
||||
return json.loads(text)
|
||||
|
||||
|
||||
def available(c):
|
||||
@@ -60,7 +80,7 @@ def current_rows(c, source_id, cache):
|
||||
records = {}; scopes = {}
|
||||
for signature in signatures:
|
||||
raw = c.execute('SELECT records_json FROM source_scope_revisions WHERE version_sha256=?', (signature,)).fetchone()[0]
|
||||
scopes[signature] = json.loads(raw)
|
||||
scopes[signature] = decode_payload(raw)
|
||||
for row in scopes[signature]:
|
||||
ext = row['external_id']
|
||||
if ext in records and records[ext] != row:
|
||||
|
||||
Reference in New Issue
Block a user