Triplestore: initalize per-user triplestores

This commit is contained in:
cominch 2012-06-14 11:46:53 +02:00
parent 60f92a4902
commit a95127c9af
3 changed files with 70 additions and 42 deletions

View File

@ -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);
@ -99,8 +100,12 @@ public class JenaTripleStore {
}
}
public static void saveFile(String filename) {
saveFile(filename, model);
}
public static void saveFile(String filename) {
public static void saveFile(String filename, Model model) {
Log.logInfo("TRIPLESTORE", "Saving triplestore with " + model.size() + " triples to " + filename);
FileOutputStream fout;
try {
@ -177,9 +182,13 @@ 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();
@ -191,57 +200,69 @@ public class JenaTripleStore {
de.anomic.data.UserDB.Entry e = it.next();
String username = e.getUserName();
Log.logInfo("TRIPLESTORE", "Init " + username);
String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+username+"_triplestore.rdf";
Model tmp = ModelFactory.createDefaultModel();
Log.logInfo("TRIPLESTORE", "Loading from " + filename);
try {
InputStream in = FileManager.get().open(filename);
// read the RDF/XML file
tmp.read(in, null);
}
finally
{
privatestorage.put(username, tmp);
}
File triplestore = new File(switchboard.getConfig("triplestore", new File(switchboard.getDataPath(), "DATA/TRIPLESTORE").getAbsolutePath()));
File currentuserfile = new File(triplestore, "private_store_"+username+".rdf");
Log.logInfo("TRIPLESTORE", "Init " + username + " from "+currentuserfile.getAbsolutePath());
Model tmp = ModelFactory.createDefaultModel();
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);
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) {
catch (Exception anyex) {
Log.logException(anyex);
}
// create separate model
}
}
public static void savePrivateStores(Switchboard switchboard) {
Log.logInfo("TRIPLESTORE", "Saving user triplestores");
if (privatestorage == null) return;
for (Entry<String, Model> s : privatestorage.entrySet()) {
File triplestore = new File(switchboard.getConfig("triplestore", new File(switchboard.getDataPath(), "DATA/TRIPLESTORE").getAbsolutePath()));
File currentuserfile = new File(triplestore, "private_store_"+s.getKey()+".rdf");
String filename = new File(switchboard.getConfig("dataRoot", ""), "DATA/TRIPLESTORE").toString()+"/"+s.getKey()+"_triplestore.rdf";
FileOutputStream fout;
try {
fout = new FileOutputStream(filename);
s.getValue().write(fout);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.logWarning("TRIPLESTORE", "Saving to " + filename+" failed");
}
saveFile (currentuserfile.getAbsolutePath(), s.getValue());
}
}

View File

@ -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;
@ -631,6 +632,9 @@ public final class Switchboard extends serverSwitch
+ " entries"
+ ", "
+ ppRamString(userDbFile.length() / 1024));
// init user triplestores
JenaTripleStore.initPrivateStores();
// init html parser evaluation scheme
File parserPropertiesPath = new File("defaults/");
@ -660,7 +664,7 @@ public final class Switchboard extends serverSwitch
}
}
}.start();
// define a realtime parsable mimetype list
this.log.logConfig("Parser: Initializing Mime Type deny list");
TextParser.setDenyMime(getConfig(SwitchboardConstants.PARSER_MIME_DENY, ""));

View File

@ -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)");