perf(observabilidade): synchronous=NORMAL e upsertMany transaccional
This commit is contained in:
@@ -15,6 +15,7 @@ export interface ListFilters {
|
|||||||
|
|
||||||
export interface SessionsDb {
|
export interface SessionsDb {
|
||||||
upsertSession(meta: SessionMeta): void
|
upsertSession(meta: SessionMeta): void
|
||||||
|
upsertMany(metas: SessionMeta[]): void
|
||||||
listSessions(filters: ListFilters): SessionMeta[]
|
listSessions(filters: ListFilters): SessionMeta[]
|
||||||
countSessions(filters: ListFilters): number
|
countSessions(filters: ListFilters): number
|
||||||
getSession(id: string): SessionMeta | null
|
getSession(id: string): SessionMeta | null
|
||||||
@@ -104,6 +105,7 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
|||||||
mkdirSync(dirname(dbPath), { recursive: true })
|
mkdirSync(dirname(dbPath), { recursive: true })
|
||||||
const db = new Database(dbPath)
|
const db = new Database(dbPath)
|
||||||
db.pragma('journal_mode = WAL')
|
db.pragma('journal_mode = WAL')
|
||||||
|
db.pragma('synchronous = NORMAL')
|
||||||
db.exec(SCHEMA)
|
db.exec(SCHEMA)
|
||||||
|
|
||||||
const upsertStmt = db.prepare(`
|
const upsertStmt = db.prepare(`
|
||||||
@@ -133,6 +135,16 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
|||||||
indexed_at = excluded.indexed_at
|
indexed_at = excluded.indexed_at
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
const upsertManyTxn = db.transaction((metas: SessionMeta[]) => {
|
||||||
|
for (const meta of metas) {
|
||||||
|
upsertStmt.run({
|
||||||
|
...meta,
|
||||||
|
tools_used: JSON.stringify(meta.tools_used),
|
||||||
|
skills_invoked: JSON.stringify(meta.skills_invoked),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
upsertSession(meta) {
|
upsertSession(meta) {
|
||||||
upsertStmt.run({
|
upsertStmt.run({
|
||||||
@@ -141,6 +153,9 @@ export function openSessionsDb(dbPath: string): SessionsDb {
|
|||||||
skills_invoked: JSON.stringify(meta.skills_invoked),
|
skills_invoked: JSON.stringify(meta.skills_invoked),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
upsertMany(metas) {
|
||||||
|
upsertManyTxn(metas)
|
||||||
|
},
|
||||||
listSessions(filters) {
|
listSessions(filters) {
|
||||||
const { sql, params } = buildWhere(filters)
|
const { sql, params } = buildWhere(filters)
|
||||||
const limit = filters.limit ?? 50
|
const limit = filters.limit ?? 50
|
||||||
|
|||||||
@@ -68,4 +68,14 @@ describe('sessions db', () => {
|
|||||||
db.upsertSession(sampleMeta({ session_id: 'b', jsonl_path: '/tmp/b.jsonl' }))
|
db.upsertSession(sampleMeta({ session_id: 'b', jsonl_path: '/tmp/b.jsonl' }))
|
||||||
expect(db.countSessions({})).toBe(2)
|
expect(db.countSessions({})).toBe(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('upsertMany insere batch em transacção', () => {
|
||||||
|
const db = openSessionsDb(dbPath)
|
||||||
|
db.upsertMany([
|
||||||
|
sampleMeta({ session_id: 'x', jsonl_path: '/tmp/x.jsonl' }),
|
||||||
|
sampleMeta({ session_id: 'y', jsonl_path: '/tmp/y.jsonl' }),
|
||||||
|
sampleMeta({ session_id: 'z', jsonl_path: '/tmp/z.jsonl' }),
|
||||||
|
])
|
||||||
|
expect(db.countSessions({})).toBe(3)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user