# -*- coding: utf-8 -*-
"""只操作本程序创建的临时对象；全部数据为合成样本。"""
import os, sys, json, sqlite3, tempfile, pathlib, subprocess, time, socket, stat
from pathlib import Path
def emit(**kw):
    print(json.dumps(kw, ensure_ascii=False, sort_keys=True))
with tempfile.TemporaryDirectory(prefix='ywebs-next132-b2-vacuum-') as d:
    p=str(Path(d)/'demo.db'); c=sqlite3.connect(p); c.execute('CREATE TABLE t(id INTEGER PRIMARY KEY,data BLOB)')
    c.executemany('INSERT INTO t VALUES(?,?)',[(i,b'x'*4096) for i in range(400)]); c.commit()
    def snapshot(): return {'bytes':Path(p).stat().st_size,'pages':c.execute('PRAGMA page_count').fetchone()[0],'free':c.execute('PRAGMA freelist_count').fetchone()[0],'rows':c.execute('SELECT count(*) FROM t').fetchone()[0]}
    before=snapshot(); c.execute('DELETE FROM t WHERE id<350'); c.commit(); deleted=snapshot(); c.execute('VACUUM'); after=snapshot()
    assert deleted['bytes']==before['bytes'] and deleted['free']>0 and after['bytes']<deleted['bytes'] and after['rows']==50
    emit(before=before,deleted=deleted,vacuumed=after); c.close()
