Example #1
0
        /// <summary>
        /// Internal prepare
        /// </summary>
        /// <param name="readService"></param>
        /// <param name="data"></param>
        /// <param name="subOps"></param>
        unsafe void PrepareInternal(IReadService readService, List <DeleteChild> subOps)
        {
            // We inspect children.
            ulong childrenTS;
            Block block = readService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                childrenTS = header->ChildrenBTree;
            }

            // We check children count.
            BPlusTree         tree    = new BPlusTree(childrenTS);
            List <ObjectInfo> objects = tree.ListAll(readService);

            // We update stage index/count.
            for (int j = 0; j < objects.Count; j++)
            {
                BlockStream childTagStream = BlockStream.FromBase(objects[j].Address, readService);
                ChildTag    childTag       = Common.DeserializeFromArray(childTagStream.Read(objects[j].Size)) as ChildTag;

                foreach (KeyValuePair <string, ulong> child in childTag.Children)
                {
                    DeleteChild subOp = new DeleteChild(child.Value, childrenTS, child.Key);
                    subOps.Add(subOp);
                }
            }

            subOps.Add(this);
        }
Example #2
0
        public unsafe IDriverNode Find(string path)
        {
            // 1) Locate children TS first.
            ulong childrenTS;
            Block block = journal.ReadService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                childrenTS = header->ChildrenBTree;
            }

            // 2) Search in B+ tree.
            BPlusTree  tree = new BPlusTree(childrenTS);
            ObjectInfo info = tree.Find(journal.ReadService, (uint)path.GetHashCode());

            if (info == null)
            {
                return(null);
            }

            // 3) Find the actual object address.
            BlockStream stream   = BlockStream.FromBase(info.Address, journal.ReadService);
            ChildTag    childTag = Common.DeserializeFromArray(stream.Read(info.Size)) as ChildTag;
            ulong?      address  = childTag.Find(path);

            if (!address.HasValue)
            {
                return(null);                   // May only match hash value.
            }
            // Return the physical node (most current version).
            return(new PhysicalNode(path, address.Value, journal));
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PhysicalNode"/> class.
        /// </summary>
        /// <param name="commonAddress">The common address.</param>
        /// <param name="journal">The journal.</param>
        public unsafe PhysicalNode(string name, ulong commonAddress, Journalling.IJournal journal)
        {
            this.name          = name;
            this.commonAddress = commonAddress;
            this.journal       = journal;

            // We must extract most current version.
            Block block = journal.ReadService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                this.versionAddress = header->CurrentVersionAddress;
            }
        }
        public unsafe void Prepare(IReadService readService, out OperationStartupData data)
        {
            Block block = readService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                version     = header->CurrentVersionNumber;
                versionTree = new BPlusTree(header->VersionsBTree);
            }

            // We estimate how must we need.
            uint allocations = 15; //< Approximate estimate.

            data = new OperationStartupData(commonAddress, allocations, true);
        }
Example #5
0
        public unsafe IDriverNode CreateChild(string name, string defaultType, StreamOptions flags)
        {
            ulong childTS;
            Block block = journal.ReadService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                childTS = header->ChildrenBTree;
            }

            Operations.CreateChild op = new Operations.CreateChild(childTS, commonAddress, name, defaultType, flags);
            journal.Execute(op);

            // We create it now.
            return(new PhysicalNode(name, op.CommonAddress, op.VersionAddress, journal));
        }
Example #6
0
        public unsafe void DeleteChild(string name)
        {
            // 1) Find children TS.
            ulong childTS = 0;
            Block block   = journal.ReadService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                childTS = header->ChildrenBTree;
            }

            // 2) Find child.
            BPlusTree   tree         = new BPlusTree(childTS);
            ObjectInfo  info         = tree.Find(journal.ReadService, (uint)name.GetHashCode());
            BlockStream stream       = BlockStream.FromBase(info.Address, journal.ReadService);
            ChildTag    tag          = Common.DeserializeFromArray(stream.Read(info.Size)) as ChildTag;
            ulong?      childAddress = tag.Find(name);

            // Execute operation
            Operations.DeleteChild deleteChild = new DeleteChild(childAddress.Value, childTS, name);
            journal.Execute(deleteChild);
        }
