Skip to content

Commit

Permalink
Allowing to index byte arrays and arrays of byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
ayende committed Dec 14, 2011
1 parent 1cd5546 commit bcca1cc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ private IEnumerable<AbstractField> CreateFields(string name, object value, Index
yield return (AbstractField)value;
yield break;
}

if(value is byte[])
{
yield return CreateBinaryFieldWithCaching(name, (byte[])value, indexDefinition.GetStorage(name, defaultStorage));
yield break;
}

var itemsToIndex = value as IEnumerable;
if( itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex))
Expand Down Expand Up @@ -277,6 +281,23 @@ private static bool ShouldTreatAsEnumerable(IEnumerable itemsToIndex)

return true;
}

private Field CreateBinaryFieldWithCaching(string name, byte[] value, Field.Store store)
{
var cacheKey = new
{
name,
store,
multipleItemsSameFieldCountSum = multipleItemsSameFieldCount.Sum()
};
Field field;
if (fieldsCache.TryGetValue(cacheKey, out field) == false)
{
fieldsCache[cacheKey] = field = new Field(name, value, store);
}
field.SetValue(value);
return field;
}
private Field CreateFieldWithCaching(string name, string value, Field.Store store, Field.Index index)
{
var cacheKey = new
Expand Down
6 changes: 5 additions & 1 deletion Raven.Database/Indexing/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Composition;
Expand Down Expand Up @@ -168,7 +169,8 @@ private static RavenJObject CreateDocumentFromFields(Document document, IEnumera
{
return g.First();
}
return new KeyValuePair<string, RavenJToken>(g.Key, new RavenJArray(g.Select(x => x.Value)));
var ravenJTokens = g.Select(x => x.Value).ToArray();
return new KeyValuePair<string, RavenJToken>(g.Key, new RavenJArray((IEnumerable)ravenJTokens));
});
foreach (var keyValuePair in q)
{
Expand All @@ -179,6 +181,8 @@ private static RavenJObject CreateDocumentFromFields(Document document, IEnumera

private static KeyValuePair<string, RavenJToken> CreateProperty(Field fld, Document document)
{
if(fld.IsBinary())
return new KeyValuePair<string, RavenJToken>(fld.Name(), fld.GetBinaryValue());
var stringValue = fld.StringValue();
if (document.GetField(fld.Name() + "_ConvertToJson") != null)
{
Expand Down
92 changes: 92 additions & 0 deletions Raven.Tests/MailingList/Vitaly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Linq;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
using Xunit;

namespace Raven.Tests.MailingList
{
public class Vitaly
{
public class ActivityShot
{
public byte[] Thumbnail { get; set; }

public DateTimeOffset Edited { get; set; }
}

public class DailyActivity
{
public DateTime Date { get; set; }

public byte[][] Thumbnails
{
get;
set;
}
}


public class DailyActivityIndex : AbstractIndexCreationTask<ActivityShot, DailyActivity>
{
public DailyActivityIndex()
{
Map = activityShots => from shot in activityShots
select new
{
Date = shot.Edited.Date,
Thumbnails = new byte[][] { shot.Thumbnail }
};

Reduce = results => from result in results
group result by result.Date into g
select new
{
Date = g.Key,
Thumbnails = from dailyActivity in g
from thumbnail in dailyActivity.Thumbnails
select thumbnail
};
}

}

[Fact]
public void Test()
{
var activityShot1 = new ActivityShot
{
Edited = new DateTime(2011, 1, 1),
Thumbnail = new byte[] { 1 }
};

var activityShot2 = new ActivityShot
{
Edited = new DateTime(2011, 10, 10),
Thumbnail = new byte[] { 2 }
};

using (var documentStore = new EmbeddableDocumentStore
{
RunInMemory = true
}.Initialize())
{
new DailyActivityIndex().Execute(documentStore);

using (var session = documentStore.OpenSession())
{
session.Store(activityShot1);
session.Store(activityShot2);

session.SaveChanges();
}

using (var session = documentStore.OpenSession())
{
session.Query<DailyActivity, DailyActivityIndex>().Customize(x => x.WaitForNonStaleResults()).ToArray();
}
}

}
}
}
1 change: 1 addition & 0 deletions Raven.Tests/Raven.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@
<Compile Include="MailingList\Stats.cs" />
<Compile Include="MailingList\TimeZoneQueries.cs" />
<Compile Include="MailingList\transformedresults_customid_test.cs" />
<Compile Include="MailingList\Vitaly.cs" />
<Compile Include="MailingList\Vlad.cs" />
<Compile Include="ManagedStorage\Attachments.cs">
<SubType>Code</SubType>
Expand Down

0 comments on commit bcca1cc

Please sign in to comment.