public ArrayStorage(string fullPath)
        {
            this.fullPath = fullPath;

            locker      = new ValueLock();
            cacheByte   = new DictionarySafe <long, byte[]>();
            cacheString = new DictionarySafe <long, string>();

            var inf = new FileInfo(fullPath);

            poolReaders = new Pool <FileStream>();
            poolWriters = new Pool <FileStream>();

            vars = new AsVars(fullPath + ".var");
            list = new ArrayStorageList(fullPath + ".asl");

            if (!inf.Exists)
            {
                if (!inf.Directory.Exists)
                {
                    inf.Directory.Create();
                }

                inf.Create().Close();

                // Выставляем первоначальный размер, что бы не дупустить запись по 0 адресу
                vars.Length = 10;
            }

            if (inf.IsReadOnly)
            {
                inf.IsReadOnly = false;
            }
        }
Beispiel #2
0
        // Попытка запустить базу данных
        public void Start()
        {
            try
            {
                ValueLock.Lock(ref locker);

                if (Status != DatabaseStatus.Ready)
                {
                    return;
                }

                GetStatus();

                if (Status == DatabaseStatus.Ready)
                {
                    Storage = new ArrayStorage(DirectoryPath + "DefaultStorage.sto");
                    Index   = new Index(DirectoryPath + "DefaultIndex.ind");

                    OnStart();

                    Status = DatabaseStatus.Started;
                }
            }
            finally
            {
                locker = 0;
            }
        }
        public SortedList(int capacity, ISortedListComparer <T> comparer)
        {
            this.capacity = capacity / 2;
            this.comparer = comparer;

            totalLength = 0;
            free        = 0;
            locker      = new ValueLock();
            catalog     = new SortedTreeNode[this.capacity];

            CreateNode(0, 0, 0, 0);
        }
Beispiel #4
0
 /// <summary>
 /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 /// </summary>
 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
 ///                 </exception>
 public void Clear()
 {
     ValueLock.EnterWriteLock();
     try
     {
         Count = 0;
     }
     finally
     {
         ValueLock.ExitWriteLock();
     }
 }
Beispiel #5
0
 /// <summary>
 /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 /// </summary>
 /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 ///                 </param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
 ///                 </exception>
 public void Add(T item)
 {
     ValueLock.EnterWriteLock();
     try
     {
         AutoGrow      = true;
         base[Count++] = item;
         AutoGrow      = false;
     }
     finally
     {
         ValueLock.ExitWriteLock();
     }
 }
Beispiel #6
0
        public PageFile(string fullPath, int pageSize, PageFileIOMode ioMode)
        {
            this.fullPath = fullPath;
            this.pageSize = pageSize;
            this.ioMode = ioMode;

            Capacity = 1000;
            locker = new ValueLock();
            Key = fullPath.CalculateHashString();
            poolReaders = new Pool<PageFileReader>();
            poolWriters = new Pool<PageFileWriter>();

            var inf = new FileInfo(fullPath);

            // Создаем заголовок файла.
            if (!inf.Exists)
            {
                //fullPath.CheckPath();

                stream = CreateStream();
                stream.SetLength(HeaderSize + Capacity * pageSize);

                InitFile();

                header[1] = pageSize;
            }
            else if (inf.Length < HeaderSize)
            {
                throw new Exception("File corrupt.");
            }
            else
            {
                stream = CreateStream();

                InitFile();

                if (header[1] != pageSize)
                {
                    throw new Exception("Wrong page size.");
                }
            }

            if (inf.IsReadOnly)
            {
                inf.IsReadOnly = false;
            }

            deleted = new UInt32Stack(fullPath + ".del");
        }
Beispiel #7
0
        private async Task <T> GetOrSetInternalAsync <T>(string cacheKey, Func <Task <T> > asyncValueFactory)
        {
            var value = this.GetOrCreateCachedValue(cacheKey);

            if (!value.HasValue)
            {
                using (await ValueLock.EnterAsync(value).ConfigureAwait(false))
                {
                    if (!value.HasValue && asyncValueFactory != null)
                    {
                        value.SetValue(await asyncValueFactory().ConfigureAwait(false));
                    }
                }
            }

            return(value.GetValue <T>());
        }
