Skip to content

Commit

Permalink
Break up calls to objectSearch where url limit excedes 2000
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCsaky committed Aug 23, 2023
1 parent 62fe333 commit df6ecfd
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions frontend/src/store/objectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export const useObjectStore = defineStore('object', () => {
}

async function fetchObjects(
params: ObjectSearchPermissionsOptions = {},
tagset?: Array<Tag>,
params: ObjectSearchPermissionsOptions = {},
tagset?: Array<Tag>,
metadata?: Array<MetadataPair>) {
try {
appStore.beginIndeterminateLoading();
Expand All @@ -127,7 +127,7 @@ export const useObjectStore = defineStore('object', () => {
.filter((objectId: string) => !params.objectId || objectId === params.objectId))
];

let response = Array<COMSObject>();
const response = Array<COMSObject>();
if (uniqueIds.length) {
// If metadata specified, search for objects with matching metadata
const headers: Record<string, string> = {};
Expand All @@ -137,16 +137,52 @@ export const useObjectStore = defineStore('object', () => {
}
}

response = (await objectService.searchObjects({
bucketId: params.bucketId ? [params.bucketId] : undefined,
objectId: uniqueIds,
tagset: tagset ? tagset.reduce((acc, cur) => ({ ...acc, [cur.key]: cur.value }), {}) : undefined,

// Added to allow deletion of objects before versioning implementation
// TODO: Verify if needed after versioning implemented
deleteMarker: false,
latest: true
}, headers)).data;
/**
* split calls to `objectService.searchObjects()` if URL limit excedes 2000 characters
* ref: https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
*/
let maxLength = 2000;
// minus deleteMarker=false
maxLength = maxLength - 19;
// minus latest=false
maxLength = maxLength - 13;
// minus a single bucketId (bucketId[]=<uuid>)
if(params.bucketId) { maxLength = maxLength - 48; }
// if tagset parameters passed
if(tagset){
for (const tag in tagset) {
maxLength = maxLength - 10 - tagset[tag].key.length - tagset[tag].value.length;
}
}

// 48 chars for each objectId (&objectId[]=<uuid>)
const numObjs = Math.floor(maxLength / 48);

// for array of unique object ID's
const arrLength = uniqueIds.length;
if(arrLength > numObjs) {

const iterations = Math.ceil(arrLength / numObjs);
console.log('uniqueIds.length:', uniqueIds.length, 'maxLength:', maxLength, 'numObjs:', numObjs, 'iterations:', iterations);

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

This line has a length of 136. Maximum allowed is 120

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

Unexpected console statement

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

This line has a length of 136. Maximum allowed is 120

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

Unexpected console statement

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

This line has a length of 136. Maximum allowed is 120

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

Unexpected console statement

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

This line has a length of 136. Maximum allowed is 120

Check warning on line 167 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

Unexpected console statement

for (let i=0; i < iterations; i++ ) {
const ids = uniqueIds.slice(i * numObjs, ((i * numObjs) + numObjs ));
console.log('ids:', ids);

Check warning on line 171 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

Unexpected console statement

Check warning on line 171 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

Unexpected console statement

Check warning on line 171 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (16.x)

Unexpected console statement

Check warning on line 171 in frontend/src/store/objectStore.ts

View workflow job for this annotation

GitHub Actions / Unit Tests (Frontend) (18.x)

Unexpected console statement

const group = (await objectService.searchObjects({
bucketId: params.bucketId ? [params.bucketId] : undefined,
objectId: ids,
tagset: tagset ? tagset.reduce((acc, cur) => ({ ...acc, [cur.key]: cur.value }), {}) : undefined,
// Added to allow deletion of objects before versioning implementation
// TODO: Verify if needed after versioning implemented
deleteMarker: false,
latest: true
}, headers)).data;

response.concat(group);
}
}

// Remove old values matching search parameters
const matches = (x: COMSObject) => (
Expand All @@ -159,7 +195,7 @@ export const useObjectStore = defineStore('object', () => {
// Merge and assign
state.objects.value = difference.concat(response);

// Track all the object IDs in this bucket that the user would have access to in the table
// Track all the object IDs in this bucket that the user would have access to in the table
// (even if filters are applied)
if(!tagset?.length && !metadata?.length) {
state.unfilteredObjectIds.value = state.objects.value
Expand Down

0 comments on commit df6ecfd

Please sign in to comment.