Created
April 10, 2013 19:37
-
-
Save mikelehen/5357753 to your computer and use it in GitHub Desktop.
Prune history of a Firepad.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This should be easier, but due to an oversight in the checkpoint id's, it's a tad tricky (you need this revision/id stuff). | |
// I'll rework things to be cleaner in the near future. | |
function pruneHistory(firepadRef) { | |
var characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | |
function revisionToId(revision) { | |
if (revision === 0) { | |
return 'A0'; | |
} | |
var str = ''; | |
while (revision > 0) { | |
var digit = (revision % characters.length); | |
str = characters[digit] + str; | |
revision -= digit; | |
revision /= characters.length; | |
} | |
// Prefix with length (starting at 'A' for length 1) to ensure the id's sort lexicographically. | |
var prefix = characters[str.length + 9]; | |
return prefix + str; | |
} | |
function revisionFromId(revisionId) { | |
var revision = 0; | |
for(var i = 1; i < revisionId.length; i++) { | |
revision *= characters.length; | |
revision += characters.indexOf(revisionId[i]); | |
} | |
return revision; | |
} | |
firepadRef.child('checkpoint/id').once('value', function(idSnapshot) { | |
var checkpointId = idSnapshot.val(); | |
if (checkpointId !== null) { | |
var pruneBefore = revisionToId(revisionFromId(checkpointId) - 1); | |
firepadRef.child('history').once('value', function(historySnapshot) { | |
historySnapshot.forEach(function(itemSnapshot) { | |
if (itemSnapshot.name() < pruneBefore) { | |
itemSnapshot.ref().remove(); | |
}; | |
}); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment