1
Fork 0

Merge aliases and search-index

This commit is contained in:
Guillaume Gomez 2020-05-04 14:51:06 +02:00
parent 9697c467ac
commit f581cf7544
7 changed files with 118 additions and 108 deletions

View file

@ -531,6 +531,7 @@ function getSearchElement() {
var OUTPUT_DATA = 1;
var NO_TYPE_FILTER = -1;
var currentResults, index, searchIndex;
var ALIASES = {};
var params = getQueryStringParams();
// Populate search bar with query string search term when provided,
@ -963,46 +964,60 @@ function getSearchElement() {
return itemTypes[ty.ty] + ty.path + ty.name;
}
function createAliasFromItem(item) {
return {
crate: item.crate,
name: item.name,
path: item.path,
desc: item.desc,
ty: item.ty,
parent: item.parent,
type: item.parent,
is_alias: true,
};
}
function handleAliases(ret, query, filterCrates) {
if (ALIASES) {
var aliases = [];
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
ALIASES[filterCrates][query.search]) {
aliases = ALIASES[filterCrates][query.search];
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
for (var i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(ALIASES[crate][query.search][i]);
}
}
});
var aliases = [];
var i;
if (filterCrates !== undefined &&
ALIASES[filterCrates] &&
ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
createAliasFromItem(searchIndex[ALIASES[filterCrates][query.search]]));
}
aliases.sort(function(aaa, bbb) {
if (aaa.path < bbb.path) {
return 1;
} else if (aaa.path === bbb.path) {
return 0;
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
aliases.push(
createAliasFromItem(
searchIndex[ALIASES[crate][query.search][i]]));
}
}
return -1;
});
for (var i = 0; i < aliases.length; ++i) {
var alias = aliases[i];
alias.is_alias = true;
if (typeof alias.parent === "number") {
alias.parent = rawSearchIndex[alias.crate].p[alias.parent];
}
alias.alias = query.raw;
alias.path = alias.p || alias.crate;
var res = buildHrefAndPath(aliases[i]);
alias.displayPath = pathSplitter(res[0]);
alias.fullPath = alias.displayPath + alias.name;
alias.href = res[1];
ret.others.unshift(alias);
if (ret.others.length > MAX_RESULTS) {
ret.others.pop();
}
}
aliases.sort(function(aaa, bbb) {
if (aaa.path < bbb.path) {
return 1;
} else if (aaa.path === bbb.path) {
return 0;
}
return -1;
});
for (i = 0; i < aliases.length; ++i) {
var alias = aliases[i];
alias.alias = query.raw;
var res = buildHrefAndPath(alias);
alias.displayPath = pathSplitter(res[0]);
alias.fullPath = alias.displayPath + alias.name;
alias.href = res[1];
ret.others.unshift(alias);
if (ret.others.length > MAX_RESULTS) {
ret.others.pop();
}
}
}
@ -1683,10 +1698,13 @@ function getSearchElement() {
searchIndex = [];
var searchWords = [];
var i;
var currentIndex = 0;
for (var crate in rawSearchIndex) {
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
var crateSize = 0;
searchWords.push(crate);
searchIndex.push({
crate: crate,
@ -1696,6 +1714,7 @@ function getSearchElement() {
desc: rawSearchIndex[crate].doc,
type: null,
});
currentIndex += 1;
// an array of [(Number) item type,
// (String) name,
@ -1707,6 +1726,9 @@ function getSearchElement() {
// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].p;
// a array of [(String) alias name
// [Number] index to items]
var aliases = rawSearchIndex[crate].a;
// convert `rawPaths` entries into object form
var len = paths.length;
@ -1725,9 +1747,18 @@ function getSearchElement() {
var lastPath = "";
for (i = 0; i < len; ++i) {
var rawRow = items[i];
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
path: rawRow[2] || lastPath, desc: rawRow[3],
parent: paths[rawRow[4]], type: rawRow[5]};
if (!rawRow[2]) {
rawRow[2] = lastPath;
}
var row = {
crate: crate,
ty: rawRow[0],
name: rawRow[1],
path: rawRow[2],
desc: rawRow[3],
parent: paths[rawRow[4]],
type: rawRow[5],
};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
@ -1736,7 +1767,24 @@ function getSearchElement() {
searchWords.push("");
}
lastPath = row.path;
crateSize += 1;
}
if (aliases) {
ALIASES[crate] = {};
var j, local_aliases;
for (i = 0; i < aliases.length; ++i) {
var alias_name = aliases[i][0];
if (!ALIASES[crate].hasOwnProperty(alias_name)) {
ALIASES[crate][alias_name] = [];
}
local_aliases = aliases[i][1];
for (j = 0; j < local_aliases.length; ++j) {
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
}
}
}
currentIndex += crateSize;
}
return searchWords;
}