Exemplo n.º 1
0
        /// <summary>
        /// Creates a memory mapped array.
        /// </summary>
        public Array(MemoryMap map, long length,
                     long accessorSize, int bufferSize, int cacheSize)
        {
            if (accessorSize < 0)
            {
                throw new ArgumentOutOfRangeException("accessorSize");
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            _map            = map;
            _accessors      = new List <MappedAccessor <T> >();
            _accessorSize   = accessorSize;
            _createAccessor = MemoryMap.GetCreateAccessorFuncFor <T>();

            // create first accessor.
            var accessor = _createAccessor(_map, _accessorSize);

            _accessors.Add(accessor);
            _length = length;

            _bufferSize              = bufferSize;
            _cachedBuffer            = null;
            _cachedBuffers           = new LRUCache <long, CachedBuffer>(cacheSize);
            _cachedBuffers.OnRemove += new LRUCache <long, CachedBuffer> .OnRemoveDelegate(buffer_OnRemove);

            var blockCount = (int)System.Math.Max(System.Math.Ceiling((double)length / _accessorSize), 1);

            for (int i = 1; i < blockCount; i++)
            {
                _accessors.Add(_createAccessor(_map, _accessorSize));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Make this index writable again by injecting a new memory map.
        /// </summary>
        public void MakeWritable(MemoryMap map)
        {
            if (!this.IsReadonly)
            {
                throw new InvalidOperationException("Index is already writable, check IsReadonly.");
            }

            var accessor = _accessors[0];

            _map                 = map;
            _createAccessor      = MemoryMap.GetCreateAccessorFuncFor <T>();
            _accessorSize        = accessor.Capacity; // the only way to handle this now.
            _nextPositionInBytes = _accessorSize;
            if (_accessorSize == 0)
            { // this was empty, start over.
                _accessors.Clear();
                _accessorSize = 1024;
            }
            if (!accessor.ElementSizeFixed)
            { // use the size in bytes.
                _accessorSizeElements = _accessorSize;
            }
            else
            { // use the size in elements.
                _accessorSizeElements = accessor.CapacityElements;
            }
            if (_accessors.Count == 0)
            { // was empty, create at least one.
                _accessors.Add(_createAccessor(_map, _accessorSizeElements));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a memory mapped array based on existing data.
        /// </summary>
        public Array(MappedAccessor <T> accessor, int bufferSize, int cacheSize)
        {
            _accessors = new List <MappedAccessor <T> >();
            _accessors.Add(accessor);
            _accessorSize   = accessor.CapacityElements;
            _createAccessor = null;

            // create first accessor.
            _length = accessor.CapacityElements;

            _bufferSize              = bufferSize;
            _cachedBuffer            = null;
            _cachedBuffers           = new LRUCache <long, CachedBuffer>(cacheSize);
            _cachedBuffers.OnRemove += new LRUCache <long, CachedBuffer> .OnRemoveDelegate(buffer_OnRemove);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new index.
 /// </summary>
 public Index(MemoryMap map, int accessorSize)
 {
     _map            = map;
     _createAccessor = MemoryMap.GetCreateAccessorFuncFor <T>();
     _accessorSize   = accessorSize;
     _accessors      = new System.Collections.Generic.List <MappedAccessor <T> >();
     _accessors.Add(_createAccessor(_map, _accessorSize));
     _accessorSize = _accessors[0].Capacity; // make sure to get the size in bytes, not elements for fixed-size structures.
     if (!_accessors[0].ElementSizeFixed)
     {                                       // use the size in bytes.
         _accessorSizeElements = _accessorSize;
     }
     else
     { // use the size in elements.
         _accessorSizeElements = _accessors[0].CapacityElements;
     }
     _accessorBytesLost = new System.Collections.Generic.List <long>();
     _accessorBytesLost.Add(0);
 }