/// <summary> /// 新建对象数组 /// </summary> /// <param name="identity">自增值</param> private void newArray(int identity) { int length = array.Length << 1; valueLock[] newArray = new valueLock[length > identity ? length : getArrayLength(identity)]; array.CopyTo(newArray, 0); array = newArray; }
/// <summary> /// 加载日志添加对象 /// </summary> /// <param name="value">添加的对象</param> /// <param name="logSize">日志字节长度</param> protected override void loadInsert(valueType value, int logSize) { int identity = value.PrimaryKey; if (identity > maxIdentity) { if (count == currentIndex) { if (array == null) { array = new valueLock[identityArrayLength]; } else if (currentIndex == array.Length) { valueLock[] newArray = new valueLock[array.Length << 1]; array.CopyTo(newArray, 0); array = newArray; } } else if (currentIndex == array.Length) { if ((count << 1) > array.Length) { valueLock[] newArray = new valueLock[array.Length << 1]; currentIndex = 0; foreach (valueLock cacheValue in array) { if (cacheValue.NotDelete) { newArray[currentIndex++] = cacheValue; } } array = newArray; } else { for (currentIndex = 0; array[currentIndex].NotDelete; ++currentIndex) { ; } for (int index = currentIndex; currentIndex != count; array[currentIndex++] = array[index]) { while (array[++index].IsDelete) { ; } } } } array[currentIndex++].Set(value, logSize); ++count; maxIdentity = identity; } else { log.Error.Add("自增值错误 " + identity.toString() + " <= " + maxIdentity.toString(), false, false); } }
/// <summary> /// 删除对象 /// </summary> /// <param name="key">关键字</param> /// <returns>被删除对象+日志字节长度,失败返回null</returns> protected override keyValue <valueType, int> delete(keyType key) { valueLock cacheValue = new valueLock(); Monitor.Enter(cacheLock); try { if (values != null && values.TryGetValue(key, out cacheValue)) { values.Remove(key); } } finally { Monitor.Exit(cacheLock); } return(new keyValue <valueType, int>(cacheValue.Value, cacheValue.LogSize)); }
/// <summary> /// 加载日志添加对象 /// </summary> /// <param name="value">添加的对象</param> /// <param name="logSize">日志字节长度</param> protected override void loadInsert(valueType value, int logSize) { int identity = value.PrimaryKey; if (array == null) { array = new valueLock[getArrayLength(identity)]; } else if (identity >= array.Length) { newArray(identity); } count += array[identity].SetIfNull(value, logSize); if (identity > currentIdentity) { currentIdentity = identity; } }
/// <summary> /// 根据关键字获取对象 /// </summary> /// <param name="key">关键字</param> /// <returns>对象数据,失败返回null</returns> private valueType get(keyType key) { valueLock valueLock = getNoLock(key); if (valueLock.Value == null) { Monitor.Enter(cacheLock); try { if (values != null) { values.TryGetValue(key, out valueLock); } } finally { Monitor.Exit(cacheLock); } } return(valueLock.Value); }
/// <summary> /// 获取操作队列锁 /// </summary> /// <param name="key">关键字</param> /// <returns>操作队列锁,失败返回null</returns> protected override object getLock(keyType key) { valueLock value = getNoLock(key); if (value.Value != null) { return(value.Lock); } Monitor.Enter(cacheLock); try { if (values != null && values.TryGetValue(key, out value)) { return(value.Lock); } } finally { Monitor.Exit(cacheLock); } return(null); }
/// <summary> /// 根据关键字获取数据 /// </summary> /// <param name="key">关键字</param> /// <param name="value">数据对象</param> /// <returns>是否存在对象</returns> protected override bool get(keyType key, ref valueType value) { valueLock valueLock = getNoLock(key); if (valueLock.Value == null) { Monitor.Enter(cacheLock); if (values != null) { values.TryGetValue(key, out valueLock); } Monitor.Exit(cacheLock); } if (valueLock.Value != null) { value = valueLock.Value; return(true); } return(false); }