protected synchronized void flush ( ) throws RepositoryException , MalformedQueryException , UpdateExecutionException , IOException { if ( cache . isEmpty ( ) ) { return ; } StringBuffer entireQuery = new StringBuffer ( ) ; SPARQLQueryBindingSet bindingSet = new SPARQLQueryBindingSet ( ) ; for ( Namespace ns : cache . getNamespaces ( ) ) { entireQuery . append ( "PREFIX " + ns . getPrefix ( ) + ": <" + ns . getName ( ) + ">. " ) ; } entireQuery . append ( "DELETE DATA { " ) ; Set < Resource > distinctCtx = new HashSet < Resource > ( ) ; for ( Resource context : cache . contexts ( ) ) { distinctCtx . add ( context ) ; } for ( Resource ctx : distinctCtx ) { if ( ctx != null ) { entireQuery . append ( " GRAPH <" + ctx + "> { " ) ; } for ( Statement stmt : cache . filter ( null , null , null , ctx ) ) { entireQuery . append ( "<" + stmt . getSubject ( ) . stringValue ( ) + "> " ) ; entireQuery . append ( "<" + stmt . getPredicate ( ) . stringValue ( ) + "> " ) ; Value object = stmt . getObject ( ) ; if ( object instanceof Literal ) { Literal lit = ( Literal ) object ; entireQuery . append ( "\"" ) ; entireQuery . append ( SPARQLUtil . encodeString ( lit . getLabel ( ) ) ) ; entireQuery . append ( "\"" ) ; if ( null == lit . getLanguage ( ) ) { entireQuery . append ( "^^<" + lit . getDatatype ( ) . stringValue ( ) + ">" ) ; } else { entireQuery . append ( "@" + lit . getLanguage ( ) . toString ( ) ) ; } } else { entireQuery . append ( "<" + object . stringValue ( ) + "> " ) ; } entireQuery . append ( "." ) ; } if ( ctx != null ) { entireQuery . append ( " }" ) ; } } entireQuery . append ( "} " ) ; log . info ( entireQuery . toString ( ) ) ; client . sendUpdateQuery ( entireQuery . toString ( ) , bindingSet , false , null ) ; lastCacheAccess = new Date ( ) ; //log.info("success writing cache: {}",String.valueOf(cache.size())); cache . clear ( ) ; }
flushes the cache writing triples as graph