Ejemplo n.º 1
0
        IndexItem GetIndexItem(long index)
        {
            IndexItem indexItem;

            var indexPage = _indexPageFactory.GetPage(GetIndexPageIndex(index));

            using (var stream = indexPage.GetReadStream(GetIndexItemOffset(index), IndexItemSize))
            {
                indexItem = IndexItem.ReadFromStream(stream);
            }
            _indexPageFactory.ReleasePage(indexPage.Index);

            return(indexItem);
        }
Ejemplo n.º 2
0
        private void PersistMetaData()
        {
            var metaPage = _metaPageFactory.GetPage(0);

            using (var writeStream = metaPage.GetWriteStream(0, _metaDataItemSize))
            {
                _metaData.WriteToStream(writeStream);
            }

            _metaPageFactory.ReleasePage(0);
        }
Ejemplo n.º 3
0
        public void Enqueue(ReadOnlySpan <byte> itemData)
        {
            lock (_lockObject)
            {
                // Throw or silently return if itemData is null?
                if (itemData == null)
                {
                    return;
                }

                if (itemData.Length > _dataPageSize)
                {
                    throw new ArgumentOutOfRangeException(nameof(itemData),
                                                          "Item data length is greater than queue data page size");
                }

                if (_tailDataItemOffset + itemData.Length > _dataPageSize) // Not enough space in current page
                {
                    _tailDataPageIndex++;
                    _tailDataItemOffset = 0;
                }

                // Get data page
                var dataPage = _dataPageFactory.GetPage(_tailDataPageIndex);

                // Get write stream
                using (var writeStream = dataPage.GetWriteStream(_tailDataItemOffset, itemData.Length))
                {
                    // Write data to write stream
                    writeStream.Write(itemData);
                }

                // Release our reference to the data page
                _dataPageFactory.ReleasePage(_tailDataPageIndex);

                // Udate index
                // Get index page
                var indexPage = _indexPageFactory.GetPage(GetIndexPageIndex(_metaData.TailIndex));

                // Get write stream
                using (var writeStream =
                           indexPage.GetWriteStream(GetIndexItemOffset(_metaData.TailIndex), _indexItemSize))
                {
                    var indexItem = new IndexItem
                    {
                        DataPageIndex = _tailDataPageIndex,
                        ItemOffset    = _tailDataItemOffset,
                        ItemLength    = itemData.Length
                    };
                    indexItem.WriteToStream(writeStream);
                }

                _indexPageFactory.ReleasePage(GetIndexPageIndex(_metaData.TailIndex));

                // Advance
                _tailDataItemOffset += itemData.Length;

                // Update meta data
                _metaData.TailIndex++;

                _queueMonitor.Update(_metaData.TailIndex);
                PersistMetaData();
            }
        }