Skip to content

Commit

Permalink
RavenDB-21957 Use IndexVersion.TimeTicksSupportInJavaScriptIndexes in…
Browse files Browse the repository at this point in the history
…stead of IndexVersion.CurrentVersion to check it ticks are supported by JS index and move the detection to Jint converters only. Avoid passing parameter after out parameter.
  • Loading branch information
arekpalinski committed Jan 17, 2024
1 parent 3fa0645 commit a9ed8f1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public abstract class ConverterBase : IDisposable
protected readonly bool _storeValue;
protected readonly string _storeValueFieldName;
protected readonly int _numberOfBaseFields;
protected readonly bool _javascriptTicksSupport;

protected ConverterBase(Index index, bool storeValue, bool indexImplicitNull, bool indexEmptyEntries, int numberOfBaseFields, string keyFieldName,
string storeValueFieldName, ICollection<IndexField> fields = null)
Expand All @@ -51,8 +50,6 @@ protected ConverterBase(Index index, bool storeValue, bool indexImplicitNull, bo
foreach (var field in fields)
dictionary[field.Name] = field;
_fields = dictionary;

_javascriptTicksSupport = index.Type.IsJavaScript() && index.Definition.Version >= IndexDefinitionBaseServerSide.IndexVersion.CurrentVersion;
}

