Task<object> IScriptingCommands.Eval(int db, string script, string[] keyArgs, object[] valueArgs, bool useCache,
                                             bool inferStrings, bool queueJump)
        {
            if (string.IsNullOrEmpty(script)) throw new ArgumentNullException("script");
            var args =
                new RedisMessage.RedisParameter[
                    2 + (keyArgs == null ? 0 : keyArgs.Length) + (valueArgs == null ? 0 : valueArgs.Length)];
            args[1] = keyArgs == null ? 0 : keyArgs.Length;
            int idx = 2;
            if (keyArgs != null)
            {
                for (int i = 0; i < keyArgs.Length; i++)
                {
                    args[idx++] = keyArgs[i];
                }
            }
            if (valueArgs != null)
            {
                for (int i = 0; i < valueArgs.Length; i++)
                {
                    args[idx++] = RedisMessage.RedisParameter.Create(valueArgs[i]);
                }
            }
            if (!useCache)
            {
                args[0] = script;
                return ExecuteScript(RedisMessage.Create(db, RedisLiteral.EVAL, args), inferStrings, queueJump);
            }

            // note this does a SCRIPT LOAD if it is the first time it is seen on this connection
            args[0] = GetScriptHash(script);
            return ExecuteScript(RedisMessage.Create(db, RedisLiteral.EVALSHA, args), inferStrings, queueJump);
        }
        Task <object> IScriptingCommands.Eval(int db, string script, string[] keyArgs, object[] valueArgs, bool useCache, bool inferStrings, bool queueJump)
        {
            if (string.IsNullOrEmpty(script))
            {
                throw new ArgumentNullException("script");
            }
            var args = new RedisMessage.RedisParameter[2 + (keyArgs == null ? 0 : keyArgs.Length) + (valueArgs == null ? 0 : valueArgs.Length)];

            args[1] = keyArgs == null ? 0 : keyArgs.Length;
            int idx = 2;

            if (keyArgs != null)
            {
                for (int i = 0; i < keyArgs.Length; i++)
                {
                    args[idx++] = keyArgs[i];
                }
            }
            if (valueArgs != null)
            {
                for (int i = 0; i < valueArgs.Length; i++)
                {
                    args[idx++] = RedisMessage.RedisParameter.Create(valueArgs[i]);
                }
            }
            if (!useCache)
            {
                args[0] = script;
                return(ExecuteScript(RedisMessage.Create(db, RedisLiteral.EVAL, args), inferStrings, queueJump));
            }

            // note this does a SCRIPT LOAD if it is the first time it is seen on this connection
            args[0] = GetScriptHash(script);
            return(ExecuteScript(RedisMessage.Create(db, RedisLiteral.EVALSHA, args), inferStrings, queueJump));
        }
Exemple #3
0
        private Task <long> ExecMultiAddRemove(int db, RedisLiteral command, string key, byte[][] values, bool queueJump)
        {
            RedisFeatures features;

            if (values.Length > 1 && ((features = Features) == null || !features.SetVaradicAddRemove))
            {
                RedisTransaction tran    = this as RedisTransaction;
                bool             execute = false;
                if (tran == null)
                {
                    tran    = CreateTransaction();
                    execute = true;
                }
                Task <bool>[] tasks = new Task <bool> [values.Length];

                for (int i = 0; i < values.Length; i++)
                {
                    tasks[i] = ExecuteBoolean(RedisMessage.Create(db, command, key, values[i]), queueJump);
                }
                TaskCompletionSource <long> final = new TaskCompletionSource <long>();
                tasks[values.Length - 1].ContinueWith(t =>
                {
                    try
                    {
                        if (t.ShouldSetResult(final))
                        {
                            long count = 0;
                            for (int i = 0; i < tasks.Length; i++)
                            {
                                if (tran.Wait(tasks[i]))
                                {
                                    count++;
                                }
                            }
                            final.TrySetResult(count);
                        }
                    }
                    catch (Exception ex)
                    {
                        final.SafeSetException(ex);
                    }
                });
                if (execute)
                {
                    tran.Execute(queueJump);
                }
                return(final.Task);
            }
            else
            {
                var args = new RedisMessage.RedisParameter[values.Length + 1];
                args[0] = key;
                for (int i = 0; i < values.Length; i++)
                {
                    args[i + 1] = values[i];
                }
                return(ExecuteInt64(RedisMessage.Create(db, command, args), queueJump));
            }
        }
        static RedisMessage GetBlockingPop(int db, RedisLiteral command, string[] keys, int timeoutSeconds)
        {
            var args = new RedisMessage.RedisParameter[keys.Length + 1];

            for (int i = 0; i < keys.Length; i++)
            {
                args[i] = keys[i];
            }
            args[keys.Length] = timeoutSeconds;
            return(RedisMessage.Create(db, command, args));
        }
