<commit_before>﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using RealmNet.Interop;

namespace RealmNet
{
    public class Realm : Handled
    {
        public static ICoreProvider ActiveCoreProvider;
        [ThreadStatic] static Transaction ActiveTransaction;  

        public static Realm GetInstance(string path = null)
        {
            return new Realm(ActiveCoreProvider, path);
        }

        internal static Realm RealmWithActiveTransactionThisTread()
        {
            return ActiveTransaction?._realm;
        }

        internal static void ForgetActiveTransactionThisTread()
        {
            ActiveTransaction = null;  // transaction Dispose should reset its realm's state to Ready
        }

        private readonly ICoreProvider _coreProvider;

        private ISharedGroupHandle SharedGroupHandle => Handle as ISharedGroupHandle;
        private IGroupHandle _transactionGroupHandle;

        private Realm(ICoreProvider coreProvider, string path) 
        {
            _coreProvider = coreProvider;
            SetHandle(coreProvider.CreateSharedGroup(path), false);
        }

        // TODO consider retiring this in favor of just creating object instances
        public T CreateObject<T>() where T : RealmObject
        {
            return (T)CreateObject(typeof(T));
        }


        public object CreateObject(Type objectType)
        {
            // relies on RealmObject ctor calling back to AdoptNewObject
            return (RealmObject)Activator.CreateInstance(objectType);
        }

        internal void AdoptNewObject(RealmObject adoptingObject)
        {
            AdoptNewObject(adoptingObject, this, _coreProvider, _transactionGroupHandle);
        }

        internal static void AdoptNewObject(RealmObject adoptingObject, Realm usingRealm, ICoreProvider usingProvider, IGroupHandle transGroupHandle)
        {
            var objectType = adoptingObject.GetType();
            if (!usingProvider.HasTable(transGroupHandle, objectType.Name))
                CreateTableFor(objectType, usingProvider, transGroupHandle);
            var rowIndex = usingProvider.AddEmptyRow(transGroupHandle, objectType.Name);
            adoptingObject._Manage(usingRealm, usingProvider, rowIndex);
        }

        private static void CreateTableFor(Type objectType, ICoreProvider usingProvider, IGroupHandle transGroupHandle)
        {
            var tableName = objectType.Name;

            if (!objectType.GetTypeInfo().GetCustomAttributes(typeof(WovenAttribute), true).Any())
                Debug.WriteLine("WARNING! The type " + tableName + " is a RealmObject but it has not been woven.");

            usingProvider.AddTable(transGroupHandle, tableName);

            var propertiesToMap = objectType.GetTypeInfo().DeclaredProperties.Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof (IgnoreAttribute)));
            foreach (var p in propertiesToMap)
            {
                var propertyName = p.Name;<commit_msg>Changed wrongly named RowIndex to RowHandle<commit_after>﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using RealmNet.Interop;

namespace RealmNet
{
    public class Realm : Handled
    {
        public static ICoreProvider ActiveCoreProvider;
        [ThreadStatic] static Transaction ActiveTransaction;  

        public static Realm GetInstance(string path = null)
        {
            return new Realm(ActiveCoreProvider, path);
        }

        internal static Realm RealmWithActiveTransactionThisTread()
        {
            return ActiveTransaction?._realm;
        }

        internal static void ForgetActiveTransactionThisTread()
        {
            ActiveTransaction = null;  // transaction Dispose should reset its realm's state to Ready
        }

        private readonly ICoreProvider _coreProvider;

        private ISharedGroupHandle SharedGroupHandle => Handle as ISharedGroupHandle;
        private IGroupHandle _transactionGroupHandle;

        private Realm(ICoreProvider coreProvider, string path) 
        {
            _coreProvider = coreProvider;
            SetHandle(coreProvider.CreateSharedGroup(path), false);
        }

        // TODO consider retiring this in favor of just creating object instances
        public T CreateObject<T>() where T : RealmObject
        {
            return (T)CreateObject(typeof(T));
        }


        public object CreateObject(Type objectType)
        {
            // relies on RealmObject ctor calling back to AdoptNewObject
            return (RealmObject)Activator.CreateInstance(objectType);
        }

        internal void AdoptNewObject(RealmObject adoptingObject)
        {
            AdoptNewObject(adoptingObject, this, _coreProvider, _transactionGroupHandle);
        }

        internal static void AdoptNewObject(RealmObject adoptingObject, Realm usingRealm, ICoreProvider usingProvider, IGroupHandle transGroupHandle)
        {
            var objectType = adoptingObject.GetType();
            if (!usingProvider.HasTable(transGroupHandle, objectType.Name))
                CreateTableFor(objectType, usingProvider, transGroupHandle);
            var rowHandle = usingProvider.AddEmptyRow(transGroupHandle, objectType.Name);
            adoptingObject._Manage(usingRealm, usingProvider, rowHandle);
        }

        private static void CreateTableFor(Type objectType, ICoreProvider usingProvider, IGroupHandle transGroupHandle)
        {
            var tableName = objectType.Name;

            if (!objectType.GetTypeInfo().GetCustomAttributes(typeof(WovenAttribute), true).Any())
                Debug.WriteLine("WARNING! The type " + tableName + " is a RealmObject but it has not been woven.");

            usingProvider.AddTable(transGroupHandle, tableName);

            var propertiesToMap = objectType.GetTypeInfo().DeclaredProperties.Where(p => p.CustomAttributes.All(a => a.AttributeType != typeof (IgnoreAttribute)));
            foreach (var p in propertiesToMap)
            {
                var propertyName = p.Name;
{'fraction_non_alphanumeric': 0.06859903381642513, 'fraction_numerical': 0.0, 'mean_word_length': 2.7280912364945977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '320748', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1476, 'n_words': 392}