void ICollection.CopyTo(Array array, int index) { if (array == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.array); } int length = array.Length; if ((index < 0) || (index > length)) { Thrower.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_Index); } int num2 = (length - index); if (num2 < _size) { Thrower.ThrowArgumentException(ExceptionResource.Argument_InvalidOfLen); } // If the head is above tha tail we need to do two copies. The first copy will get // the elements between the head and the end of the queue and the second copy will get // the elements between the top of the queue and the tail. if (_tail < _head) { int num3 = _array.Length - _head; Array.Copy(_array, _head, array, index, num3); Array.Copy(_array, 0, array, index + num3, _tail); } else { Array.Copy(_array, _head, array, index, _head - _tail); } }
/// <inheritdoc/> public bool GetIfPresent(string key, out T value) { if (key == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.key); } // TODO(neylor.silva) check if the cache is empty. long now = Clock.NanoTime; CacheEntry <T> entry = GetLiveEntry(key, now); if (entry != null) { value = entry.ValueReference.Value; if (!IsNull(value)) { // TODO(neylor.silva): record the cache hit. RecordRead(entry, now); value = ScheduleRefresh(entry, key, value, now, default_cache_loader_); return(true); } } else { // TODO(neylor.silva): record the cache misses } value = default(T); return(false); }
/// <inheritdoc/> public T Put(string key, T value) { if (key == null || IsNull(value)) { Thrower.ThrowArgumentNullException(key == null ? ExceptionArgument.key : ExceptionArgument.value); } lock (mutex_) { long now = Clock.NanoTime; CacheEntry <T> entry = cache_provider_.Get <CacheEntry <T> >(CacheKey(key)); if (entry != null) { // TODO(neylor.silva): notify the caller about the item removal // REPLACED. } else { // create a new entry entry = new CacheEntry <T>(key); } // add the clobber/new entry entry to cache. SetValue(entry, key, value, now); } return(value); }
/// <summary> /// Initializes a new instance of the <see cref="LoadingCache{T}"/> using the /// specified cache provider, builder and automatic loader. /// </summary> /// <param name="provider"> /// A <see cref="ICacheProvider"/> that is used to cache object. /// </param> /// <param name="builder"> /// A <see cref="CacheBuilder{T}"/> object that contains information about /// the cache configuration. /// </param> /// <param name="loader"> /// An <see cref="CacheLoader{T}"/> that is used to automatically load new /// items in cache when an item for a given key does not exists or is /// expired. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="provider"/>, <paramref name="builder"/> or /// <paramref name="loader"/> are <c>null</c>. /// </exception> public LoadingCache(ICacheProvider provider, CacheBuilder <T> builder, CacheLoader <T> loader) : this(provider, builder) { if (loader == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.loader); } default_cache_loader_ = loader; }
/// <summary> /// Trigger an event on every matched element /// </summary> /// <param name="selector">The name of the objects to trigger the event</param> /// <param name="evt">An event type to trigger</param> public void Trigger(string selector, string evt) { if (selector == null || evt == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.any); } sevens_.Enqueue( string.Concat( "{\"type\":\"trigger\",", "\"selector\":\"", selector, "\",", "\"evt\":\"", evt, "\"}") ); }
/// <summary> /// Initializes a new instance_ of the TransferQueue class that contains elements copied from the specified /// collection and has specified size. /// </summary> /// <param name="collection">The collection whose elements are copied to the new TransferQueue</param> /// <exception cref="ArgumentNullException">collection is null</exception> public TransferQueue(IEnumerable <T> collection, int size) : this(size) { if (collection == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.collection); } using (IEnumerator <T> enumerator = collection.GetEnumerator()) { while (enumerator.MoveNext()) { Enqueue(enumerator.Current); } } }
/// <summary> /// Set one or more CSS properties_ for the set of matched elements. /// </summary> /// <param name="selector">A string conaining a selector expression</param> /// <param name="cssattr">A CSS property name</param> /// <param name="value">A value to set for the property</param> public void SetCss(string selector, string property_name, string value) { if (selector == null || property_name == null || value == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.any); } sevens_.Enqueue( string.Concat( "{\"type\":\"setcss\",", "\"selector\":\"", selector, "\",", "\"cssattr\":\"", property_name, "\",", "\"value\":\"", value, "\"}") ); }
/// <summary> /// Set a attribute for the set of matched elements. /// </summary> /// <param name="selector">A string containing a selector expression</param> /// <param name="attribute">The name of the attribute to set</param> /// <param name="value">A value to set for the attribute</param> public void SetAttribute(string selector, string attribute, string value) { if (selector == null || attribute == null || value == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.any); } sevens_.Enqueue( string.Concat( "{\"type\":\"setattr\",", "\"selector\":\"", selector, "\",", "\"attribute\":\"", attribute, "\",", "\"value\":\"", value, "\"}") ); }
/// <summary> /// Initializes a new empty <see cref="AndersonTree{TKey,TValue}"/> /// class. /// </summary> /// <param name="comparer"> /// The comparer to use when comparing items. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="comparer"/> is <c>null</c>. /// </exception> public AndersonTree(IComparer <TKey> comparer) { if (comparer == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.comparer); } sentinel_ = new AndersonTreeNode <TKey, TValue>(); sentinel_.Level = 0; sentinel_.Right = sentinel_; sentinel_.Left = sentinel_; root_ = sentinel_; count_ = 0; comparer_ = comparer; key_is_value_type_ = typeof(TKey).IsValueType; }
/// <summary> /// Initializes a new instance of the <see cref="LoadingCache{T}"/> class /// by using the specified cache provider and builder. /// </summary> /// <param name="provider"> /// A <see cref="ICacheProvider"/> that is used to cache object. /// </param> /// <param name="builder"> /// A <see cref="CacheBuilder{T}"/> object that contains information about /// the cache configuration. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="provider"/> or <paramref name="builder"/> are /// <c>null</c>. /// </exception> public LoadingCache(ICacheProvider provider, CacheBuilder <T> builder) { if (provider == null || builder == null) { Thrower.ThrowArgumentNullException(provider == null ? ExceptionArgument.provider : ExceptionArgument.builder); } cache_provider_ = provider; cache_guid_ = Guid.NewGuid().ToString("N"); expire_after_access_nanos_ = builder.ExpiryAfterAccessNanos; expire_after_write_nanos_ = builder.ExpiryAfterWriteNanos; refresh_nanos_ = builder.RefreshNanos; strength_type_ = builder.ValueStrength; mutex_ = new object(); t_is_value_type_ = typeof(T).IsValueType; default_cache_loader_ = CacheLoader <T> .From(delegate { throw new NotSupportedException(); }); }
/// <summary> /// Adds the <see cref="EventHandler{TEventArgs}"/> to the list of /// listeners to execute. If execution has already begun, the listener is /// executed immediately. /// </summary> /// <param name="runnable"></param> /// <param name="executor"></param> /// <exception cref="ArgumentNullException"> /// <paramref name="runnable"/> or <paramref name="executor"/> or are /// <c>null</c>. /// </exception> public void Add(RunnableDelegate runnable, IExecutor executor) { if (runnable == null || executor == null) { Thrower.ThrowArgumentNullException( (runnable == null) ? ExceptionArgument.runnable : ExceptionArgument.executor); } bool execute_immediate = false; // Lock while we check state. We must maitain the lock while adding the // new pair so that another thread can't run the list out from under us. // We only add to the list if we have not yet started execution. lock (runnables_) { if (!executed_) { SerialExecutorDelegatePair pair = new SerialExecutorDelegatePair(runnable, executor); runnables_.Enqueue(pair); } else { execute_immediate = true; } } // Execute the runnable immediately. Because of scheduling this may end // up getting called before some of the previously added runnables, but // we're OK with that. If we want to change the contract to guarantee // ordering among runnables we'd have to modify the logic here to allow // it. if (execute_immediate) { new SerialExecutorDelegatePair(runnable, executor).Execute(); } }
/// <summary> /// Gets the value associated with the given key, creating or retrieving /// that value if necessary. /// </summary> /// <param name="key">The identifier for the cache item to retrieve. /// </param> /// <param name="loader">A <see cref="CacheLoader{T}"/> object that could /// be used to create the value if it is not present in the cache.</param> /// <remarks> /// No state associated with this cache is modified until loading is /// complete. /// </remarks> /// <exception cref="ExecutionException">A failure occur while loading /// the item using the specified loader delegate.</exception> /// <exception cref="ArgumentNullException"><paramref name="key"/> or /// <paramref name="loader"/> are <c>null</c>.</exception> public T Get(string key, CacheLoader <T> loader) { if (key == null || loader == null) { Thrower.ThrowArgumentNullException( key == null ? ExceptionArgument.key : ExceptionArgument.loader); } try { T value; CacheEntry <T> entry; // the cache provider should provide the thread safeness behavior for // the reading operation. bool ok = cache_provider_.Get(CacheKey(key), out entry); if (ok) { long now = Clock.NanoTime; if (GetLiveValue(entry, now, out value)) { RecordRead(entry, now); return(ScheduleRefresh(entry, key, value, now, loader)); } IValueReference <T> value_reference = entry.ValueReference; if (value_reference.IsLoading) { return(WaitForLoadingValue(entry, key, value_reference)); } } // at this point entry does not exists or is expired, so lets load // the value. return(LockedGetOrLoad(key, loader)); } catch (Exception e) { MustLogger.ForCurrentProcess.Error(kTypeForLogger + "Get]", e); throw; } }
public void ThrowArgumentNullException() { Thrower.ThrowArgumentNullException(ExceptionArgument.any); }
/// <summary> /// Inserts an item to the AndersonTree /// </summary> /// <param name="key"> /// The key of the value to insert into the tree. /// </param> /// <param name="value"> /// The value to insert in the tree. /// </param> /// <param name="add"> /// A value indicating when the item will be added or modified. /// </param> void Insert(TKey key, TValue value, bool add) { if (!key_is_value_type_ && key == null) { Thrower.ThrowArgumentNullException(ExceptionArgument.key); } // empty tree if (root_.Level == 0) { root_ = new AndersonTreeNode <TKey, TValue>(key, value, sentinel_); count_++; return; } int cmp, dir; List <AndersonTreeNode <TKey, TValue> > path = new List <AndersonTreeNode <TKey, TValue> >(count_); AndersonTreeNode <TKey, TValue> node = root_; // Find the place to insert the item // - If the item is found and we trying to add a new one, // throw an exception - no duplicate items allowed. // - If a leaf is reached, insert the item in the correct place. // - Else, traverse the tree further. while (true) { path.Add(node); cmp = comparer_.Compare(key, node.Key); if (cmp == 0) { if (add) { Thrower.ThrowArgumentException( ExceptionResource.Argument_AddingDuplicate); } if (node.Level != 0) { node.Value = value; } return; } dir = (cmp < 0) ? 0 : 1; if (node.childs[dir].Level == 0) { break; } node = node.childs[dir]; } // create the new node node.childs[dir] = new AndersonTreeNode <TKey, TValue>(key, value, sentinel_); // Walk back and rebalance int top = path.Count; while (--top >= 0) { AndersonTreeNode <TKey, TValue> n = path[top]; // which child ? if (top != 0) { dir = (path[top - 1].Right == n) ? 1 : 0; } Skew(ref n); Split(ref n); // Fix the parent if (top != 0) { path[top - 1].childs[dir] = n; } else { root_ = n; } } count_++; }