Exemplo n.º 1
0
        private static void Get(string pars, ref System.IO.StreamWriter w, ref System.IO.StreamReader r, User user)
        {
            string[] items = null;
            if (pars.Contains(" "))
            {
                items = pars.Split(' ');
            }
            else
            {
                items = new string[] { pars };
            }

            Cache cache = null;

            lock (MainClass.GlobalCaches)
            {
                if (!MainClass.GlobalCaches.ContainsKey(user))
                {
                    SendError(ErrorCode.InternalError, ref w);
                    return;
                }
                cache = MainClass.GlobalCaches[user];
            }
            foreach (string curr in items)
            {
                Cache.Item item = cache.Get(curr);
                if (item != null)
                {
                    Send("VALUE " + curr + " " + item.flags.ToString() + " " + item.value.Length.ToString() + "\r\n" + item.value, ref w);
                }
            }
            Send("END", ref w);
        }
Exemplo n.º 2
0
        private static void increment(string pars, ref System.IO.StreamWriter w, ref System.IO.StreamReader r, User user)
        {
            if (!pars.Contains(" "))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            string[] values = pars.Split(' ');

            if (values.Length < 2)
            {
                SendError(ErrorCode.MissingValues, ref w);
                return;
            }

            string key = values[0];
            int    jump;

            if (!int.TryParse(values[1], out jump))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            Cache cache = MainClass.GlobalCaches[user];

            Cache.Item item = cache.Get(key, true);

            if (item == null)
            {
                Send("NOT_FOUND", ref w);
                cache.incr_misses++;
                return;
            }

            int current;

            if (!int.TryParse(item.value, out current))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            current += jump;
            cache.incr_hits++;

            cache.hardSet(key, new Cache.Item(current.ToString(), item.expiry, item.flags));
            Send(current.ToString(), ref w);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Set the specified parameters, r and w.
        /// </summary>
        /// <param name="parameters">Parameters.</param>
        /// <param name="r">The red component.</param>
        /// <param name="w">The width.</param>
        private static int Set(string parameters, ref System.IO.StreamReader r, ref System.IO.StreamWriter w, User user)
        {
            string key = null;
            int flags = 0;
            int exptime = 0;
            int size = 0;
            //<command name> <key> <flags> <exptime> <bytes>
            string[] part = null;
            part = parameters.Split(' ');
            if (part.Length < 4)
            {
                // invalid format
                SendError (ErrorCode.MissingValues, ref w);
                return 1;
            }

            key = part[0];
            if (!int.TryParse (part[1], out flags))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return 1;
            }

            if (!int.TryParse (part[2], out exptime))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return 1;
            }

            if (!int.TryParse (part[3], out size))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return 1;
            }

            if (size < 0)
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return 1;
            }

            if ((ulong)size > Configuration.InstanceMemoryLimitByteSize)
            {
                SendError (ErrorCode.OutOfMemory, ref w);
                return 3;
            }

            // everything is ok let's go
            string chunk = r.ReadLine();
            while (chunk.Length < size)
            {
                chunk += "\n" + r.ReadLine();
            }

            if (chunk.Length > size)
            {
                // too big
                SendError (ErrorCode.InvalidValues, ref w);
                return 4;
            }

            Cache.Item Item = new Cache.Item(chunk, exptime, flags);

            lock (MainClass.GlobalCaches)
            {
                if (FreeSize (MainClass.GlobalCaches[user]) < Item.getSize ())
                {
                    // we don't have enough free size let's try to free some
                    if (!MainClass.GlobalCaches[user].FreeSpace(Item.getSize ()))
                    {
                        // error
                        SendError (ErrorCode.OutOfMemory, ref w);
                        return 1;
                    }
                }
                MainClass.GlobalCaches[user].Set (key, Item);
            }

            if (!parameters.EndsWith ("noreply"))
            {
                Send ("STORED", ref w);
            }

            // unknown error
            return 0;
        }
