Skip to content

Commit e14013c

Browse files
authored
update: dbm types (#46)
* update: dbm types * add: file manager types * update: paths * add: comment
1 parent cdc4d12 commit e14013c

17 files changed

+257
-169
lines changed

meerkat-dbm/src/dbm/dbm.spec.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { AsyncDuckDB } from '@duckdb/duckdb-wasm';
22
import log from 'loglevel';
33
import {
44
FileBufferStore,
5-
FileData,
65
FileManagerType,
7-
Table,
86
} from '../file-manager/file-manager-type';
9-
import { DBM, DBMConstructorOptions } from './dbm';
7+
import { FileData, Table, TableWiseFiles } from '../types';
8+
import { DBM } from './dbm';
109
import { InstanceManagerType } from './instance-manager';
11-
10+
import { DBMConstructorOptions } from './types';
1211
export class MockFileManager implements FileManagerType {
1312
private fileBufferStore: Record<string, FileBufferStore> = {};
1413
private tables: Record<string, Table> = {};
@@ -80,16 +79,8 @@ export class MockFileManager implements FileManagerType {
8079
}
8180
}
8281

83-
async getFilesNameForTables(tableNames: string[]): Promise<
84-
{
85-
tableName: string;
86-
files: string[];
87-
}[]
88-
> {
89-
const data: {
90-
tableName: string;
91-
files: string[];
92-
}[] = [];
82+
async getFilesNameForTables(tableNames: string[]): Promise<TableWiseFiles[]> {
83+
const data: TableWiseFiles[] = [];
9384

9485
for (const tableName of tableNames) {
9586
const files: string[] = [];
@@ -184,8 +175,8 @@ describe('DBM', () => {
184175
});
185176

186177
describe('queryWithTableNames', () => {
187-
it('should call the beforeQuery hook', async () => {
188-
const beforeQuery = jest.fn();
178+
it('should call the preQuery hook', async () => {
179+
const preQuery = jest.fn();
189180

190181
await fileManager.registerFileBuffer({
191182
fileName: 'file1',
@@ -197,13 +188,13 @@ describe('DBM', () => {
197188
'SELECT * FROM table1',
198189
['table1'],
199190
{
200-
beforeQuery,
191+
preQuery,
201192
}
202193
);
203194

204-
expect(beforeQuery).toBeCalledTimes(1);
195+
expect(preQuery).toBeCalledTimes(1);
205196

206-
expect(beforeQuery).toBeCalledWith([
197+
expect(preQuery).toBeCalledWith([
207198
{
208199
tableName: 'table1',
209200
files: ['file1'],
@@ -271,7 +262,7 @@ describe('DBM', () => {
271262
// If instanceManager.terminateDB is a method
272263
jest.spyOn(instanceManager, 'terminateDB');
273264

274-
const onDuckdbShutdown = jest.fn();
265+
const onDuckDBShutdown = jest.fn();
275266

276267
// If instanceManager.terminateDB is a function
277268
instanceManager.terminateDB = jest.fn();
@@ -282,7 +273,7 @@ describe('DBM', () => {
282273
onEvent: (event) => {
283274
console.log(event);
284275
},
285-
onDuckdbShutdown: onDuckdbShutdown,
276+
onDuckDBShutdown: onDuckDBShutdown,
286277
options: {
287278
shutdownInactiveTime: 100,
288279
},
@@ -320,9 +311,9 @@ describe('DBM', () => {
320311
await new Promise((resolve) => setTimeout(resolve, 200));
321312

322313
/**
323-
* Expect onDuckdbShutdown to be called
314+
* Expect onDuckDBShutdown to be called
324315
*/
325-
expect(onDuckdbShutdown).toBeCalled();
316+
expect(onDuckDBShutdown).toBeCalled();
326317

327318
/**
328319
* Expect instanceManager.terminateDB to be called

meerkat-dbm/src/dbm/dbm.ts

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,35 @@ import { DBMEvent } from '../logger/event-types';
44
import { DBMLogger } from '../logger/logger-types';
55
import { InstanceManagerType } from './instance-manager';
66

7-
export interface DBMConstructorOptions {
8-
fileManager: FileManagerType;
9-
logger: DBMLogger;
10-
onEvent?: (event: DBMEvent) => void;
11-
onDuckdbShutdown?: () => void;
12-
instanceManager: InstanceManagerType;
13-
options?: {
14-
/**
15-
* shutdown the database after inactive of this time in milliseconds
16-
*/
17-
shutdownInactiveTime?: number;
18-
};
19-
}
20-
21-
type BeforeQueryHook = (
22-
data: {
23-
tableName: string;
24-
files: string[];
25-
}[]
26-
) => Promise<void>;
27-
28-
type QueryOptions = {
29-
beforeQuery?: BeforeQueryHook;
30-
metadata?: object;
31-
};
7+
import { DBMConstructorOptions, QueryOptions, QueryQueueItem } from './types';
328

339
export class DBM {
3410
private fileManager: FileManagerType;
3511
private instanceManager: InstanceManagerType;
3612
private connection: AsyncDuckDBConnection | null = null;
37-
private queriesQueue: {
38-
query: string;
39-
tableNames: string[];
40-
promise: {
41-
resolve: (value: any) => void;
42-
reject: (reason?: any) => void;
43-
};
44-
/**
45-
* Timestamp when the query was added to the queue
46-
*/
47-
timestamp: number;
48-
options?: QueryOptions;
49-
}[] = [];
50-
private beforeQuery?: ({
51-
query,
52-
tableByFiles,
53-
}: {
54-
query: string;
55-
tableByFiles: {
56-
tableName: string;
57-
files: string[];
58-
}[];
59-
}) => Promise<void>;
13+
private queriesQueue: QueryQueueItem[] = [];
14+
6015
private queryQueueRunning = false;
6116
private logger: DBMLogger;
6217
private onEvent?: (event: DBMEvent) => void;
6318
private options: DBMConstructorOptions['options'];
6419
private terminateDBTimeout: NodeJS.Timeout | null = null;
65-
private onDuckdbShutdown?: () => void;
20+
private onDuckDBShutdown?: () => void;
6621

6722
constructor({
6823
fileManager,
6924
logger,
7025
onEvent,
7126
options,
7227
instanceManager,
73-
onDuckdbShutdown,
28+
onDuckDBShutdown,
7429
}: DBMConstructorOptions) {
7530
this.fileManager = fileManager;
7631
this.logger = logger;
7732
this.onEvent = onEvent;
7833
this.options = options;
7934
this.instanceManager = instanceManager;
80-
this.onDuckdbShutdown = onDuckdbShutdown;
35+
this.onDuckDBShutdown = onDuckDBShutdown;
8136
}
8237

8338
private async _shutdown() {
@@ -86,8 +41,8 @@ export class DBM {
8641
this.connection = null;
8742
}
8843
this.logger.debug('Shutting down the DB');
89-
if (this.onDuckdbShutdown) {
90-
this.onDuckdbShutdown();
44+
if (this.onDuckDBShutdown) {
45+
this.onDuckDBShutdown();
9146
}
9247
await this.fileManager.onDBShutdownHandler();
9348
await this.instanceManager.terminateDB();
@@ -159,10 +114,10 @@ export class DBM {
159114
);
160115

161116
/**
162-
* Execute the beforeQuery hook
117+
* Execute the preQuery hook
163118
*/
164-
if (options?.beforeQuery) {
165-
await options.beforeQuery(tablesFileData);
119+
if (options?.preQuery) {
120+
await options.preQuery(tablesFileData);
166121
}
167122

168123
/**

meerkat-dbm/src/dbm/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './dbm';
2+
export * from './instance-manager';
3+

meerkat-dbm/src/dbm/instance-manager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { AsyncDuckDB } from '@duckdb/duckdb-wasm';
22

33
export interface InstanceManagerType {
44
/**
5+
* @description
56
* Get the instance of the database after initializing it
67
*/
78
getDB: () => Promise<AsyncDuckDB>;
89
/**
10+
* @description
911
* Terminate the database instance, primarily used for memory cleanup
1012
*/
1113
terminateDB: () => Promise<void>;

meerkat-dbm/src/dbm/types.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { FileManagerType } from '../file-manager/file-manager-type';
2+
import { DBMEvent } from '../logger/event-types';
3+
import { DBMLogger } from '../logger/logger-types';
4+
import { TableWiseFiles } from '../types';
5+
import { InstanceManagerType } from './instance-manager';
6+
7+
export interface DBMConstructorOptions {
8+
/**
9+
* @description
10+
* It handles all file operations such as file registration, file retrieval, file deletion
11+
* including mounting and unmounting of files in DuckDB instance.
12+
*/
13+
fileManager: FileManagerType;
14+
15+
/**
16+
* @description
17+
* It manages the lifecycle of the DuckDB database instance.
18+
* It provides methods for obtaining an initialized DuckDB instance and terminating the instance.
19+
*/
20+
instanceManager: InstanceManagerType;
21+
22+
/**
23+
* @description
24+
* Represents an logger instance, which will be used for logging messages throughout the DBM's execution.
25+
*/
26+
logger: DBMLogger;
27+
28+
/**
29+
* @description
30+
* A callback function that handles events emitted by the DBM.
31+
*/
32+
onEvent?: (event: DBMEvent) => void;
33+
34+
/**
35+
* @description
36+
* A callback function that handles shutdown event of DuckDB.
37+
*/
38+
onDuckDBShutdown?: () => void;
39+
40+
/**
41+
* @description
42+
* Configuration options for the DBM.
43+
*/
44+
options?: {
45+
/**
46+
* @description
47+
* Denotes the shutdown time for the database after inactivity, in milliseconds.
48+
* If not specified, the DB will not shutdown.
49+
*/
50+
shutdownInactiveTime?: number;
51+
};
52+
}
53+
54+
/**
55+
* Configuration options for query execution.
56+
*/
57+
export interface QueryOptions {
58+
/**
59+
* @description
60+
* A callback function which will be executed before the query is executed.
61+
* @param tableWiseFiles - An array of tables with associated file names.
62+
*/
63+
preQuery?: (tableWiseFiles: TableWiseFiles[]) => Promise<void>;
64+
65+
/**
66+
* @description
67+
* Additional information for the query, which will be emitted in the DBM events.
68+
*/
69+
metadata?: object;
70+
}
71+
72+
export interface QueryQueueItem {
73+
query: string;
74+
tableNames: string[];
75+
promise: {
76+
resolve: (value: any) => void;
77+
reject: (reason?: any) => void;
78+
};
79+
/**
80+
* Timestamp indicating when the query was added to the queue.
81+
*/
82+
timestamp: number;
83+
options?: QueryOptions;
84+
}

0 commit comments

Comments
 (0)