Example #7
0
        public unsafe IDriverNode GetVersion(ulong version)
        {
            // 1) Extract version typed stream.
            ulong versionTS;
            Block block = journal.ReadService.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                versionTS = header->VersionsBTree;
            }

            // 2) Search for version
            BPlusTree  versionTree = new BPlusTree(versionTS);
            ObjectInfo info        = versionTree.Find(journal.ReadService, (uint)version.GetHashCode());

            if (info == null)
            {
                return(null);
            }

            // 3) Locate the version.
            BlockStream versionTagStream = BlockStream.FromBase(info.Address, journal.ReadService);
            VersionTag  versionTag       = Common.DeserializeFromArray(versionTagStream.Read(info.Size)) as VersionTag;

            ulong?rVersionAddress = versionTag.Find(version);

            if (!rVersionAddress.HasValue)
            {
                return(null);
            }

            // 4) Return the node.
            return(new PhysicalNode(name, commonAddress, rVersionAddress.Value, journal));
        }
Example #8
0
        /// <summary>
        /// Executes the operation.
        /// </summary>
        /// <param name="service">Service for operation.</param>
        unsafe void ExecuteInternal(IService service)
        {
            ulong childrenTS;
            ulong versionTS;

            // 1) We extract child TS and version TS
            Block block = service.Read(BlockType.NodeHeaderBlock, commonAddress);

            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                childrenTS = header->ChildrenBTree;
                versionTS  = header->VersionsBTree;
            }

            // 2) Can get rid of node.
            service.DeAllocate(commonAddress);

            // 3) We delete children TS, it should be empty (since deletes were already called on nodes).
            service.DeAllocate(childrenTS);

            // 4) We go through versions.
            BPlusTree         versionsTree = new BPlusTree(versionTS);
            List <ObjectInfo> versions     = versionsTree.ListAll(service);

            // 5) We delete each version.
            foreach (ObjectInfo info in versions)
            {
                // a) Read version tag.
                BlockStream versionTagStream = BlockStream.FromBase(info.Address, service);
                VersionTag  versionTag       = Common.DeserializeFromArray(versionTagStream.Read(info.Size)) as VersionTag;
                versionTagStream.Deallocate();

                foreach (KeyValuePair <ulong, ulong> versionToNode in versionTag.VersionAddress)
                {
                    block = service.Read(BlockType.NodeHeaderBlock, versionToNode.Value);

                    List <ulong> typedStreams = NodeVersionHelper.ListAllTypedStreamsAsAddresses(block, service);

                    // b) Delete all typed streams.
                    for (int i = 0; i < typedStreams.Count; i++)
                    {
                        // 1) We delete the typed stream object.
                        block = service.Read(BlockType.TypedStreamHeader, typedStreams[i]);
                        fixed(byte *p = block.Data)
                        {
                            TypedStreamHeader *header = (TypedStreamHeader *)p;

                            // We delete single object.
                            if ((header->Options & StreamOptions.SingleObject) != 0)
                            {
                                if (header->ObjectsAddress != 0)
                                {
                                    BlockStream stream = BlockStream.FromBase(header->ObjectsAddress, service);
                                    stream.Deallocate();
                                }
                            }
                            else
                            {
                                // We delete all children.
                                BPlusTree tree = new BPlusTree(header->ObjectsAddress);
                                foreach (ObjectInfo info2 in tree.ListAll(service))
                                {
                                    BlockStream stream = BlockStream.FromBase(info2.Address, service);
                                    stream.Deallocate();
                                }
                            }
                        }

                        // 2) We also delete the header itself.
                        service.DeAllocate(typedStreams[i]);
                    }

                    // c) We deallocate version block.
                    service.DeAllocate(versionToNode.Value);
                }
            }

            // 6) We delete the tree.
            versionsTree.DeallocateTree(service);

            // 7) We must erase the node from root.
            ObjectInfo  childInfo      = parentChildTree.Find(service, (uint)childName.GetHashCode());
            BlockStream childTagStream = BlockStream.FromBase(childInfo.Address, service);

            byte[]   childTagData = childTagStream.Read(childInfo.Size);
            ChildTag childTag     = Common.DeserializeFromArray(childTagData) as ChildTag;

            childTagStream.Deallocate();

            if (childTag.Children.Count == 1)
            {
                // Simply delete it.
                parentChildTree.Remove(service, (uint)childName.GetHashCode(), 1, false);
            }
            else
            {
                // We have to replace it.
                childTag.Remove(childName);
                childTagData   = Common.SerializeToArray(childTag);
                childTagStream = service.AllocationContext.CreateBlockStream((ulong)childTagData.LongLength);
                childTagStream.Write(childTagData);

                parentChildTree.Replace(service, new ObjectInfo((uint)childName.GetHashCode(),
                                                                (ulong)childTagData.LongLength, childTagStream.BaseAddress));
            }
        }
