public void PatchJson(string storeId, JToken token) { JArray inputs; if (token is JObject) { inputs = new JArray() { token }; } else if (token is JArray) { inputs = token as JArray; } else { throw new InvalidOperationException("Invalid patch"); } try { foreach (var input in inputs) { if (!(input is JObject)) { throw new InvalidOperationException("Invalid patch"); } var patch = TripleConverter.FromJson(input as JObject); var (data, _, _) = GetSetGraphs(storeId); var retract = new List<Triple>(); foreach (var triple in patch) { var t = data.SPI(triple.Subject, triple.Predicate, triple.Object.Index); if (t != null) { retract.Add(t); } } var assert = patch.Where(x => !x.Object.IsNull); data.BatchRetractAssert(retract, assert); } } catch (Exception e) { _logger.LogError("Patch JSON failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); throw e; } }
public void To_Patch() { var item = new { id = "100", name = "name101", contains = new { name = "name200", values = new object[] { new { age = (int?)null, } } } }; var json = JObject.FromObject(item); var graph = TripleConverter.FromJson(json); var expected = new List <Triple>(); expected.Add(new Triple("100", "name", TripleObject.FromData("name101"))); expected.Add(new Triple("100", "contains", "100#contains")); expected.Add(new Triple("100#contains", "name", TripleObject.FromData("name200"))); expected.Add(new Triple("100#contains", "values", "100#contains#values#0")); expected.Add(new Triple("100#contains#values#0", "age", TripleObject.FromData(null))); CollectionAssert.AreEqual(expected, graph.ToArray(), new UnorderedTripleComparer()); }
public void Patch_Single_Item_Array_With_Triple_Returns() { var doc = new { id = "100", name = "Device 100", contains = new object[] { new { id = "nested0", name = "Nested 0" } } }; var patch = new { remove = new { id = "100", otherName = "Other Name", contains = new object[] { new { id = "nested0", } } }, add = new { id = "100", contains = new object[] { new { id = "nested4", name = "Nested 4" } } } }; var expected = new { id = "100", name = "Device 100", contains = new object[] { new { id = "nested4", name = "Nested 4" } } }; StoreProcessor.Assert("app3", JObject.FromObject(doc), false); StoreProcessor.PatchTriple("app3", JObject.FromObject(patch)); var rsp = StoreProcessor.GetSubject("app3", "100", null, 3); var rspString = TripleConverter.FromJson(rsp).ToArray(); var expectedString = TripleConverter.FromJson(JObject.FromObject(expected)).ToArray(); CollectionAssert.AreEquivalent(rspString, expectedString); }
public JObject GetSet(string storeId) { using (var op = _storeOperationFactory.Write(storeId)) { var(data, _, _) = GetSetGraphs(storeId); return(TripleConverter.ToJson(data)); } }
public void Assert(string storeId, JToken input, bool strict) { try { JArray value; if (input is JObject) { value = new JArray(input); } else if (input is JArray) { value = (JArray)input; } else { throw _storeErrors.InvalidType; } var (data, _, _) = GetSetGraphs(storeId); foreach (var item in value) { if (!(item is JObject)) { throw _storeErrors.InvalidItem; } var jobj = (JObject)item; if (!jobj.ContainsKey(Constants.ID) || jobj[Constants.ID].Type != JTokenType.String) { throw _storeErrors.MustHaveId; } if (strict && data.S(jobj[Constants.ID].ToString()).Any()) { throw new StoreException($"Already contains object with id {jobj[Constants.ID]}", _storeErrors.AlreadyContainsIdError); } var graph = TripleConverter.FromJson((JObject)item); data.Assert(graph); } } catch (Exception e) { _logger.LogError("Assert failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); throw e; } }
public void Delete(string storeId, JToken token) { using (var op = _storeOperationFactory.Write(storeId)) { JArray inputs; try { if (token is JObject) { inputs = new JArray() { token }; } else if (token is JArray) { inputs = token as JArray; } else { throw new InvalidOperationException("Invalid delete"); } foreach (var input in inputs) { if (!(input is JObject)) { throw new InvalidOperationException("Invalid delete"); } var(data, _, _) = GetSetGraphs(storeId); var triples = TripleConverter.FromJson(input as JObject); data.Retract(triples); } } catch (Exception e) { _logger.LogError("Delete failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); } } }
public JObject Query(string storeId, JObject query, string[] expand, int level) { using (var op = _storeOperationFactory.Write(storeId)) { try { var(data, _, _) = GetSetGraphs(storeId); ObjectQueryModel queryModel; try { queryModel = query.ToObject <ObjectQueryModel>(); } catch (Exception e) { throw new StoreException(e.Message, _storeErrors.UnableToParseQuery); } var result = new ObjectQueryExecutor().Query(queryModel, data); dynamic response = new { values = result.Values?.Select(x => { var expanded = GraphOperator.Expand(data, x.Subject, level, expand); var rspGraph = new MemoryGraph(); rspGraph.Assert(expanded).ToList(); return(TripleConverter.ToJson(x.Subject, rspGraph)); }), continuation = result.Continuation, aggregates = result.Aggregates }; return(JObject.FromObject(response)); } catch (Exception e) { _logger.LogError("Query failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); throw e; } } }
public void AssertMeta(string storeId, JObject value) { using (var op = _storeOperationFactory.Write(storeId)) { var graph = TripleConverter.FromJson(value); var(data, infer, meta) = GetSetGraphs(storeId); _reasoner.Spin(data, infer, meta); } }
public void Patch_With_Values_Returns() { var doc = new { id = "100", name = "Device 100", data = new { temp = 50, humidity = 70, pressure = 1001.02, region = "seattle/1" } }; var patch = new { id = "100", data = new { temp = 55, pressure = 1001.03, region = "seattle/1" } }; var expected = new { id = "100", name = "Device 100", data = new { id = "100#data", temp = 55, humidity = 70, pressure = 1001.03, region = "seattle/1", }, }; StoreProcessor.Assert("app3", JObject.FromObject(doc), false); StoreProcessor.PatchJson("app3", JObject.FromObject(patch)); var rsp = StoreProcessor.GetSubject("app3", "100", null, 3); var rspString = TripleConverter.FromJson(rsp).ToArray(); var expectedString = TripleConverter.FromJson(JObject.FromObject(expected)).ToArray(); CollectionAssert.AreEquivalent(rspString, expectedString); }
public void PatchTriple(string storeId, JObject input) { try { var (data, _, _) = GetSetGraphs(storeId); var remove = input["remove"]; if (remove != null && remove is JObject) { var triples = TripleConverter.FromJson((JObject)remove); var toRemove = new List<Triple>(); foreach (var item in triples) { if (item.Object.Index == -1) { toRemove.Add(item); } else { var arrayMatches = data.SP(item.Subject, item.Predicate).Where(x => x.Object.Value == item.Object.Value && x.Object.IsID == item.Object.IsID); toRemove.AddRange(arrayMatches); } } data.Retract(toRemove); } var add = input["add"]; if (add != null && add is JObject) { var triples = TripleConverter.FromJson((JObject)add); var toAssert = new List<Triple>(); var spCounter = new Dictionary<string, Dictionary<string, int>>(); foreach (var item in triples) { if (item.Object.Index == -1) { toAssert.Add(item); } else { if (!spCounter.ContainsKey(item.Subject) || !spCounter[item.Subject].ContainsKey(item.Predicate)) { if (!spCounter.ContainsKey(item.Subject)) { spCounter[item.Subject] = new Dictionary<string, int>(); } if (!spCounter[item.Subject].ContainsKey(item.Predicate)) { spCounter[item.Subject][item.Predicate] = -1; var arrayLast = data.SP(item.Subject, item.Predicate).LastOrDefault(); spCounter[item.Subject][item.Predicate] = arrayLast == null ? -1 : arrayLast.Object.Index; } } var arrayIndex = ++spCounter[item.Subject][item.Predicate]; toAssert.Add(new Triple(item.Subject, item.Predicate, new TripleObject(item.Object.Value, item.Object.IsID, item.Object.TokenType, arrayIndex))); } } data.Assert(toAssert); } } catch (Exception e) { _logger.LogError("Patch triple failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); throw e; } }
public void To_Triples() { var item = new { id = "100", name = "name100", age = (int)20, contains = new { name = "name200", values = new object[] { new { name = "name300", age = (int)30, }, new { name = "name400" } } } }; var json = JObject.FromObject(item); var graph = TripleConverter.FromJson(json); var expected = new List <Triple>(); expected.Assert("100", "name", TripleObject.FromData("name100")); expected.Assert("100", "age", TripleObject.FromData(20)); expected.Assert("100", "contains", "100#contains"); expected.Assert("100#contains", "name", TripleObject.FromData("name200")); expected.Assert("100#contains", "values", "100#contains#values#0"); expected.Assert("100#contains#values#0", "name", TripleObject.FromData("name300")); expected.Assert("100#contains#values#0", "age", TripleObject.FromData(30)); expected.Assert("100#contains", "values", "100#contains#values#1"); expected.Assert("100#contains#values#1", "name", TripleObject.FromData("name400")); CollectionAssert.AreEqual(expected, graph.ToArray(), new UnorderedTripleComparer()); }
public void PatchTriple(string storeId, JObject input) { using (var op = _storeOperationFactory.Write(storeId)) { try { var(data, _, _) = GetSetGraphs(storeId); var remove = input["remove"]; if (remove != null && remove is JObject) { var triples = TripleConverter.FromJson((JObject)remove); data.Retract(triples); } var add = input["add"]; if (add != null && add is JObject) { var triples = TripleConverter.FromJson((JObject)add); data.Assert(triples); } } catch (Exception e) { _logger.LogError("Patch triple failed. {Message}\n {StackTrace}", e.Message, e.StackTrace); throw e; } } }
public JObject GetSet(string storeId) { var (data, _, _) = GetSetGraphs(storeId); return TripleConverter.ToJson(data); }