public void Save(IndexValue indexValue) { var result = _indexMapper.MapEntry(indexValue); foreach (var doc in result) { Save(doc); } }
/// <summary> /// Meant for mapping the root IndexValue (and all the stuff below it) /// </summary> /// <param name="indexValue"></param> /// <returns>List of BsonDocuments, one for the root and one for each contained index in it.</returns> public List<BsonDocument> MapEntry(IndexValue indexValue) { var result = new List<BsonDocument>(); if (indexValue.Name == "root") { EntryToDocument(indexValue, 0, result); return result; } else throw new ArgumentException("MapEntry is only meant for mapping a root IndexValue.", "indexValue"); }
private BsonElement IndexValueToElement(IndexValue indexValue) { if (indexValue.Name == "_id") indexValue.Name = "fhir_id"; //_id is reserved in Mongo for the primary key and must be unique. if (indexValue.Values.Count == 1) { return new BsonElement(indexValue.Name, Map(indexValue.Values[0])); } BsonArray values = new BsonArray(); foreach (var value in indexValue.Values) { values.Add(Map(value)); } return new BsonElement(indexValue.Name, values); }
private void EntryToDocument(IndexValue indexValue, int level, List<BsonDocument> result) { //Add the real values (not contained) to a document and add that to the result. List<IndexValue> notNestedValues = indexValue.Values.Where(exp => (exp is IndexValue) && ((IndexValue)exp).Name != "contained").Select(exp => (IndexValue)exp).ToList(); var doc = new BsonDocument(new BsonElement(InternalField.LEVEL, level)); doc.AddRange(notNestedValues.Select(iv => IndexValueToElement(iv))); result.Add(doc); //Then do that recursively for all contained indexed resources. List<IndexValue> containedValues = indexValue.Values.Where(exp => (exp is IndexValue) && ((IndexValue)exp).Name == "contained").Select(exp => (IndexValue)exp).ToList(); foreach (var contained in containedValues) { EntryToDocument(contained, level + 1, result); } }
public void TestMapRootIndexValue() { //"root" element should be skipped. IndexValue iv = new IndexValue("root"); iv.Values.Add(new IndexValue("internal_resource", new StringValue("Patient"))); var results = sut.MapEntry(iv); Assert.AreEqual(1, results.Count); var result = results[0]; Assert.IsTrue(result.IsBsonDocument); Assert.AreEqual(2, result.AsBsonDocument.ElementCount); var firstElement = result.AsBsonDocument.GetElement(0); Assert.AreEqual("internal_level", firstElement.Name); var secondElement = result.GetElement(1); Assert.AreEqual("internal_resource", secondElement.Name); Assert.IsTrue(secondElement.Value.IsString); Assert.AreEqual("Patient", secondElement.Value.AsString); }
private BsonValue MapExpression(IndexValue indexValue) { return new BsonDocument(IndexValueToElement(indexValue)); }
public static IEnumerable <IndexValue> IndexValues(this IndexValue root) { return(root.Values.Where(v => v is IndexValue).Select(v => (IndexValue)v)); }
private void AddContainedResources(DomainResource resource, IndexValue parent) { parent.Values.AddRange(resource.Contained.Where(c => c is DomainResource).Select( c => { IKey containedKey = c.ExtractKey(); //containedKey.ResourceId = key.ResourceId + "#" + c.Id; return IndexResourceRecursively((c as DomainResource), containedKey, "contained"); })); }
private void AddMetaParts(Resource resource, IKey key, IndexValue entry) { entry.Values.Add(new IndexValue("internal_forResource", new StringValue(key.ToUriString()))); entry.Values.Add(new IndexValue(IndexFieldNames.RESOURCE, new StringValue(resource.TypeName))); entry.Values.Add(new IndexValue(IndexFieldNames.ID, new StringValue(resource.TypeName + "/" + key.ResourceId))); entry.Values.Add(new IndexValue(IndexFieldNames.JUSTID, new StringValue(resource.Id))); entry.Values.Add(new IndexValue(IndexFieldNames.SELFLINK, new StringValue(key.ToUriString()))); //CK TODO: This is actually Mongo-specific. Move it to Spark.Mongo, but then you will have to communicate the key to the MongoIndexMapper. //var fdt = resource.Meta?.LastUpdated != null ? new FhirDateTime(resource.Meta.LastUpdated.Value) : FhirDateTime.Now(); //entry.Values.Add(new IndexValue(IndexFieldNames.LASTUPDATED, (_elementIndexer.Map(fdt)))); }
private IndexValue IndexResourceRecursively(Resource resource, IKey key, string rootPartName = "root") { var searchParametersForResource = _fhirModel.FindSearchParameters(resource.GetType()); if(searchParametersForResource != null) { var result = new IndexValue(rootPartName); AddMetaParts(resource, key, result); foreach (var par in searchParametersForResource) { var newIndexPart = new IndexValue(par.Code); foreach (var path in par.GetPropertyPath()) _resourceVisitor.VisitByPath(resource, obj => { if (obj is Element) { newIndexPart.Values.AddRange(_elementIndexer.Map(obj as Element)); } } , path); if (newIndexPart.Values.Any()) { result.Values.Add(newIndexPart); } } if (resource is DomainResource) AddContainedResources((DomainResource)resource, result); return result; } return null; }