Exemplo n.º 4
0
        private static void Prepend(string pars, ref System.IO.StreamWriter w, ref System.IO.StreamReader r, User user)
        {
            if (!pars.Contains (" "))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            string[] part = pars.Split (' ');

            if (part.Length < 4)
            {
                SendError (ErrorCode.MissingValues, ref w);
                return;
            }

            string key = part[0];
            int flags = 0;
            int exptime = 0;
            int size = 0;
            //<command name> <key> <flags> <exptime> <bytes>

            if (!int.TryParse (part[1], out flags))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            if (!int.TryParse (part[2], out exptime))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            if (!int.TryParse (part[3], out size))
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            if (size < 0)
            {
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            if ((ulong)size > Configuration.InstanceMemoryLimitByteSize)
            {
                // error
                SendError (ErrorCode.OutOfMemory, ref w);
                return;
            }

            // everything is ok let's go
            string chunk = r.ReadLine();
            while (chunk.Length < size)
            {
                chunk += "\n" + r.ReadLine();
            }

            if (chunk.Length > size)
            {
                // too big
                SendError (ErrorCode.InvalidValues, ref w);
                return;
            }

            Cache cache = MainClass.GlobalCaches[user];

            Cache.Item item = cache.Get (key, true);

            if (item == null)
            {
                Send ("NOT_FOUND", ref w);
                return;
            }

            Cache.Item replacement = new Cache.Item(item.value + chunk, item.expiry, item.flags);

            if (FreeSize (cache) < replacement.getSize())
            {
                // we don't have enough free size let's try to free some
                if (!cache.FreeSpace(replacement.getSize()))
                {
                    // error
                    SendError (ErrorCode.OutOfMemory, ref w);
                    return;
                }
            }

            cache.hardSet (key, replacement);

            if (!pars.EndsWith("noreply"))
            {
                Send ("STORED", ref w);
            }
        }
Exemplo n.º 5
0
        private static int Replace(string parameters, ref System.IO.StreamReader r, ref System.IO.StreamWriter w, User user)
        {
            string key     = null;
            int    flags   = 0;
            int    exptime = 0;
            int    size    = 0;
            //<command name> <key> <flags> <exptime> <bytes>
            List <string> part = new List <string>();

            part.AddRange(parameters.Split(' '));
            if (part.Count < 4)
            {
                // invalid format
                return(1);
            }

            key = part[0];
            if (!int.TryParse(part[1], out flags))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (!int.TryParse(part[2], out exptime))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (!int.TryParse(part[3], out size))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (size < 0)
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if ((ulong)size > Configuration.InstanceMemoryLimitByteSize)
            {
                // error
                SendError(ErrorCode.OutOfMemory, ref w);
                return(3);
            }

            // everything is ok let's go
            string chunk = r.ReadLine();

            while (chunk.Length < size)
            {
                chunk += "\n" + r.ReadLine();
            }

            if (chunk.Length > size)
            {
                // too big
                SendError(ErrorCode.ValueTooBig, ref w);
                return(4);
            }

            Cache.Item Item = new Cache.Item(chunk, exptime, flags);

            lock (MainClass.GlobalCaches)
            {
                if (FreeSize(MainClass.GlobalCaches[user]) < Item.getSize())
                {
                    // we don't have enough free size let's try to free some
                    if (!MainClass.GlobalCaches[user].FreeSpace(Item.getSize()))
                    {
                        // error
                        SendError(ErrorCode.OutOfMemory, ref w);
                        return(1);
                    }
                }
                if (MainClass.GlobalCaches[user].Replace(key, Item))
                {
                    if (!parameters.EndsWith("noreply"))
                    {
                        Send("STORED", ref w);
                    }
                }
                else
                {
                    if (!parameters.EndsWith("noreply"))
                    {
                        Send("NOT_STORED", ref w);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 6
0
        private static int cas(string parameters, ref System.IO.StreamReader r, ref System.IO.StreamWriter w, User user)
        {
            string key     = null;
            int    flags   = 0;
            int    exptime = 0;
            int    size    = 0;
            double CAS     = 0;

            //<command name> <key> <flags> <exptime> <bytes>
            string[] part = null;
            part = parameters.Split(' ');
            if (part.Length < 5)
            {
                // invalid format
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            key = part[0];
            if (!int.TryParse(part[1], out flags))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (!int.TryParse(part[2], out exptime))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (!int.TryParse(part[3], out size))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if (size < 0)
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            if ((ulong)size > Configuration.InstanceMemoryLimitByteSize)
            {
                // error
                SendError(ErrorCode.OutOfMemory, ref w);
                return(3);
            }

            if (!double.TryParse(part[4], out CAS))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return(1);
            }

            // everything is ok let's go
            string chunk = r.ReadLine();

            while (chunk.Length < size)
            {
                chunk += "\n" + r.ReadLine();
            }

            if (chunk.Length > size)
            {
                // too big
                SendError(ErrorCode.InvalidValues, ref w);
                return(4);
            }

            Cache.Item Item = new Cache.Item(chunk, exptime, flags);

            lock (MainClass.GlobalCaches)
            {
                if (FreeSize(MainClass.GlobalCaches[user]) < Item.getSize())
                {
                    SendError(ErrorCode.OutOfMemory, ref w);
                    return(1);
                }
                if (MainClass.GlobalCaches[user].ReplaceCas(key, Item, CAS))
                {
                    if (!parameters.EndsWith("noreply"))
                    {
                        Send("STORED", ref w);
                    }
                }
                else
                {
                    if (!parameters.EndsWith("noreply"))
                    {
                        Send("NOT_STORED", ref w);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 7
0
        private static void Prepend(string pars, ref System.IO.StreamWriter w, ref System.IO.StreamReader r, User user)
        {
            if (!pars.Contains(" "))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            string[] part = pars.Split(' ');

            if (part.Length < 4)
            {
                SendError(ErrorCode.MissingValues, ref w);
                return;
            }

            string key     = part[0];
            int    flags   = 0;
            int    exptime = 0;
            int    size    = 0;

            //<command name> <key> <flags> <exptime> <bytes>

            if (!int.TryParse(part[1], out flags))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            if (!int.TryParse(part[2], out exptime))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            if (!int.TryParse(part[3], out size))
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            if (size < 0)
            {
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            if ((ulong)size > Configuration.InstanceMemoryLimitByteSize)
            {
                // error
                SendError(ErrorCode.OutOfMemory, ref w);
                return;
            }

            // everything is ok let's go
            string chunk = r.ReadLine();

            while (chunk.Length < size)
            {
                chunk += "\n" + r.ReadLine();
            }

            if (chunk.Length > size)
            {
                // too big
                SendError(ErrorCode.InvalidValues, ref w);
                return;
            }

            Cache cache = MainClass.GlobalCaches[user];

            Cache.Item item = cache.Get(key, true);

            if (item == null)
            {
                Send("NOT_FOUND", ref w);
                return;
            }

            Cache.Item replacement = new Cache.Item(item.value + chunk, item.expiry, item.flags);

            if (FreeSize(cache) < replacement.getSize())
            {
                // we don't have enough free size let's try to free some
                if (!cache.FreeSpace(replacement.getSize()))
                {
                    // error
                    SendError(ErrorCode.OutOfMemory, ref w);
                    return;
                }
            }

            cache.hardSet(key, replacement);

            if (!pars.EndsWith("noreply"))
            {
                Send("STORED", ref w);
            }
        }