Example #9
0
        public unsafe void Execute(uint stage, IService service)
        {
            // 1) We create the node common block.
            versionAddress = service.AllocationContext.AllocateBlock();
            commonAddress  = service.AllocationContext.AllocateBlock();
            BPlusTree versionTS = new BPlusTree(service.AllocationContext.CreateEmptyBTree());

            // Fill common block and write.
            Block commonBlock = new Block(service.BlockSize);

            fixed(byte *p = commonBlock.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                header->ChildrenBTree         = service.AllocationContext.CreateEmptyBTree();
                header->CurrentVersionAddress = versionAddress;
                header->CurrentVersionNumber  = 0;
                header->HeaderMagic           = NodeCommonHeader.Magic;
                header->ParentAddress         = parentAddress;
                header->VersionsBTree         = versionTS.RootAddress;
            }

            service.Write(BlockType.NodeHeaderBlock, commonAddress, commonBlock);

            // 2) We create the node default typed stream.
            Block tsblock = new Block(service.BlockSize);

            ulong tsBlock = service.AllocationContext.AllocateBlock();

            fixed(byte *p = tsblock.Data)
            {
                TypedStreamHeader *header = (TypedStreamHeader *)p;

                header->ObjectsAddress = (defaultTSOptions & StreamOptions.SingleObject) == 0 ?
                                         service.AllocationContext.CreateEmptyBTree() : 0;
                header->Options    = defaultTSOptions;
                header->ObjectSize = 0;

                int i;

                for (i = 0; i < defaultTSType.Length; i++)
                {
                    header->Type[i] = defaultTSType[i];
                }

                // Null terminate it.
                header->Type[i] = '\0';
            }

            service.Write(BlockType.TypedStreamHeader, tsBlock, tsblock);

            // 3) We create the node version block.
            // Fill version block and write.
            Block versionBlock = new Block(service.BlockSize);

            fixed(byte *pp = versionBlock.Data)
            {
                NodeVersionHeader *header = (NodeVersionHeader *)pp;

                header->CreationTime       = DateTime.Now;
                header->DefaultTypedStream = 0;
                header->HeaderMagic        = NodeVersionHeader.Magic;
                header->ModifiedTime       = DateTime.Now;
                header->NodeCommonAddress  = commonAddress;
                header->StreamCount        = 0; //< Is actually one but NodeHelper changes this.
                header->VersionNumber      = 0;
            }

            // We must add default typed stream to block.
            NodeVersionHelper.AddTypedStream(tsBlock, defaultTSType, versionBlock);
            service.Write(BlockType.NodeHeaderBlock, versionAddress, versionBlock);

            // 4) We add to children tree.
            ObjectInfo info = childrenTree.Find(service, (uint)childName.GetHashCode());

            if (info == null)
            {
                // We simply create a new object at address.
                ChildTag childTag = new ChildTag();
                childTag.HashCode = childName.GetHashCode();
                childTag.Add(childName, commonAddress);
                byte[] childData = Common.SerializeToArray(childTag);

                // We write it to block stream.
                BlockStream stream = service.AllocationContext.CreateBlockStream((ulong)childData.LongLength);
                stream.Write(childData);

                // We now add it to B+ tree.
                childrenTree.Add(service, new ObjectInfo((uint)childName.GetHashCode(), (ulong)childData.LongLength, stream.BaseAddress));
            }
            else
            {
                // The index at hash already exists, we must add it, first read it.
                BlockStream stream   = BlockStream.FromBase(info.Address, service);
                ChildTag    childTag = Common.DeserializeFromArray(stream.Read(info.Size)) as ChildTag;

                // Steam not used anymore.
                stream.Deallocate();

                // We modify child tag.
                childTag.Add(childName, commonAddress);
                byte[] childData = Common.SerializeToArray(childTag);

                // We must write it to new address.
                stream = service.AllocationContext.CreateBlockStream((ulong)childData.LongLength);
                stream.Write(childData);

                // Now we replace the data in tree.
                childrenTree.Replace(service,
                                     new ObjectInfo((uint)childName.GetHashCode(), (ulong)childData.LongLength, stream.BaseAddress));
            }

            // 5) Add version to typed stream.
            VersionTag versionTag = new VersionTag((0).GetHashCode());

            versionTag.Add(0, versionAddress);
            byte[] versionTagData = Common.SerializeToArray(versionTag);

            // Write to stream.
            BlockStream versionTagStream = service.AllocationContext.CreateBlockStream((ulong)versionTagData.LongLength);

            versionTagStream.Write(versionTagData);

            versionTS.Add(service, new ObjectInfo((uint)(0).GetHashCode(),
                                                  (ulong)versionTagData.LongLength, versionTagStream.BaseAddress));
        }
