Ejemplo n.º 1
0
        /*
         *
         *  Layout: [ Doc Guid (16 bytes) | PageLink[0] (5 bytes) | PageLink[1] (5 bytes) ] --> 26 bytes
         *  We can fit 157 in a 4k page. Gives us 6 ranks (126 entries) -> 3276 bytes
         *  Our pages are currently 4061 bytes, so we have plenty of spare space if we can find useful metadata to store.
         *
         *  We assume but don't store a root page with guid {127,127...,127}. The first two entries are 'left' and 'right' on the second level.
         *
         */

        public IndexPage()
        {
            _links = new VersionedLink[EntryCount];
            for (int i = 0; i < EntryCount; i++)
            {
                _links[i] = new VersionedLink();
            }

            _docIds = new Guid[EntryCount];
        }
Ejemplo n.º 2
0
        [NotNull] private VersionedLink GetLink(int headOffset)
        {
            var result = new VersionedLink();

            lock (_fslock)
            {
                _fs.Seek(MAGIC_SIZE + (VersionedLink.ByteSize * headOffset), SeekOrigin.Begin);
                result.Defrost(_fs);
            }
            return(result);
        }
Ejemplo n.º 3
0
        private void SetLink(int headOffset, VersionedLink value)
        {
            if (value == null)
            {
                throw new Exception("Attempted to set invalid header link");
            }
            var strm = value.Freeze();

            lock (_fslock)
            {
                _fs.Seek(MAGIC_SIZE + (VersionedLink.ByteSize * headOffset), SeekOrigin.Begin);
                strm.CopyTo(_fs);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Update a link to set an invalid link. Both versions of the link will be lost.
        /// Returns true if a change was made. False if the link was not found in this index page
        /// </summary>
        /// <param name="docId">ID of document to update</param>
        /// <returns></returns>
        public bool Remove(Guid docId)
        {
            // find the entry to update
            var index = Find(docId);

            if (index < 0 || index >= EntryCount)
            {
                return(false);                                  // not found
            }
            if (_docIds[index] == ZeroDocId)
            {
                return(false);                             // not found
            }
            if (_docIds[index] != docId)
            {
                throw new Exception("IndexPage.Remove: Logic error");
            }

            _links[index] = new VersionedLink(); // entirely reset
            return(true);
        }
Ejemplo n.º 5
0
        public static void InitialiseDb([NotNull] Stream fs)
        {
            if (!fs.CanWrite)
            {
                throw new Exception("Tried to initialise a read-only stream");
            }

            fs.Seek(0, SeekOrigin.Begin);
            foreach (var b in HEADER_MAGIC)
            {
                fs.WriteByte(b);
            }

            // write disabled links for the three core chains
            var indexVersion      = new VersionedLink();
            var pathLookupVersion = new VersionedLink();
            var freeListVersion   = new VersionedLink();

            indexVersion.Freeze().CopyTo(fs);
            pathLookupVersion.Freeze().CopyTo(fs);
            freeListVersion.Freeze().CopyTo(fs);
            Flush(fs);
        }
Ejemplo n.º 6
0
 private void SetFreeListLink(VersionedLink value)
 {
     SetLink(2, value);
 }
Ejemplo n.º 7
0
 private void SetPathLookupLink(VersionedLink value)
 {
     SetLink(1, value);
 }
Ejemplo n.º 8
0
 private void SetIndexPageLink(VersionedLink value)
 {
     SetLink(0, value);
 }