Esempio n. 1
0
        private MutateOpResult Mutate(string key, ulong value, object initialValue, long expirationTimeInSeconds, ulong casUnique, UpdateType updateType)
        {
            if (string.IsNullOrEmpty(key) || (initialValue != null && IsUnsignedNumeric(initialValue) == false))
            {
                ThrowInvalidArgumentsException();
            }

            MutateOpResult returnObject = new MutateOpResult();

            try
            {
                CacheItem getObject = _cache.GetCacheItem(key);
                if (getObject == null)
                {
                    if (initialValue == null || expirationTimeInSeconds == uint.MaxValue)
                    {
                        returnObject.ReturnResult = Result.ITEM_NOT_FOUND;
                        returnObject.Value        = null;
                    }
                    else
                    {
                        OperationResult opResult = InsertItemSuccessfully(key, 10, expirationTimeInSeconds,
                                                                          BitConverter.GetBytes(Convert.ToUInt32(initialValue)));
                        returnObject.Value        = opResult.Value;
                        returnObject.ReturnResult = opResult.ReturnResult;
                        returnObject.MutateResult = Convert.ToUInt64(initialValue);
                    }
                }
                else
                {
                    MemcachedItem memCacheItem = (MemcachedItem)getObject.Value;
                    if ((casUnique > 0 && memCacheItem.InternalVersion == casUnique) || casUnique == 0)
                    {
                        returnObject = UpdateIfNumeric(key, getObject, value, updateType);
                    }
                    else
                    {
                        returnObject.ReturnResult = Result.ITEM_MODIFIED;
                        returnObject.Value        = null;
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }
            return(returnObject);
        }
Esempio n. 2
0
		private ulong? incrementDecrement(string cmd, string key, bool keyIsChecked, ulong value, uint hash) {
			if (!keyIsChecked) {
				checkKey(key);
			}
            if (memcachedProvider == null)
            {
                ConnectionError();
                return null;
            }
            MutateOpResult opResult=new MutateOpResult();
            try
            {
                switch (cmd)
                {
                    case "incr":
                        opResult = memcachedProvider.Increment(key, value, null, 0, 0);
                        break;
                    case "decr":
                        opResult = memcachedProvider.Decrement(key, value, null, 0, 0);
                        break;
                }
                if (opResult.ReturnResult == Result.SUCCESS)
                    return opResult.MutateResult;
                else
                    return null;
            }
            catch (Exception e)
            {
                return null;
            }
		}
Esempio n. 3
0
        private MutateOpResult UpdateIfNumeric(string key, CacheItem cacheItem, ulong value, UpdateType updateType)
        {
            MutateOpResult returnObject  = new MutateOpResult();
            MemcachedItem  memCachedItem = (MemcachedItem)cacheItem.Value;

            if (memCachedItem != null)
            {
                ObjectArrayData objectDataArray = GetObjectArrayData(memCachedItem.Data);

                string tempObjectString = "";
                try
                {
                    tempObjectString = Encoding.ASCII.GetString(objectDataArray.dataBytes);
                }
                catch (Exception e)
                {
                    ThrowCacheRuntimeException(e);
                }

                if (IsUnsignedNumeric(tempObjectString))
                {
                    ulong originalValue = Convert.ToUInt64(tempObjectString);
                    ulong finalValue;

                    if (updateType == UpdateType.Increment)
                    {
                        finalValue = originalValue + value;
                    }
                    else
                    {
                        if (value > originalValue)
                        {
                            finalValue = 0;
                        }
                        else
                        {
                            finalValue = originalValue - value;
                        }
                    }

                    try
                    {
                        MemcachedItem memCacheItem = new MemcachedItem();
                        memCacheItem.Data = CreateObjectArray(objectDataArray.flags, Encoding.ASCII.GetBytes(finalValue + ""));


                        ulong getVersion = GetLatestVersion();

                        memCacheItem.InternalVersion = getVersion;

                        cacheItem.Value = memCacheItem;

                        _cache.Insert(key, cacheItem);

                        returnObject.ReturnResult = Result.SUCCESS;
                        returnObject.Value        = getVersion;
                        returnObject.MutateResult = finalValue;
                    }
                    catch (Exception e)
                    {
                        ThrowCacheRuntimeException(e);
                    }
                }
                else
                {
                    returnObject.ReturnResult = Result.ITEM_TYPE_MISMATCHED;
                    returnObject.Value        = null;
                    returnObject.MutateResult = 0;
                }
            }
            return(returnObject);
        }
Esempio n. 4
0
		/// <summary>
		/// Increments/decrements the value at the specified key by inc.
		/// 
		/// Note that the server uses a 32-bit unsigned integer, and checks for
		/// underflow. In the event of underflow, the result will be zero.  Because
		/// Java lacks unsigned types, the value is returned as a 64-bit integer.
		/// The server will only decrement a value if it already exists;
		/// if a value is not found, -1 will be returned.
		/// 
		/// TODO: C# has unsigned types.  We can fix this.
		/// </summary>
		/// <param name="cmdname">increment/decrement</param>
		/// <param name="key">cache key</param>
		/// <param name="inc">amount to incr or decr</param>
		/// <param name="hashCode">if not null, then the int hashcode to use</param>
		/// <returns>new value or -1 if not exist</returns>
		private long IncrementOrDecrement(string cmdname, string key, long inc, object hashCode) 
		{
            if (memcachedProvider == null)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$", "localhost"));
                }
                return -1;
            }
            if (inc < 0)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error(GetLocalizedString("CLIENT_ERROR cannot increment or decrement non-numeric value"));
                }
                return -1;
            }
			try 
			{
                MutateOpResult opResult = new MutateOpResult();
                switch (cmdname)
                {
                    case "incr":
                        opResult = memcachedProvider.Increment(key, (ulong)inc, null, 0, 0);
                        break;
                    case "decr":
                        opResult = memcachedProvider.Decrement(key, (ulong)inc, null, 0, 0);
                        break;
                }
                // get result back
                string line = "";

                if (opResult.ReturnResult == Result.SUCCESS)
                    line = opResult.MutateResult.ToString();
                else
                    if (opResult.ReturnResult == Result.ITEM_NOT_FOUND)
                        line = "NOT_FOUND";
                    else
                        if(opResult.ReturnResult==Result.ITEM_TYPE_MISMATCHED)
                            line = "CLIENT_ERROR cannot increment or decrement non-numeric value";


				string cmd = cmdname + " " + key + " " + inc + "\r\n";
				if(log.IsDebugEnabled)
				{
					log.Debug(GetLocalizedString("incr-decr command").Replace("$$Cmd$$", cmd));
				}
				
				if(new Regex("\\d+").Match(line).Success) 
				{

					return long.Parse(line, new NumberFormatInfo());

				} 
				else if(NOTFOUND == line) 
				{
					if(log.IsInfoEnabled)
					{
						log.Info(GetLocalizedString("incr-decr key not found").Replace("$$Key$$", key));
					}
				} 
				else 
				{
					if(log.IsErrorEnabled)
					{
						log.Error(GetLocalizedString("incr-decr key error").Replace("$$Key$$", key));
					}
				}
			}
			catch(Exception e) 
			{
				// exception thrown
				if(log.IsErrorEnabled)
				{
					log.Error(GetLocalizedString("incr-decr IOException"), e);
				}
                if (e is System.OverflowException)
                    throw e;

			}
			return -1;
		}
