-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleandates.js
52 lines (46 loc) · 1.76 KB
/
cleandates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var request = require("request");
var url = process.env.SPARQLURL.slice(0,-7) + "/eval";
var stepSize = 1000;
function runScript (script, callback) {
request.post({
url : url,
body : script,
headers : {
"Content-type" : "text/javascript"
}
}, callback);
}
function deleteScript (offset) {
return '' +
'var lastSeen, subject, subjects, triples;\n' +
'lastSeen = "<http://purl.org/tcga/core#lastSeen>";\n' +
'subjects = store.runSparql("select distinct ?s where { ?s <http://purl.org/tcga/core#lastSeen> ?o . } limit '+stepSize+' offset '+offset+'");\n' +
'subjects.forEach(function (sub) {\n' +
' subject = sub[0];\n' +
' triples = store.getTriplesArray(subject, lastSeen);\n' +
' triples.sort();\n' +
' triples.slice(0,-1).forEach( function (triple) {\n' +
' store.deleteTriples(triple.subject, triple.predicate, triple.object);\n' +
' });\n' +
'});\n';
}
var subjects = 'subjects = store.runSparql("select distinct ?s where { ?s <http://purl.org/tcga/core#lastSeen> ?o . }");' +
'subjects.count()';
runScript(subjects, function (e, r, b) {
var numSubjects = 0, numOk = 0;
function deleteAStep (offset) {
runScript(deleteScript(offset), function (e, r, b) {
if (e) console.log("Error deleting at offset", offset, ":", e);
else if (numOk < numSubjects) {
numOk += stepSize;
console.log("Processed", numOk, "of", numSubjects,".");
deleteAStep(numOk);
}
});
}
if (e) return console.log("Error getting subjects: ", e);
else {
numSubjects = parseInt(b, 10);
deleteAStep(0);
}
});