<commit_before>﻿/*
 * Copyright (c) 2010-2012 Achim 'ahzf' Friedland <<EMAIL>>
 * This file is part of Illias <http://www.github.com/ahzf/Illias>
 *
 * Licensed 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.
 */

#region Usings

using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Data;
using System.Diagnostics;
using System.Windows.Threading;

using MySql.Data.MySqlClient;

#endregion

namespace de.ahzf.Illias.SQL
{

    /// <summary>
    /// A class to keep the database connection running
    /// within its own thread (mySQL wants it this way).
    /// </summary>
    public class DBThreadClass : IDisposable
    {

        #region Data

        private IDbConnection                           CurrentDBConnection;
        private Thread                                  DBThread;
        private ConcurrentQueue<Action<IDbConnection>>  TaskQueue;
        private Dispatcher                              Dispatcher;
        private Boolean                                 EndDatabaseThread;
        private readonly Object                         LockObject;

        #endregion

        #region Properties

        #region DBAccessString

        /// <summary>
        /// The database access string.
        /// </summary>
        public String DBAccessString { get; set; }

        #endregion

        #region IsDefined

        /// <summary>
        /// Checks is the current database connection may be defined.
        /// </summary>
        public Boolean IsDefined
        {
            get
            {
                return CurrentDBConnection != null;
            }
        }

        #endregion

        #region ThreadId

        /// <summary>
        /// The id of this thread.
        /// </summary>
        public Int32 ThreadId
        {
            get
            {
                return Thread.CurrentThread.ManagedThreadId;
            }
        }

        #endregion

        #endregion

        #region Constructor(s)

        /// <summary>
        /// A class to keep the database connection running
        /// within its own thread (mySQL wants it this way).
        /// </summary>
        /// <param name="Dispatcher">The WPF dispatcher.</param>
        public DBThreadClass(Dispatcher Dispatcher)
        {

            this.Dispatcher  = Dispatcher;
            this.TaskQueue   = new ConcurrentQueue<Action<IDbConnection>>();
            this.LockObject  = new Object();

            DBThread = new Thread(() =>
            {

                Thread.CurrentThread.Name = "Database thread";
                Action<IDbConnection> ToDo;

                while (!EndDatabaseThread)
                {

                    if (TaskQueue.TryDequeue(out ToDo))
                        ToDo(CurrentDBConnection);

                    else
                        lock (LockObject) {
                            Monitor.Wait(LockObject);
                        }

                }

                ToDo = null;

            });

            DBThread.Start();

        }

        #endregion


        #region (private) Connect()

        private IDbConnection Connect()
        {

            if (CurrentDBConnection == null || CurrentDBConnection.State == ConnectionState.Closed)
                if (DBAccessString != null)
                {
                    CurrentDBConnection = new MySqlConnection(DBAccessString);
                    CurrentDBConnection.Open();
                }

            return CurrentDBConnection;

        }

        #endregion

        #region Reset()

        /// <summary>
        /// Reset the current database connection.
        /// </summary>
        public Boolean Reset()
        {

            if (CurrentDBConnection != null)
            {

                // If connection not closed => close it!
                if (CurrentDBConnection.State != ConnectionState.Closed)<commit_msg>updates<commit_after>﻿/*
 * Copyright (c) 2010-2012 Achim 'ahzf' Friedland <<EMAIL>>
 * This file is part of Illias <http://www.github.com/ahzf/Illias>
 *
 * Licensed 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.
 */

#region Usings

using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Data;
using System.Diagnostics;
using System.Windows.Threading;

//using MySql.Data.MySqlClient;

#endregion

namespace de.ahzf.Illias.SQL
{

    /// <summary>
    /// A class to keep the database connection running
    /// within its own thread (mySQL wants it this way).
    /// </summary>
    public class DBThreadClass : IDisposable
    {

        #region Data

        private IDbConnection                           CurrentDBConnection;
        private Thread                                  DBThread;
        private ConcurrentQueue<Action<IDbConnection>>  TaskQueue;
        private Dispatcher                              Dispatcher;
        private Boolean                                 EndDatabaseThread;
        private readonly Object                         LockObject;
        private readonly Func<String, IDbConnection>    DBConnectionDelegate;

        #endregion

        #region Properties

        #region DBAccessString

        /// <summary>
        /// The database access string.
        /// </summary>
        public String DBAccessString { get; set; }

        #endregion

        #region IsDefined

        /// <summary>
        /// Checks is the current database connection may be defined.
        /// </summary>
        public Boolean IsDefined
        {
            get
            {
                return CurrentDBConnection != null;
            }
        }

        #endregion

        #region ThreadId

        /// <summary>
        /// The id of this thread.
        /// </summary>
        public Int32 ThreadId
        {
            get
            {
                return Thread.CurrentThread.ManagedThreadId;
            }
        }

        #endregion

        #endregion

        #region Constructor(s)

        /// <summary>
        /// A class to keep the database connection running
        /// within its own thread (mySQL wants it this way).
        /// </summary>
        /// <param name="Dispatcher">The WPF dispatcher.</param>
        public DBThreadClass(Func<String, IDbConnection> DBConnectionDelegate, Dispatcher Dispatcher)
        {

            this.Dispatcher            = Dispatcher;
            this.TaskQueue             = new ConcurrentQueue<Action<IDbConnection>>();
            this.LockObject            = new Object();
            this.DBConnectionDelegate  = DBConnectionDelegate;

            DBThread = new Thread(() =>
            {

                Thread.CurrentThread.Name = "Database thread";
                Action<IDbConnection> ToDo;

                while (!EndDatabaseThread)
                {

                    if (TaskQueue.TryDequeue(out ToDo))
                        ToDo(CurrentDBConnection);

                    else
                        lock (LockObject) {
                            Monitor.Wait(LockObject);
                        }

                }

                ToDo = null;

            });

            DBThread.Start();

        }

        #endregion


        #region (private) Connect()

        private IDbConnection Connect()
        {

            if (CurrentDBConnection == null || CurrentDBConnection.State == ConnectionState.Closed)
                if (DBAccessString != null)
                {
                    CurrentDBConnection = DBConnectionDelegate(DBAccessString);
                    CurrentDBConnection.Open();
                }

            return CurrentDBConnection;

        }

        #endregion

        #region Reset()

        /// <summary>
        /// Reset the current database connection.
        /// </summary>
        public Boolean Reset()
        {

            if (CurrentDBConnection != null)
            {

                // If connection not closed => close it!
                if (CurrentDBConnection.State != ConnectionState.Closed)
{'fraction_non_alphanumeric': 0.0828698553948832, 'fraction_numerical': 0.003114571746384872, 'mean_word_length': 1.487825124515772, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3582989', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2054, 'n_words': 636}