<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.fuseki.mgt ;

import static java.lang.String.format ;

import java.io.* ;
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.zip.GZIPOutputStream ;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import org.apache.jena.atlas.io.IO ;
import org.apache.jena.atlas.lib.FileOps ;
import org.apache.jena.atlas.logging.Log ;
import org.apache.jena.fuseki.FusekiException ;
import org.apache.jena.fuseki.FusekiLib ;
import org.apache.jena.fuseki.server.DatasetRef ;
import org.apache.jena.fuseki.server.DatasetRegistry ;
import org.apache.jena.fuseki.servlets.HttpAction ;
import org.apache.jena.fuseki.servlets.ServletBase ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFDataMgr ;
import org.apache.jena.web.HttpSC ;

import com.hp.hpl.jena.sparql.core.DatasetGraph ;
import com.hp.hpl.jena.sparql.util.Utils ;

public class ActionBackup extends ServletBase
{
    public ActionBackup() { super() ; }

    // Limit to one backup at a time.
    public static final ExecutorService backupService = Executors.newFixedThreadPool(1) ;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        // Split SPARQL ServletBase into Servletbase. 
        
        // request.getRemoteUser() ;
        // request.getUserPrincipal() ;

        String dataset = FusekiLib.safeParameter(request, "dataset") ;
        if ( dataset == null )
        {
            response.sendError(HttpSC.BAD_REQUEST_400, "Required parameter missing: ?dataset=") ;
            return ;
        }
        
        if ( ! dataset.startsWith("/") )
            dataset="/"+dataset ;
        
        // HttpSession session = request.getSession(true) ;
        // session.setAttribute("dataset", dataset) ;
        // session.setMaxInactiveInterval(15*60) ; // 10 mins

        boolean known = DatasetRegistry.get().isRegistered(dataset) ;
        if (!known)
        {
            response.sendError(HttpSC.BAD_REQUEST_400, "No such dataset: " + dataset) ;
            return ;
        }
        
        long id = allocRequestId(request, response);
        HttpAction action = new HttpAction(id, request, response, false) ;
        DatasetRef ref = DatasetRegistry.get().get(dataset) ;
        scheduleBackup(action) ;
    }

    static final String BackupArea = "backups" ;  
    
    private void scheduleBackup(final HttpAction action)
    {
        String dsName = action.dsRef.name ;
        final String ds = dsName.startsWith("/")? dsName : "/"+dsName ;
        
        String timestamp = Utils.nowAsString("yyyy-MM-dd_HH-mm-ss") ;
        final String filename = BackupArea + ds + "_" + timestamp ;
        FileOps.ensureDir(BackupArea) ;
        
        try {
            final Callable<Boolean> task = new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception
                {
                    log.info(format("[%d] Start backup %s to '%s'", action.id, ds, filename)) ;
                    action.beginRead() ;<commit_msg>Backup action was not setting the DatasetRef.  The DatasetRef now has to be set explicitly (makes procesisng normal SPARQL requests cleaner - a change elsewhere).<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.fuseki.mgt ;

import static java.lang.String.format ;

import java.io.* ;
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
import java.util.zip.GZIPOutputStream ;

import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

import org.apache.jena.atlas.io.IO ;
import org.apache.jena.atlas.lib.FileOps ;
import org.apache.jena.atlas.logging.Log ;
import org.apache.jena.fuseki.FusekiException ;
import org.apache.jena.fuseki.FusekiLib ;
import org.apache.jena.fuseki.server.DatasetRef ;
import org.apache.jena.fuseki.server.DatasetRegistry ;
import org.apache.jena.fuseki.servlets.HttpAction ;
import org.apache.jena.fuseki.servlets.ServletBase ;
import org.apache.jena.riot.Lang ;
import org.apache.jena.riot.RDFDataMgr ;
import org.apache.jena.web.HttpSC ;

import com.hp.hpl.jena.sparql.core.DatasetGraph ;
import com.hp.hpl.jena.sparql.util.Utils ;

public class ActionBackup extends ServletBase
{
    public ActionBackup() { super() ; }

    // Limit to one backup at a time.
    public static final ExecutorService backupService = Executors.newFixedThreadPool(1) ;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String dataset = FusekiLib.safeParameter(request, "dataset") ;
        if ( dataset == null )
        {
            response.sendError(HttpSC.BAD_REQUEST_400, "Required parameter missing: ?dataset=") ;
            return ;
        }
        
        if ( ! dataset.startsWith("/") )
            dataset="/"+dataset ;
        
        // HttpSession session = request.getSession(true) ;
        // session.setAttribute("dataset", dataset) ;
        // session.setMaxInactiveInterval(15*60) ; // 10 mins

        boolean known = DatasetRegistry.get().isRegistered(dataset) ;
        if (!known)
        {
            response.sendError(HttpSC.BAD_REQUEST_400, "No such dataset: " + dataset) ;
            return ;
        }
        
        long id = allocRequestId(request, response);
        HttpAction action = new HttpAction(id, request, response, false) ;
        DatasetRef ref = DatasetRegistry.get().get(dataset) ;
        action.setDataset(ref);
        scheduleBackup(action) ;
    }

    static final String BackupArea = "backups" ;  
    
    private void scheduleBackup(final HttpAction action)
    {
        String dsName = action.dsRef.name ;
        final String ds = dsName.startsWith("/")? dsName : "/"+dsName ;
        
        String timestamp = Utils.nowAsString("yyyy-MM-dd_HH-mm-ss") ;
        final String filename = BackupArea + ds + "_" + timestamp ;
        FileOps.ensureDir(BackupArea) ;
        
        try {
            final Callable<Boolean> task = new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception
                {
                    log.info(format("[%d] Start backup %s to '%s'", action.id, ds, filename)) ;
                    action.beginRead() ;
{'fraction_non_alphanumeric': 0.09853068280034573, 'fraction_numerical': 0.004198049141869367, 'mean_word_length': 2.818953323903819, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4370605', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2292, 'n_words': 613}