public async Task Sample() { using (var store = new DocumentStore()) { #region email string email = "*****@*****.**"; User user = new User { Email = email }; using (IDocumentSession session = store.OpenSession()) { session.Store(user); // At this point, the user document has an Id assigned // Try to reserve a new user email // Note: This operation takes place outside of the session transaction, // It is a cluster-wide reservation CompareExchangeResult <string> cmpXchgResult = store.Operations.Send( new PutCompareExchangeValueOperation <string>("emails/" + email, user.Id, 0)); if (cmpXchgResult.Successful == false) { throw new Exception("Email is already in use"); } // At this point we managed to reserve/save the user email - // The document can be saved in SaveChanges session.SaveChanges(); } #endregion #region query_cmpxchg using (IDocumentSession session = store.OpenSession()) { var query = from u in session.Query <User>() where u.Email == RavenQuery.CmpXchg <string>("emails/[email protected]") select u; var q = session.Advanced .DocumentQuery <User>() .WhereEquals("Email", CmpXchg.Value("emails/[email protected]")); } #endregion } }
public static bool GetValueFromExpressionWithoutConversion(Expression expression, out object value) { switch (expression.NodeType) { case ExpressionType.Constant: value = ((ConstantExpression)expression).Value; return(true); case ExpressionType.MemberAccess: value = GetMemberValue(((MemberExpression)expression)); return(true); case ExpressionType.MemberInit: var memberInitExpression = ((MemberInitExpression)expression); value = Expression.Lambda(memberInitExpression).Compile().DynamicInvoke(); return(true); case ExpressionType.New: value = GetNewExpressionValue(expression); return(true); case ExpressionType.Lambda: var lambda = ((LambdaExpression)expression); value = lambda.Compile().DynamicInvoke(); return(true); case ExpressionType.Call: if (expression is MethodCallExpression mce) { if (mce.Method.DeclaringType == typeof(RavenQuery) && mce.Method.Name == nameof(RavenQuery.CmpXchg)) { if (TryGetMethodArguments(mce, out var args) == false) { value = null; return(false); } value = CmpXchg.Value((string)args[0]); return(true); } } value = Expression.Lambda(expression).Compile().DynamicInvoke(); return(true); case ExpressionType.Convert: var unaryExpression = (UnaryExpression)expression; if (unaryExpression.Type.IsNullableType()) { return(GetValueFromExpressionWithoutConversion(unaryExpression.Operand, out value)); } value = Expression.Lambda(expression).Compile().DynamicInvoke(); return(true); case ExpressionType.NewArrayInit: var expressions = ((NewArrayExpression)expression).Expressions; var values = new object[expressions.Count]; value = null; if (expressions.Where((t, i) => !GetValueFromExpressionWithoutConversion(t, out values[i])).Any()) { return(false); } value = values; return(true); case ExpressionType.NewArrayBounds: value = null; expressions = ((NewArrayExpression)expression).Expressions; var constantExpression = (ConstantExpression)expressions.FirstOrDefault(); if (constantExpression == null) { return(false); } if (constantExpression.Value.GetType() != typeof(int)) { return(false); } var length = (int)constantExpression.Value; value = new object[length]; return(true); default: value = null; return(false); } }