Example #10
0
        public unsafe void Execute(uint stage, IService service)
        {
            string defaultTSType = typeof(object).FullName;

            // 1) We create the node common block.
            ulong     versionAddress = service.AllocationContext.AllocateBlock();
            ulong     commonAddress  = service.AllocationContext.AllocateBlock();
            BPlusTree versionTS      = new BPlusTree(service.AllocationContext.CreateEmptyBTree());

            // Fill common block and write.
            Block commonBlock = new Block(service.BlockSize);

            fixed(byte *p = commonBlock.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                header->ChildrenBTree         = service.AllocationContext.CreateEmptyBTree();
                header->CurrentVersionAddress = versionAddress;
                header->CurrentVersionNumber  = 0;
                header->HeaderMagic           = NodeCommonHeader.Magic;
                header->ParentAddress         = 0;
                header->VersionsBTree         = versionTS.RootAddress;
            }

            service.Write(BlockType.NodeHeaderBlock, commonAddress, commonBlock);

            // 2) We create the node default typed stream.
            Block tsblock = new Block(service.BlockSize);

            fixed(byte *p = tsblock.Data)
            {
                TypedStreamHeader *header = (TypedStreamHeader *)p;

                header->ObjectsAddress = service.AllocationContext.AllocateBlock();
                header->Options        = StreamOptions.None;
                header->ObjectSize     = 0;

                int i;

                for (i = 0; i < defaultTSType.Length; i++)
                {
                    header->Type[i] = defaultTSType[i];
                }

                // Null terminate it.
                header->Type[i] = '\0';
            }

            ulong tsBlock = service.AllocationContext.AllocateBlock();

            service.Write(BlockType.TypedStreamHeader, tsBlock, tsblock);

            // 3) We create the node version block.
            // Fill version block and write.
            Block versionBlock = new Block(service.BlockSize);

            fixed(byte *pp = versionBlock.Data)
            {
                NodeVersionHeader *header = (NodeVersionHeader *)pp;

                header->CreationTime       = DateTime.Now;
                header->DefaultTypedStream = 0;
                header->HeaderMagic        = NodeVersionHeader.Magic;
                header->ModifiedTime       = DateTime.Now;
                header->NodeCommonAddress  = commonAddress;
                header->StreamCount        = 0; //< Is actually one but NodeHelper changes this.
                header->VersionNumber      = 0;
            }

            // We must add default typed stream to block.
            NodeVersionHelper.AddTypedStream(tsBlock, defaultTSType, versionBlock);
            service.Write(BlockType.NodeHeaderBlock, versionAddress, versionBlock);

            // 4) We must create versions
            VersionTag versionTag = new VersionTag((0).GetHashCode());

            versionTag.Add(0, versionAddress);
            byte[] versionTagData = Common.SerializeToArray(versionTag);

            // Write to stream.
            BlockStream versionTagStream = service.AllocationContext.CreateBlockStream((ulong)versionTagData.LongLength);

            versionTagStream.Write(versionTagData);

            versionTS.Add(service, new ObjectInfo((uint)(0).GetHashCode(),
                                                  (ulong)versionTagData.LongLength, versionTagStream.BaseAddress));

            // 5) We must write to database header.
            Block block = service.Read(BlockType.NoCache, 0);

            fixed(byte *ppp = block.Data)
            {
                DatabaseHeader *header = (DatabaseHeader *)ppp;

                header->RootObjectAddress = commonAddress;
            }

            // Write the header.
            service.Write(BlockType.NoCache, 0, block);
        }
