Esempio n. 1
0
        public void DeleteTable(string name)
        {
            var tableTree = ReadTree(name, RootObjectType.Table);

            var writtenSchemaData     = tableTree.DirectRead(TableSchema.SchemasSlice);
            var writtenSchemaDataSize = tableTree.GetDataSize(TableSchema.SchemasSlice);
            var schema = TableSchema.ReadFrom(Allocator, writtenSchemaData, writtenSchemaDataSize);

            var table = OpenTable(schema, name);

            // delete table data

            table.DeleteByPrimaryKey(Slices.BeforeAllKeys, x =>
            {
                if (schema.Key.IsGlobal)
                {
                    return(table.IsOwned(x.Reader.Id));
                }

                return(true);
            });

            if (schema.Key.IsGlobal == false)
            {
                var pkTree = table.GetTree(schema.Key);

                DeleteTree(pkTree, isInRoot: false);

                tableTree.Delete(pkTree.Name);
            }

            // index trees should be already removed but just in case let's go over them and ensure they're really deleted

            foreach (var indexDef in schema.Indexes.Values)
            {
                if (indexDef.IsGlobal) // must not delete global indexes
                {
                    continue;
                }

                if (tableTree.Read(indexDef.Name) == null)
                {
                    continue;
                }

                var indexTree = table.GetTree(indexDef);

                DeleteTree(indexTree, isInRoot: false);

                tableTree.Delete(indexTree.Name);
            }

            foreach (var indexDef in schema.FixedSizeIndexes.Values)
            {
                if (indexDef.IsGlobal)  // must not delete global indexes
                {
                    continue;
                }

                if (tableTree.Read(indexDef.Name) == null)
                {
                    continue;
                }

                var index = table.GetFixedSizeTree(indexDef);

                DeleteFixedTree(index, isInRoot: false);

                tableTree.Delete(index.Name);
            }

            // raw data sections

            table.ActiveDataSmallSection.FreeRawDataSectionPages();

            if (tableTree.Read(TableSchema.ActiveCandidateSectionSlice) != null)
            {
                using (var it = table.ActiveCandidateSection.Iterate())
                {
                    if (it.Seek(long.MinValue))
                    {
                        var sectionPageNumber = it.CurrentKey;
                        var section           = new ActiveRawDataSmallSection(this, sectionPageNumber);

                        section.FreeRawDataSectionPages();
                    }
                }

                DeleteFixedTree(table.ActiveCandidateSection, isInRoot: false);
            }

            if (tableTree.Read(TableSchema.InactiveSectionSlice) != null)
            {
                DeleteFixedTree(table.InactiveSections, isInRoot: false);
            }

            DeleteTree(name);

            using (Slice.From(Allocator, name, ByteStringType.Immutable, out var nameSlice))
            {
                _tables.Remove(nameSlice);
            }
        }