Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
add namespace support for collection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Aug 8, 2024
1 parent 4384565 commit 18e4a12
Showing 1 changed file with 59 additions and 18 deletions.
77 changes: 59 additions & 18 deletions src/assembly/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,63 +129,71 @@ export class CollectionClassificationResultObject {
@external("hypermode", "upsertToCollection_v2")
declare function hostUpsertToCollection(
collection: string,
namespace: string,
keys: string[],
texts: string[],
labels: string[][],
): CollectionMutationResult;

// @ts-expect-error: decorator
@external("hypermode", "deleteFromCollection")
@external("hypermode", "deleteFromCollection_v2")
declare function hostDeleteFromCollection(
collection: string,
namespace: string,
key: string,
): CollectionMutationResult;

// @ts-expect-error: decorator
@external("hypermode", "searchCollection")
@external("hypermode", "searchCollection_v2")
declare function hostSearchCollection(
collection: string,
namespace: string,
searchMethod: string,
text: string,
limit: i32,
returnText: bool,
): CollectionSearchResult;

// @ts-expect-error: decorator
@external("hypermode", "nnClassifyCollection")
@external("hypermode", "nnClassifyCollection_v2")
declare function hostNnClassifyCollection(
collection: string,
namespace: string,
searchMethod: string,
text: string,
): CollectionClassificationResult;

// @ts-expect-error: decorator
@external("hypermode", "recomputeSearchMethod")
@external("hypermode", "recomputeSearchMethod_v2")
declare function hostRecomputeSearchMethod(
collection: string,
namespace: string,
searchMethod: string,
): SearchMethodMutationResult;

// @ts-expect-error: decorator
@external("hypermode", "computeDistance")
@external("hypermode", "computeDistance_v2")
declare function hostComputeDistance(
collection: string,
namespace: string,
searchMethod: string,
key1: string,
key2: string,
): CollectionSearchResultObject;

// @ts-expect-error: decorator
@external("hypermode", "getTextFromCollection")
@external("hypermode", "getTextFromCollection_v2")
declare function hostGetTextFromCollection(
collection: string,
namespace: string,
key: string,
): string;

// @ts-expect-error: decorator
@external("hypermode", "getTextsFromCollection")
@external("hypermode", "getTextsFromCollection_v2")
declare function hostGetTextsFromCollection(
collection: string,
namespace: string,
): Map<string, string>;

// add batch upsert
Expand All @@ -194,6 +202,7 @@ export function upsertBatch(
keys: string[] | null,
texts: string[],
labelsArr: string[][] = [],
namespace: string = "",
): CollectionMutationResult {
if (collection.length == 0) {
console.error("Collection is empty.");
Expand All @@ -218,7 +227,13 @@ export function upsertBatch(
keysArr = keys;
}

const result = hostUpsertToCollection(collection, keysArr, texts, labelsArr);
const result = hostUpsertToCollection(
collection,
namespace,
keysArr,
texts,
labelsArr,
);
if (utils.resultIsInvalid(result)) {
console.error("Error upserting to Text index.");
return new CollectionMutationResult(
Expand All @@ -238,6 +253,7 @@ export function upsert(
key: string | null,
text: string,
labels: string[] = [],
namespace: string = "",
): CollectionMutationResult {
if (collection.length == 0) {
console.error("Collection is empty.");
Expand Down Expand Up @@ -266,7 +282,13 @@ export function upsert(

const labelsArr: string[][] = [labels];

const result = hostUpsertToCollection(collection, keys, texts, labelsArr);
const result = hostUpsertToCollection(
collection,
namespace,
keys,
texts,
labelsArr,
);
if (utils.resultIsInvalid(result)) {
console.error("Error upserting to Text index.");
return new CollectionMutationResult(
Expand All @@ -283,6 +305,7 @@ export function upsert(
export function remove(
collection: string,
key: string,
namespace: string = "",
): CollectionMutationResult {
if (collection.length == 0) {
console.error("Collection is empty.");
Expand All @@ -302,7 +325,7 @@ export function remove(
"delete",
);
}
const result = hostDeleteFromCollection(collection, key);
const result = hostDeleteFromCollection(collection, namespace, key);
if (utils.resultIsInvalid(result)) {
console.error("Error deleting from Text index.");
return new CollectionMutationResult(
Expand All @@ -324,6 +347,7 @@ export function search(
text: string,
limit: i32,
returnText: bool = false,
namespace: string = "",
): CollectionSearchResult {
if (text.length == 0) {
console.error("Text is empty.");
Expand All @@ -337,6 +361,7 @@ export function search(
}
const result = hostSearchCollection(
collection,
namespace,
searchMethod,
text,
limit,
Expand All @@ -361,6 +386,7 @@ export function nnClassify(
collection: string,
searchMethod: string,
text: string,
namespace: string = "",
): CollectionClassificationResult {
if (text.length == 0) {
console.error("Text is empty.");
Expand All @@ -373,7 +399,12 @@ export function nnClassify(
[],
);
}
const result = hostNnClassifyCollection(collection, searchMethod, text);
const result = hostNnClassifyCollection(
collection,
namespace,
searchMethod,
text,
);
if (utils.resultIsInvalid(result)) {
console.error("Error classifying Text index.");
return new CollectionClassificationResult(
Expand All @@ -391,6 +422,7 @@ export function nnClassify(
export function recomputeSearchMethod(
collection: string,
searchMethod: string,
namespace: string = "",
): SearchMethodMutationResult {
if (collection.length == 0) {
console.error("Collection is empty.");
Expand All @@ -412,7 +444,7 @@ export function recomputeSearchMethod(
searchMethod,
);
}
const result = hostRecomputeSearchMethod(collection, searchMethod);
const result = hostRecomputeSearchMethod(collection, namespace, searchMethod);
if (utils.resultIsInvalid(result)) {
console.error("Error recomputing Text index.");
return new SearchMethodMutationResult(
Expand All @@ -434,15 +466,17 @@ export function computeSimilarity(
searchMethod: string,
key1: string,
key2: string,
namespace: string = "",
): CollectionSearchResultObject {
return computeDistance(collection, searchMethod, key1, key2);
return computeDistance(collection, searchMethod, key1, key2, namespace);
}

export function computeDistance(
collection: string,
searchMethod: string,
key1: string,
key2: string,
namespace: string = "",
): CollectionSearchResultObject {
if (collection.length == 0) {
console.error("Collection is empty.");
Expand All @@ -460,10 +494,14 @@ export function computeDistance(
console.error("Key2 is empty.");
return new CollectionSearchResultObject("", "", 0.0, 0.0);
}
return hostComputeDistance(collection, searchMethod, key1, key2);
return hostComputeDistance(collection, namespace, searchMethod, key1, key2);
}

export function getText(collection: string, key: string): string {
export function getText(
collection: string,
key: string,
namespace: string = "",
): string {
if (collection.length == 0) {
console.error("Collection is empty.");
return "";
Expand All @@ -472,13 +510,16 @@ export function getText(collection: string, key: string): string {
console.error("Key is empty.");
return "";
}
return hostGetTextFromCollection(collection, key);
return hostGetTextFromCollection(collection, namespace, key);
}

export function getTexts(collection: string): Map<string, string> {
export function getTexts(
collection: string,
namespace: string = "",
): Map<string, string> {
if (collection.length == 0) {
console.error("Collection is empty.");
return new Map<string, string>();
}
return hostGetTextsFromCollection(collection);
return hostGetTextsFromCollection(collection, namespace);
}

0 comments on commit 18e4a12

Please sign in to comment.