Exemplo n.º 1
0
 public static void NullifyMixedPage(MixedPage page, ITransaction tran)
 {
     for (int i = 0; i < page.MaxRowCount(); i++)
     {
         RowHolder rhf = new RowHolder(new ColumnType[] { ColumnType.Int });
         rhf.SetField <int>(0, 0);
         page.Insert(rhf, tran);
     }
 }
Exemplo n.º 2
0
        public async Task <int> CreateObject(ColumnCreateDefinition def, ITransaction tran)
        {
            if (await this.Exists(def, tran))
            {
                throw new ElementWithSameNameExistsException();
            }

            RowHolder             rh          = new RowHolder(columnDefinitions);
            PagePointerOffsetPair namePointer = await this.stringHeap.Add(def.ColumnName.ToCharArray(), tran);

            rh.SetField <int>(0, def.ColumnId);
            rh.SetField <int>(1, def.TableId);
            rh.SetField <PagePointerOffsetPair>(2, namePointer);
            rh.SetField <int>(3, (int)def.ColumnType.ColumnType);
            rh.SetField <int>(4, def.ColumnType.RepCount);

            await pageListCollection.Add(rh, tran);

            return(def.ColumnId);
        }
Exemplo n.º 3
0
        public async Task <int> CreateObject(TableCreateDefinition def, ITransaction tran)
        {
            if (await this.Exists(def, tran))
            {
                throw new ElementWithSameNameExistsException();
            }

            if (def.ColumnNames.Length != def.ColumnTypes.Length)
            {
                throw new ArgumentException();
            }

            int id = 1;

            if (!(await pageListCollection.IsEmpty(tran)))
            {
                int maxId = await pageListCollection.Max <int>(rh => rh.GetField <int>(0), startMin : 0, tran);

                id = maxId + 1;
            }

            MixedPage rootPage = await this.pageAllocator.AllocateMixedPage(def.ColumnTypes, PageManagerConstants.NullPageId, PageManagerConstants.NullPageId, tran);

            RowHolder             rh          = new RowHolder(columnDefinitions);
            PagePointerOffsetPair namePointer = await this.stringHeap.Add(def.TableName.ToCharArray(), tran);

            rh.SetField <int>(0, id);
            rh.SetField <PagePointerOffsetPair>(1, namePointer);
            rh.SetField <long>(2, (long)rootPage.PageId());
            await pageListCollection.Add(rh, tran);

            for (int i = 0; i < def.ColumnNames.Length; i++)
            {
                ColumnCreateDefinition ccd = new ColumnCreateDefinition(id, def.ColumnNames[i], def.ColumnTypes[i], i);
                await columnManager.CreateObject(ccd, tran);
            }

            return(id);
        }
Exemplo n.º 4
0
        private void MetadataInitialSetup()
        {
            using ITransaction tran = this.logManager.CreateTransaction(this.pageAllocator, false, "MetadataSetup");

            MixedPage mdColumnsFirstPage = this.pageAllocator.AllocateMixedPage(MetadataColumnsManager.GetSchemaDefinition(), PageManagerConstants.NullPageId, PageManagerConstants.NullPageId, tran).Result;

            this.columnsManager = new MetadataColumnsManager(this.pageAllocator, mdColumnsFirstPage, this.stringHeap);

            MixedPage mdTableFirstPage = this.pageAllocator.AllocateMixedPage(MetadataTablesManager.GetSchemaDefinition(), PageManagerConstants.NullPageId, PageManagerConstants.NullPageId, tran).Result;

            this.tableManager = new MetadataTablesManager(this.pageAllocator, mdTableFirstPage, this.stringHeap, this.columnsManager);

            RowHolder rhf = new RowHolder(this.masterPageColumnDefinition);

            rhf.SetField <int>(0, (int)MetadataObjectEnum.MdTableId);
            rhf.SetField <long>(1, (long)mdTableFirstPage.PageId());

            masterMetadataCollection.Add(rhf, tran).Wait();
            rhf.SetField <int>(0, (int)MetadataObjectEnum.MdColumnId);
            rhf.SetField <long>(1, (long)mdColumnsFirstPage.PageId());
            masterMetadataCollection.Add(rhf, tran).Wait();

            tran.Commit().Wait();
        }
Exemplo n.º 5
0
        public void Set(int pos, ITransaction transaction)
        {
            if (pos >= this.MaxItemCount() || pos < 0)
            {
                throw new ArgumentException();
            }

            (int positionInIntArray, int offsetInIntArray) = (pos / (sizeof(int) * 8), pos % (sizeof(int) * 8));
            RowHolder rhf = new RowHolder(columnTypes);

            storage.At((ushort)positionInIntArray, transaction, ref rhf);

            byte[] val = BitConverter.GetBytes(rhf.GetField <int>(0));
            BitArray.Set(offsetInIntArray, val);
            int updatedVal = BitConverter.ToInt32(val);

            rhf.SetField <int>(0, updatedVal);

            storage.Update(rhf, (ushort)positionInIntArray, transaction);
        }
