@ -2,11 +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 ;
@ -26,6 +29,8 @@ import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode ;
import com.hp.hpl.jena.rdf.model.ResIterator ;
import com.hp.hpl.jena.rdf.model.Resource ;
import com.hp.hpl.jena.rdf.model.Selector ;
import com.hp.hpl.jena.rdf.model.SimpleSelector ;
import com.hp.hpl.jena.rdf.model.StmtIterator ;
import com.hp.hpl.jena.util.FileManager ;
@ -108,14 +113,27 @@ public class JenaTripleStore {
}
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" ) ;
}
}
@ -161,11 +179,11 @@ public class JenaTripleStore {
}
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 ) {
@ -174,11 +192,11 @@ public class JenaTripleStore {
}
public static String getPrivateObject ( final String subject , final String predicate , final String username ) {
Log . logInfo ( "TRIPLESTORE" , "GET " + subject + " - " + predicate + " ... (" + 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 ) {
@ -236,82 +254,81 @@ public class JenaTripleStore {
} ;
}
public static void initPrivateStores ( ) {
public static Model getSubmodelBySubject ( String subject ) {
Selector q = new SimpleSelector ( model . getResource ( subject ) , ( Property ) null , ( RDFNode ) null ) ;
final Model m = model . query ( q ) ;
m . setNsPrefix ( Tagging . DEFAULT_PREFIX , Tagging . DEFAULT_NAMESPACE ) ;
m . setNsPrefix ( DCTerms . PREFIX , DCTerms . NAMESPACE ) ;
return m ;
}
Switchboard switchboard = Switchboard . getSwitchboard ( ) ;
public static String getMetadataByURLHash ( byte [ ] urlhash ) {
String subject = YaCyMetadata . hashURI ( urlhash ) ;
Model model = JenaTripleStore . getSubmodelBySubject ( subject ) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
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 ) ;
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 ( ) ;
}
}