Do more things in parallel in getSkipSegments

This commit is contained in:
Ajay
2022-07-05 19:23:02 -04:00
parent 8ed695bcdc
commit 7d396f3782
2 changed files with 11 additions and 7 deletions

View File

@@ -27,6 +27,10 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, service:
if (cache.shadowHiddenSegmentIPs[videoID] === undefined) cache.shadowHiddenSegmentIPs[videoID] = {}; if (cache.shadowHiddenSegmentIPs[videoID] === undefined) cache.shadowHiddenSegmentIPs[videoID] = {};
if (cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] === undefined) { if (cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] === undefined) {
if (cache.userHashedIP === undefined && cache.userHashedIPPromise === undefined) {
cache.userHashedIPPromise = getHashCache((getIP(req) + config.globalSalt) as IPAddress);
}
const service = getService(req?.query?.service as string); const service = getService(req?.query?.service as string);
const fetchData = () => privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ? AND "service" = ?', const fetchData = () => privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ? AND "service" = ?',
[videoID, segment.timeSubmitted, service]) as Promise<{ hashedIP: HashedIP }[]>; [videoID, segment.timeSubmitted, service]) as Promise<{ hashedIP: HashedIP }[]>;
@@ -35,9 +39,8 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, service:
const ipList = cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted]; const ipList = cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted];
if (ipList?.length > 0 && cache.userHashedIP === undefined) { if (ipList?.length > 0 && cache.userHashedIP === undefined && cache.userHashedIPPromise) {
//hash the IP only if it's strictly necessary cache.userHashedIP = await cache.userHashedIPPromise;
cache.userHashedIP = await getHashCache((getIP(req) + config.globalSalt) as IPAddress);
} }
//if this isn't their ip, don't send it to them //if this isn't their ip, don't send it to them
const shouldShadowHide = cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted]?.some( const shouldShadowHide = cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted]?.some(
@@ -133,7 +136,7 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash,
return acc; return acc;
}, {}); }, {});
for (const [videoID, videoData] of Object.entries(segmentPerVideoID)) { await Promise.all(Object.entries(segmentPerVideoID).map(async ([videoID, videoData]) => {
const data: VideoData = { const data: VideoData = {
hash: videoData.hash, hash: videoData.hash,
segments: [], segments: [],
@@ -153,7 +156,7 @@ async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash,
if (data.segments.length > 0) { if (data.segments.length > 0) {
segments[videoID] = data; segments[videoID] = data;
} }
} }));
return segments; return segments;
} catch (err) { } catch (err) {
@@ -283,7 +286,7 @@ async function buildSegmentGroups(segments: DBSegment[]): Promise<OverlappingSeg
let overlappingSegmentsGroups: OverlappingSegmentGroup[] = []; let overlappingSegmentsGroups: OverlappingSegmentGroup[] = [];
let currentGroup: OverlappingSegmentGroup; let currentGroup: OverlappingSegmentGroup;
let cursor = -1; //-1 to make sure that, even if the 1st segment starts at 0, a new group is created let cursor = -1; //-1 to make sure that, even if the 1st segment starts at 0, a new group is created
for (const segment of segments) { await Promise.all(segments.map(async (segment) => {
if (segment.startTime >= cursor) { if (segment.startTime >= cursor) {
currentGroup = { segments: [], votes: 0, reputation: 0, locked: false, required: false }; currentGroup = { segments: [], votes: 0, reputation: 0, locked: false, required: false };
overlappingSegmentsGroups.push(currentGroup); overlappingSegmentsGroups.push(currentGroup);
@@ -309,7 +312,7 @@ async function buildSegmentGroups(segments: DBSegment[]): Promise<OverlappingSeg
} }
cursor = Math.max(cursor, segment.endTime); cursor = Math.max(cursor, segment.endTime);
} }));
overlappingSegmentsGroups = splitPercentOverlap(overlappingSegmentsGroups); overlappingSegmentsGroups = splitPercentOverlap(overlappingSegmentsGroups);
overlappingSegmentsGroups.forEach((group) => { overlappingSegmentsGroups.forEach((group) => {

View File

@@ -101,6 +101,7 @@ export interface VideoData {
export interface SegmentCache { export interface SegmentCache {
shadowHiddenSegmentIPs: SBRecord<VideoID, SBRecord<string, {hashedIP: HashedIP}[]>>, shadowHiddenSegmentIPs: SBRecord<VideoID, SBRecord<string, {hashedIP: HashedIP}[]>>,
userHashedIP?: HashedIP userHashedIP?: HashedIP
userHashedIPPromise?: Promise<HashedIP>;
} }
export interface DBLock { export interface DBLock {