Exemple #1
0
 /// <summary>
 /// Adds a meta value of a given category and entity to a cache set.
 /// </summary>
 /// <param name="entitykey"></param>
 /// <param name="metaCategory"></param>
 /// <param name="metaValue"></param>
 public void AddEntityMetaCategory(string entitykey, uint metaCategory, string metaValue, bool sorted = false)
 {
     _lock.EnterWriteLock();
     if (!_entityMetaValues.ContainsKey(metaCategory))
     {
         _entityMetaValues[metaCategory] = new ConcurrentDictionary <string, HashSet <string> >();
     }
     if (!_entityMetaValues[metaCategory].ContainsKey(entitykey))
     {
         _entityMetaValues[metaCategory][entitykey] = new HashSet <string>();
     }
     _entityMetaValues[metaCategory][entitykey].Add(metaValue);
     if (sorted)
     {
         if (!_setFlags.ContainsKey(metaCategory))
         {
             _setFlags[metaCategory] = new Dictionary <string, SetFlags>();
         }
         if (!_setFlags[metaCategory].ContainsKey(entitykey))
         {
             _setFlags[metaCategory][entitykey] = new SetFlags(false);
         }
         _setFlags[metaCategory][entitykey] = _setFlags[metaCategory][entitykey] = new SetFlags(true);
     }
     if (_lock.IsWriteLockHeld)
     {
         _lock.ExitWriteLock();
     }
 }
Exemple #2
0
        public static StoreResult Set(string id, uint customBits, byte[] data, DateTime expire, ulong changeId, SetFlags flags)
        {
            string[] spl = id.Split(Config.TagCharacter);
            string[] tags;
            string   itemId = spl[0];

            if (itemId.Length == 0 || data.Length > Config.MaxStorage)
            {
                return(StoreResult.NotStored);
            }

            if (spl.Length > 1)
            {
                tags = new string[spl.Length - 1];
                Array.Copy(spl, 1, tags, 0, tags.Length);
            }
            else
            {
                tags = null;
            }
            spl = null;             // not needed anymore so free memory as quick as possible

            lock (WriteLock) {
                if (Storage.ContainsKey(itemId))
                {
                    if ((flags & SetFlags.CheckChangeId) == SetFlags.CheckChangeId && Storage[itemId].LastChangeId != changeId)
                    {
                        return(StoreResult.Exists);
                    }
                    if ((flags & SetFlags.Replace) != SetFlags.Replace)
                    {
                        return(StoreResult.NotStored);
                    }
                    Delete(itemId, -1);
                }
                else if ((flags & SetFlags.Create) != SetFlags.Create)
                {
                    return(((flags & SetFlags.CheckChangeId) == SetFlags.CheckChangeId) ? StoreResult.NotFound : StoreResult.NotStored);
                }

                if (StoredSize + (ulong)data.Length > (ulong)Config.MaxStorage)
                {
                    bool clean = true;
                    while (StoredSize + (ulong)data.Length > (ulong)Config.MaxStorage)
                    {
                        if (clean)
                        {
                            Log.WriteLine(Log.Level.Debug, "'max_storage' limit reached. Cleaning up expired items");
                            // clean up all expired entries
                            List <string> toRemove = new List <string>();
                            DateTime      now      = DateTime.Now;
                            foreach (KeyValuePair <string, CacheItem> cpair in Storage)
                            {
                                if (cpair.Value.Expire <= now)
                                {
                                    toRemove.Add(cpair.Key);
                                }
                            }
                            foreach (string remId in toRemove)
                            {
                                Delete(remId, -1);
                            }
                            clean = false;
                        }
                        else
                        {
                            Log.WriteLine(Log.Level.Debug, "Cleaning up expired items did not give us enough memory. Cleaning up old items.");
                            // still not enough memory .. cleanup oldest used entries
                            IOrderedEnumerable <KeyValuePair <string, CacheItem> > sorted = Storage.OrderBy(p => p.Value.LastUse);
                            long toFree = (long)data.Length - (Config.MaxStorage - (long)StoredSize);
                            foreach (KeyValuePair <string, CacheItem> cpair in sorted)
                            {
                                toFree -= cpair.Value.Data.Length;
                                Delete(cpair.Key, -1);
                                if (toFree <= 0)
                                {
                                    break;
                                }
                            }
                            if (toFree > 0)
                            {
                                Log.WriteLine(Log.Level.Important, "Seems there was a memory leak. Flushing whole cache.");
                                Flush(-1);
                                break;
                            }
                        }
                        // too much ... need to clean up a bit
                    }
                }
                CacheItem item = new CacheItem(tags, customBits, data, expire, unchecked (NextChangeId++));
                Storage.Add(itemId, item);
                StoredSize += (ulong)data.Length;
                if (tags != null)
                {
                    Tag tag;
                    foreach (string tagId in tags)
                    {
                        if (Tags.ContainsKey(tagId))
                        {
                            tag = Tags[tagId];
                        }
                        else
                        {
                            tag = new Tag();
                            Tags.Add(tagId, tag);
                        }
                        tag.Items.Add(itemId);
                    }
                }
            }
            return(StoreResult.Stored);
        }
Exemple #3
0
 public static extern int DevicePowerSetDeviceState(string deviceDescription, SetFlags setFlags, IntPtr setData);