Esempio n. 5
0
        private MutateOpResult UpdateIfNumeric(string key, CacheItem cacheItem, ulong value, UpdateType updateType)
        {
            MutateOpResult returnObject = new MutateOpResult();
            MemcachedItem memCachedItem = (MemcachedItem)cacheItem.Value;
            if (memCachedItem != null)
            {
                ObjectArrayData objectDataArray = GetObjectArrayData(memCachedItem.Data);

                string tempObjectString = "";
                try
                {
                    tempObjectString = Encoding.ASCII.GetString(objectDataArray.dataBytes);
                }
                catch (Exception e)
                {
                    ThrowCacheRuntimeException(e);
                }

                if (IsUnsignedNumeric(tempObjectString))
                {
                    ulong originalValue = Convert.ToUInt64(tempObjectString);
                    ulong finalValue;

                    if (updateType == UpdateType.Increment)
                    {
                        finalValue = originalValue + value;
                    }
                    else
                    {
                        if (value > originalValue)
                            finalValue = 0;
                        else
                            finalValue = originalValue - value;
                    }

                    try
                    {

                        MemcachedItem memCacheItem = new MemcachedItem();
                        memCacheItem.Data = CreateObjectArray(objectDataArray.flags, Encoding.ASCII.GetBytes(finalValue + ""));


                        ulong getVersion = GetLatestVersion();

                        memCacheItem.InternalVersion = getVersion;

                        cacheItem.Value = memCacheItem;

                        _cache.Insert(key, cacheItem);

                        returnObject.ReturnResult = Result.SUCCESS;
                        returnObject.Value = getVersion;
                        returnObject.MutateResult = finalValue;
                    }
                    catch (Exception e)
                    {
                        ThrowCacheRuntimeException(e);
                    }
                }
                else
                {
                    returnObject.ReturnResult = Result.ITEM_TYPE_MISMATCHED;
                    returnObject.Value = null;
                    returnObject.MutateResult = 0;
                }
            }
            return returnObject;
        }
Esempio n. 6
0
        private MutateOpResult Mutate(string key, ulong value, object initialValue, long expirationTimeInSeconds, ulong casUnique, UpdateType updateType)
        {
            if (string.IsNullOrEmpty(key)|| (initialValue != null && IsUnsignedNumeric(initialValue) == false))
                ThrowInvalidArgumentsException();

            MutateOpResult returnObject = new MutateOpResult();
            try
            {
               
                CacheItem getObject = _cache.GetCacheItem(key);
                if (getObject == null)
                {
                    if (initialValue == null || expirationTimeInSeconds == uint.MaxValue)
                    {
                        returnObject.ReturnResult = Result.ITEM_NOT_FOUND;
                        returnObject.Value = null;
                    }
                    else
                    {
                        OperationResult opResult = InsertItemSuccessfully(key, 10, expirationTimeInSeconds,
                        BitConverter.GetBytes(Convert.ToUInt32(initialValue)));
                        returnObject.Value = opResult.Value;
                        returnObject.ReturnResult = opResult.ReturnResult;
                        returnObject.MutateResult = Convert.ToUInt64(initialValue);
                    }
                }
                else
                {
                    MemcachedItem memCacheItem = (MemcachedItem)getObject.Value;
                    if ((casUnique > 0 && memCacheItem.InternalVersion == casUnique) || casUnique == 0)
                        returnObject = UpdateIfNumeric(key, getObject, value, updateType);
                    else
                    {
                        returnObject.ReturnResult = Result.ITEM_MODIFIED;
                        returnObject.Value = null;
                    }
                }
            }
            catch (Exception e)
            {
                ThrowCacheRuntimeException(e);
            }
            return returnObject;
        }