Skip to content

Commit 04f9235

Browse files
authored
Merge pull request #162 from weaviate/dev/support-new-tenant-naming-convention
DevV3/Use new OFFLOAD nomenclature and updated OpenAPI schema
2 parents fae8d6c + 0541640 commit 04f9235

File tree

9 files changed

+261
-100
lines changed

9 files changed

+261
-100
lines changed

src/collections/collection/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import generate, { Generate } from '../generate/index.js';
1313
import { Iterator } from '../iterator/index.js';
1414
import query, { Query } from '../query/index.js';
1515
import sort, { Sort } from '../sort/index.js';
16-
import tenants, { TenantInput, Tenants } from '../tenants/index.js';
16+
import tenants, { TenantBase, Tenants } from '../tenants/index.js';
1717
import { QueryMetadata, QueryProperty, QueryReference } from '../types/index.js';
1818

1919
export interface Collection<T = undefined, N = string> {
@@ -78,10 +78,11 @@ export interface Collection<T = undefined, N = string> {
7878
*
7979
* This method does not send a request to Weaviate. It only returns a new collection object that is specific to the tenant you specify.
8080
*
81-
* @param {string | TenantInput} tenant The tenant name or tenant object to use.
81+
* @typedef {TenantBase} TT A type that extends TenantBase.
82+
* @param {string | TT} tenant The tenant name or tenant object to use.
8283
* @returns {Collection<T, N>} A new collection object specific to the tenant you specified.
8384
*/
84-
withTenant: (tenant: string | TenantInput) => Collection<T, N>;
85+
withTenant: <TT extends TenantBase>(tenant: string | TT) => Collection<T, N>;
8586
}
8687

8788
export type IteratorOptions<T> = {
@@ -136,7 +137,7 @@ const collection = <T, N>(
136137
),
137138
withConsistency: (consistencyLevel: ConsistencyLevel) =>
138139
collection<T, N>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
139-
withTenant: (tenant: string | TenantInput) =>
140+
withTenant: <TT extends TenantBase>(tenant: string | TT) =>
140141
collection<T, N>(
141142
connection,
142143
capitalizedName,

src/collections/deserialize/index.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { WeaviateDeserializationError } from '../../errors.js';
2+
import { Tenant as TenantREST } from '../../openapi/types.js';
23
import { BatchObject as BatchObjectGRPC, BatchObjectsReply } from '../../proto/v1/batch.js';
34
import { BatchDeleteReply } from '../../proto/v1/batch_delete.js';
45
import { ListValue, Properties as PropertiesGrpc, Value } from '../../proto/v1/properties.js';
56
import { MetadataResult, PropertiesResult, SearchReply } from '../../proto/v1/search_get.js';
7+
import { TenantActivityStatus, TenantsGetReply } from '../../proto/v1/tenants.js';
68
import { DbVersionSupport } from '../../utils/dbVersion.js';
79
import { referenceFromObjects } from '../references/utils.js';
10+
import { Tenant } from '../tenants/index.js';
811
import {
912
BatchObject,
1013
BatchObjectsReturn,
@@ -298,4 +301,51 @@ export class Deserialize {
298301
: (undefined as any),
299302
};
300303
}
304+
305+
private static activityStatusGRPC(status: TenantActivityStatus): Tenant['activityStatus'] {
306+
switch (status) {
307+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_COLD:
308+
return 'INACTIVE';
309+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_HOT:
310+
return 'ACTIVE';
311+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_FROZEN:
312+
return 'OFFLOADED';
313+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_FREEZING:
314+
return 'OFFLOADING';
315+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_UNFREEZING:
316+
return 'ONLOADING';
317+
default:
318+
throw new Error(`Unsupported tenant activity status: ${status}`);
319+
}
320+
}
321+
322+
public static activityStatusREST(status: TenantREST['activityStatus']): Tenant['activityStatus'] {
323+
switch (status) {
324+
case 'COLD':
325+
return 'INACTIVE';
326+
case 'HOT':
327+
return 'ACTIVE';
328+
case 'FROZEN':
329+
return 'OFFLOADED';
330+
case 'FREEZING':
331+
return 'OFFLOADING';
332+
case 'UNFREEZING':
333+
return 'ONLOADING';
334+
case undefined:
335+
return 'ACTIVE';
336+
default:
337+
throw new Error(`Unsupported tenant activity status: ${status}`);
338+
}
339+
}
340+
341+
public static tenantsGet(reply: TenantsGetReply) {
342+
const tenants: Record<string, Tenant> = {};
343+
reply.tenants.forEach((t) => {
344+
tenants[t.name] = {
345+
name: t.name,
346+
activityStatus: Deserialize.activityStatusGRPC(t.activityStatus),
347+
};
348+
});
349+
return tenants;
350+
}
301351
}

src/collections/query/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ describe('Testing of the collection.query methods with a multi-tenancy collectio
909909
expect(obj2.objects[0].uuid).toEqual(id2);
910910
});
911911

912-
it.skip('should find the objects in their tenants by nearObject', async () => {
912+
it('should find the objects in their tenants by nearObject', async () => {
913913
const obj1 = await collection.withTenant(tenantOne).query.nearObject(id1);
914914
const obj2 = await collection.withTenant(tenantTwo).query.nearObject(id2);
915915
expect(obj1.objects.length).toEqual(1);

src/collections/serialize/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
import { ReferenceGuards } from '../references/classes.js';
8383
import { Beacon } from '../references/index.js';
8484
import { uuidToBeacon } from '../references/utils.js';
85+
import { TenantBC, TenantCreate, TenantUpdate } from '../tenants/types.js';
8586
import {
8687
BatchObject,
8788
BatchObjects,
@@ -1135,4 +1136,66 @@ export class Serialize {
11351136
return { batch: batch, mapped: objs };
11361137
});
11371138
};
1139+
1140+
public static tenantsCreate(tenant: TenantBC | TenantCreate): {
1141+
name: string;
1142+
activityStatus?: 'HOT' | 'COLD';
1143+
} {
1144+
let activityStatus: 'HOT' | 'COLD' | undefined;
1145+
switch (tenant.activityStatus) {
1146+
case 'ACTIVE':
1147+
activityStatus = 'HOT';
1148+
break;
1149+
case 'INACTIVE':
1150+
activityStatus = 'COLD';
1151+
break;
1152+
case 'HOT':
1153+
case 'COLD':
1154+
case undefined:
1155+
activityStatus = tenant.activityStatus;
1156+
break;
1157+
case 'FROZEN':
1158+
throw new WeaviateInvalidInputError(
1159+
'Invalid activity status. Please provide one of the following: ACTIVE, INACTIVE, HOT, COLD.'
1160+
);
1161+
default:
1162+
throw new WeaviateInvalidInputError(
1163+
'Invalid activity status. Please provide one of the following: ACTIVE, INACTIVE, HOT, COLD.'
1164+
);
1165+
}
1166+
return {
1167+
name: tenant.name,
1168+
activityStatus,
1169+
};
1170+
}
1171+
1172+
public static tenantUpdate = (
1173+
tenant: TenantBC | TenantUpdate
1174+
): { name: string; activityStatus: 'HOT' | 'COLD' | 'FROZEN' } => {
1175+
let activityStatus: 'HOT' | 'COLD' | 'FROZEN';
1176+
switch (tenant.activityStatus) {
1177+
case 'ACTIVE':
1178+
activityStatus = 'HOT';
1179+
break;
1180+
case 'INACTIVE':
1181+
activityStatus = 'COLD';
1182+
break;
1183+
case 'OFFLOADED':
1184+
activityStatus = 'FROZEN';
1185+
break;
1186+
case 'HOT':
1187+
case 'COLD':
1188+
case 'FROZEN':
1189+
activityStatus = tenant.activityStatus;
1190+
break;
1191+
default:
1192+
throw new WeaviateInvalidInputError(
1193+
'Invalid activity status. Please provide one of the following: ACTIVE, INACTIVE, HOT, COLD, OFFLOADED.'
1194+
);
1195+
}
1196+
return {
1197+
name: tenant.name,
1198+
activityStatus,
1199+
};
1200+
};
11381201
}

0 commit comments

Comments
 (0)