Exemplo n.º 6
0
        public async Task <PhyOpTableInsert> ParseInsertStatement(Sql.insertStatement insertStatement, ITransaction tran, InputStringNormalizer stringNormalizer)
        {
            string tableName = insertStatement.Table;

            MetadataTablesManager tableManager = metadataManager.GetTableManager();
            MetadataTable         table        = await tableManager.GetByName(tableName, tran).ConfigureAwait(false);

            ColumnInfo[] columnInfosFromTable = table.Columns.Select(mt => mt.ColumnType).ToArray();

            RowHolder rowHolder = new RowHolder(columnInfosFromTable);

            int colNum = 0;

            foreach (var value in insertStatement.Values)
            {
                if (value.IsFloat)
                {
                    if (columnInfosFromTable[colNum].ColumnType == ColumnType.Double)
                    {
                        rowHolder.SetField <double>(colNum, ((Sql.value.Float)value).Item);
                    }
                    else
                    {
                        throw new InvalidColumnTypeException();
                    }
                }
                else if (value.IsInt)
                {
                    if (columnInfosFromTable[colNum].ColumnType == ColumnType.Int)
                    {
                        rowHolder.SetField <int>(colNum, ((Sql.value.Int)value).Item);
                    }
                    else if (columnInfosFromTable[colNum].ColumnType == ColumnType.Double)
                    {
                        // Int can be cast to double without data loss.
                        rowHolder.SetField <double>(colNum, (double)((Sql.value.Int)value).Item);
                    }
                    else
                    {
                        throw new InvalidColumnTypeException();
                    }
                }
                else if (value.IsString)
                {
                    if (columnInfosFromTable[colNum].ColumnType == ColumnType.String)
                    {
                        // TODO: For string heap (strings of variable length separate logic is needed.

                        string input = ((Sql.value.String)value).Item;
                        input = stringNormalizer.ApplyReplacementTokens(input);
                        rowHolder.SetField(colNum, input.ToCharArray());
                    }
                    else
                    {
                        throw new InvalidColumnTypeException();
                    }
                }
                else
                {
                    throw new ArgumentException();
                }

                colNum++;
            }

            PhyOpStaticRowProvider opStatic = new PhyOpStaticRowProvider(rowHolder);

            PhyOpTableInsert op = new PhyOpTableInsert(table.Collection, opStatic);

            return(op);
        }
Exemplo n.º 7
0
        // TODO: This is just bad.
        // It is very hard to keep all type -> agg mappings.
        // Needs refactoring.
        public static void ApplyAgg(MetadataColumn mc, ref RowHolder inputRowHolder, Sql.aggType aggType, ref RowHolder stateRowHolder)
        {
            // TODO: Can't use ColumnId as fetcher.
            if (mc.ColumnType.ColumnType == ColumnType.Int)
            {
                int inputValue = inputRowHolder.GetField <int>(mc.ColumnId);
                int stateValue = stateRowHolder.GetField <int>(mc.ColumnId);

                if (aggType.IsMax)
                {
                    if (inputValue.CompareTo(stateValue) == 1)
                    {
                        // Update state.
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField <int>(mc.ColumnId, inputValue);
                    }
                }
                else if (aggType.IsMin)
                {
                    if (inputValue.CompareTo(stateValue) == -1)
                    {
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField <int>(mc.ColumnId, inputValue);
                    }
                }
                else if (aggType.IsSum)
                {
                    stateRowHolder.SetField <int>(mc.ColumnId, inputValue + stateValue);
                }
                else if (aggType.IsCount)
                {
                    stateRowHolder.SetField <int>(mc.ColumnId, 1 + stateValue);
                }
                else
                {
                    throw new InvalidProgramException("Aggregate not supported.");
                }
            }
            else if (mc.ColumnType.ColumnType == ColumnType.Double)
            {
                double inputValue = inputRowHolder.GetField <double>(mc.ColumnId);
                double stateValue = stateRowHolder.GetField <double>(mc.ColumnId);

                if (aggType.IsMax)
                {
                    if (inputValue.CompareTo(stateValue) == 1)
                    {
                        // Update state.
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField <double>(mc.ColumnId, (double)inputValue);
                    }
                }
                else if (aggType.IsMin)
                {
                    if (inputValue.CompareTo(stateValue) == -1)
                    {
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField <double>(mc.ColumnId, inputValue);
                    }
                }
                else if (aggType.IsSum)
                {
                    stateRowHolder.SetField <double>(mc.ColumnId, inputValue + stateValue);
                }
                else
                {
                    throw new InvalidProgramException("Aggregate not supported.");
                }
            }
            else if (mc.ColumnType.ColumnType == ColumnType.String)
            {
                // TODO: Since char[] doesn't implement IComparable need to cast it to string.
                // This is super slow...
                // Consider creating your own string type.
                string inputValue = new string(inputRowHolder.GetStringField(mc.ColumnId));
                string stateValue = new string(stateRowHolder.GetStringField(mc.ColumnId));

                if (aggType.IsMax)
                {
                    if (inputValue.CompareTo(stateValue) == 1)
                    {
                        // Update state.
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField(mc.ColumnId, inputValue.ToCharArray());
                    }
                }
                else if (aggType.IsMin)
                {
                    if (inputValue.CompareTo(stateValue) == -1)
                    {
                        // TODO: boxing/unboxing hurts perf.
                        stateRowHolder.SetField(mc.ColumnId, inputValue.ToCharArray());
                    }
                }
                else
                {
                    throw new InvalidProgramException("Aggregate not supported.");
                }
            }
            else
            {
                Debug.Fail("Invalid column type");
                throw new InvalidProgramException("Invalid state.");
            }
        }