Exemple #5
0
        Task IHashCommands.Set(int db, string key, Dictionary <string, byte[]> values, bool queueJump)
        {
            var keyAndFields = new RedisMessage.RedisParameter[(values.Count * 2) + 1];
            int index        = 0;

            keyAndFields[index++] = key;
            foreach (var pair in values)
            {
                keyAndFields[index++] = pair.Key;
                keyAndFields[index++] = pair.Value;
            }
            return(ExecuteVoid(RedisMessage.Create(db, RedisLiteral.HMSET, keyAndFields), queueJump));
        }
        static RedisMessage.RedisParameter[] Parse(Dictionary <string, byte[]> values)
        {
            if (values == null)
            {
                return(new RedisMessage.RedisParameter[0]);
            }
            var arr   = new RedisMessage.RedisParameter[values.Count * 2];
            int index = 0;

            foreach (var pair in values)
            {
                arr[index++] = pair.Key;
                arr[index++] = pair.Value;
            }
            return(arr);
        }
        internal virtual Task Prepare(string[] scripts)
        {
            if (scripts == null)
            {
                throw new ArgumentNullException();
            }
            var seenHashes = new HashSet <string>();
            List <Tuple <string, string> > fetch = new List <Tuple <string, string> >(scripts.Length);

            for (int i = 0; i < scripts.Length; i++)
            {
                string script = scripts[i];
                if (!scriptCache.ContainsKey(script))
                {
                    string hash = ComputeHash(script);
                    seenHashes.Add(hash);
                    fetch.Add(Tuple.Create(hash, script));
                }
            }
            if (fetch.Count == 0)
            {
                // no point checking, since we'd still be at the mercy of ad-hoc SCRIPT FLUSH timing
                return(AlwaysTrue);
            }

            RedisMessage.RedisParameter[] args = new RedisMessage.RedisParameter[1 + fetch.Count];
            args[0] = RedisLiteral.EXISTS;
            int idx = 1;

            foreach (var pair in fetch)
            {
                args[idx++] = pair.Item1; // hash
            }

            TaskCompletionSource <bool> result = new TaskCompletionSource <bool>();

            var state = Tuple.Create(result, this, fetch);

            ExecuteRaw(RedisMessage.Create(-1, RedisLiteral.SCRIPT, args), false, state).ContinueWith(scriptCompletionCallback);

            return(result.Task);
        }
        private Task <long> BitOp(int db, RedisLiteral operation, string destination, string[] keys, bool queueJump)
        {
            if (keys == null)
            {
                throw new ArgumentNullException("keys");
            }
            if (keys.Length == 0)
            {
                throw new ArgumentException("keys");
            }
            var args = new RedisMessage.RedisParameter[keys.Length + 2];

            args[0] = operation;
            args[1] = destination;
            for (int i = 0; i < keys.Length; i++)
            {
                args[i + 2] = keys[i];
            }
            return(ExecuteInt64(RedisMessage.Create(db, RedisLiteral.BITOP, args), queueJump));
        }
 static RedisMessage GetBlockingPop(int db, RedisLiteral command, string[] keys, int timeoutSeconds)
 {
     var args = new RedisMessage.RedisParameter[keys.Length + 1];
     for (int i = 0; i < keys.Length; i++)
         args[i] = keys[i];
     args[keys.Length] = timeoutSeconds;
     return RedisMessage.Create(db, command, args);
 }
