Skip to content

Commit b6f23a1

Browse files
committed
Add batching of tenants.create and tenants.update with serial method invocations
1 parent 181b0a3 commit b6f23a1

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

.github/workflows/main.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ on:
77
pull_request:
88

99
env:
10-
WEAVIATE_124: 1.24.20
11-
WEAVIATE_125: 1.25.7
12-
WEAVIATE_126: preview--3aca6c5
10+
WEAVIATE_124: 1.24.21
11+
WEAVIATE_125: 1.25.8
12+
WEAVIATE_126: preview--caba86b
1313

1414
jobs:
1515
checks:

src/collections/serialize/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,19 @@ export class Serialize {
13291329
});
13301330
};
13311331

1332-
public static tenantsCreate(tenant: TenantBC | TenantCreate): {
1332+
public static tenants<T, M>(tenants: T[], mapper: (tenant: T) => M): M[][] {
1333+
const mapped = [];
1334+
const batches = Math.ceil(tenants.length / 100);
1335+
for (let i = 0; i < batches; i++) {
1336+
const batch = tenants.slice(i * 100, (i + 1) * 100);
1337+
mapped.push(batch.map(mapper));
1338+
}
1339+
return mapped;
1340+
}
1341+
1342+
public static tenantCreate<T extends TenantBC | TenantCreate>(
1343+
tenant: T
1344+
): {
13331345
name: string;
13341346
activityStatus?: 'HOT' | 'COLD';
13351347
} {
@@ -1361,9 +1373,9 @@ export class Serialize {
13611373
};
13621374
}
13631375

1364-
public static tenantUpdate = (
1365-
tenant: TenantBC | TenantUpdate
1366-
): { name: string; activityStatus: 'HOT' | 'COLD' | 'FROZEN' } => {
1376+
public static tenantUpdate<T extends TenantBC | TenantUpdate>(
1377+
tenant: T
1378+
): { name: string; activityStatus: 'HOT' | 'COLD' | 'FROZEN' } {
13671379
let activityStatus: 'HOT' | 'COLD' | 'FROZEN';
13681380
switch (tenant.activityStatus) {
13691381
case 'ACTIVE':
@@ -1389,5 +1401,5 @@ export class Serialize {
13891401
name: tenant.name,
13901402
activityStatus,
13911403
};
1392-
};
1404+
}
13931405
}

src/collections/tenants/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ const tenants = (
4444
return result;
4545
});
4646
return {
47-
create: (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) =>
48-
new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantsCreate))
49-
.do()
50-
.then((res) => res.map(parseTenantREST)),
47+
create: async (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) => {
48+
const out: Tenant[] = [];
49+
for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantCreate).map(
50+
(tenants) =>
51+
new TenantsCreator(connection, collection, tenants).do().then((res) => res.map(parseTenantREST))
52+
)) {
53+
out.push(...res);
54+
}
55+
return out;
56+
},
5157
get: async function () {
5258
const check = await dbVersionSupport.supportsTenantsGetGRPCMethod();
5359
return check.supports ? getGRPC() : getREST();
@@ -63,10 +69,16 @@ const tenants = (
6369
collection,
6470
parseValueOrValueArray(tenants).map(parseStringOrTenant)
6571
).do(),
66-
update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) =>
67-
new TenantsUpdater(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantUpdate))
68-
.do()
69-
.then((res) => res.map(parseTenantREST)),
72+
update: async (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => {
73+
const out: Tenant[] = [];
74+
for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantUpdate).map(
75+
(tenants) =>
76+
new TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST))
77+
)) {
78+
out.push(...res);
79+
}
80+
return out;
81+
},
7082
};
7183
};
7284

src/collections/tenants/integration.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,22 @@ describe('Testing of the collection.tenants methods', () => {
160160
expect(result).not.toHaveProperty('non-existing');
161161
});
162162
});
163+
164+
it('should be able to create and update 1000 tenants', async () => {
165+
const howManyToAdd = 1000;
166+
const howManyPreExisting = Object.entries(await collection.tenants.get()).length;
167+
const howMany = howManyToAdd + howManyPreExisting;
168+
const tenants = Array.from({ length: howManyToAdd }, (_, i) => ({
169+
name: `tenant-${i}`,
170+
}));
171+
await collection.tenants.create(tenants);
172+
const getTenants = await collection.tenants.get();
173+
expect(Object.entries(getTenants).length).toBe(howMany);
174+
expect(Object.values(getTenants).every((tenant) => tenant.activityStatus === 'ACTIVE')).toBe(true);
175+
const updated = await collection.tenants.update(
176+
Object.values(getTenants).map((tenant) => ({ name: tenant.name, activityStatus: 'INACTIVE' }))
177+
);
178+
expect(Object.entries(updated).length).toBe(howMany);
179+
expect(Object.values(updated).every((tenant) => tenant.activityStatus === 'INACTIVE')).toBe(true);
180+
});
163181
});

0 commit comments

Comments
 (0)