Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/ayende/ravendb into RavenDB-42
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkadiusz Palinski committed Feb 5, 2013
2 parents fdb4490 + f2797d3 commit aeaac08
Show file tree
Hide file tree
Showing 24 changed files with 712 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Raven.Client.Lightweight/Connection/SerializationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static IEnumerable<JsonDocument> RavenJObjectsToJsonDocuments(IEnumerable
Key = key,
LastModified = lastModified,
Etag = etag,
TempIndexScore = metadata == null ? null : metadata.Value<float?>("Temp-Index-Score"),
TempIndexScore = metadata == null ? null : metadata.Value<float?>(Constants.TemporaryScoreValue),
NonAuthoritativeInformation = nai,
Metadata = metadata.FilterHeaders(),
DataAsJson = doc,
Expand Down
22 changes: 17 additions & 5 deletions Raven.Client.Lightweight/Document/AbstractDocumentQuery.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.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
Expand Down Expand Up @@ -1027,24 +1028,35 @@ public void WhereIn(string fieldName, IEnumerable<object> values)
.Append(">:(");

var first = true;
AddItemToInClause(fieldName, list, first);
queryText.Append(") ");
}

private void AddItemToInClause(string fieldName, IEnumerable<object> list, bool first)
{
foreach (var value in list)
{
if(first == false)
var enumerable = value as IEnumerable;
if (enumerable != null && value is string == false)
{
AddItemToInClause(fieldName, enumerable.Cast<object>(), first);
return;
}
if (first == false)
{
queryText.Append(",");
}
first = false;
var whereParams = new WhereParams
{
AllowWildcards = true,
IsAnalyzed = true,
FieldName = fieldName,
AllowWildcards = true,
IsAnalyzed = true,
FieldName = fieldName,
Value = value
};
EnsureValidFieldName(whereParams);
queryText.Append(TransformToEqualValue(whereParams).Replace(",", "`,`"));
}
queryText.Append(") ");
}

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion Raven.Client.Lightweight/Document/EntityToJson.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Raven.Abstractions.Data;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.Linq;
Expand Down Expand Up @@ -158,6 +159,13 @@ private static void TrySimplifyingJson(RavenJObject jObject, JsonContract contra
}
}

