Skip to content

Commit 7b37329

Browse files
authored
feat: dbm partition (#57)
* add: partition filter * update: test * update: interface * fix: build * update: interface * bump version
1 parent f31cccf commit 7b37329

File tree

14 files changed

+228
-109
lines changed

14 files changed

+228
-109
lines changed

benchmarking/benchmarking-app/src/app/dbm-context/raw-dbm-context.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ export const RawDBMProvider = ({ children }: { children: JSX.Element }) => {
3434
},
3535
});
3636
/**
37-
* Making the queryWithTableNames simply run the queries without sequence which is the default behavior
37+
* Making the queryWithTables simply run the queries without sequence which is the default behavior
3838
*/
39-
dbm.queryWithTableNames = async ({ query, tableNames }) => {
39+
dbm.queryWithTables = async ({ query, tables }) => {
4040
return dbm.query(query);
4141
};
4242
setdbm(dbm);

benchmarking/benchmarking-app/src/app/query-benchmarking/query-benchmarking.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const QueryBenchmarking = () => {
2323
const eachQueryStart = performance.now();
2424

2525
const promiseObj = dbm
26-
.queryWithTableNames({ query: TEST_QUERIES[i], tableNames: ['taxi'] })
26+
.queryWithTables({ query: TEST_QUERIES[i], tables: [{ name: 'taxi' }] })
2727
.then((results) => {
2828
const end = performance.now();
2929
const time = end - eachQueryStart;

meerkat-dbm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/meerkat-dbm",
3-
"version": "0.0.143",
3+
"version": "0.0.15",
44
"dependencies": {
55
"tslib": "^2.3.0",
66
"@duckdb/duckdb-wasm": "^1.28.0",

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

+39-45
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { FileData, Table, TableWiseFiles } from '../types';
99
import { DBM } from './dbm';
1010
import { InstanceManagerType } from './instance-manager';
11-
import { DBMConstructorOptions } from './types';
11+
import { DBMConstructorOptions, TableConfig } from './types';
1212

1313
export class MockFileManager implements FileManagerType {
1414
private fileBufferStore: Record<string, FileBufferStore> = {};
@@ -53,7 +53,8 @@ export class MockFileManager implements FileManagerType {
5353
return fileBuffer.buffer;
5454
}
5555

56-
async mountFileBufferByTableNames(tableNames: string[]): Promise<void> {
56+
async mountFileBufferByTables(tables: TableConfig[]): Promise<void> {
57+
const tableNames = tables.map((table) => table.name);
5758
for (const tableName of tableNames) {
5859
for (const key in this.fileBufferStore) {
5960
if (this.fileBufferStore[key].tableName === tableName) {
@@ -64,17 +65,6 @@ export class MockFileManager implements FileManagerType {
6465
}
6566
}
6667

67-
async unmountFileBufferByTableNames(tableNames: string[]): Promise<void> {
68-
for (const tableName of tableNames) {
69-
for (const key in this.fileBufferStore) {
70-
if (this.fileBufferStore[key].tableName === tableName) {
71-
// unmount operation here
72-
console.log(`Unmounted file buffer for ${key}`);
73-
}
74-
}
75-
}
76-
}
77-
7868
async getFilesByTableName(tableName: string): Promise<FileData[]> {
7969
const files: FileData[] = [];
8070

@@ -96,15 +86,17 @@ export class MockFileManager implements FileManagerType {
9686
}
9787
}
9888

99-
async getFilesNameForTables(tableNames: string[]): Promise<TableWiseFiles[]> {
89+
async getFilesNameForTables(
90+
tableNames: TableConfig[]
91+
): Promise<TableWiseFiles[]> {
10092
const data: TableWiseFiles[] = [];
10193

102-
for (const tableName of tableNames) {
103-
const files: string[] = [];
94+
for (const { name: tableName } of tableNames) {
95+
const files: FileData[] = [];
10496

10597
for (const key in this.fileBufferStore) {
10698
if (this.fileBufferStore[key].tableName === tableName) {
107-
files.push(key);
99+
files.push({ fileName: key });
108100
}
109101
}
110102

@@ -170,6 +162,8 @@ describe('DBM', () => {
170162
let dbm: DBM;
171163
let instanceManager: InstanceManager;
172164

165+
const tables = [{ name: 'table1' }];
166+
173167
beforeAll(async () => {
174168
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
175169
//@ts-ignore
@@ -201,7 +195,7 @@ describe('DBM', () => {
201195
});
202196
});
203197

204-
describe('queryWithTableNames', () => {
198+
describe('queryWithTables', () => {
205199
it('should call the preQuery hook', async () => {
206200
const preQuery = jest.fn();
207201

@@ -211,9 +205,9 @@ describe('DBM', () => {
211205
buffer: new Uint8Array(),
212206
});
213207

214-
const result = await dbm.queryWithTableNames({
208+
const result = await dbm.queryWithTables({
215209
query: 'SELECT * FROM table1',
216-
tableNames: ['table1'],
210+
tables: tables,
217211
options: {
218212
preQuery,
219213
},
@@ -224,27 +218,27 @@ describe('DBM', () => {
224218
expect(preQuery).toBeCalledWith([
225219
{
226220
tableName: 'table1',
227-
files: ['file1'],
221+
files: [{ fileName: 'file1' }],
228222
},
229223
]);
230224
});
231225

232226
it('should execute a query with table names', async () => {
233-
const result = await dbm.queryWithTableNames({
227+
const result = await dbm.queryWithTables({
234228
query: 'SELECT * FROM table1',
235-
tableNames: ['table1'],
229+
tables: tables,
236230
});
237231
expect(result).toEqual(['SELECT * FROM table1']);
238232
});
239233

240234
it('should execute multiple queries with table names', async () => {
241-
const promise1 = dbm.queryWithTableNames({
235+
const promise1 = dbm.queryWithTables({
242236
query: 'SELECT * FROM table1',
243-
tableNames: ['table1'],
237+
tables: tables,
244238
});
245-
const promise2 = dbm.queryWithTableNames({
239+
const promise2 = dbm.queryWithTables({
246240
query: 'SELECT * FROM table2',
247-
tableNames: ['table1'],
241+
tables: tables,
248242
});
249243
/**
250244
* Number of queries in the queue should be 1 as the first query is running
@@ -271,9 +265,9 @@ describe('DBM', () => {
271265
/**
272266
* Execute another query
273267
*/
274-
const promise3 = dbm.queryWithTableNames({
268+
const promise3 = dbm.queryWithTables({
275269
query: 'SELECT * FROM table3',
276-
tableNames: ['table1'],
270+
tables: tables,
277271
});
278272

279273
/**
@@ -314,17 +308,17 @@ describe('DBM', () => {
314308
/**
315309
* Execute a query
316310
*/
317-
const promise1 = dbm.queryWithTableNames({
311+
const promise1 = dbm.queryWithTables({
318312
query: 'SELECT * FROM table1',
319-
tableNames: ['table1'],
313+
tables: tables,
320314
});
321315

322316
/**
323317
* Execute another query
324318
*/
325-
const promise2 = dbm.queryWithTableNames({
319+
const promise2 = dbm.queryWithTables({
326320
query: 'SELECT * FROM table2',
327-
tableNames: ['table1'],
321+
tables: tables,
328322
});
329323

330324
/**
@@ -375,17 +369,17 @@ describe('DBM', () => {
375369
/**
376370
* Execute a query
377371
*/
378-
const promise1 = dbm.queryWithTableNames({
372+
const promise1 = dbm.queryWithTables({
379373
query: 'SELECT * FROM table1',
380-
tableNames: ['table1'],
374+
tables: tables,
381375
});
382376

383377
/**
384378
* Execute another query
385379
*/
386-
const promise2 = dbm.queryWithTableNames({
380+
const promise2 = dbm.queryWithTables({
387381
query: 'SELECT * FROM table2',
388-
tableNames: ['table1'],
382+
tables: tables,
389383
});
390384

391385
/**
@@ -420,9 +414,9 @@ describe('DBM', () => {
420414
/**
421415
* Execute a query
422416
*/
423-
await dbm.queryWithTableNames({
417+
await dbm.queryWithTables({
424418
query: 'SELECT * FROM table1',
425-
tableNames: ['table1'],
419+
tables: tables,
426420
});
427421

428422
/**
@@ -443,9 +437,9 @@ describe('DBM', () => {
443437

444438
// check the current query throws error abort is emitted
445439
try {
446-
const promise = dbm.queryWithTableNames({
440+
const promise = dbm.queryWithTables({
447441
query: 'SELECT * FROM table1',
448-
tableNames: ['table1'],
442+
tables: tables,
449443
options: {
450444
signal: abortController1.signal,
451445
},
@@ -467,17 +461,17 @@ describe('DBM', () => {
467461

468462
const mockDBMQuery = jest.spyOn(dbm, 'query');
469463

470-
const promise1 = dbm.queryWithTableNames({
464+
const promise1 = dbm.queryWithTables({
471465
query: 'SELECT * FROM table1',
472-
tableNames: ['table1'],
466+
tables: tables,
473467
options: {
474468
signal: abortController1.signal,
475469
},
476470
});
477471

478-
const promise2 = dbm.queryWithTableNames({
472+
const promise2 = dbm.queryWithTables({
479473
query: 'SELECT * FROM table2',
480-
tableNames: ['table2'],
474+
tables: [{ name: 'table2' }],
481475
options: {
482476
signal: abortController2.signal,
483477
},

meerkat-dbm/src/dbm/dbm.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { v4 as uuidv4 } from 'uuid';
44
import { FileManagerType } from '../file-manager/file-manager-type';
55
import { DBMEvent, DBMLogger } from '../logger';
66
import { InstanceManagerType } from './instance-manager';
7-
import { DBMConstructorOptions, QueryOptions, QueryQueueItem } from './types';
7+
import {
8+
DBMConstructorOptions,
9+
QueryOptions,
10+
QueryQueueItem,
11+
TableConfig,
12+
} from './types';
813

914
export class DBM {
1015
private fileManager: FileManagerType;
@@ -94,16 +99,16 @@ export class DBM {
9499
return this.connection;
95100
}
96101

97-
private async _queryWithTableNames(
102+
private async _queryWithTables(
98103
query: string,
99-
tableNames: string[],
104+
tables: TableConfig[],
100105
options?: QueryOptions
101106
) {
102107
/**
103108
* Load all the files into the database
104109
*/
105110
const startMountTime = Date.now();
106-
await this.fileManager.mountFileBufferByTableNames(tableNames);
111+
await this.fileManager.mountFileBufferByTables(tables);
107112
const endMountTime = Date.now();
108113

109114
this.logger.debug(
@@ -119,9 +124,7 @@ export class DBM {
119124
metadata: options?.metadata,
120125
});
121126

122-
const tablesFileData = await this.fileManager.getFilesNameForTables(
123-
tableNames
124-
);
127+
const tablesFileData = await this.fileManager.getFilesNameForTables(tables);
125128

126129
/**
127130
* Execute the preQuery hook
@@ -186,6 +189,7 @@ export class DBM {
186189
* Get the first query
187190
*/
188191
this.currentQueryItem = this.queriesQueue.shift();
192+
189193
/**
190194
* If there is no query, stop the queue
191195
*/
@@ -212,9 +216,9 @@ export class DBM {
212216
/**
213217
* Execute the query
214218
*/
215-
const result = await this._queryWithTableNames(
219+
const result = await this._queryWithTables(
216220
this.currentQueryItem.query,
217-
this.currentQueryItem.tableNames,
221+
this.currentQueryItem.tables,
218222
this.currentQueryItem.options
219223
);
220224
const endTime = Date.now();
@@ -305,13 +309,13 @@ export class DBM {
305309
signal?.addEventListener('abort', signalHandler);
306310
}
307311

308-
public async queryWithTableNames({
312+
public async queryWithTables({
309313
query,
310-
tableNames,
314+
tables,
311315
options,
312316
}: {
313317
query: string;
314-
tableNames: string[];
318+
tables: TableConfig[];
315319
options?: QueryOptions;
316320
}) {
317321
const connectionId = uuidv4();
@@ -321,7 +325,7 @@ export class DBM {
321325

322326
this.queriesQueue.push({
323327
query,
324-
tableNames,
328+
tables,
325329
promise: {
326330
resolve,
327331
reject,

meerkat-dbm/src/dbm/types.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,22 @@ export interface QueryOptions {
7474
signal?: AbortSignal;
7575
}
7676

77+
export interface TableConfig {
78+
/**
79+
* @description
80+
* Name of the table.
81+
*/
82+
name: string;
83+
/**
84+
* @description
85+
* Partitions of the table.
86+
*/
87+
partitions?: string[];
88+
}
89+
7790
export interface QueryQueueItem {
7891
query: string;
79-
tableNames: string[];
92+
tables: TableConfig[];
8093
promise: {
8194
resolve: (value: any) => void;
8295
reject: (reason?: any) => void;

0 commit comments

Comments
 (0)