<commit_before>﻿using System;
using System.Data;
using System.Reflection;
using Aurora.Framework;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Services.Interfaces;

namespace Aurora.Services.DataService.Connectors.Database.Asset
{
    public class LocalAssetMainConnector : IAssetDataPlugin
    {
        private IGenericData m_Gd;

        #region Implementation of IAuroraDataPlugin

        public string Name
        {
            get { return "IAssetDataPlugin"; }
        }

        public void Initialize(IGenericData genericData, IConfigSource source, IRegistryCore simBase,
                               string defaultConnectionString)
        {
            if (source.Configs["AuroraConnectors"].GetString("AssetConnector", "LocalConnector") != "LocalConnector")
                return;
            m_Gd = genericData;

            if (source.Configs[Name] != null)
                defaultConnectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);

            genericData.ConnectToDatabase(defaultConnectionString, "Asset",
                                          source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
            DataManager.DataManager.RegisterPlugin(this);
        }

        #endregion

        #region Implementation of IAssetDataPlugin

        public AssetBase GetAsset(UUID uuid)
        {
            return GetAsset(uuid, true);
        }

        public AssetBase GetMeta(UUID uuid)
        {
            IDataReader dr = null;
            try
            {
                dr = m_Gd.QueryData("where id = '" + uuid + "' LIMIT 1", "assets",
                                    "id, name, description, assetType, local, temporary, asset_flags, creatorID");
                while (dr.Read())
                {
                    return LoadAssetFromDataRead(dr);
                }
                MainConsole.Instance.Warn("[LocalAssetDatabase] GetMeta(" + uuid + ") - Asset " + uuid + " was not found.");
            }
            catch (Exception e)
            {
                MainConsole.Instance.Error("[LocalAssetDatabase]: Failed to fetch asset " + uuid + ", " + e);
            }
            finally
            {
                if (dr != null) dr.Close();
            }
            return null;
        }

        public UUID Store(AssetBase asset)
        {
            StoreAsset(asset);
            return asset.ID;
        }

        public bool StoreAsset(AssetBase asset)
        {
            try
            {
                if (asset.Name.Length > 64)
                    asset.Name = asset.Name.Substring(0, 64);
                if (asset.Description.Length > 128)
                    asset.Description = asset.Description.Substring(0, 128);
                if (ExistsAsset(asset.ID))
                {
                    AssetBase oldAsset = GetAsset(asset.ID);
                    if (oldAsset == null || (oldAsset.Flags & AssetFlags.Rewritable) == AssetFlags.Rewritable)
                    {
                        MainConsole.Instance.Debug("[LocalAssetDatabase]: Asset already exists in the db, overwriting - " + asset.ID);
                        Delete(asset.ID, true);
                        InsertAsset(asset, asset.ID);
                    }
                    else
                    {
                        MainConsole.Instance.Warn("[LocalAssetDatabase]: Asset already exists in the db, fixing ID... - " + asset.ID);
                        InsertAsset(asset, UUID.Random());
                    }
                }
                else
                {
                    InsertAsset(asset, asset.ID);
                }
            }
            catch (Exception e)
            {
                MainConsole.Instance.ErrorFormat("[LocalAssetDatabase]: Failure creating asset {0} with name \"{1}\". Error: {2}",
                                  asset.ID, asset.Name, e);
            }
            return true;
        }