Exemple #10
0
        internal virtual Task Prepare(string[] scripts)
        {
            var result = new TaskCompletionSource <bool>();

            if (scripts == null)
            {
                throw new ArgumentNullException();
            }
            var seenHashes = new HashSet <string>();
            var fetch      = new List <Tuple <string, string> >(scripts.Length);

            for (int i = 0; i < scripts.Length; i++)
            {
                string script = scripts[i];
                if (!scriptCache.ContainsKey(script))
                {
                    string hash = ComputeHash(script);
                    seenHashes.Add(hash);
                    fetch.Add(Tuple.Create(hash, script));
                }
            }
            if (fetch.Count == 0)
            {
                // no point checking, since we'd still be at the mercy of ad-hoc SCRIPT FLUSH timing
                result.SetResult(true);
                return(result.Task); // already complete
            }

            var args = new RedisMessage.RedisParameter[1 + fetch.Count];

            args[0] = RedisLiteral.EXISTS;
            int idx = 1;

            foreach (var pair in fetch)
            {
                args[idx++] = pair.Item1; // hash
            }

            ExecuteRaw(RedisMessage.Create(-1, RedisLiteral.SCRIPT, args), false).ContinueWith(queryTask =>
            {
                if (Condition.ShouldSetResult(queryTask, result))
                {
                    RedisResult[] existResults = queryTask.Result.ValueItems;
                    Task last = null;
                    for (int i = 0; i < existResults.Length; i++)
                    {
                        string hash = fetch[i].Item1, script = fetch[i].Item2;
                        if (existResults[i].ValueInt64 == 0)
                        {
                            // didn't exist
                            last =
                                ExecuteVoid(
                                    RedisMessage.Create(-1, RedisLiteral.SCRIPT, RedisLiteral.LOAD, script), true);
                        }
                        // at this point, either it *already* existed, or we've issued a queue-jumping LOAD command,
                        // so all *subsequent* calles can reliably assume that it will exist on the server from now on
                        lock (scriptCache)
                        {
                            scriptCache[script] = hash;
                        }
                    }
                    if (last == null)
                    {
                        // nothing needed
                        result.TrySetResult(true);
                    }
                    else
                    {
                        last.ContinueWith(
                            loadTask =>
                            { if (Condition.ShouldSetResult(loadTask, result))
                              {
                                  result.TrySetResult(true);
                              }
                            });
                    }
                }
            });

            return(result.Task);
        }
Exemple #11
0
        private Task<long> ExecMultiAddRemove(int db, RedisLiteral command, string key, byte[][] values, bool queueJump)
        {
            RedisFeatures features;
            if (values.Length > 1 && ((features = Features) == null || !features.SetVaradicAddRemove))
            {
                RedisTransaction tran = this as RedisTransaction;
                bool execute = false;
                if (tran == null)
                {
                    tran = CreateTransaction();
                    execute = true;
                }
                Task<bool>[] tasks = new Task<bool>[values.Length];

                for (int i = 0; i < values.Length; i++)
                {
                    tasks[i] = ExecuteBoolean(RedisMessage.Create(db, command, key, values[i]), queueJump);
                }
                TaskCompletionSource<long> final = new TaskCompletionSource<long>();
                tasks[values.Length - 1].ContinueWith(t =>
                {
                    try
                    {
                        if (t.ShouldSetResult(final))
                        {
                            long count = 0;
                            for (int i = 0; i < tasks.Length; i++)
                            {
                                if (tran.Wait(tasks[i]))
                                {
                                    count++;
                                }
                            }
                            final.TrySetResult(count);
                        }
                    }
                    catch (Exception ex)
                    {
                        final.SafeSetException(ex);
                    }
                });
                if (execute) tran.Execute(queueJump);
                return final.Task;
            }
            else
            {
                var args = new RedisMessage.RedisParameter[values.Length + 1];
                args[0] = key;
                for (int i = 0; i < values.Length; i++)
                {
                    args[i + 1] = values[i];
                }
                return ExecuteInt64(RedisMessage.Create(db, command, args), queueJump);
            }
        }
