<commit_before>/*
 * Copyright (C) 2014-2017 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * 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 de.longri.cachebox3.settings;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import de.longri.cachebox3.CB;
import de.longri.cachebox3.apis.groundspeak_api.GroundspeakAPI;
import de.longri.cachebox3.settings.types.*;
import de.longri.cachebox3.sqlite.Database;
import de.longri.cachebox3.utils.NamedRunnable;
import de.longri.gdx.sqlite.GdxSqliteCursor;
import de.longri.gdx.sqlite.GdxSqlitePreparedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by Longri on 02.08.16.
 */
public class Config extends Settings {
    static final Logger log = LoggerFactory.getLogger(Config.class);

    public static void AcceptChanges() {
        if (settingsList.dirtyList.size > 0)
            writeToDB();
        else
            log.debug("no Setting are dirty, don't write to DB");
    }


    static final AtomicBoolean inWrite = new AtomicBoolean(false);

    /**
     * Return true, if setting changes need restart
     *
     * @return
     */
    private static synchronized boolean writeToDB() {
        if (inWrite.get()) {
            log.warn("Config is in write state, can't run again! try again at 1 sec");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (inWrite.get()) {
                log.warn("Config is in write state, can't run again!");
                return false;
            }
        }
        log.debug("Start write Config to DB!");
        log.debug("Set Config in write to true");
        inWrite.set(true);
        final Array<SettingBase<?>> dirtyList = new Array<>();
        while (settingsList.dirtyList.size > 0) {
            dirtyList.add(settingsList.dirtyList.pop());
        }

        log.debug("Start Async writing {} settings changed", dirtyList.size);
        CB.postAsync(new NamedRunnable("Config") {
            @Override
            public void run() {
                try {
                    // Write into DB
                    SettingsDAO dao = new SettingsDAO();

                    final Database data = Database.Data;
                    final Database settingsDB = Database.Settings;


                    //splitt into local and global list!
                    final Array<SettingBase<?>> localList = new Array<>();
                    final Array<SettingBase<?>> globalList = new Array<>();
                    boolean isAPI = false;
                    while (dirtyList.size > 0) {
                        SettingBase<?> setting = dirtyList.pop();
                        if (setting.name.equals("GcAPIStaging") || setting.name.equals("GcAPI")) {
                            isAPI = true;
                        }
                        if (setting.getStoreType() == SettingStoreType.Local) {
                            localList.add(setting);
                        } else {
                            globalList.add(setting);
                        }
                    }

                    final AtomicBoolean WAITLOCAL = new AtomicBoolean(true);
                    final AtomicBoolean WAITGLOBAL = new AtomicBoolean(true);


                    if (localList.size > 0) {
                        if (!(data == null || !data.isStarted())) {
                            log.debug("Start Async writing Config to local");
                            CB.postAsync(new NamedRunnable("write settings local") {
                                @Override
                                public void run() {
                                    writeToDB(data, localList, WAITLOCAL);
                                }
                            });
                        } else {
                            log.warn("Can't write local Config, DB's not started");
                            WAITLOCAL.set(false);
                        }
                    } else {
                        WAITLOCAL.set(false);
                    }

                    if (globalList.size > 0) {
                        if (data == null || settingsDB == null || !data.isStarted() || !settingsDB.isStarted()) {
                            log.debug("Start Async writing Config to global");
                            CB.postAsync(new NamedRunnable("write settings global") {
                                @Override
                                public void run() {
                                    writeToDB(settingsDB, globalList, WAITGLOBAL);
                                }
                            });
                        } else {
                            log.warn("Can't write global Config, DB's not started");
                            WAITGLOBAL.set(false);
                        }
                    } else {<commit_msg>fix store of changed DB<commit_after>/*
 * Copyright (C) 2014-2017 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * 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 de.longri.cachebox3.settings;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import de.longri.cachebox3.CB;
import de.longri.cachebox3.apis.groundspeak_api.GroundspeakAPI;
import de.longri.cachebox3.settings.types.*;
import de.longri.cachebox3.sqlite.Database;
import de.longri.cachebox3.utils.NamedRunnable;
import de.longri.gdx.sqlite.GdxSqliteCursor;
import de.longri.gdx.sqlite.GdxSqlitePreparedStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by Longri on 02.08.16.
 */
public class Config extends Settings {
    static final Logger log = LoggerFactory.getLogger(Config.class);

    public static void AcceptChanges() {
        if (settingsList.dirtyList.size > 0)
            writeToDB();
        else
            log.debug("no Setting are dirty, don't write to DB");
    }


    static final AtomicBoolean inWrite = new AtomicBoolean(false);

    /**
     * Return true, if setting changes need restart
     *
     * @return
     */
    private static synchronized boolean writeToDB() {
        if (inWrite.get()) {
            log.warn("Config is in write state, can't run again! try again at 1 sec");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (inWrite.get()) {
                log.warn("Config is in write state, can't run again!");
                return false;
            }
        }
        log.debug("Start write Config to DB!");
        log.debug("Set Config in write to true");
        inWrite.set(true);
        final Array<SettingBase<?>> dirtyList = new Array<>();
        while (settingsList.dirtyList.size > 0) {
            dirtyList.add(settingsList.dirtyList.pop());
        }

        log.debug("Start Async writing {} settings changed", dirtyList.size);
        CB.postAsync(new NamedRunnable("Config") {
            @Override
            public void run() {
                try {
                    // Write into DB
                    SettingsDAO dao = new SettingsDAO();

                    final Database data = Database.Data;
                    final Database settingsDB = Database.Settings;


                    //splitt into local and global list!
                    final Array<SettingBase<?>> localList = new Array<>();
                    final Array<SettingBase<?>> globalList = new Array<>();
                    boolean isAPI = false;
                    while (dirtyList.size > 0) {
                        SettingBase<?> setting = dirtyList.pop();
                        if (setting.name.equals("GcAPIStaging") || setting.name.equals("GcAPI")) {
                            isAPI = true;
                        }
                        if (setting.getStoreType() == SettingStoreType.Local) {
                            localList.add(setting);
                        } else {
                            globalList.add(setting);
                        }
                    }

                    final AtomicBoolean WAITLOCAL = new AtomicBoolean(true);
                    final AtomicBoolean WAITGLOBAL = new AtomicBoolean(true);


                    if (localList.size > 0) {
                        if (!(data == null || !data.isStarted())) {
                            log.debug("Start Async writing Config to local");
                            CB.postAsync(new NamedRunnable("write settings local") {
                                @Override
                                public void run() {
                                    writeToDB(data, localList, WAITLOCAL);
                                }
                            });
                        } else {
                            log.warn("Can't write local Config, DB's not started");
                            WAITLOCAL.set(false);
                        }
                    } else {
                        WAITLOCAL.set(false);
                    }

                    if (globalList.size > 0) {
                        if (!(settingsDB == null || !settingsDB.isStarted())) {
                            log.debug("Start Async writing Config to global");
                            CB.postAsync(new NamedRunnable("write settings global") {
                                @Override
                                public void run() {
                                    writeToDB(settingsDB, globalList, WAITGLOBAL);
                                }
                            });
                        } else {
                            log.warn("Can't write global Config, DB's not started");
                            WAITGLOBAL.set(false);
                        }
                    } else {
{'fraction_non_alphanumeric': 0.0934315866084425, 'fraction_numerical': 0.005822416302765648, 'mean_word_length': 1.4363918439716312, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '928308', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2578, 'n_words': 727}