Skip to content
Browse files

Fixing column length with big documents and indexes

  • Loading branch information...
1 parent 0bfe48e commit 1c9918fbf6e427e23de2353486767f6f6fee5a88 @sebastienros committed May 27, 2012
View
BIN YesSql.Samples.Performance120223.vsp
Binary file not shown.
View
1 src/YesSql.Core/Data/Mappings/DocumentAlteration.cs
@@ -38,6 +38,7 @@ public void Alter(AutoPersistenceModel model)
// mapping.Id(x => x.Id).GeneratedBy.HiLo("100");
mapping.Map(x => x.Type).Index("idx_document_type");
+ mapping.Map(x => x.Content).Length(4001);
foreach (var index in _hasDocumentsIndexes)
{
View
2 src/YesSql.Core/Data/Mappings/DocumentTypeSource.cs
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
-using System.Linq;
using FluentNHibernate;
using FluentNHibernate.Diagnostics;
using YesSql.Core.Data.Models;
-using YesSql.Core.Indexes;
namespace YesSql.Core.Data.Mappings
{
View
12 src/YesSql.Core/Data/Mappings/IndexAlteration.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Reflection;
using FluentNHibernate;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
@@ -42,6 +41,9 @@ public void Alter(AutoPersistenceModel model)
// automatically creates a database index on properties with the [Indexed] attribute
model.Conventions.Setup(s => s.Add<IndexedPropertyConvention>());
+
+ // configure the size of the column
+ model.Conventions.Setup(s => s.Add<StringColumnLengthConvention>());
}
}
@@ -66,5 +68,13 @@ protected override void Apply(IndexedAttribute attribute, IPropertyInstance inst
instance.Index("idx__" + instance.Property.Name);
}
}
+
+ public class StringColumnLengthConvention : AttributePropertyConvention<StringLengthAttribute>
+ {
+ protected override void Apply(StringLengthAttribute attribute, IPropertyInstance instance)
+ {
+ instance.Length(attribute.Length);
+ }
+ }
}
View
13 src/YesSql.Core/Data/Mappings/StringLengthAttribute.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace YesSql.Core.Data.Mappings {
+ public class StringLengthAttribute : Attribute
+ {
+ public StringLengthAttribute(int length)
+ {
+ Length = length;
+ }
+
+ public int Length { get; private set; }
+ }
+}
View
8 src/YesSql.Core/Data/Mappings/StringMaxLengthAttribute.cs
@@ -0,0 +1,8 @@
+namespace YesSql.Core.Data.Mappings {
+ public class StringMaxLengthAttribute : StringLengthAttribute
+ {
+ public StringMaxLengthAttribute() : base(4001)
+ {
+ }
+ }
+}
View
59 src/YesSql.Core/Data/MsSqlCeConfiguration.cs
@@ -1,23 +1,58 @@
-using FluentNHibernate.Cfg.Db;
+using System.Data;
+using System.Reflection;
+using FluentNHibernate.Cfg.Db;
using NHibernate.Dialect;
using NHibernate.Driver;
+using NHibernate.SqlTypes;
-namespace YesSql.Core.Data {
- public class MsSqlCeConfiguration : PersistenceConfiguration<MsSqlCeConfiguration> {
- protected MsSqlCeConfiguration() {
- Driver<SqlServerCeDriver>();
+namespace YesSql.Core.Data
+{
+ public class MsSqlCeConfiguration : PersistenceConfiguration<MsSqlCeConfiguration>
+ {
+ protected MsSqlCeConfiguration()
+ {
+ Driver<CustomSqlServerCeDriver>();
}
- public static MsSqlCeConfiguration MsSqlCe40 {
+ public static MsSqlCeConfiguration MsSqlCe40
+ {
get { return new MsSqlCeConfiguration().Dialect<CustomMsSqlCe40Dialect>(); }
+
}
- }
- public class CustomMsSqlCe40Dialect : MsSqlCe40Dialect
- {
- public override bool SupportsVariableLimit {
- get {
- return true;
+ /// <summary>
+ /// Custom driver so that Text/NText fields are not truncated at 4000 characters
+ /// </summary>
+ public class CustomSqlServerCeDriver : SqlServerCeDriver
+ {
+ protected override void InitializeParameter(IDbDataParameter dbParam, string name, SqlType sqlType)
+ {
+ base.InitializeParameter(dbParam, name, sqlType);
+
+ PropertyInfo dbParamSqlDbTypeProperty = dbParam.GetType().GetProperty("SqlDbType");
+
+ if (sqlType.Length <= 4000)
+ {
+ return;
+ }
+
+ switch (sqlType.DbType)
+ {
+ case DbType.String:
+ dbParamSqlDbTypeProperty.SetValue(dbParam, SqlDbType.NText, null);
+ break;
+ case DbType.AnsiString:
+ dbParamSqlDbTypeProperty.SetValue(dbParam, SqlDbType.Text, null);
+ break;
+ }
+ }
+ }
+
+ public class CustomMsSqlCe40Dialect : MsSqlCe40Dialect
+ {
+ public override bool SupportsVariableLimit
+ {
+ get { return true; }
}
}
}
View
12 src/YesSql.Core/Data/QueryExtensions.cs
@@ -5,20 +5,24 @@
using YesSql.Core.Services;
namespace YesSql.Core.Data {
- public static class QueryExtensions {
- public static IQuery<T> Query<T>(this ISession session) where T : class {
+ public static class QueryExtensions
+ {
+ public static IQuery<T> Query<T>(this ISession session) where T : class
+ {
return session.Query().For<T>();
}
public static IQuery<T, TIndex> Query<T, TIndex>(this ISession session)
where T : class
- where TIndex : IIndex {
+ where TIndex : IIndex
+ {
return session.Query().For<T>().With<TIndex>();
}
public static IQuery<T, TIndex> Query<T, TIndex>(this ISession session, Expression<Func<TIndex, bool>> predicate)
where T : class
- where TIndex : IIndex {
+ where TIndex : IIndex
+ {
return session.Query().For<T>().With<TIndex>(predicate);
}
View
24 src/YesSql.Core/Data/Session.cs
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -174,29 +173,34 @@ public Query.IQuery Query()
return new DefaultQuery(_session, this);
}
- public T As<T>(Document doc) where T : class {
- if (doc == null) {
+ public T As<T>(Document doc) where T : class
+ {
+ if (doc == null)
+ {
return null;
}
object cached;
- if (_identityMap.TryGetValue(doc.Id, out cached)) {
- return (T)cached;
+ if (_identityMap.TryGetValue(doc.Id, out cached))
+ {
+ return (T) cached;
}
var obj = _serializer.Deserialize(doc) as T;
- if (obj == null) {
- throw new ArgumentException("Document of type '" + doc.Type + "' cannot be deserialized as '" + typeof(T).Name + "'");
+ if (obj == null)
+ {
+ throw new ArgumentException("Document of type '" + doc.Type + "' cannot be deserialized as '" +
+ typeof (T).Name + "'");
}
_documents.Add(obj, doc.Id);
_identityMap.Add(doc.Id, obj);
return obj;
}
- public IEnumerable<T> As<T>(IEnumerable<Document> doc) where T : class {
-
+ public IEnumerable<T> As<T>(IEnumerable<Document> doc) where T : class
+ {
return doc.Select(As<T>);
}
@@ -360,7 +364,7 @@ private void Reduce()
}
/// <summary>
- /// Creates a Func&lt;IIndex, object&gt; dynamically, based on GroupKey attributes
+ /// Creates a Func{IIndex, object}; dynamically, based on GroupKey attributes
/// this function will be used as the keySelector for Linq.Grouping
/// </summary>
private Func<IIndex, object> GetGroupingMetod(IndexDescriptor descriptor)
View
2 src/YesSql.Core/Properties/AssemblyInfo.cs
@@ -5,5 +5,5 @@
[assembly: AssemblyCompany("Sébastien Ros")]
[assembly: AssemblyCopyright("Sébastien Ros")]
-[assembly: AssemblyVersion("0.3.1")]
+[assembly: AssemblyVersion("0.3.2")]
[assembly: AssemblyFileVersion("1.0.0.0")]
View
3 src/YesSql.Core/YesSql.Core.csproj
@@ -48,12 +48,15 @@
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Data" />
<Reference Include="System.Web.Extensions" />
</ItemGroup>
<ItemGroup>
<Compile Include="Data\DefaultIdentifierFactory.cs" />
<Compile Include="Data\IIdAccessor.cs" />
<Compile Include="Data\IIdentifierFactory.cs" />
+ <Compile Include="Data\Mappings\StringMaxLengthAttribute.cs" />
+ <Compile Include="Data\Mappings\StringLengthAttribute.cs" />
<Compile Include="Data\Mappings\DocumentAlteration.cs">
<SubType>Code</SubType>
</Compile>
View
16 src/YesSql.Tests/CoreTests.cs
@@ -797,5 +797,21 @@ public void ShouldSaveCustomObjectAsync()
}
}
+
+ [Fact]
+ public void ShouldSaveBigDocuments()
+ {
+ using(var session = _store.CreateSession())
+ {
+ var bill = new Person
+ {
+ Firstname = new String('x', 10000),
+ };
+
+
+ session.Save(bill);
+ session.Commit();
+ }
+ }
}
}
View
3 src/YesSql.Tests/Indexes/PersonByName.cs
@@ -1,5 +1,4 @@
-using System.Linq;
-using YesSql.Core.Indexes;
+using YesSql.Core.Indexes;
using YesSql.Tests.Models;
namespace YesSql.Tests.Indexes

0 comments on commit 1c9918f

Please sign in to comment.
Something went wrong with that request. Please try again.