Skip to content

Instantly share code, notes, and snippets.

@thomcc
Created February 20, 2018 19:30
Show Gist options
  • Save thomcc/6abc36919caac6c02cf7cbf3b267cb68 to your computer and use it in GitHub Desktop.
Save thomcc/6abc36919caac6c02cf7cbf3b267cb68 to your computer and use it in GitHub Desktop.
// Run me from a scratchpad.
(function() {
ChromeUtils.import("resource://gre/modules/SyncedBookmarksMirror.jsm");
ChromeUtils.import("resource://services-sync/util.js");
const RECORDS_TO_INSERT = 10000;
function randUpTo(n) {
return Math.floor(Math.random() * n);
}
function randChoice(arr) {
return arr[randUpTo(arr.length)];
}
function getRandomParent(roots, folders) {
// 2/3 of the time add to the roots -- seems realistic i guess
if (Math.random() < 2/3) {
return randChoice(roots);
} else {
let folder = randChoice(folders);
// try a few times to get a shallow folder but not too many.
for (let i = 0; i < 5 && folder.depth > 6; ++i) {
folder = randChoice(folders);
}
return folder;
}
}
async function buildBookmarkTree(max = 3000) {
const roots = [
{ id: "menu", type: "folder", title: "Bookmarks Menu", children: [], depth: 0 },
{ id: "unfiled", type: "folder", title: "Other Bookmarks", children: [], depth: 0 },
{ id: "toolbar", type: "folder", title: "Bookmarks Toolbar", children: [], depth: 0 },
];
const folders = roots.slice();
const records = roots.slice();
max += roots.length;
while (records.length < max) {
let parent = getRandomParent(roots, folders);
let count = randUpTo(4) + 1;
for (let i = 0; i < count; ++i) {
let isFolder = Math.random() < 0.1;
let entropy = randUpTo(1e6).toString(36);
let record = {
id: Utils.makeGUID(),
title: `Bookmark${isFolder ? "Folder" : ""} ${records.length} (${entropy})`,
};
if (isFolder) {
record.type = "folder";
record.children = [];
record.depth = parent.depth + 1;
folders.push(record);
} else {
record.type = "bookmark";
record.bmkUri = `https://www.bugzilla.com/${records.length}?r=${entropy}`;
}
parent.children.push(record.id);
records.push(record);
}
}
console.log("Opening buffer");
const buf = await SyncedBookmarksMirror.open({
path: ":memory:",
recordTelemetryEvent() {},
});
console.log("Inserting records into buffer");
await buf.store(records);
console.log("Applying mirror to Places");
await buf.apply();
console.log("Finalizing buffer");
await buf.finalize();
console.log(`Inserted ${records.length - roots.length} records.`);
}
buildBookmarkTree(RECORDS_TO_INSERT).catch(e => console.error("Failed to insert records", e));
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment