<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 com.hp.hpl.jena.tdb.setup ;

import java.io.File ;
import java.io.IOException ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Properties ;

import org.apache.jena.atlas.io.IO ;
import org.apache.jena.atlas.lib.ColumnMap ;
import org.apache.jena.atlas.lib.FileOps ;
import org.apache.jena.atlas.lib.PropertyUtils ;
import org.apache.jena.atlas.lib.StrUtils ;
import org.slf4j.Logger ;

import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.sparql.engine.main.QC ;
import com.hp.hpl.jena.sparql.engine.optimizer.reorder.ReorderLib ;
import com.hp.hpl.jena.sparql.engine.optimizer.reorder.ReorderTransformation ;
import com.hp.hpl.jena.sparql.sse.SSEParseException ;
import com.hp.hpl.jena.tdb.TDB ;
import com.hp.hpl.jena.tdb.TDBException ;
import com.hp.hpl.jena.tdb.base.block.BlockMgr ;
import com.hp.hpl.jena.tdb.base.file.BufferChannel ;
import com.hp.hpl.jena.tdb.base.file.FileSet ;
import com.hp.hpl.jena.tdb.base.file.Location ;
import com.hp.hpl.jena.tdb.index.IndexParams ;
import com.hp.hpl.jena.tdb.solver.OpExecutorTDB1 ;
import com.hp.hpl.jena.tdb.store.* ;
import com.hp.hpl.jena.tdb.store.nodetable.NodeTable ;
import com.hp.hpl.jena.tdb.store.nodetupletable.NodeTupleTable ;
import com.hp.hpl.jena.tdb.store.nodetupletable.NodeTupleTableConcrete ;
import com.hp.hpl.jena.tdb.store.tupletable.TupleIndex ;
import com.hp.hpl.jena.tdb.sys.* ;

/**
 * This class is the process of building a dataset. Records
 * BlockMgr/BufferChannel/NodeTable for use by the transaction builder.
 */

public class DatasetBuilderStd implements DatasetBuilder {
    private static final Logger log = TDB.logInfo ;

    private NodeTableBuilder    nodeTableBuilder ;
    private TupleIndexBuilder   tupleIndexBuilder ;
    private Recorder            recorder = null ;   
    
    public static DatasetGraphTDB create(Location location) {
        SystemParams params = paramsForLocation(location) ;
        DatasetBuilderStd x = new DatasetBuilderStd() ;
        x.standardSetup() ;
        return x.build(location, params) ;
    }

    public static DatasetGraphTDB create() {
        return create(Location.mem()) ;
    }

    public static DatasetBuilderStd stdBuilder() {
        DatasetBuilderStd x = new DatasetBuilderStd() ;
        x.standardSetup() ;
        return x ;
    }

    protected DatasetBuilderStd() { }

    // Used by DatasetBuilderTxn
    public DatasetBuilderStd(BlockMgrBuilder blockMgrBuilder, NodeTableBuilder nodeTableBuilder) {
        set(blockMgrBuilder, nodeTableBuilder) ;
    }

    protected void set(BlockMgrBuilder blockMgrBuilder, NodeTableBuilder nodeTableBuilder) {
        recorder = new Recorder() ;
        BlockMgrBuilder blockMgrBuilderRec = new BlockMgrBuilderRecorder(blockMgrBuilder, recorder) ;

        IndexBuilder indexBuilder = new BuilderIndex.IndexBuilderStd(blockMgrBuilderRec, blockMgrBuilderRec) ;
        RangeIndexBuilder rangeIndexBuilder = new BuilderIndex.RangeIndexBuilderStd(blockMgrBuilderRec, blockMgrBuilderRec) ;

        this.nodeTableBuilder = nodeTableBuilder ;
        nodeTableBuilder = new NodeTableBuilderRecorder(nodeTableBuilder, recorder) ;

        TupleIndexBuilder tupleIndexBuilder = new BuilderDB.TupleIndexBuilderStd(rangeIndexBuilder) ;
        
        this.nodeTableBuilder = nodeTableBuilder ;
        this.tupleIndexBuilder = tupleIndexBuilder ;
    }

    private static SystemParams paramsForLocation(Location location) {
        if ( location.exists(DB_CONFIG_FILE) ) {
            log.debug("Existing configuration file found") ;
            Properties properties = new Properties() ;
            try { 
                PropertyUtils.loadFromFile(properties, DB_CONFIG_FILE) ;
            } catch (IOException ex) { IO.exception(ex) ; throw new TDBException("Bad configuration file", ex) ; }
        }
        return SystemParams.getDftSystemParams() ;
    }

//    private void checkIfConfig(Location location) {
//    }

    private void checkIfNew(Location location) {
        if ( location.isMem() ) {
            return ;
        }
        
        if ( FileOps.existsAnyFiles(location.getDirectoryPath()) ) {
            
        }

        if ( location.exists(DB_CONFIG_FILE) ) {
            log.debug("Existing config file") ;<commit_msg>Put back access point for subclassing<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 com.hp.hpl.jena.tdb.setup ;

import java.io.File ;
import java.io.IOException ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.Properties ;

import org.apache.jena.atlas.io.IO ;
import org.apache.jena.atlas.lib.ColumnMap ;
import org.apache.jena.atlas.lib.FileOps ;
import org.apache.jena.atlas.lib.PropertyUtils ;
import org.apache.jena.atlas.lib.StrUtils ;
import org.slf4j.Logger ;

import com.hp.hpl.jena.query.ARQ ;
import com.hp.hpl.jena.sparql.engine.main.QC ;
import com.hp.hpl.jena.sparql.engine.optimizer.reorder.ReorderLib ;
import com.hp.hpl.jena.sparql.engine.optimizer.reorder.ReorderTransformation ;
import com.hp.hpl.jena.sparql.sse.SSEParseException ;
import com.hp.hpl.jena.tdb.TDB ;
import com.hp.hpl.jena.tdb.TDBException ;
import com.hp.hpl.jena.tdb.base.block.BlockMgr ;
import com.hp.hpl.jena.tdb.base.file.BufferChannel ;
import com.hp.hpl.jena.tdb.base.file.FileSet ;
import com.hp.hpl.jena.tdb.base.file.Location ;
import com.hp.hpl.jena.tdb.index.IndexParams ;
import com.hp.hpl.jena.tdb.solver.OpExecutorTDB1 ;
import com.hp.hpl.jena.tdb.store.* ;
import com.hp.hpl.jena.tdb.store.nodetable.NodeTable ;
import com.hp.hpl.jena.tdb.store.nodetupletable.NodeTupleTable ;
import com.hp.hpl.jena.tdb.store.nodetupletable.NodeTupleTableConcrete ;
import com.hp.hpl.jena.tdb.store.tupletable.TupleIndex ;
import com.hp.hpl.jena.tdb.sys.* ;

/**
 * This class is the process of building a dataset. Records
 * BlockMgr/BufferChannel/NodeTable for use by the transaction builder.
 */

public class DatasetBuilderStd implements DatasetBuilder {
    private static final Logger log = TDB.logInfo ;

    private NodeTableBuilder    nodeTableBuilder ;
    private TupleIndexBuilder   tupleIndexBuilder ;
    private Recorder            recorder = null ;   
    
    public static DatasetGraphTDB create(Location location) {
        SystemParams params = paramsForLocation(location) ;
        DatasetBuilderStd x = new DatasetBuilderStd() ;
        x.standardSetup() ;
        return x.build(location, params) ;
    }

    public static DatasetGraphTDB create() {
        return create(Location.mem()) ;
    }

    public static DatasetBuilderStd stdBuilder() {
        DatasetBuilderStd x = new DatasetBuilderStd() ;
        x.standardSetup() ;
        return x ;
    }

    protected DatasetBuilderStd() { }

    // Used by DatasetBuilderTxn
    public DatasetBuilderStd(BlockMgrBuilder blockMgrBuilder, NodeTableBuilder nodeTableBuilder) {
        set(blockMgrBuilder, nodeTableBuilder) ;
    }

    protected void set(NodeTableBuilder nodeTableBuilder, TupleIndexBuilder tupleIndexBuilder) {
        this.nodeTableBuilder = nodeTableBuilder ;
        this.tupleIndexBuilder = tupleIndexBuilder ;
    }
    
    protected void set(BlockMgrBuilder blockMgrBuilder, NodeTableBuilder nodeTableBuilder) {
        recorder = new Recorder() ;
        BlockMgrBuilder blockMgrBuilderRec = new BlockMgrBuilderRecorder(blockMgrBuilder, recorder) ;

        IndexBuilder indexBuilder = new BuilderIndex.IndexBuilderStd(blockMgrBuilderRec, blockMgrBuilderRec) ;
        RangeIndexBuilder rangeIndexBuilder = new BuilderIndex.RangeIndexBuilderStd(blockMgrBuilderRec, blockMgrBuilderRec) ;

        this.nodeTableBuilder = nodeTableBuilder ;
        nodeTableBuilder = new NodeTableBuilderRecorder(nodeTableBuilder, recorder) ;

        TupleIndexBuilder tupleIndexBuilder = new BuilderDB.TupleIndexBuilderStd(rangeIndexBuilder) ;
        set(nodeTableBuilder, tupleIndexBuilder) ;
    }

    private static SystemParams paramsForLocation(Location location) {
        if ( location.exists(DB_CONFIG_FILE) ) {
            log.debug("Existing configuration file found") ;
            Properties properties = new Properties() ;
            try { 
                PropertyUtils.loadFromFile(properties, DB_CONFIG_FILE) ;
            } catch (IOException ex) { IO.exception(ex) ; throw new TDBException("Bad configuration file", ex) ; }
        }
        return SystemParams.getDftSystemParams() ;
    }

//    private void checkIfConfig(Location location) {
//    }

    private void checkIfNew(Location location) {
        if ( location.isMem() ) {
            return ;
        }
        
        if ( FileOps.existsAnyFiles(location.getDirectoryPath()) ) {
            
        }

        if ( location.exists(DB_CONFIG_FILE) ) {
            log.debug("Existing config file") ;
{'fraction_non_alphanumeric': 0.089573732718894, 'fraction_numerical': 0.001152073732718894, 'mean_word_length': 3.3677148846960168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3437727', 'n_tokens_mistral': 3074, 'n_tokens_neox': 3064, 'n_words': 705}