protected static bool IsArrayOfTypeValueObject(BlittableJsonReaderObject val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ protected void InsertRegularField(IndexField field, object value, JsonOperationC
return;

case ValueType.ConvertToJson:
var val = TypeConverter.ToBlittableSupportedType(value, supportJsStringToDateConversions: _javascriptTicksSupport);
var val = TypeConverter.ToBlittableSupportedType(value);
if (val is not DynamicJsonValue json)
{
InsertRegularField(field, val, indexContext, ref entryWriter, scope, out shouldSkip, nestedArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public CoraxJintDocumentConverter(MapReduceIndex index, bool storeValue = false)
public abstract class CoraxJintDocumentConverterBase : CoraxDocumentConverterBase
{
private readonly IndexFieldOptions _allFields;

private readonly bool _ticksSupport;

protected CoraxJintDocumentConverterBase(Index index, IndexDefinition definition, bool storeValue, int numberOfBaseFields, string keyFieldName,
string storeValueFieldName, ICollection<IndexField> fields = null, bool canContainSourceDocumentId = false) :
base(index, storeValue, index.Configuration.IndexMissingFieldsAsNull, index.Configuration.IndexEmptyEntries, numberOfBaseFields, keyFieldName, storeValueFieldName, canContainSourceDocumentId, fields) //todo
{
definition.Fields.TryGetValue(Constants.Documents.Indexing.Fields.AllFields, out _allFields);

_ticksSupport = index.Type.IsJavaScript() && index.Definition.Version >= IndexDefinitionBaseServerSide.IndexVersion.TimeTicksSupportInJavaScriptIndexes;
}

public override ByteStringContext<ByteStringMemoryCache>.InternalScope SetDocumentFields(LazyStringValue key, LazyStringValue sourceDocumentId, object doc, JsonOperationContext indexContext, out LazyStringValue id, out ByteString output, out float? documentBoost)
Expand Down Expand Up @@ -127,8 +129,8 @@ public override ByteStringContext<ByteStringMemoryCache>.InternalScope SetDocume

void ProcessAsJson(JsValue actualValue, IndexField field, ref CoraxLib.IndexEntryWriter entryWriter, IWriterScope writerScope, ObjectInstance documentToProcess, out bool shouldSkip)
{
var value = TypeConverter.ToBlittableSupportedType(actualValue, flattenArrays: false, forIndexing: true, engine: documentToProcess.Engine,
context: indexContext, supportJsStringToDateConversions: _javascriptTicksSupport);
var value = TypeConverter.ToBlittableSupportedType(actualValue, flattenArrays: false, forIndexing: true, canTryJsStringToDateConversion: _ticksSupport,
engine: documentToProcess.Engine, context: indexContext);
InsertRegularField(field, value, indexContext, ref entryWriter, writerScope, out shouldSkip);
}

Expand Down Expand Up @@ -182,8 +184,8 @@ void ProcessObject(JsValue valueToInsert, in string propertyAsString, IndexField
}
else
{
value = TypeConverter.ToBlittableSupportedType(val, flattenArrays: false, forIndexing: true, engine: documentToProcess.Engine,
context: indexContext, supportJsStringToDateConversions: _javascriptTicksSupport);
value = TypeConverter.ToBlittableSupportedType(val, flattenArrays: false, forIndexing: true,
canTryJsStringToDateConversion: _ticksSupport, engine: documentToProcess.Engine, context: indexContext);

InsertRegularField(field, value, indexContext, ref entryWriter, writerScope, out shouldSkip);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ public abstract class JintLuceneDocumentConverterBase : LuceneDocumentConverterB
{
private readonly IndexFieldOptions _allFields;

protected readonly bool _ticksSupport;

protected JintLuceneDocumentConverterBase(Index index, IndexDefinition indexDefinition, int numberOfBaseFields = 1, string keyFieldName = null,
bool storeValue = false, string storeValueFieldName = Constants.Documents.Indexing.Fields.ReduceKeyValueFieldName)
: base(index, index.Configuration.IndexEmptyEntries, numberOfBaseFields, keyFieldName, storeValue, storeValueFieldName)
{
indexDefinition.Fields.TryGetValue(Constants.Documents.Indexing.Fields.AllFields, out _allFields);

_ticksSupport = index.Type.IsJavaScript() && index.Definition.Version >= IndexDefinitionBaseServerSide.IndexVersion.TimeTicksSupportInJavaScriptIndexes;
}

protected override int GetFields<T>(T instance, LazyStringValue key, LazyStringValue sourceDocumentId, object document, JsonOperationContext indexContext,
Expand Down Expand Up @@ -164,8 +168,8 @@ void ProcessObject(JsValue valueToInsert, in string propertyAsString, IndexField
}
else
{
value = TypeConverter.ToBlittableSupportedType(val, flattenArrays: false, forIndexing: true, engine: documentToProcess.Engine,
context: indexContext, supportJsStringToDateConversions: _javascriptTicksSupport);
value = TypeConverter.ToBlittableSupportedType(val, flattenArrays: false, forIndexing: true, canTryJsStringToDateConversion: _ticksSupport, engine: documentToProcess.Engine,
context: indexContext);
numberOfCreatedFields = GetRegularFields(instance, field, CreateValueForIndexing(value, propertyBoost), indexContext, sourceDocument, out _);

newFields += numberOfCreatedFields;
Expand Down Expand Up @@ -231,8 +235,8 @@ void ProcessObject(JsValue valueToInsert, in string propertyAsString, IndexField

int ProcessAsJson(JsValue actualValue, IndexField field, float? propertyBoost)
{
var value = TypeConverter.ToBlittableSupportedType(actualValue, flattenArrays: false, forIndexing: true, engine: documentToProcess.Engine,
context: indexContext, supportJsStringToDateConversions: _javascriptTicksSupport);
var value = TypeConverter.ToBlittableSupportedType(actualValue, flattenArrays: false, forIndexing: true, canTryJsStringToDateConversion: _ticksSupport, engine: documentToProcess.Engine,
context: indexContext);
return GetRegularFields(instance, field, CreateValueForIndexing(value, propertyBoost), indexContext, sourceDocument, out _);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ int HandleObject(BlittableJsonReaderObject val)

if (valueType == ValueType.ConvertToJson)
{
var val = TypeConverter.ToBlittableSupportedType(value, supportJsStringToDateConversions: _javascriptTicksSupport);
var val = TypeConverter.ToBlittableSupportedType(value);
if (!(val is DynamicJsonValue json))
{
return GetRegularFields(instance, field, val, indexContext, sourceDocument, out _, nestedArray: nestedArray);
Expand Down
36 changes: 18 additions & 18 deletions src/Raven.Server/Utils/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ public static bool IsSupportedType(object value)
return UnlikelyIsSupportedType(value);
}

public static object ToBlittableSupportedType(object value, bool flattenArrays = false, bool forIndexing = false, Engine engine = null, JsonOperationContext context = null, bool supportJsStringToDateConversions = false)
public static object ToBlittableSupportedType(object value, bool flattenArrays = false, bool forIndexing = false, bool canTryJsStringToDateConversion = true, Engine engine = null, JsonOperationContext context = null)
{
return ToBlittableSupportedType(value, value, flattenArrays, forIndexing, 0, engine, context, out _, supportJsStringToDateConversions: supportJsStringToDateConversions);
return ToBlittableSupportedType(value, value, flattenArrays, forIndexing, 0, canTryJsStringToDateConversion, engine, context, out _);
}

public static object ToBlittableSupportedType(object value, out BlittableSupportedReturnType returnType, bool flattenArrays = false, bool forIndexing = false, Engine engine = null, JsonOperationContext context = null, bool supportJsStringToDateConversions = false)
public static object ToBlittableSupportedType(object value, out BlittableSupportedReturnType returnType, bool flattenArrays = false, bool forIndexing = false, bool canTryJsStringToDateConversion = true, Engine engine = null, JsonOperationContext context = null)
{
return ToBlittableSupportedType(value, value, flattenArrays, forIndexing, 0, engine, context, out returnType, supportJsStringToDateConversions: supportJsStringToDateConversions);
return ToBlittableSupportedType(value, value, flattenArrays, forIndexing, 0, canTryJsStringToDateConversion, engine, context, out returnType);
}

public enum BlittableSupportedReturnType : int
Expand Down Expand Up @@ -241,7 +241,7 @@ private static BlittableSupportedReturnType DoBlittableSupportedTypeInternal(Typ
return BlittableSupportedReturnType.Runtime;
}

private static object ToBlittableSupportedType(object root, object value, bool flattenArrays, bool forIndexing, int recursiveLevel, Engine engine, JsonOperationContext context, out BlittableSupportedReturnType blittableSupportedReturnType, bool supportJsStringToDateConversions)
private static object ToBlittableSupportedType(object root, object value, bool flattenArrays, bool forIndexing, int recursiveLevel, bool canTryJsStringToDateConversion, Engine engine, JsonOperationContext context, out BlittableSupportedReturnType blittableSupportedReturnType)
{
RuntimeHelpers.EnsureSufficientExecutionStack();
if (recursiveLevel > MaxAllowedRecursiveLevelForType)
Expand Down Expand Up @@ -288,7 +288,7 @@ private static object ToBlittableSupportedType(object root, object value, bool f
{
var jsString = js.AsString();

if (supportJsStringToDateConversions && TryConvertStringValue(jsString, out var jsDate))
if (canTryJsStringToDateConversion && TryConvertStringValue(jsString, out var jsDate))
return jsDate;

return jsString;
Expand Down Expand Up @@ -320,7 +320,7 @@ private static object ToBlittableSupportedType(object root, object value, bool f
else if (js.IsArray())
{
var arr = js.AsArray();
var convertedArray = EnumerateArray(root, arr, flattenArrays, forIndexing, recursiveLevel + 1, engine, context, supportJsStringToDateConversions);
var convertedArray = EnumerateArray(root, arr, flattenArrays, forIndexing, recursiveLevel + 1, canTryJsStringToDateConversion, engine, context);
return new DynamicJsonArray(flattenArrays ? Flatten(convertedArray) : convertedArray);
}
else if (js.IsObject())
Expand All @@ -343,7 +343,7 @@ private static object ToBlittableSupportedType(object root, object value, bool f

var enumerable = (IEnumerable)value;
if (ShouldTreatAsEnumerable(enumerable))
return EnumerableToJsonArray(flattenArrays ? Flatten(enumerable) : enumerable, root, flattenArrays, forIndexing, recursiveLevel, engine, context, supportJsStringToDateConversions);
return EnumerableToJsonArray(flattenArrays ? Flatten(enumerable) : enumerable, root, flattenArrays, forIndexing, recursiveLevel, canTryJsStringToDateConversion, engine, context);

break;
}
Expand All @@ -354,8 +354,8 @@ private static object ToBlittableSupportedType(object root, object value, bool f
var dictionary = (IDictionary)value;
foreach (var key in dictionary.Keys)
{
var keyAsString = KeyAsString(key: ToBlittableSupportedType(root, key, flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, engine: engine, context: context, out _, supportJsStringToDateConversions));
@object[keyAsString] = ToBlittableSupportedType(root, dictionary[key], flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, engine: engine, context: context, out _, supportJsStringToDateConversions);
var keyAsString = KeyAsString(key: ToBlittableSupportedType(root, key, flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, canTryJsStringToDateConversion, engine: engine, context: context, out _));
@object[keyAsString] = ToBlittableSupportedType(root, dictionary[key], flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, canTryJsStringToDateConversion, engine: engine, context: context, out _);
}

return @object;
Expand All @@ -367,8 +367,8 @@ private static object ToBlittableSupportedType(object root, object value, bool f
var dDictionary = (IDictionary<object, object>)value;
foreach (var key in dDictionary.Keys)
{
var keyAsString = KeyAsString(key: ToBlittableSupportedType(root, key, flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, engine: engine, context: context, out _, supportJsStringToDateConversions));
@object[keyAsString] = ToBlittableSupportedType(root, dDictionary[key], flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, engine: engine, context: context, out _, supportJsStringToDateConversions);
var keyAsString = KeyAsString(key: ToBlittableSupportedType(root, key, flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, canTryJsStringToDateConversion, engine: engine, context: context, out _));
@object[keyAsString] = ToBlittableSupportedType(root, dDictionary[key], flattenArrays, forIndexing, recursiveLevel: recursiveLevel + 1, canTryJsStringToDateConversion, engine: engine, context: context, out _);
}

return @object;
Expand All @@ -389,11 +389,11 @@ private static object ToBlittableSupportedType(object root, object value, bool f
var propertyValue = property.Value;
if (propertyValue is IEnumerable<object> propertyValueAsEnumerable && ShouldTreatAsEnumerable(propertyValue))
{
inner[property.Key] = EnumerableToJsonArray(propertyValueAsEnumerable, root, flattenArrays, forIndexing, recursiveLevel, engine, context, supportJsStringToDateConversions);
inner[property.Key] = EnumerableToJsonArray(propertyValueAsEnumerable, root, flattenArrays, forIndexing, recursiveLevel, canTryJsStringToDateConversion, engine, context);
continue;
}

inner[property.Key] = ToBlittableSupportedType(root, propertyValue, flattenArrays, forIndexing, recursiveLevel + 1, engine, context, out _, supportJsStringToDateConversions);
inner[property.Key] = ToBlittableSupportedType(root, propertyValue, flattenArrays, forIndexing, recursiveLevel + 1, canTryJsStringToDateConversion, engine, context, out _);
}

return inner;
Expand Down Expand Up @@ -435,23 +435,23 @@ private static void ThrowInvalidObject(JsValue jsValue)
throw new InvalidOperationException("Invalid type " + jsValue);
}

private static IEnumerable<object> EnumerateArray(object root, ArrayInstance arr, bool flattenArrays, bool forIndexing, int recursiveLevel, Engine engine, JsonOperationContext context, bool supportJsStringToDateConversions)
private static IEnumerable<object> EnumerateArray(object root, ArrayInstance arr, bool flattenArrays, bool forIndexing, int recursiveLevel, bool canTryJsStringToDateConversion, Engine engine, JsonOperationContext context)
{
foreach (var (key, val) in arr.GetOwnPropertiesWithoutLength())
{
yield return ToBlittableSupportedType(root, val.Value, flattenArrays, forIndexing, recursiveLevel, engine, context, out _, supportJsStringToDateConversions: supportJsStringToDateConversions);
yield return ToBlittableSupportedType(root, val.Value, flattenArrays, forIndexing, recursiveLevel, canTryJsStringToDateConversion, engine, context, out _);
}
}

private static DynamicJsonArray EnumerableToJsonArray(IEnumerable propertyEnumerable, object root, bool flattenArrays, bool forIndexing, int recursiveLevel, Engine engine, JsonOperationContext context, bool supportJsStringToDateConversions)
private static DynamicJsonArray EnumerableToJsonArray(IEnumerable propertyEnumerable, object root, bool flattenArrays, bool forIndexing, int recursiveLevel, bool canTryJsStringToDateConversion, Engine engine, JsonOperationContext context)
{
RuntimeHelpers.EnsureSufficientExecutionStack();

var dja = new DynamicJsonArray();

foreach (var x in propertyEnumerable)
{
dja.Add(ToBlittableSupportedType(root, x, flattenArrays, forIndexing, recursiveLevel + 1, engine, context, out _, supportJsStringToDateConversions: supportJsStringToDateConversions));
dja.Add(ToBlittableSupportedType(root, x, flattenArrays, forIndexing, recursiveLevel + 1, canTryJsStringToDateConversion, engine, context, out _));
}

return dja;
Expand Down

0 comments on commit a9ed8f1

Please sign in to comment.