<commit_before>using System.IO;
using System.Linq;
using Baseline;
using Marten.Schema;
using Marten.Util;

namespace Marten.Storage
{
    public class DocumentTable : Table
    {
        public DocumentTable(DocumentMapping mapping) : base(mapping.Table)
        {
            var pgIdType = TypeMappings.GetPgType(mapping.IdMember.GetMemberType());
            AddPrimaryKey(new TableColumn("id", pgIdType));

            AddColumn("data", "jsonb", "NOT NULL");

            AddColumn<LastModifiedColumn>();
            AddColumn<VersionColumn>();
            AddColumn<DotNetTypeColumn>();

            foreach (var field in mapping.DuplicatedFields)
            {
                AddColumn(new DuplicatedFieldColumn(field));
            }

            if (mapping.IsHierarchy())
            {
                AddColumn(new DocumentTypeColumn(mapping));
            }

            if (mapping.DeleteStyle == DeleteStyle.SoftDelete)
            {
                AddColumn<DeletedColumn>();
                Indexes.Add(new IndexDefinition(mapping, DocumentMapping.DeletedColumn));
                AddColumn<DeletedAtColumn>();
            }

            if (mapping.TenancyStyle == TenancyStyle.Conjoined)
            {
                AddColumn<TenantIdColumn>();
            }

            Indexes.AddRange(mapping.Indexes);
            ForeignKeys.AddRange(mapping.ForeignKeys);
        }


        public string BuildTemplate(string template)
        {
            return template
                .Replace(DdlRules.SCHEMA, Identifier.Schema)
                .Replace(DdlRules.TABLENAME, Identifier.Name)
                .Replace(DdlRules.COLUMNS, _columns.Select(x => x.Name).Join(", "))
                .Replace(DdlRules.NON_ID_COLUMNS, _columns.Where(x => !x.Name.EqualsIgnoreCase("id")).Select(x => x.Name).Join(", "))
                .Replace(DdlRules.METADATA_COLUMNS, _columns.OfType<SystemColumn>().Select(x => x.Name).Join(", "));
        }

        public void WriteTemplate(DdlTemplate template, StringWriter writer)
        {
            var text = template?.TableCreation;
            if (text.IsNotEmpty())
            {
                writer.WriteLine();
                writer.WriteLine(BuildTemplate(text));
            }
        }<commit_msg>adding a default index on tenant id<commit_after>using System.IO;
using System.Linq;
using Baseline;
using Marten.Schema;
using Marten.Util;

namespace Marten.Storage
{
    public class DocumentTable : Table
    {
        public DocumentTable(DocumentMapping mapping) : base(mapping.Table)
        {
            var pgIdType = TypeMappings.GetPgType(mapping.IdMember.GetMemberType());
            AddPrimaryKey(new TableColumn("id", pgIdType));

            AddColumn("data", "jsonb", "NOT NULL");

            AddColumn<LastModifiedColumn>();
            AddColumn<VersionColumn>();
            AddColumn<DotNetTypeColumn>();

            foreach (var field in mapping.DuplicatedFields)
            {
                AddColumn(new DuplicatedFieldColumn(field));
            }

            if (mapping.IsHierarchy())
            {
                AddColumn(new DocumentTypeColumn(mapping));
            }

            if (mapping.DeleteStyle == DeleteStyle.SoftDelete)
            {
                AddColumn<DeletedColumn>();
                Indexes.Add(new IndexDefinition(mapping, DocumentMapping.DeletedColumn));
                AddColumn<DeletedAtColumn>();
            }

            if (mapping.TenancyStyle == TenancyStyle.Conjoined)
            {
                AddColumn<TenantIdColumn>();
                Indexes.Add(new IndexDefinition(mapping, TenantIdColumn.Name));
            }

            Indexes.AddRange(mapping.Indexes);
            ForeignKeys.AddRange(mapping.ForeignKeys);
        }


        public string BuildTemplate(string template)
        {
            return template
                .Replace(DdlRules.SCHEMA, Identifier.Schema)
                .Replace(DdlRules.TABLENAME, Identifier.Name)
                .Replace(DdlRules.COLUMNS, _columns.Select(x => x.Name).Join(", "))
                .Replace(DdlRules.NON_ID_COLUMNS, _columns.Where(x => !x.Name.EqualsIgnoreCase("id")).Select(x => x.Name).Join(", "))
                .Replace(DdlRules.METADATA_COLUMNS, _columns.OfType<SystemColumn>().Select(x => x.Name).Join(", "));
        }

        public void WriteTemplate(DdlTemplate template, StringWriter writer)
        {
            var text = template?.TableCreation;
            if (text.IsNotEmpty())
            {
                writer.WriteLine();
                writer.WriteLine(BuildTemplate(text));
            }
        }
{'fraction_non_alphanumeric': 0.10919292188174363, 'fraction_numerical': 0.0, 'mean_word_length': 1.9559948979591837, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, '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': '3728781', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1191, 'n_words': 191}