public object Combine(object value) { if (!(value is IJSONDocument)) { throw new Exception("At MaxDataCombiner: Document needs to be in IJSONDocument format"); } IJSONDocument document = (IJSONDocument)value; IJSONDocument updateDoc = new JSONDocument(); foreach (var attribute in _attributes) { object value1 = document[_userDefinedName]; object value2 = _document[_userDefinedName]; int comparer; if (value1 is IComparable) { comparer = JSONComparer.Compare((IComparable)value1, value2); } else { comparer = JsonWrapper.Wrap(value1).CompareTo(JsonWrapper.Wrap(value2)); } if (comparer > 0) { updateDoc.Add(_userDefinedName, value1); } } JsonDocumentUtil.Update(_document, updateDoc); //_document.Update(updateDoc); return(_document); }
public long ExecuteNonQuery(string queryText, ICollection<IParameter> parameters) { if (string.IsNullOrEmpty(queryText)) throw new ArgumentNullException("queryText can not be null or empty string"); if (typeof(T) == typeof(IJSONDocument) || typeof(T) == typeof(JSONDocument)) { Type type; if (!JsonDocumentUtil.IsSupportedParameterType(parameters, out type)) { throw new ArgumentException(string.Format("Type {0} is not supported on Collection<JSONDocument>", type), "parameters"); } } Query query = new Query(); query.QueryText = queryText; query.Parameters = (List<IParameter>)parameters; WriteQueryOperation writeQueryOperation = new WriteQueryOperation(); writeQueryOperation.Database = _database.DatabaseName; writeQueryOperation.Collection = _collectionName; writeQueryOperation.Query = query; var writeQueryResponse = _database.ExecutionMapper.ExecuteNonQuery(writeQueryOperation); if (writeQueryResponse.IsSuccessfull) { return writeQueryResponse.AffectedDocuments; } if (writeQueryResponse.ErrorParams != null && writeQueryResponse.ErrorParams.Length > 0) throw new Exception(string.Format("Operation failed Error :" + Common.ErrorHandling.ErrorMessages.GetErrorMessage(writeQueryResponse.ErrorCode, writeQueryResponse.ErrorParams))); throw new Exception("Operation failed Error: " + Common.ErrorHandling.ErrorMessages.GetErrorMessage(writeQueryResponse.ErrorCode)); }
public static bool KeyInfoEquals(string key, object type, IDictionary <string, object> doc, Dictionary <string, object> optionals, ref Dictionary <string, object> configValues, bool isOptionalValidate) { if (type is ExtendedJSONDataTypes) { if (!JsonDocumentUtil.GetExtendedJsonType(doc[key]).Equals(type)) { return(false); } if (!configValues.ContainsKey(key.ToLower())) { configValues.Add(key.ToLower(), doc[key]); } } else if (type is IValidator) { object jsonValue = null; if (type is ValueValidator) { jsonValue = doc; } else { jsonValue = doc[key]; } if (!((IValidator)type).Validate(key, jsonValue, optionals, ref configValues, isOptionalValidate)) { return(false); } } return(true); }
public ICollectionReader ExecuteReader(string queryText, ICollection<IParameter> parameters) { if (string.IsNullOrEmpty(queryText)) throw new ArgumentNullException("queryText can not be null or empty string"); if (typeof(T) == typeof(IJSONDocument) || typeof(T) == typeof(JSONDocument)) { Type type; if (!JsonDocumentUtil.IsSupportedParameterType(parameters, out type)) { throw new ArgumentException(string.Format("Type {0} is not supported on Collection<JSONDocument>", type), "parameters"); } } Query query = new Query(); query.QueryText = queryText; query.Parameters = parameters.Cast<IParameter>().ToList(); ReadQueryOperation readQueryOperation = new ReadQueryOperation(); readQueryOperation.Database = _database.DatabaseName; readQueryOperation.Collection = _collectionName; readQueryOperation.Query = query; ReadQueryResponse readQueryResponse = (ReadQueryResponse)_database.ExecutionMapper.ExecuteReader(readQueryOperation); if (!readQueryResponse.IsSuccessfull) { if (readQueryResponse.ErrorParams != null && readQueryResponse.ErrorParams.Length > 0) throw new Exception(string.Format("Operation failed Error: " + Common.ErrorHandling.ErrorMessages.GetErrorMessage(readQueryResponse.ErrorCode, readQueryResponse.ErrorParams))); throw new Exception("Operation failed Error: " + Common.ErrorHandling.ErrorMessages.GetErrorMessage(readQueryResponse.ErrorCode)); } CollectionReader reader = new CollectionReader((DataChunk)readQueryResponse.DataChunk, _database.ExecutionMapper, _database.DatabaseName, _collectionName); return reader; }
public object ExecuteScalar(string queryText, ICollection<IParameter> parameters) { if (string.IsNullOrEmpty(queryText)) throw new ArgumentNullException("queryText can not be null or empty string"); if (typeof(T) == typeof(IJSONDocument) || typeof(T) == typeof(JSONDocument)) { Type type; if (!JsonDocumentUtil.IsSupportedParameterType(parameters, out type)) { throw new ArgumentException(string.Format("Type {0} is not supported on Collection<JSONDocument>", type), "parameters"); } } Query query = new Query(); query.QueryText = queryText; query.Parameters = parameters.Cast<IParameter>().ToList(); ReadQueryOperation readQueryOperation = new ReadQueryOperation(); readQueryOperation.Database = _database.DatabaseName; readQueryOperation.Collection = _collectionName; readQueryOperation.Query = query; return _database.ExecutionMapper.ExecuteScalar(readQueryOperation); }
public UpdateResult <JSONDocument> UpdateDocument(JSONDocument update) { UpdateResult <JSONDocument> result = new UpdateResult <JSONDocument>(); CacheItem item; if (_documentCache.TryGetValue(new DocumentKey(update.Key), out item)) { result.OldDocument = item.Document.Clone() as JSONDocument; JsonDocumentUtil.Update(item.Document, update); //_documentCache[update.Key].Document.Update(update); result.RowId = item.Metadata.RowId; result.NewDocument = item.Document.Clone() as JSONDocument; } return(result); }
public object Combine(object value) { if (!(value is IJSONDocument)) { throw new Exception("At AverageDataCombiner: Document needs to be in IJSONDocument format"); } bool isUpdateApplicable = false; IJSONDocument document = (IJSONDocument)value; IJSONDocument updateDoc = new JSONDocument(); foreach (var attribute in _attributes) { IJSONDocument sumAndCount = new JSONDocument(); if (document.GetAttributeDataType(_userDefinedName) == ExtendedJSONDataTypes.Array) { if (_document.Contains("$" + _userDefinedName) && _document.GetAttributeDataType("$" + _userDefinedName) == ExtendedJSONDataTypes.Array) { double[] sumAndCountDoc = document.GetArray <double>(_userDefinedName); double sum = sumAndCountDoc[0]; double count = sumAndCountDoc[1]; double[] existingSumAndCount = _document.GetArray <double>("$" + _userDefinedName); double sum1 = existingSumAndCount[0]; double count1 = existingSumAndCount[1]; double combinedSum = sum + sum1; double combinedCount = count + count1; sumAndCountDoc[0] = combinedSum; sumAndCountDoc[1] = combinedCount; updateDoc.Add("$" + _userDefinedName, sumAndCountDoc); updateDoc.Add(_userDefinedName, combinedSum / combinedCount); isUpdateApplicable = true; } } } if (isUpdateApplicable) { JsonDocumentUtil.Update(_document, updateDoc); } return(_document); }
public object Combine(object value) { if (!(value is IJSONDocument)) { throw new Exception("At SumDataCombiner: Document needs to be in IJSONDocument format"); } IJSONDocument document = (IJSONDocument)value; IJSONDocument updateDoc = new JSONDocument(); foreach (var attribute in _attributes) { double value1 = document.GetAsDouble(_userDefinedName); double value2 = _document.GetAsDouble(_userDefinedName); updateDoc.Add(_userDefinedName, value1 + value2); } JsonDocumentUtil.Update(_document, updateDoc); //_document.Update(updateDoc); return(_document); }
public override IEnumerable <KeyValuePair <AttributeValue, long> > Enumerate(QueryCriteria value) { var andPredicate = new ANDPredicate(); for (int i = 0; i < sequence.Length; i++) { var currentElement = new SingleAttributeValue(new ArrayElement(sequence[i], i)); andPredicate.AddChildPredicate(new EqualsPredicate(Source, currentElement, IsInverse)); } foreach (var kvp in andPredicate.Enumerate(value)) { IJSONDocument document = value.Store.GetDocument(kvp.Value, null); //Array array = document.GetArray(source.Attributes[0].Name); Array array = JsonDocumentUtil.GetArray(document, source.Attributes.Name); if (array.Length == sequence.Length) { yield return(kvp); } } }
public bool MoveNext() { if (!_closed) { if (_combiners.Count < 1) { throw new Exception("DataCombiner is not defined for Group By Operation"); } if (_current == null && _lastUnSentElement == null) // True first time only { if (_dataSelector.MoveNext()) { _lastUnSentElement = (ISetElement)_dataSelector.Current; } else { _closed = true; return(false); } } if (_lastUnSentElement == null) { _closed = true; return(false); } // bool found = false; _current = _lastUnSentElement; _lastUnSentElement = null; foreach (IDataCombiner dataCombiner in _combiners) { dataCombiner.Reset(); _current.Value = dataCombiner.Initialize(_current.Value) as IJSONDocument; } while (_dataSelector.MoveNext()) { _lastUnSentElement = (ISetElement)_dataSelector.Current; if (_comparer.Compare(_current.Value, _lastUnSentElement.Value) == 0) { //JSONDocumentComparer jdocComparer = (JSONDocumentComparer)_comparer; //foreach (string field in jdocComparer.FieldNamesGroupBy) // Can be optimized for better performance. Will do it later if required // _current.Value.Remove(field); foreach (IDataCombiner dataCombiner in _combiners) { JsonDocumentUtil.Update(_current.Value, (IJSONDocument)dataCombiner.Combine(_lastUnSentElement.Value)); // _current.Value.Update((IJSONDocument)dataCombiner.Combine(_lastUnSentElement.Value)); } _lastUnSentElement = null; } else { return(true); } } return(true); } return(false); }
public void UpdateAttribute(IJSONDocument document, IJsonValue newValue) { if (document.Contains(_name)) { if (_indecies != null && _indecies.Length > 0) { ExtendedJSONDataTypes type = document.GetAttributeDataType(_name); if (type == ExtendedJSONDataTypes.Array) { List <IJsonValue> ijsonList = JsonDocumentUtil.ToIJsonList((IEnumerable)document[_name]); if (_indecies != null && _indecies.Length > 0) { if (_childAttribute != null && ijsonList.Count > _indecies[0]) { if (ijsonList[_indecies[0]].Value is IJSONDocument) { _childAttribute.UpdateAttribute(ijsonList[_indecies[0]].Value as IJSONDocument, newValue); } } else { // Update value in list and replace list in the document. ijsonList[_indecies[0]] = newValue; var values = new ArrayList(ijsonList.Count); foreach (var jsonValue in ijsonList) { values.Add(jsonValue.Value); } document[_name] = values.ToArray(); } } else { foreach (IJsonValue jsondoc in ijsonList) { ((JSONDocument)jsondoc.Value)[_name] = newValue.Value; } } } } else { if (_childAttribute != null) { if (document[_name] is IJSONDocument) { _childAttribute.UpdateAttribute(document[_name] as IJSONDocument, newValue); } else { ExtendedJSONDataTypes type = document.GetAttributeDataType(_name); if (type == ExtendedJSONDataTypes.Array) { List <IJsonValue> ijsonList = JsonDocumentUtil.ToIJsonList((IEnumerable)document[_name]); foreach (IJsonValue jsondoc in ijsonList) { _childAttribute.UpdateAttribute((JSONDocument)jsondoc.Value, newValue); } } } } else { document[_name] = newValue.Value; } } } else { document[_name] = newValue.Value; } }