updated short url algo to handle case of other claims but no matching prefix

This commit is contained in:
bill bittner 2017-08-10 13:04:31 -07:00
parent 4b3d6f72cc
commit 42924f0de8

View file

@ -4,36 +4,48 @@ const lbryApi = require('./lbryApi');
function determineShortClaimId (claimId, height, claimList) { function determineShortClaimId (claimId, height, claimList) {
logger.debug('determining short url based on claim id and claim list'); logger.debug('determining short url based on claim id and claim list');
logger.debug('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists // remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return claim.claim_id !== claimId; return claim.claim_id !== claimId;
}); });
logger.debug('claim list length:', claimList.length); logger.debug('claim list length without this claim:', claimList.length);
// if there are no other claims, return the first letter of the claim id // If there are no other claims, return the first letter of the claim id...
if (claimList.length === 0) { if (claimList.length === 0) {
return claimId.substring(0, 1); return claimId.substring(0, 1);
// otherwise determine the proper url // ...otherwise determine the proper short id.
} else { } else {
let i = 0;
const claimListCopy = claimList; const claimListCopy = claimList;
while (claimList.length !== 0) { // filter out matching claims let i = 0;
// find the longest shared prefix (there is a better way to do this that filters, checks next filter, then filters (i.e. combine this step and next))
while (claimList.length !== 0) {
i++; i++;
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return (claim.claim_id.substring(0, i) === claimId.substring(0, i)); const otherClaimIdSegmentToCompare = claim.claim_id.substring(0, i);
const thisClaimIdSegmentToCompare = claimId.substring(0, i);
logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?');
return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare);
}); });
} }
i -= 1; // use that longest shared prefix to get only those competing claims
const lastMatch = claimId.substring(0, i); const lastMatchIndex = i - 1;
const lastMatch = claimId.substring(0, lastMatchIndex);
const matchingClaims = claimListCopy.filter(claim => { logger.debug('last match index:', lastMatchIndex, 'last match:', lastMatch);
return (claim.claim_id.substring(0, i) === lastMatch); if (lastMatchIndex === 0) { // if no other claims share a prefix, return with first letter.
return claimId.substring(0, 1);
}
const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => {
return (claim.claim_id.substring(0, lastMatchIndex) === lastMatch);
}); });
for (let j = 0; j < matchingClaims.length; j++) { // for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character
if (matchingClaims[j].height < height) { const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => {
return claimId.substring(0, i + 1); return (a.height < b.height);
});
// compare to the earliest one, if it is earlier, this claim takes the extra character
if (sortedMatchingClaims[0].height < height) {
return claimId.substring(0, lastMatchIndex + 1);
} }
} return claimId.substring(0, lastMatchIndex);
return claimId.substring(0, i);
} }
} }