-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk-storage.service.ts
More file actions
241 lines (212 loc) · 6.69 KB
/
chunk-storage.service.ts
File metadata and controls
241 lines (212 loc) · 6.69 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
/**
* Chunk Storage Service
* Handles persistence of text chunks and metadata to database
*/
import { TextChunk } from '../types';
import { logger } from '../utils/logger';
import { db } from '../database/connection';
export class ChunkStorageService {
/**
* Save chunks to database
*/
async saveChunks(chunks: TextChunk[]): Promise<void> {
if (chunks.length === 0) {
logger.warn('No chunks to save');
return;
}
const documentId = chunks[0].documentId;
logger.info('Saving chunks to database', {
documentId,
chunkCount: chunks.length,
});
await db.transaction(async (client) => {
for (const chunk of chunks) {
await client.query(
`INSERT INTO text_chunks
(id, document_id, page_number, chunk_index, text, normalized_text,
language, token_count, bounding_box, metadata)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (document_id, chunk_index)
DO UPDATE SET
text = EXCLUDED.text,
normalized_text = EXCLUDED.normalized_text,
language = EXCLUDED.language,
token_count = EXCLUDED.token_count,
bounding_box = EXCLUDED.bounding_box,
metadata = EXCLUDED.metadata`,
[
chunk.id,
chunk.documentId,
chunk.pageNumber,
chunk.chunkIndex,
chunk.text,
chunk.normalizedText,
chunk.language,
chunk.tokenCount,
chunk.boundingBox ? JSON.stringify(chunk.boundingBox) : null,
chunk.metadata ? JSON.stringify(chunk.metadata) : null,
]
);
}
});
logger.info('Chunks saved successfully', {
documentId,
chunkCount: chunks.length,
});
}
/**
* Retrieve chunks for a document
*/
async getChunksByDocument(documentId: string): Promise<TextChunk[]> {
logger.debug('Retrieving chunks for document', { documentId });
const result = await db.query(
`SELECT id, document_id, page_number, chunk_index, text, normalized_text,
language, token_count, bounding_box, metadata, created_at
FROM text_chunks
WHERE document_id = $1
ORDER BY chunk_index ASC`,
[documentId]
);
const chunks: TextChunk[] = result.rows.map((row) => ({
id: row.id,
documentId: row.document_id,
pageNumber: row.page_number,
chunkIndex: row.chunk_index,
text: row.text,
normalizedText: row.normalized_text,
language: row.language,
tokenCount: row.token_count,
boundingBox: row.bounding_box,
metadata: row.metadata,
}));
logger.debug('Retrieved chunks', {
documentId,
chunkCount: chunks.length,
});
return chunks;
}
/**
* Retrieve chunks for a specific page
*/
async getChunksByPage(documentId: string, pageNumber: number): Promise<TextChunk[]> {
logger.debug('Retrieving chunks for page', { documentId, pageNumber });
const result = await db.query(
`SELECT id, document_id, page_number, chunk_index, text, normalized_text,
language, token_count, bounding_box, metadata, created_at
FROM text_chunks
WHERE document_id = $1 AND page_number = $2
ORDER BY chunk_index ASC`,
[documentId, pageNumber]
);
return result.rows.map((row) => ({
id: row.id,
documentId: row.document_id,
pageNumber: row.page_number,
chunkIndex: row.chunk_index,
text: row.text,
normalizedText: row.normalized_text,
language: row.language,
tokenCount: row.token_count,
boundingBox: row.bounding_box,
metadata: row.metadata,
}));
}
/**
* Get single chunk by ID
*/
async getChunkById(chunkId: string): Promise<TextChunk | null> {
logger.debug('Retrieving chunk by ID', { chunkId });
const result = await db.query(
`SELECT id, document_id, page_number, chunk_index, text, normalized_text,
language, token_count, bounding_box, metadata, created_at
FROM text_chunks
WHERE id = $1`,
[chunkId]
);
if (result.rows.length === 0) {
return null;
}
const row = result.rows[0];
return {
id: row.id,
documentId: row.document_id,
pageNumber: row.page_number,
chunkIndex: row.chunk_index,
text: row.text,
normalizedText: row.normalized_text,
language: row.language,
tokenCount: row.token_count,
boundingBox: row.bounding_box,
metadata: row.metadata,
};
}
/**
* Delete chunks for a document
*/
async deleteChunks(documentId: string): Promise<void> {
logger.info('Deleting chunks for document', { documentId });
await db.query('DELETE FROM text_chunks WHERE document_id = $1', [documentId]);
logger.info('Chunks deleted', { documentId });
}
/**
* Get chunk count for a document
*/
async getChunkCount(documentId: string): Promise<number> {
const result = await db.query(
'SELECT COUNT(*) as count FROM text_chunks WHERE document_id = $1',
[documentId]
);
return parseInt(result.rows[0].count, 10);
}
/**
* Get total token count for a document
*/
async getTotalTokenCount(documentId: string): Promise<number> {
const result = await db.query(
'SELECT COALESCE(SUM(token_count), 0) as total FROM text_chunks WHERE document_id = $1',
[documentId]
);
return parseInt(result.rows[0].total, 10);
}
/**
* Get chunk statistics for a document
*/
async getChunkStats(documentId: string): Promise<{
totalChunks: number;
totalTokens: number;
avgTokensPerChunk: number;
minTokens: number;
maxTokens: number;
}> {
logger.debug('Getting chunk statistics', { documentId });
const result = await db.query(
`SELECT
COUNT(*) as total_chunks,
COALESCE(SUM(token_count), 0) as total_tokens,
COALESCE(AVG(token_count), 0) as avg_tokens,
COALESCE(MIN(token_count), 0) as min_tokens,
COALESCE(MAX(token_count), 0) as max_tokens
FROM text_chunks
WHERE document_id = $1`,
[documentId]
);
const row = result.rows[0];
return {
totalChunks: parseInt(row.total_chunks, 10),
totalTokens: parseInt(row.total_tokens, 10),
avgTokensPerChunk: parseFloat(row.avg_tokens),
minTokens: parseInt(row.min_tokens, 10),
maxTokens: parseInt(row.max_tokens, 10),
};
}
/**
* Check if chunks exist for document
*/
async hasChunks(documentId: string): Promise<boolean> {
const result = await db.query(
'SELECT EXISTS(SELECT 1 FROM text_chunks WHERE document_id = $1) as exists',
[documentId]
);
return result.rows[0].exists;
}
}