Exemple #12
0
 Task IHashCommands.Set(int db, string key, Dictionary<string, byte[]> values, bool queueJump)
 {
     var keyAndFields = new RedisMessage.RedisParameter[(values.Count * 2) + 1];
     int index = 0;
     keyAndFields[index++] = key;
     foreach (var pair in values)
     {
         keyAndFields[index++] = pair.Key;
         keyAndFields[index++] = pair.Value;
     }
     return ExecuteVoid(RedisMessage.Create(db, RedisLiteral.HMSET, keyAndFields), queueJump);
 }
        internal virtual Task Prepare(string[] scripts)
        {
            var result = new TaskCompletionSource<bool>();

            if (scripts == null) throw new ArgumentNullException();
            var seenHashes = new HashSet<string>();
            var fetch = new List<Tuple<string, string>>(scripts.Length);
            for (int i = 0; i < scripts.Length; i++)
            {
                string script = scripts[i];
                if (!scriptCache.ContainsKey(script))
                {
                    string hash = ComputeHash(script);
                    seenHashes.Add(hash);
                    fetch.Add(Tuple.Create(hash, script));
                }
            }
            if (fetch.Count == 0)
            {
                // no point checking, since we'd still be at the mercy of ad-hoc SCRIPT FLUSH timing
                result.SetResult(true);
                return result.Task; // already complete
            }

            var args = new RedisMessage.RedisParameter[1 + fetch.Count];
            args[0] = RedisLiteral.EXISTS;
            int idx = 1;
            foreach (var pair in fetch)
            {
                args[idx++] = pair.Item1; // hash
            }

            ExecuteRaw(RedisMessage.Create(-1, RedisLiteral.SCRIPT, args), false).ContinueWith(queryTask =>
                {
                    if (Condition.ShouldSetResult(queryTask, result))
                    {
                        RedisResult[] existResults = queryTask.Result.ValueItems;
                        Task last = null;
                        for (int i = 0; i < existResults.Length; i++)
                        {
                            string hash = fetch[i].Item1, script = fetch[i].Item2;
                            if (existResults[i].ValueInt64 == 0)
                            {
                                // didn't exist
                                last =
                                    ExecuteVoid(
                                        RedisMessage.Create(-1, RedisLiteral.SCRIPT, RedisLiteral.LOAD, script), true);
                            }
                            // at this point, either it *already* existed, or we've issued a queue-jumping LOAD command,
                            // so all *subsequent* calles can reliably assume that it will exist on the server from now on
                            lock (scriptCache)
                            {
                                scriptCache[script] = hash;
                            }
                        }
                        if (last == null)
                        {
                            // nothing needed
                            result.TrySetResult(true);
                        }
                        else
                        {
                            last.ContinueWith(
                                loadTask =>
                                    { if (Condition.ShouldSetResult(loadTask, result)) result.TrySetResult(true); });
                        }
                    }
                });

            return result.Task;
        }
Exemple #14
0
 static RedisMessage.RedisParameter[] Parse(Dictionary<string, byte[]> values)
 {
     if (values == null) return new RedisMessage.RedisParameter[0];
     var arr = new RedisMessage.RedisParameter[values.Count * 2];
     int index = 0;
     foreach (var pair in values)
     {
         arr[index++] = pair.Key;
         arr[index++] = pair.Value;
     }
     return arr;
 }
Exemple #15
0
 private Task<long> BitOp(int db, RedisLiteral operation, string destination, string[] keys, bool queueJump)
 {
     if (keys == null) throw new ArgumentNullException("keys");
     if (keys.Length == 0) throw new ArgumentException("keys");
     var args = new RedisMessage.RedisParameter[keys.Length + 2];
     args[0] = operation;
     args[1] = destination;
     for (int i = 0; i < keys.Length; i++)
     {
         args[i + 2] = keys[i];
     }
     return ExecuteInt64(RedisMessage.Create(db, RedisLiteral.BITOP, args), queueJump);
 }