        public void UpdateContent(UUID id, byte[] asset, out UUID newID)
        {
            newID = UUID.Zero;

            AssetBase oldAsset = GetAsset(id);
            if (oldAsset == null)
                return;

            if ((oldAsset.Flags & AssetFlags.Rewritable) == AssetFlags.Rewritable)
            {
                try
                {
                    m_Gd.Update("assets", new object[] { asset }, new[] { "data" }, new[] { "id" }, new object[] { id });
                }
                catch (Exception e)
                {<commit_msg>factoring out array-style IGenericData.Update()<commit_after>﻿using System;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using Aurora.Framework;
using Nini.Config;
using OpenMetaverse;
using OpenSim.Services.Interfaces;

namespace Aurora.Services.DataService.Connectors.Database.Asset
{
    public class LocalAssetMainConnector : IAssetDataPlugin
    {
        private IGenericData m_Gd;

        #region Implementation of IAuroraDataPlugin

        public string Name
        {
            get { return "IAssetDataPlugin"; }
        }

        public void Initialize(IGenericData genericData, IConfigSource source, IRegistryCore simBase,
                               string defaultConnectionString)
        {
            if (source.Configs["AuroraConnectors"].GetString("AssetConnector", "LocalConnector") != "LocalConnector")
                return;
            m_Gd = genericData;

            if (source.Configs[Name] != null)
                defaultConnectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);

            genericData.ConnectToDatabase(defaultConnectionString, "Asset",
                                          source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
            DataManager.DataManager.RegisterPlugin(this);
        }

        #endregion

        #region Implementation of IAssetDataPlugin

        public AssetBase GetAsset(UUID uuid)
        {
            return GetAsset(uuid, true);
        }

        public AssetBase GetMeta(UUID uuid)
        {
            IDataReader dr = null;
            try
            {
                dr = m_Gd.QueryData("where id = '" + uuid + "' LIMIT 1", "assets",
                                    "id, name, description, assetType, local, temporary, asset_flags, creatorID");
                while (dr.Read())
                {
                    return LoadAssetFromDataRead(dr);
                }
                MainConsole.Instance.Warn("[LocalAssetDatabase] GetMeta(" + uuid + ") - Asset " + uuid + " was not found.");
            }
            catch (Exception e)
            {
                MainConsole.Instance.Error("[LocalAssetDatabase]: Failed to fetch asset " + uuid + ", " + e);
            }
            finally
            {
                if (dr != null) dr.Close();
            }
            return null;
        }

        public UUID Store(AssetBase asset)
        {
            StoreAsset(asset);
            return asset.ID;
        }

        public bool StoreAsset(AssetBase asset)
        {
            try
            {
                if (asset.Name.Length > 64)
                    asset.Name = asset.Name.Substring(0, 64);
                if (asset.Description.Length > 128)
                    asset.Description = asset.Description.Substring(0, 128);
                if (ExistsAsset(asset.ID))
                {
                    AssetBase oldAsset = GetAsset(asset.ID);
                    if (oldAsset == null || (oldAsset.Flags & AssetFlags.Rewritable) == AssetFlags.Rewritable)
                    {
                        MainConsole.Instance.Debug("[LocalAssetDatabase]: Asset already exists in the db, overwriting - " + asset.ID);
                        Delete(asset.ID, true);
                        InsertAsset(asset, asset.ID);
                    }
                    else
                    {
                        MainConsole.Instance.Warn("[LocalAssetDatabase]: Asset already exists in the db, fixing ID... - " + asset.ID);
                        InsertAsset(asset, UUID.Random());
                    }
                }
                else
                {
                    InsertAsset(asset, asset.ID);
                }
            }
            catch (Exception e)
            {
                MainConsole.Instance.ErrorFormat("[LocalAssetDatabase]: Failure creating asset {0} with name \"{1}\". Error: {2}",
                                  asset.ID, asset.Name, e);
            }
            return true;
        }

        public void UpdateContent(UUID id, byte[] asset, out UUID newID)
        {
            newID = UUID.Zero;

            AssetBase oldAsset = GetAsset(id);
            if (oldAsset == null)
                return;

            if ((oldAsset.Flags & AssetFlags.Rewritable) == AssetFlags.Rewritable)
            {
                try
                {
                    Dictionary<string, object> values = new Dictionary<string, object>(1);
                    values["data"] = asset;

                    QueryFilter filter = new QueryFilter();
                    filter.andFilters["id"] = id;

                    m_Gd.Update("assets", values, null, filter, null, null);
                }
                catch (Exception e)
                {
{'fraction_non_alphanumeric': 0.09479434447300772, 'fraction_numerical': 0.0035347043701799484, 'mean_word_length': 1.3977914740626605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6037812', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2212, 'n_words': 502}