public NodeMeta(INodeFileStream stream, INode node, long parentOffset)
        {
            this.Offset   = stream.Offset;
            this.Root     = parentOffset == DiskNode.HEADER_BYTES;
            this.Accuracy = node.Accuracy.Next;

            if (node.Header == -1)
            {
                if (node.Next != null && node.Next.Any(n => n != null))
                {
                    this.Header = node.Next.Where(n => n != null).Select(nc => nc.Header).Distinct().SingleOrDefault();
                }
                else
                {
                    this.Header = -1;
                }
            }
            else
            {
                this.Header = node.Header;
            }
        }
 public SerializationResults(INodeFileStream stream, INode node, long parentOffset)
 {
     this.Meta.Add(new NodeMeta(stream, node, parentOffset));
 }
Beispiel #3
0
        internal SerializationResults Serialize(INodeFileStream lockedNodeFileStream, long ParentOffset = 0)
        {
            SerializationResults results = new SerializationResults(lockedNodeFileStream, this, ParentOffset);
            long thisOffset = results.Single().Offset;
            //parent 0 - 8

            byte childListSize = (byte)(ParentOffset == 0 ? 4 : 2);

            byte[] toWrite = new byte[DiskNode.NODE_SIZE + childListSize];

            ParentOffset.ToInt40Array().CopyTo(toWrite, 0);

            for (int i = 0; i < 2; i++)
            {
                BitConverter.GetBytes(Results[i]).CopyTo(toWrite, 5 + i * 2);
            }

            unchecked
            {
                toWrite[9] = (byte)Header;
            }

            BitConverter.GetBytes(Value).CopyTo(toWrite, 10);

            toWrite[12] = (byte)ChildHeader;

            BitConverter.GetBytes(SkipChildren).CopyTo(toWrite, 13);

            int nCount = ChildCount;

            byte[] childBytes;

            if (childListSize == 4)
            {
                childBytes = BitConverter.GetBytes(nCount);
            }
            else
            {
                if (nCount > ushort.MaxValue)
                {
                    throw new Exception($"Cant not exceed {ushort.MaxValue} children on non-root node");
                }
                else
                {
                    ushort usCount = (ushort)nCount;
                    childBytes = BitConverter.GetBytes(usCount);
                }
            }

            childBytes.CopyTo(toWrite, DiskNode.NODE_SIZE);

            lockedNodeFileStream.Write(toWrite);

            if (nCount > 0)
            {
                int skip = (nCount * DiskNode.NEXT_SIZE);

                long ChildListOffset = lockedNodeFileStream.Offset;

                byte[] skipBytes = new byte[skip];

                lockedNodeFileStream.Write(skipBytes);

                byte[] nextOffsets = new byte[nCount * DiskNode.NEXT_SIZE];

                int i;

                for (i = 0; i < nCount; i++)
                {
                    MemoryNode nextn = this.next[i];

                    byte[] offsetBytes;

                    if (nextn != null)
                    {
                        offsetBytes = lockedNodeFileStream.Offset.ToInt40Array();
                    }
                    else
                    {
                        offsetBytes = new byte[] { 0, 0, 0, 0, 0 };
                    }

                    offsetBytes.CopyTo(nextOffsets, i * DiskNode.NEXT_SIZE);

                    if (nextn != null)
                    {
                        results.AddRange(nextn.Serialize(lockedNodeFileStream, thisOffset));
                    }
                }

                long lastOffset = lockedNodeFileStream.Offset;

                lockedNodeFileStream.Seek(ChildListOffset - childBytes.Length - 5);

                lockedNodeFileStream.Write(lastOffset.ToInt40Array());

                lockedNodeFileStream.Seek(ChildListOffset);

                lockedNodeFileStream.Write(nextOffsets);

                lockedNodeFileStream.Seek(lastOffset);
            }

            return(results);
        }