public BlittableObjectProperty(BlittableObjectInstance parent, string property) : base(null, true, null, null) { _parent = parent; _property = property; var index = _parent.Blittable?.GetPropertyIndex(_property); if (index == null || index == -1) { if (_parent.LuceneDocument != null) { // if it isn't on the document, check if it is stored in the index? var fieldType = QueryResultRetrieverBase.GetFieldType(property, _parent.LuceneDocument); var values = _parent.LuceneDocument.GetValues(property, _parent.LuceneState); if (fieldType.IsArray) { Value = JsValue.FromObject(_parent.Engine, values); } else if (values.Length == 1) { Value = fieldType.IsJson ? new JsonParser(_parent.Engine).Parse(values[0]) : new JsValue(values[0]); } else { Value = JsValue.Undefined; } } else { Value = JsValue.Undefined; } } else { Value = GetPropertyValue(_property, index.Value); } }
private bool TryGetValueFromLucene(BlittableObjectInstance parent, string property, out JsValue value) { value = null; if (parent.LuceneDocument == null || parent.LuceneIndexFields == null) { return(false); } if (parent.LuceneIndexFields.TryGetValue(_property, out var indexField) == false && parent.LuceneAnyDynamicIndexFields == false) { return(false); } if (indexField != null && indexField.Storage == FieldStorage.No) { return(false); } var fieldType = QueryResultRetrieverBase.GetFieldType(property, parent.LuceneDocument); if (fieldType.IsArray) { // here we need to perform a manipulation in order to generate the object from the data if (fieldType.IsJson) { Lucene.Net.Documents.Field[] propertyFields = parent.LuceneDocument.GetFields(property); JsValue[] arrayItems = new JsValue[propertyFields.Length]; for (int i = 0; i < propertyFields.Length; i++) { var field = propertyFields[i]; var stringValue = field.StringValue(parent.LuceneState); var itemAsBlittable = parent.Blittable._context.ReadForMemory(stringValue, field.Name); arrayItems[i] = TranslateToJs(parent, field.Name, BlittableJsonToken.StartObject, itemAsBlittable); } value = FromObject(parent.Engine, arrayItems); return(true); } var values = parent.LuceneDocument.GetValues(property, parent.LuceneState); value = FromObject(parent.Engine, values); return(true); } var fieldable = _parent.LuceneDocument.GetFieldable(property); if (fieldable == null) { return(false); } var val = fieldable.StringValue(_parent.LuceneState); if (fieldType.IsJson) { BlittableJsonReaderObject valueAsBlittable = parent.Blittable._context.ReadForMemory(val, property); value = TranslateToJs(parent, property, BlittableJsonToken.StartObject, valueAsBlittable); return(true); } if (fieldable.IsTokenized == false) { // NULL_VALUE and EMPTY_STRING fields aren't tokenized // this will prevent converting fields with a "NULL_VALUE" string to null switch (val) { case Client.Constants.Documents.Indexing.Fields.NullValue: value = JsValue.Null; return(true); case Client.Constants.Documents.Indexing.Fields.EmptyString: value = string.Empty; return(true); } } if (fieldType.IsNumeric) { if (long.TryParse(val, out var valueAsLong)) { value = valueAsLong; } else if (double.TryParse(val, out var valueAsDouble)) { value = valueAsDouble; } else { throw new InvalidOperationException($"Recognized field '{property}' as numeric but was unable to parse its value to 'long' or 'double'. " + $"documentId = '{parent.DocumentId}', value = {val}."); } } else { value = val; } return(true); }
public BlittableObjectProperty(BlittableObjectInstance parent, string property) : base(PropertyFlag.CustomJsValue | PropertyFlag.Writable | PropertyFlag.WritableSet | PropertyFlag.Enumerable | PropertyFlag.EnumerableSet) { _parent = parent; _property = property; var index = _parent.Blittable?.GetPropertyIndex(_property); if (index == null || index == -1) { if (_parent.LuceneDocument != null) { // if it isn't on the document, check if it is stored in the index? var fieldType = QueryResultRetrieverBase.GetFieldType(property, _parent.LuceneDocument); var values = _parent.LuceneDocument.GetValues(property, _parent.LuceneState); if (fieldType.IsArray) { // here we need to perform a manipulation in order to generate the object from the // data if (fieldType.IsJson) { Lucene.Net.Documents.Field[] propertyFields = _parent.LuceneDocument.GetFields(property); JsValue[] arrayItems = new JsValue[propertyFields.Length]; for (int i = 0; i < propertyFields.Length; i++) { var field = propertyFields[i]; var stringValue = field.StringValue(parent.LuceneState); var maxByteSize = Encodings.Utf8.GetByteCount(stringValue); var itemAsBlittable = parent.Blittable._context.ReadForMemory(stringValue, field.Name); arrayItems[i] = TranslateToJs(_parent, field.Name, BlittableJsonToken.StartObject, itemAsBlittable); } _value = JsValue.FromObject(_parent.Engine, arrayItems); } else { _value = JsValue.FromObject(_parent.Engine, values); } } else if (values.Length == 1) { if (fieldType.IsJson) { BlittableJsonReaderObject valueAsBlittable = parent.Blittable._context.ReadForMemory(values[0], property); _value = TranslateToJs(_parent, property, BlittableJsonToken.StartObject, valueAsBlittable); } else { var value = values[0]; switch (value) { case Client.Constants.Documents.Indexing.Fields.NullValue: value = null; break; case Client.Constants.Documents.Indexing.Fields.EmptyString: value = string.Empty; break; } _value = value; } } else { _value = JsValue.Undefined; } } else { _value = JsValue.Undefined; } } else { _value = GetPropertyValue(_property, index.Value); } }