Beispiel #8
0
 /// <summary>
 /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
 /// </summary>
 /// <returns>
 /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
 /// </returns>
 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 ///                 </param>
 public bool Contains(T item)
 {
     ValueLock.EnterReadLock();
     try
     {
         for (int i = 0; i < Count; i++)
         {
             if (this[i].Equals(item))
             {
                 return(true);
             }
         }
     }
     finally
     {
         ValueLock.ExitReadLock();
     }
     return(false);
 }
Beispiel #9
0
 /// <summary>
 /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
 /// </summary>
 /// <returns>
 /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
 /// </returns>
 /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.
 ///                 </param>
 public int IndexOf(T item)
 {
     ValueLock.EnterReadLock();
     try
     {
         int index;
         for (index = 0; index < Count; index++)
         {
             if (!this[index].Equals(item))
             {
                 continue;
             }
             return(index);
         }
         return(-1);
     }
     finally
     {
         ValueLock.ExitReadLock();
     }
 }
Beispiel #10
0
 public T this[int index]
 {
     get
     {
         ValueLock.EnterReadLock();
         try
         {
             if (index >= Count || index < 0)
             {
                 string msg = string.Format("Tried to access item outside the array boundaries. {0}/{1}", index, Count);
                 throw new ArgumentOutOfRangeException(msg);
             }
             return(base[index]);
         }
         finally
         {
             ValueLock.ExitReadLock();
         }
     }
     set
     {
         if (index < 0)
         {
             throw new ArgumentOutOfRangeException("Tried to access item outside the array boundaries");
         }
         try
         {
             ValueLock.EnterWriteLock();
             if (index >= Count)
             {
                 throw new ArgumentOutOfRangeException("Tried to access item outside the array boundaries");
             }
             base[index] = value;
         }
         finally
         {
             ValueLock.ExitWriteLock();
         }
     }
 }
Beispiel #11
0
        /// <summary>
        /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.
        ///                 </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
        ///                 </exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
        ///                 </exception>
        public void RemoveAt(int index)
        {
            ValueLock.EnterWriteLock();
            try
            {
                if ((index + 1) == Count)
                {
                    Count--;
                    return;
                }

                for (int i = index; i < Count - 1; i++)
                {
                    this[i] = this[i + 1];
                }
                Count--;
            }
            finally
            {
                ValueLock.ExitWriteLock();
            }
        }
Beispiel #12
0
        /// <summary>
        /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.
        ///                 </param><param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.
        ///                 </param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
        ///                 </exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
        ///                 </exception>
        public void Insert(int index, T item)
        {
            ValueLock.EnterWriteLock();
            try
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException("index", "invalid index");
                }

                Add(new T()); // make room for one more
                for (int i = Count - 1; i > index; i--)
                {
                    this[i] = this[i - 1];
                }
                this[index] = item;
            }
            finally
            {
                ValueLock.ExitWriteLock();
            }
        }
Beispiel #13
0
 /// <summary>
 /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
 /// </summary>
 /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.
 ///                 </param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.
 ///                 </param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.
 ///                 </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.
 ///                 </exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.
 ///                     -or-
 ///                 <paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.
 ///                     -or-
 ///                     The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.
 ///                     -or-
 ///                     Type cannot be cast automatically to the type of the destination <paramref name="array"/>.
 ///                 </exception>
 public void CopyTo(T[] array, int arrayIndex)
 {
     if (array == null)
     {
         throw new ArgumentNullException("array", "array is null");
     }
     if (arrayIndex < 0)
     {
         throw new ArgumentOutOfRangeException("arrayIndex", "index < 0");
     }
     ValueLock.EnterReadLock();
     try
     {
         if (Count > (array.Length - arrayIndex))
         {
             throw new ArgumentOutOfRangeException("array", "not enough room to copy");
         }
         CopyElementsToArray(array, arrayIndex);
     }
     finally
     {
         ValueLock.ExitReadLock();
     }
 }
Beispiel #14
0
 static Pool()
 {
     ht_glob     = new Dictionary <int, Dictionary <Type, Queue <T> > >(256);
     locker_glob = new ValueLock();
 }
Beispiel #15
0
 public Pool()
 {
     ht     = new Dictionary <int, Queue <T> >(256);
     locker = new ValueLock();
 }