Triplestore: initalize per-user triplestores

pull/1/head
cominch 13 years ago
parent 60f92a4902
commit a95127c9af

@ -34,6 +34,7 @@ public class JenaTripleStore {
public static Model model = ModelFactory.createDefaultModel();
static {
init();
}
private final static void init() {
model.setNsPrefix(YaCyMetadata.PREFIX, YaCyMetadata.NAMESPACE);
@ -101,6 +102,10 @@ public class JenaTripleStore {
}
public static void saveFile(String filename) {
saveFile(filename, model);
}
public static void saveFile(String filename, Model model) {
Log.logInfo("TRIPLESTORE", "Saving triplestore with " + model.size() + " triples to " + filename);
FileOutputStream fout;
try {
@ -177,10 +182,14 @@ public class JenaTripleStore {
};
}
public static void initPrivateStores(Switchboard switchboard) {
public static void initPrivateStores() {
Switchboard switchboard = Switchboard.getSwitchboard();
Log.logInfo("TRIPLESTORE", "Init private stores");
if (privatestorage == null) privatestorage = new ConcurrentHashMap<String, Model>();
if (privatestorage != null) privatestorage.clear();
try {
@ -191,56 +200,68 @@ public class JenaTripleStore {
de.anomic.data.UserDB.Entry e = it.next();
String username = e.getUserName();
Log.logInfo("TRIPLESTORE", "Init " + username);
File triplestore = new File(switchboard.getConfig("triplestore", new File(switchboard.getDataPath(), "DATA/TRIPLESTORE").getAbsolutePath()));
String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+username+"_triplestore.rdf";
File currentuserfile = new File(triplestore, "private_store_"+username+".rdf");
Model tmp = ModelFactory.createDefaultModel();
Log.logInfo("TRIPLESTORE", "Init " + username + " from "+currentuserfile.getAbsolutePath());
Log.logInfo("TRIPLESTORE", "Loading from " + filename);
Model tmp = ModelFactory.createDefaultModel();
try {
InputStream in = FileManager.get().open(filename);
tmp.setNsPrefix(YaCyMetadata.PREFIX, YaCyMetadata.NAMESPACE);
tmp.setNsPrefix(Tagging.DEFAULT_PREFIX, Tagging.DEFAULT_NAMESPACE);
tmp.setNsPrefix(HttpHeader.PREFIX, HttpHeader.NAMESPACE);
tmp.setNsPrefix(Geo.PREFIX, Geo.NAMESPACE);
tmp.setNsPrefix("pnd", "http://dbpedia.org/ontology/individualisedPnd");
tmp.setNsPrefix(DCTerms.PREFIX, DCTerms.NAMESPACE);
// read the RDF/XML file
tmp.read(in, null);
}
finally
{
privatestorage.put(username, tmp);
}
if (currentuserfile.exists()) {
}
Log.logInfo("TRIPLESTORE", "Loading from " + currentuserfile.getAbsolutePath());
InputStream is = FileManager.get().open(currentuserfile.getAbsolutePath());
if (is != null) {
// read the RDF/XML file
tmp.read(is, null);
Log.logInfo("TRIPLESTORE", "loaded " + tmp.size() + " triples from " + currentuserfile.getAbsolutePath());
} else {
throw new IOException("cannot read " + currentuserfile.getAbsolutePath());
}
}
if (tmp != null) {
privatestorage.put(username, tmp);
}
}
catch (Exception anyex) {
}
// create separate model
catch (Exception anyex) {
Log.logException(anyex);
}
}
public static void savePrivateStores(Switchboard switchboard) {
Log.logInfo("TRIPLESTORE", "Saving user triplestores");
if (privatestorage == null) return;
for (Entry<String, Model> s : privatestorage.entrySet()) {
String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+s.getKey()+"_triplestore.rdf";
FileOutputStream fout;
try {
File triplestore = new File(switchboard.getConfig("triplestore", new File(switchboard.getDataPath(), "DATA/TRIPLESTORE").getAbsolutePath()));
fout = new FileOutputStream(filename);
File currentuserfile = new File(triplestore, "private_store_"+s.getKey()+".rdf");
s.getValue().write(fout);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.logWarning("TRIPLESTORE", "Saving to " + filename+" failed");
}
saveFile (currentuserfile.getAbsolutePath(), s.getValue());
}
}

@ -83,6 +83,7 @@ import net.yacy.cora.document.RSSFeed;
import net.yacy.cora.document.RSSMessage;
import net.yacy.cora.document.RSSReader;
import net.yacy.cora.document.UTF8;
import net.yacy.cora.lod.JenaTripleStore;
import net.yacy.cora.protocol.ClientIdentification;
import net.yacy.cora.protocol.ConnectionInfo;
import net.yacy.cora.protocol.Domains;
@ -632,6 +633,9 @@ public final class Switchboard extends serverSwitch
+ ", "
+ ppRamString(userDbFile.length() / 1024));
// init user triplestores
JenaTripleStore.initPrivateStores();
// init html parser evaluation scheme
File parserPropertiesPath = new File("defaults/");
String[] settingsList = parserPropertiesPath.list();

@ -310,12 +310,13 @@ public final class yacy {
File triplestore = new File(sb.getConfig("triplestore", new File(dataHome, "DATA/TRIPLESTORE").getAbsolutePath()));
mkdirIfNeseccary(triplestore);
for (String s: triplestore.list()) {
if ((s.endsWith(".rdf") || s.endsWith(".nt")) && !s.equals("local.rdf") && !s.endsWith("_triplestore.rdf")) JenaTripleStore.load(new File(triplestore, s).getAbsolutePath());
if ((s.endsWith(".rdf") || s.endsWith(".nt")) && !s.equals("local.rdf") && !s.endsWith("_triplestore.rdf") && !s.startsWith("private_store_")) JenaTripleStore.load(new File(triplestore, s).getAbsolutePath());
}
if (sb.getConfigBool("triplestore.persistent", false)) {
File local = new File(triplestore, "local.rdf");
if (local.exists()) JenaTripleStore.load(local.getAbsolutePath());
}
} catch (IOException e) {
Log.logException(e);
}
@ -442,6 +443,8 @@ public final class yacy {
if (sb.getConfigBool("triplestore.persistent", false)) {
File triplestore = new File(sb.getConfig("triplestore", new File(dataHome, "DATA/TRIPLESTORE").getAbsolutePath()));
JenaTripleStore.saveFile(new File(triplestore, "local.rdf").getAbsolutePath());
JenaTripleStore.savePrivateStores(sb);
}
Log.logConfig("SHUTDOWN", "goodbye. (this is the last line)");

Loading…
Cancel
Save