@ -2,12 +2,14 @@
package net.yacy.cora.lod ;
import java.io.BufferedOutputStream ;
import java.io.ByteArrayInputStream ;
import java.io.ByteArrayOutputStream ;
import java.io.File ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.util.Iterator ;
import java.util.Map.Entry ;
import java.util.concurrent.ConcurrentHashMap ;
@ -38,7 +40,7 @@ public class JenaTripleStore {
public static Model model = ModelFactory . createDefaultModel ( ) ;
static {
init ( model ) ;
}
private final static void init ( Model model ) {
model . setNsPrefix ( YaCyMetadata . PREFIX , YaCyMetadata . NAMESPACE ) ;
@ -48,7 +50,7 @@ public class JenaTripleStore {
model . setNsPrefix ( "pnd" , "http://dbpedia.org/ontology/individualisedPnd" ) ;
model . setNsPrefix ( DCTerms . PREFIX , DCTerms . NAMESPACE ) ;
}
public static long size ( ) {
return model . size ( ) ;
}
@ -81,7 +83,7 @@ public class JenaTripleStore {
InputStream is = FileManager . get ( ) . open ( fileNameOrUri ) ;
LoadNTriples ( is ) ;
}
public static void LoadNTriples ( InputStream is ) throws IOException {
Model tmp = ModelFactory . createDefaultModel ( ) ;
if ( is ! = null ) {
@ -105,20 +107,33 @@ public class JenaTripleStore {
model = model . union ( tmp ) ;
}
}
public static void saveFile ( String filename ) {
public static void saveFile ( String filename ) {
saveFile ( filename , model ) ;
}
public static void saveFile ( String filename , Model model ) {
File f = new File ( filename ) ;
File ftmp = new File ( filename + "." + System . currentTimeMillis ( ) ) ;
if ( model . size ( ) = = 0 & & ! f . exists ( ) ) {
// we don't store zero-size models if they did not exist before
Log . logInfo ( "TRIPLESTORE" , "NOT saving triplestore with " + model . size ( ) + " triples to " + filename ) ;
return ;
}
Log . logInfo ( "TRIPLESTORE" , "Saving triplestore with " + model . size ( ) + " triples to " + filename ) ;
FileOutputStream fout ;
OutputStream fout ;
try {
fout = new FileOutputStream ( filename ) ;
fout = new BufferedOutputStream( new FileOutputStream ( ftmp ) ) ;
model . write ( fout ) ;
fout . close ( ) ;
// if something went wrong until here, the original file is not overwritten
// since we are happy here, we can remove the old file and replace it with the new one
f . delete ( ) ;
if ( ! f . exists ( ) ) {
ftmp . renameTo ( f ) ;
}
Log . logInfo ( "TRIPLESTORE" , "Saved triplestore with " + model . size ( ) + " triples to " + filename ) ;
} catch ( Exception e ) {
// TODO Auto-generated catch block
Log . logWarning ( "TRIPLESTORE" , "Saving to " + filename + " failed" ) ;
}
}
@ -145,13 +160,13 @@ public class JenaTripleStore {
Property pr = model . getProperty ( predicate ) ;
JenaTripleStore . model . removeAll ( r , pr , ( Resource ) null ) ;
}
public static void addTriple ( String subject , String predicate , String object , String username ) {
if ( privatestorage ! = null & & privatestorage . containsKey ( username ) ) {
addTriple ( subject , predicate , object , privatestorage . get ( username ) ) ;
}
}
public static void addTriple ( String subject , String predicate , String object ) {
addTriple ( subject , predicate , object , model ) ;
}
@ -162,43 +177,43 @@ public class JenaTripleStore {
r . addProperty ( pr , object ) ;
Log . logInfo ( "TRIPLESTORE" , "ADD " + subject + " - " + predicate + " - " + object ) ;
}
public static String getObject ( final String subject , final String predicate ) {
Log . logInfo ( "TRIPLESTORE" , "GET " + subject + " - " + predicate + " ... " ) ;
Iterator < RDFNode > ni = JenaTripleStore . getObjects ( subject , predicate ) ;
if ( ! ni . hasNext ( ) ) return "" ;
return ni . next ( ) . toString ( ) ;
String object = "" ;
if ( ni . hasNext ( ) ) object = ni . next ( ) . toString ( ) ;
Log . logInfo ( "TRIPLESTORE" , "GET " + subject + " - " + predicate + " - " + object ) ;
return object ;
}
public static Iterator < RDFNode > getObjects ( final String subject , final String predicate ) {
public static Iterator < RDFNode > getObjects ( final String subject , final String predicate ) {
final Resource r = subject = = null ? null : JenaTripleStore . getResource ( subject ) ;
return getObjects ( r , predicate ) ;
}
public static String getPrivateObject ( final String subject , final String predicate , final String username ) {
Log . logInfo ( "TRIPLESTORE" , "GET " + subject + " - " + predicate + " ... (" + username + ")" ) ;
public static String getPrivateObject ( final String subject , final String predicate , final String username ) {
Iterator < RDFNode > ni = JenaTripleStore . getPrivateObjects ( subject , predicate , username ) ;
if ( ! ni . hasNext ( ) ) return "" ;
return ni . next ( ) . toString ( ) ;
String object = "" ;
if ( ni . hasNext ( ) ) object = ni . next ( ) . toString ( ) ;
Log . logInfo ( "TRIPLESTORE" , "GET (" + username + ") " + subject + " - " + predicate + " - " + object ) ;
return object ;
}
private static Iterator < RDFNode > getPrivateObjects ( final String subject , final String predicate , final String username ) {
if ( privatestorage ! = null & & privatestorage . containsKey ( username ) ) {
return getObjects ( privatestorage . get ( username ) . getResource ( subject ) , predicate , privatestorage . get ( username ) ) ;
}
return null ;
}
public static Iterator < RDFNode > getObjects ( final Resource r , final String predicate ) {
return getObjects ( r , predicate , model ) ;
}
private static Iterator < RDFNode > getObjects ( final Resource r , final String predicate , final Model model ) {
final Property pr = model . getProperty ( predicate ) ;
final StmtIterator iter = model . listStatements ( r , pr , ( Resource ) null ) ;
final StmtIterator iter = model . listStatements ( r , pr , ( Resource ) null ) ;
return new Iterator < RDFNode > ( ) {
@Override
public boolean hasNext ( ) {
@ -214,15 +229,15 @@ public class JenaTripleStore {
}
} ;
}
public static Iterator < Resource > getSubjects ( final String predicate ) {
return getSubjects ( predicate , model ) ;
}
private static Iterator < Resource > getSubjects ( final String predicate , final Model model ) {
final Property pr = model . getProperty ( predicate ) ;
final ResIterator iter = model . listSubjectsWithProperty ( pr ) ;
final ResIterator iter = model . listSubjectsWithProperty ( pr ) ;
return new Iterator < Resource > ( ) {
@Override
public boolean hasNext ( ) {
@ -246,7 +261,7 @@ public class JenaTripleStore {
m . setNsPrefix ( DCTerms . PREFIX , DCTerms . NAMESPACE ) ;
return m ;
}
public static String getMetadataByURLHash ( byte [ ] urlhash ) {
String subject = YaCyMetadata . hashURI ( urlhash ) ;
Model model = JenaTripleStore . getSubmodelBySubject ( subject ) ;
@ -254,83 +269,66 @@ public class JenaTripleStore {
model . write ( baos , "RDF/XML-ABBREV" ) ;
return UTF8 . String ( baos . toByteArray ( ) ) ;
}
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 {
Iterator < de . anomic . data . UserDB . Entry > it = switchboard . userDB . iterator ( true ) ;
while ( it . hasNext ( ) ) {
de . anomic . data . UserDB . Entry e = it . next ( ) ;
String username = e . getUserName ( ) ;
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 ( ) ;
init ( tmp ) ;
init ( 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 ) {
} catch ( Exception anyex ) {
Log . logException ( anyex ) ;
}
}
public static void savePrivateStores ( Switchboard switchboard ) {
public static void savePrivateStores ( ) {
Switchboard switchboard = Switchboard . getSwitchboard ( ) ;
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" ) ;
saveFile ( currentuserfile . getAbsolutePath ( ) , s . getValue ( ) ) ;
}
}
private static long lastModelSizeStored = - 1 ;
public static void saveAll ( ) {
Switchboard sb = Switchboard . getSwitchboard ( ) ;
File triplestore = new File ( sb . getConfig ( "triplestore" , new File ( sb . dataPath , "DATA/TRIPLESTORE" ) . getAbsolutePath ( ) ) ) ;
if ( model . size ( ) ! = lastModelSizeStored ) {
JenaTripleStore . saveFile ( new File ( triplestore , "local.rdf" ) . getAbsolutePath ( ) ) ;
lastModelSizeStored = model . size ( ) ;
}
JenaTripleStore . savePrivateStores ( ) ;
}
}