private static Regex arrayEndRegex = new Regex(@"\[\], [\w\.-]+$",
#if !SILVERLIGHT
RegexOptions.Compiled
#else
RegexOptions.None
#endif
);
private static bool ShouldSimplifyJsonBasedOnType(string typeValue, JsonProperty jsonProperty)
{
if (jsonProperty != null && (jsonProperty.TypeNameHandling == TypeNameHandling.All || jsonProperty.TypeNameHandling == TypeNameHandling.Arrays))
Expand All @@ -169,7 +177,7 @@ private static bool ShouldSimplifyJsonBasedOnType(string typeValue, JsonProperty
return true;
if (typeValue.StartsWith("System.Collections.Generic.Dictionary`2[["))
return true;
if (typeValue.EndsWith("[], mscorlib")) // array
if (arrayEndRegex.IsMatch(typeValue)) // array
return true;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public abstract class InMemoryDocumentSessionOperations : IDisposable
/// </summary>
protected readonly HashSet<string> knownMissingIds = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

private Dictionary<string, object> externalState;

public IDictionary<string, object> ExternalState
{
get { return externalState ?? (externalState = new Dictionary<string, object>()); }
}

#if !SILVERLIGHT
private bool hasEnlisted;
[ThreadStatic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public interface IAdvancedDocumentSessionOperations
/// </summary>
bool UseOptimisticConcurrency { get; set; }

/// <summary>
/// Allow extensions to provide additional state per session
/// </summary>
IDictionary<string, object> ExternalState { get; }

/// <summary>
/// Mark the entity as read only, change tracking won't apply
/// to such an entity. This can be done as an optimization step, so
Expand Down
9 changes: 8 additions & 1 deletion Raven.Database/DocumentDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public PutResult Put(string key, Guid? etag, RavenJObject document, RavenJObject
workContext.DocsPerSecIncreaseBy(1);
key = string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString() : key.Trim();
RemoveReservedProperties(document);
RemoveReservedProperties(metadata);
RemoveMetadataReservedProperties(metadata);
Guid newEtag = Guid.Empty;
lock (putSerialLock)
{
Expand Down Expand Up @@ -842,6 +842,13 @@ private void AssertDeleteOperationNotVetoed(string key, TransactionInformation t
}
}

private static void RemoveMetadataReservedProperties(RavenJObject metadata)
{
RemoveReservedProperties(metadata);
metadata.Remove("Raven-Last-Modified");
metadata.Remove("Last-Modified");
}

private static void RemoveReservedProperties(RavenJObject document)
{
document.Remove(string.Empty);
Expand Down
4 changes: 3 additions & 1 deletion Raven.Database/Extensions/IndexingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ public static Field.Store GetStorage(this IndexDefinition self, string name, Fie

public static Sort GetSort(this IndexQuery self, IndexDefinition indexDefinition)
{
var spatialQuery = self as SpatialIndexQuery;
if (self.SortedFields == null || self.SortedFields.Length <= 0)
{
return null;
}

var spatialQuery = self as SpatialIndexQuery;

return new Sort(self.SortedFields
.Select(sortedField =>
Expand Down
2 changes: 2 additions & 0 deletions Raven.Database/Impl/DocumentRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ private JsonDocument RetrieveDocumentInternal(
return null;
var document = GetDocumentWithCaching(queryResult.Key);
if (document != null)
{
document.Metadata[Constants.TemporaryScoreValue] = queryScore;
}
return document;
}

Expand Down
15 changes: 8 additions & 7 deletions Raven.Database/Indexing/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Raven.Abstractions.MEF;
using Raven.Database.Data;
using Raven.Database.Extensions;
using Raven.Database.Indexing.Sorting;
using Raven.Database.Linq;
using Raven.Database.Plugins;
using Raven.Database.Storage;
Expand Down Expand Up @@ -243,11 +244,11 @@ public void MergeSegments()
public abstract void IndexDocuments(AbstractViewGenerator viewGenerator, IndexingBatch batch, IStorageActionsAccessor actions, DateTime minimumTimestamp);


protected virtual IndexQueryResult RetrieveDocument(Document document, FieldsToFetch fieldsToFetch, float score)
protected virtual IndexQueryResult RetrieveDocument(Document document, FieldsToFetch fieldsToFetch, ScoreDoc score)
{
return new IndexQueryResult
{
Score = score,
Score = score.Score,
Key = document.Get(Constants.DocumentIdFieldName),
Projection = fieldsToFetch.IsProjection ? CreateDocumentFromFields(document, fieldsToFetch) : null
};
Expand Down Expand Up @@ -862,7 +863,7 @@ public IEnumerable<IndexQueryResult> Query()
{
var scoreDoc = search.ScoreDocs[i];
var document = indexSearcher.Doc(scoreDoc.Doc);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, scoreDoc.Score);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, scoreDoc);
if (ShouldIncludeInResults(indexQueryResult) == false)
{
indexQuery.SkippedResults.Value++;
Expand Down Expand Up @@ -984,7 +985,7 @@ public IEnumerable<IndexQueryResult> IntersectionQuery()
for (int i = indexQuery.Start; i < intersectResults.Count && (i - indexQuery.Start) < pageSizeBestGuess; i++)
{
Document document = indexSearcher.Doc(intersectResults[i].LuceneId);
IndexQueryResult indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i].Score);
IndexQueryResult indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i]);
if (ShouldIncludeInResults(indexQueryResult) == false)
{
indexQuery.SkippedResults.Value++;
Expand Down Expand Up @@ -1033,7 +1034,7 @@ private void RecordResultsAlreadySeenForDistinctQuery(IndexSearcher indexSearche
for (int i = 0; i < min; i++)
{
Document document = indexSearcher.Doc(search.ScoreDocs[i].Doc);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i].Score);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i]);
alreadyReturned.Add(indexQueryResult.Projection);
}
}
Expand Down Expand Up @@ -1082,7 +1083,7 @@ public Query GetLuceneQuery()
if (spatialIndexQuery != null)
{
var spatialStrategy = parent.viewGenerator.GetStrategyForField(spatialIndexQuery.SpatialFieldName);
var dq = SpatialIndex.MakeQuery(spatialStrategy, spatialIndexQuery.QueryShape, spatialIndexQuery.SpatialRelation, spatialIndexQuery.DistanceErrorPercentage);
var dq = SpatialIndex.MakeQuery(q, spatialStrategy, spatialIndexQuery.QueryShape, spatialIndexQuery.SpatialRelation, spatialIndexQuery.DistanceErrorPercentage);
if (q is MatchAllDocsQuery) return dq;

var bq = new BooleanQuery { { q, Occur.MUST }, { dq, Occur.MUST } };
Expand Down Expand Up @@ -1241,7 +1242,7 @@ public int RecordResultsAlreadySeenForDistinctQuery(TopDocs search, bool adjustS
for (int i = 0; i < min; i++)
{
Document document = indexSearcher.Doc(search.ScoreDocs[i].Doc);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i].Score);
var indexQueryResult = parent.RetrieveDocument(document, fieldsToFetch, search.ScoreDocs[i]);
alreadyReturned.Add(indexQueryResult.Projection);
}
return 0;
Expand Down
3 changes: 2 additions & 1 deletion Raven.Database/Indexing/MapReduceIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Raven.Abstractions.Logging;
using Raven.Database.Extensions;
Expand Down Expand Up @@ -195,7 +196,7 @@ internal static string ReduceKeyToString(object reduceValue)
return RavenJToken.FromObject(reduceValue).ToString(Formatting.None);
}

protected override IndexQueryResult RetrieveDocument(Document document, FieldsToFetch fieldsToFetch, float score)
protected override IndexQueryResult RetrieveDocument(Document document, FieldsToFetch fieldsToFetch, ScoreDoc score)
{
if (fieldsToFetch.IsProjection == false)
fieldsToFetch = fieldsToFetch.CloneWith(document.GetFields().Select(x => x.Name).ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@

namespace Raven.Database.Indexing.Sorting
{
public class SpatialDistanceSortField : SortField
public struct DistanceValue : IComparable
{
private readonly Point center;

public SpatialDistanceSortField(string field, bool reverse, SpatialIndexQuery qry)
: base(field, CUSTOM, reverse)
{
var shape = SpatialIndex.ReadShape(qry.QueryShape);
center = shape.GetCenter();
}

public override FieldComparator GetComparator(int numHits, int sortPos)
public double Value;
public int CompareTo(object obj)
{
return new SpatialDistanceFieldComparatorSource.SpatialDistanceFieldComparator(center, numHits);
if (obj == null)
return 1;
return Value.CompareTo(((DistanceValue) obj).Value);
}

public override FieldComparatorSource ComparatorSource
{
get
{
return new SpatialDistanceFieldComparatorSource(center);
}
}
}

public class SpatialDistanceFieldComparatorSource : FieldComparatorSource
Expand All @@ -47,25 +33,25 @@ public override FieldComparator NewComparator(string fieldname, int numHits, int

public class SpatialDistanceFieldComparator : FieldComparator
{
private readonly double[] values;
private double bottom;
private readonly DistanceValue[] values;
private DistanceValue bottom;
private readonly Point originPt;

private IndexReader currentIndexReader;

public SpatialDistanceFieldComparator(Point origin, int numHits)
{
values = new double[numHits];
values = new DistanceValue[numHits];
originPt = origin;
}

public override int Compare(int slot1, int slot2)
{
double a = values[slot1];
double b = values[slot2];
if (a > b)
var a = values[slot1];
var b = values[slot2];
if (a.Value > b.Value)
return 1;
if (a < b)
if (a.Value < b.Value)
return -1;

return 0;
Expand All @@ -79,12 +65,12 @@ public override void SetBottom(int slot)
public override int CompareBottom(int doc)
{
var v2 = CalculateDistance(doc);
if (bottom > v2)
if (bottom.Value > v2)
{
return 1;
}

if (bottom < v2)
if (bottom.Value < v2)
{
return -1;
}
Expand All @@ -94,7 +80,10 @@ public override int CompareBottom(int doc)

public override void Copy(int slot, int doc)
{
values[slot] = CalculateDistance(doc);
values[slot] = new DistanceValue
{
Value = CalculateDistance(doc)
};
}

private double CalculateDistance(int doc)
Expand All @@ -116,6 +105,8 @@ private double CalculateDistance(int doc)
return double.NaN;
}
var pt = shape as Point;
if (pt == null)
pt = shape.GetCenter();
return SpatialIndex.Context.GetDistCalc().Distance(pt, originPt);
}

Expand Down
5 changes: 4 additions & 1 deletion Raven.Database/Indexing/SpatialIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.RegularExpressions;
using GeoAPI;
using Lucene.Net.Search;
using Lucene.Net.Search.Function;
using Lucene.Net.Spatial;
using Lucene.Net.Spatial.Prefix;
using Lucene.Net.Spatial.Prefix.Tree;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static SpatialStrategy CreateStrategy(string fieldName, SpatialSearchStra
return null;
}

public static Query MakeQuery(SpatialStrategy spatialStrategy, string shapeWKT, SpatialRelation relation,
public static Query MakeQuery(Query existingQuery, SpatialStrategy spatialStrategy, string shapeWKT, SpatialRelation relation,
double distanceErrorPct = 0.025)
{
SpatialOperation spatialOperation;
Expand Down Expand Up @@ -90,6 +91,8 @@ public static Query MakeQuery(SpatialStrategy spatialStrategy, string shapeWKT,
}
var args = new SpatialArgs(spatialOperation, shape) { DistErrPct = distanceErrorPct };

if(existingQuery is MatchAllDocsQuery)
return new CustomScoreQuery(spatialStrategy.MakeQuery(args), new ValueSourceQuery(spatialStrategy.MakeRecipDistanceValueSource(shape)));
return spatialStrategy.MakeQuery(args);
}

Expand Down
2 changes: 1 addition & 1 deletion Raven.Database/Raven.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@
<Compile Include="Indexing\Sorting\RandomFieldComparatorSource.cs" />
<Compile Include="Indexing\Sorting\RandomFieldComparator.cs" />
<Compile Include="Indexing\Sorting\RandomSortField.cs" />
<Compile Include="Indexing\Sorting\SpatialDistanceSortField.cs">
<Compile Include="Indexing\Sorting\SpatialDistanceFieldComparatorSource.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Json\ScriptedJsonPatcher.cs" />
Expand Down
Loading

0 comments on commit aeaac08

Please sign in to comment.