Example #11
0
        public unsafe void Execute(uint stage, IService service)
        {
            ulong versionAddress = service.AllocationContext.AllocateBlock();

            // 1) We first create new version node.
            Block versionBlock = new Block(service.BlockSize);

            fixed(byte *p = versionBlock.Data)
            {
                NodeVersionHeader *header = (NodeVersionHeader *)p;

                header->CreationTime       = DateTime.Now;
                header->DefaultTypedStream = 0;
                header->HeaderMagic        = NodeVersionHeader.Magic;
                header->ModifiedTime       = DateTime.Now;
                header->NodeCommonAddress  = commonAddress;
                header->StreamCount        = 0; //< For now 0.
                header->VersionNumber      = version;
            }

            // 2) We create default TS.
            ulong defaultTSAddress = service.AllocationContext.AllocateBlock();
            Block block            = new Block(service.BlockSize);

            fixed(byte *p = block.Data)
            {
                TypedStreamHeader *header = (TypedStreamHeader *)p;

                header->ObjectsAddress = (defaultTSOptions & StreamOptions.SingleObject) != 0
                    ? 0 : service.AllocationContext.CreateEmptyBTree();
                header->ObjectSize = 0;
                header->Options    = defaultTSOptions;

                // Copy the name.
                int i;

                for (i = 0; i < defaultTSType.Length; i++)
                {
                    header->Type[i] = defaultTSType[i];
                }
                header->Type[i] = '\0';
            }

            service.Write(BlockType.TypedStreamHeader, defaultTSAddress, block);

            // 3) We add the default typed stream and write it.
            NodeVersionHelper.AddTypedStream(defaultTSAddress, defaultTSType, versionBlock);
            service.Write(BlockType.NodeHeaderBlock, versionAddress, versionBlock);

            // 4) And finnaly add to versions.
            ObjectInfo info = versionTree.Find(service, (uint)version.GetHashCode());

            if (info == null)
            {
                // Create block stream.
                VersionTag versionTag = new VersionTag(version.GetHashCode());
                versionTag.Add(version, versionAddress);
                byte[]      versionTagData = Common.SerializeToArray(versionTag);
                BlockStream stream         = service.AllocationContext.CreateBlockStream((ulong)versionTagData.LongLength);

                // Write the stream.
                stream.Write(versionTagData);

                // And to B+ tree.
                versionTree.Add(service, new ObjectInfo((uint)version.GetHashCode(),
                                                        (ulong)versionTagData.LongLength, stream.BaseAddress));;
            }
            else
            {
                // We first create appropriate VersionTag and kill the stream.
                BlockStream stream     = BlockStream.FromBase(info.Address, service);
                VersionTag  versionTag = Common.DeserializeFromArray(stream.Read(info.Size)) as VersionTag;
                versionTag.Add(version, versionAddress);
                stream.Deallocate();

                // We write a new version tag.
                byte[] versionTagData = Common.SerializeToArray(versionTag);
                stream = service.AllocationContext.CreateBlockStream((ulong)versionTagData.LongLength);
                stream.Write(versionTagData);

                // We replace in B+ tree.
                versionTree.Replace(service, new ObjectInfo((uint)version.GetHashCode(),
                                                            (ulong)versionTagData.LongLength, versionAddress));
            }

            // 5) The new version is replaced in common.
            block = service.Read(BlockType.NodeHeaderBlock, commonAddress);
            fixed(byte *p = block.Data)
            {
                NodeCommonHeader *header = (NodeCommonHeader *)p;

                header->CurrentVersionNumber  = version;
                header->CurrentVersionAddress = versionAddress;
            }

            service.Write(BlockType.NodeHeaderBlock, commonAddress, block);
        }