示例#1
4
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNative(IValueManager manager, Schema.IDataType dataType, object tempValue)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    return(new Scalar(manager, scalarType, (StreamID)tempValue));
                }
                if (scalarType.IsGeneric)
                {
                    return(new Scalar(manager, (Schema.ScalarType)manager.GetRuntimeType(tempValue.GetType()), tempValue));
                }
                return(new Scalar(manager, scalarType, tempValue));
            }

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

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                return(new Row(manager, rowType, (NativeRow)tempValue));
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                return(new ListValue(manager, listType, (NativeList)tempValue));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)tempValue));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)tempValue));
            }

            var runtimeType = manager.GetRuntimeType(tempValue);

            if (runtimeType != null)
            {
                return(FromNative(manager, runtimeType, tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
示例#2
0
        private void Create(IValueManager manager)
        {
            TableType = TableVar.DataType;
            RowType   = TableType.RowType;

            Schema.RowType keyRowType;
            Schema.RowType dataRowType;

            // Create the map index required to store data as described by the given table variable
            keyRowType  = new Schema.RowType(Key.Columns);
            dataRowType = new Schema.RowType();
            foreach (Schema.Column column in TableVar.DataType.Columns)
            {
                dataRowType.Columns.Add(new Schema.Column(column.Name, column.DataType));
            }

            Index =
                new NativeRowMap
                (
                    Key,
                    EqualitySorts,
                    keyRowType,
                    dataRowType
                );
        }
示例#3
0
        /// <summary>
        /// The recursive portion of the find key algorithm invoked by the FindKey method of the parent Index.
        /// </summary>
        public bool FindKey(Schema.IRowType keyRowType, NativeRow key, RowTreeSearchPath rowTreeSearchPath, out int entryNumber)
        {
            rowTreeSearchPath.Add(this);
            if (Node.NodeType == NativeRowTreeNodeType.Routing)
            {
                // Perform a binary search among the keys in this node to determine which streamid to follow for the next node
                bool result = NodeSearch(keyRowType, key, out entryNumber);

                // If the key was found, use the given entry number, otherwise, use the one before the given entry
                entryNumber = result ? entryNumber : (entryNumber - 1);
                return
                    (new RowTreeNode
                     (
                         Manager,
                         Tree,
                         RoutingNode.Nodes[entryNumber],
                         LockMode.Shared
                     ).FindKey
                     (
                         keyRowType,
                         key,
                         rowTreeSearchPath,
                         out entryNumber
                     ));
            }
            else
            {
                // Perform a binary search among the keys in this node to determine which entry, if any, is equal to the given key
                return(NodeSearch(keyRowType, key, out entryNumber));
            }
        }
示例#4
0
        public Row RequestRow(IServerProcess AProcess, Schema.IRowType ARowType, NativeRow ARow)
        {
            Row LRow = GetRow();

            LRow.Open(AProcess, ARowType, ARow);
            return(LRow);
        }
示例#5
0
 // Statement
 public override Statement EmitStatement(EmitMode mode)
 {
     if (_dataType is Schema.RowType)
     {
         RowSelectorExpression expression = new RowSelectorExpression();
         if (_specifiedRowType != null)
         {
             expression.TypeSpecifier = DataType.EmitSpecifier(mode);
         }
         Schema.IRowType rowType = _specifiedRowType != null ? _specifiedRowType : DataType;
         for (int index = 0; index < rowType.Columns.Count; index++)
         {
             expression.Expressions.Add(new NamedColumnExpression((Expression)Nodes[index].EmitStatement(mode), rowType.Columns[index].Name));
         }
         expression.Modifiers = Modifiers;
         return(expression);
     }
     else
     {
         EntrySelectorExpression expression = new EntrySelectorExpression();
         for (int index = 0; index < DataType.Columns.Count; index++)
         {
             expression.Expressions.Add(new NamedColumnExpression((Expression)Nodes[index].EmitStatement(mode), DataType.Columns[index].Name));
         }
         expression.Modifiers = Modifiers;
         return(expression);
     }
 }
示例#6
0
        ///	<summary>
        ///	Returns a row that is guaranteed to contain the same columns in the same order as the node order.
        /// If the given row does not satisfy this requirement, a row of the proper row type is created and the values from the given row are copied into it.
        /// </summary>
        protected IRow EnsureKeyRow(IRow key)
        {
            if (IsKeyRow(key))
            {
                return(key);
            }
            else
            {
                Schema.IRowType rowType = DataType.CreateRowType(String.Empty, false);
                Schema.Order    order   = Order;
                for (int index = 0; index < order.Columns.Count; index++)
                {
                    //int LColumnIndex = AKey.DataType.Columns.IndexOfName(LOrder.Columns[LIndex].Column.Name);
                    //if (LColumnIndex >= 0)
                    rowType.Columns.Add(order.Columns[index].Column.Column.Copy());
                    // BTR 4/25/2005 -> There is no difference between not having the column, and having the column, but not having a value.
                    // as such, I see no reason to throw this error, simply create the row and leave the column empty.
                    //else
                    //	throw new RuntimeException(RuntimeException.Codes.InvalidSearchArgument);
                }

                Row localKey = new Row(Manager, rowType);
                key.CopyTo(localKey);
                return(localKey);
            }
        }
示例#7
0
        public Row RequestRow(IServerProcess AProcess, Schema.IRowType ARowType, Stream AStream)
        {
            Row LRow = GetRow();

            LRow.Open(AProcess, ARowType, AStream);
            return(LRow);
        }
        private RemoteFetchData InternalFetch(Schema.IRowType rowType, Guid[] bookmarks, int count, bool skipCurrent)
        {
            Row[] rows = new Row[Math.Abs(count)];
            try
            {
                for (int index = 0; index < rows.Length; index++)
                {
                    rows[index]             = new Row(_plan.Process.ServerProcess.ValueManager, rowType);
                    rows[index].ValuesOwned = false;
                }

                CursorGetFlags flags;
                int            localCount = _serverCursor.Fetch(rows, bookmarks, count, skipCurrent, out flags);

                RemoteFetchData fetchData = new RemoteFetchData();
                fetchData.Body = new RemoteRowBody[localCount];
                for (int index = 0; index < localCount; index++)
                {
                    fetchData.Body[index].Data = rows[index].AsPhysical;
                }

                fetchData.Flags = flags;
                return(fetchData);
            }
            finally
            {
                for (int index = 0; index < rows.Length; index++)
                {
                    if (rows[index] != null)
                    {
                        rows[index].Dispose();
                    }
                }
            }
        }
示例#9
0
        protected virtual void InternalOpen()
        {
            // get a table object to supply the data
            _plan.SetActiveCursor(this);
            try
            {
                _program.Start(_params);
                _programStarted = true;

                long startTicks = TimingUtility.CurrentTicks;

                CursorNode cursorNode = _plan.CursorNode;
                //LCursorNode.EnsureApplicationTransactionJoined(FPlan.ServerProcess);
                _sourceTable = (ITable)_plan.CursorNode.SourceNode.Execute(_program);
                _sourceTable.Open();
                _sourceRowType = _sourceTable.DataType.RowType;

                _program.Statistics.ExecuteTime = TimingUtility.TimeSpanFromTicks(startTicks);
            }
            catch
            {
                InternalClose();
                throw;
            }
        }
示例#10
0
 public Row(IValueManager manager, Schema.IRowType dataType) : base(manager, dataType)
 {
     _row = new NativeRow(DataType.Columns.Count);
                 #if USEDATATYPESINNATIVEROW
     for (int index = 0; index < DataType.Columns.Count; index++)
     {
         _row.DataTypes[index] = DataType.Columns[index].DataType;
     }
                 #endif
     ValuesOwned = true;
 }
示例#11
0
 private bool GetIsIndexAffected(Schema.IRowType rowType, IRow row)
 {
     foreach (Schema.Column column in rowType.Columns)
     {
         if (row.DataType.Columns.ContainsName(column.Name))
         {
             return(true);
         }
     }
     return(false);
 }
示例#12
0
 protected Expression EmitBrowseVariantExpression(Schema.Order originOrder, Schema.IRowType origin, int originIndex, bool forward, bool inclusive)
 {
     return
         (new OrderExpression
          (
              new RestrictExpression
              (
                  _sourceExpression,
                  EmitBrowseConditionExpression(originIndex < 0 ? Order : originOrder, originIndex < 0 ? null : origin, forward, inclusive)
              ),
              (from c in (new Schema.Order(Order, !forward)).Columns select(OrderColumnDefinition) c.EmitStatement(EmitMode.ForCopy)).ToArray()
          ));
 }
示例#13
0
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNativeRow(IValueManager manager, Schema.IRowType rowType, NativeRow nativeRow, int nativeRowIndex)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
                        #if USEDATATYPESINNATIVEROW
            Schema.IDataType dataType = nativeRow.DataTypes[nativeRowIndex];
            if (dataType == null)
            {
                dataType = rowType.Columns[nativeRowIndex].DataType;
            }
                        #else
            Schema.IDataType dataType = ARowType.Columns[ANativeRowIndex].DataType;
                        #endif

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                return(new RowInternedScalar(manager, scalarType, nativeRow, nativeRowIndex));
            }

            Schema.IRowType localRowType = dataType as Schema.IRowType;
            if (localRowType != null)
            {
                return(new Row(manager, localRowType, (NativeRow)nativeRow.Values[nativeRowIndex]));
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                return(new ListValue(manager, listType, (NativeList)nativeRow.Values[nativeRowIndex]));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)nativeRow.Values[nativeRowIndex]));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)nativeRow.Values[nativeRowIndex]));
            }

            var runtimeType = manager.GetRuntimeType(nativeRow.Values[nativeRowIndex]);
            if (runtimeType != null)
            {
                return(new RowInternedScalar(manager, (Schema.IScalarType)runtimeType, nativeRow, nativeRowIndex));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
示例#14
0
        public IDataValue Copy()
        {
            // This code is duplicated in the FromNative and CopyAs methods for performance...
            object tempValue = CopyNative();

            Schema.IScalarType scalarType = DataType as Schema.IScalarType;

            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    Scalar scalar = new Scalar(Manager, scalarType, (StreamID)tempValue);
                    scalar.ValuesOwned = true;
                    return(scalar);
                }
                return(new Scalar(Manager, scalarType, tempValue));
            }

            Schema.IRowType rowType = DataType as Schema.IRowType;
            if (rowType != null)
            {
                Row row = new Row(Manager, rowType, (NativeRow)tempValue);
                row.ValuesOwned = true;
                return(row);
            }

            Schema.IListType listType = DataType as Schema.IListType;
            if (listType != null)
            {
                ListValue list = new ListValue(Manager, listType, (NativeList)tempValue);
                list.ValuesOwned = true;
                return(list);
            }

            Schema.ITableType tableType = DataType as Schema.ITableType;
            if (tableType != null)
            {
                TableValue table = new TableValue(Manager, tableType, (NativeTable)tempValue);
                table.ValuesOwned = true;
                return(table);
            }

            Schema.ICursorType cursorType = DataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(Manager, cursorType, (int)tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, DataType == null ? "<null>" : DataType.GetType().Name);
        }
示例#15
0
        /// <summary>Returns the host representation of the given native value.  This is a by-reference operation.</summary>
        public static IDataValue FromNativeList(IValueManager manager, Schema.IListType listType, NativeList nativeList, int nativeListIndex)
        {
            // This code is duplicated in the Copy method and the FromNative overloads for performance
            Schema.IDataType dataType = nativeList.DataTypes[nativeListIndex];
            if (dataType == null)
            {
                dataType = listType.ElementType;
            }

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                return(new ListInternedScalar(manager, scalarType, nativeList, nativeListIndex));
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                return(new Row(manager, rowType, (NativeRow)nativeList.Values[nativeListIndex]));
            }

            Schema.IListType localListType = dataType as Schema.IListType;
            if (localListType != null)
            {
                return(new ListValue(manager, localListType, (NativeList)nativeList.Values[nativeListIndex]));
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                return(new TableValue(manager, tableType, (NativeTable)nativeList.Values[nativeListIndex]));
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(new CursorValue(manager, cursorType, (int)nativeList.Values[nativeListIndex]));
            }

            var runtimeType = manager.GetRuntimeType(nativeList.Values[nativeListIndex]);

            if (runtimeType != null)
            {
                return(new ListInternedScalar(manager, (Schema.IScalarType)runtimeType, nativeList, nativeListIndex));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, nativeList.DataTypes[nativeListIndex] == null ? "<null>" : nativeList.DataTypes[nativeListIndex].GetType().Name);
        }
示例#16
0
        protected PlanNode EmitBrowseConditionNode
        (
            Plan plan,
            Schema.Order order,
            Schema.IRowType origin,
            bool forward,
            bool inclusive
        )
        {
            PlanNode node = null;

            for (int orderIndex = order.Columns.Count - 1; orderIndex >= 0; orderIndex--)
            {
                if ((origin != null) && (orderIndex < origin.Columns.Count))
                {
                    node =
                        Compiler.AppendNode
                        (
                            plan,
                            node,
                            Instructions.Or,
                            EmitBrowseOriginNode(plan, order, forward, inclusive, orderIndex)
                        );
                }
                else
                                        #if USEINCLUDENILSWITHBROWSE
                if (order.Columns[orderIndex].Column.IsNilable && !order.Columns[orderIndex].IncludeNils)
                                        #endif
                {
                    node =
                        Compiler.AppendNode
                        (
                            plan,
                            node,
                            Instructions.And,
                            EmitBrowseNilNode(plan, order.Columns[orderIndex].Column, false)
                        );
                }
            }

            if (node == null)
            {
                node = new ValueNode(plan.DataTypes.SystemBoolean, inclusive);
            }

            return(node);
        }
示例#17
0
        public override object CopyNativeAs(Schema.IDataType dataType)
        {
            if (_row == null)
            {
                return(null);
            }

            if (Object.ReferenceEquals(DataType, dataType))
            {
                NativeRow newRow = new NativeRow(DataType.Columns.Count);
                if (_row != null)
                {
                    for (int index = 0; index < DataType.Columns.Count; index++)
                    {
                                                #if USEDATATYPESINNATIVEROW
                        newRow.DataTypes[index] = _row.DataTypes[index];
                        newRow.Values[index]    = CopyNative(Manager, _row.DataTypes[index], _row.Values[index]);
                                                #else
                        newRow.Values[index] = CopyNative(Manager, DataType.Columns[index].DataType, FRow.Values[index]);
                                                #endif
                    }
                }
                return(newRow);
            }
            else
            {
                NativeRow newRow           = new NativeRow(DataType.Columns.Count);
                Schema.IRowType newRowType = (Schema.IRowType)dataType;
                if (_row != null)
                {
                    for (int index = 0; index < DataType.Columns.Count; index++)
                    {
                        int newIndex = newRowType.Columns.IndexOfName(DataType.Columns[index].Name);
                                                #if USEDATATYPESINNATIVEROW
                        newRow.DataTypes[newIndex] = _row.DataTypes[index];
                        newRow.Values[newIndex]    = CopyNative(Manager, _row.DataTypes[index], _row.Values[index]);
                                                #else
                        newRow.Values[newIndex] = CopyNative(Manager, DataType.Columns[index].DataType, FRow.Values[index]);
                                                #endif
                    }
                }
                return(newRow);
            }
        }
示例#18
0
        /// <summary>Returns the host representation of the given physical value.</summary>
        public static IDataValue FromPhysical(IValueManager manager, Schema.IDataType dataType, byte[] buffer, int offset)
        {
            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                Scalar scalar = new Scalar(manager, scalarType, null);
                scalar.ReadFromPhysical(buffer, offset);
                return(scalar);
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                Row row = new Row(manager, rowType);
                row.ReadFromPhysical(buffer, offset);
                return(row);
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                ListValue list = new ListValue(manager, listType);
                list.ReadFromPhysical(buffer, offset);
                return(list);
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                TableValue table = new TableValue(manager, tableType, null);
                table.ReadFromPhysical(buffer, offset);
                return(table);
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                CursorValue cursor = new CursorValue(manager, cursorType, -1);
                cursor.ReadFromPhysical(buffer, offset);
                return(cursor);
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }
示例#19
0
        protected Expression EmitBrowseConditionExpression
        (
            Schema.Order order,
            Schema.IRowType origin,
            bool forward,
            bool inclusive
        )
        {
            Expression expression = null;

            for (int orderIndex = order.Columns.Count - 1; orderIndex >= 0; orderIndex--)
            {
                if ((origin != null) && (orderIndex < origin.Columns.Count))
                {
                    expression =
                        AppendExpression
                        (
                            expression,
                            Instructions.Or,
                            EmitBrowseOriginExpression(order, forward, inclusive, orderIndex)
                        );
                }
                else
                                        #if USEINCLUDENILSWITHBROWSE
                if (order.Columns[orderIndex].Column.IsNilable && !order.Columns[orderIndex].IncludeNils)
                                        #endif
                {
                    expression =
                        AppendExpression
                        (
                            expression,
                            Instructions.And,
                            EmitBrowseNilExpression(order.Columns[orderIndex].Column, false)
                        );
                }
            }

            if (expression == null)
            {
                expression = new ValueExpression(inclusive, TokenType.Boolean);
            }

            return(expression);
        }
示例#20
0
        private Row GetIndexData(IValueManager manager, Schema.IRowType rowType, IRow[] sourceRows)
        {
            Row row = new Row(manager, rowType);

            try
            {
                int  columnIndex;
                bool found;
                for (int index = 0; index < row.DataType.Columns.Count; index++)
                {
                    found = false;
                    foreach (Row sourceRow in sourceRows)
                    {
                        columnIndex = sourceRow.DataType.Columns.IndexOfName(row.DataType.Columns[index].Name);
                        if (columnIndex >= 0)
                        {
                            if (sourceRow.HasValue(columnIndex))
                            {
                                row[index] = sourceRow.GetNativeValue(columnIndex);
                            }
                            else
                            {
                                row.ClearValue(index);
                            }
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }

                    throw new RuntimeException(RuntimeException.Codes.UnableToConstructIndexKey);
                }
                return(row);
            }
            catch
            {
                row.Dispose();
                throw;
            }
        }
示例#21
0
        /// <summary>
        /// Returns a row that is guaranteed to contain the same or fewer columns in the same order as the node order,
        ///	and once a column is null, the rest of the columns are null as well.  If the given row does not satisfy this
        /// requirement, a row of the proper row type is created and the values from the given row are copied into it.
        /// If no such row can be created, null is returned.
        /// </summary>
        protected IRow EnsurePartialKeyRow(IRow key)
        {
            if (IsPartialKeyRow(key))
            {
                return(key);
            }
            else
            {
                bool            isNull  = false;
                Schema.IRowType rowType = DataType.CreateRowType(String.Empty, false);
                Schema.Order    order   = Order;
                for (int index = 0; index < order.Columns.Count; index++)
                {
                    int columnIndex = key.DataType.Columns.IndexOfName(order.Columns[index].Column.Name);
                    if (columnIndex >= 0)
                    {
                        rowType.Columns.Add(order.Columns[index].Column.Column.Copy());
                        if (isNull && key.HasValue(columnIndex))
                        {
                            return(null);
                        }

                        if (!key.HasValue(index))
                        {
                            isNull = true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                Row localKey = new Row(Manager, rowType);
                key.CopyTo(localKey);
                return(localKey);
            }
        }
示例#22
0
 protected virtual void InternalClose()
 {
     try
     {
         try
         {
             try
             {
                 if (_bookmarks != null)
                 {
                     InternalDisposeBookmarks();
                     _bookmarks = null;
                 }
             }
             finally
             {
                 if (_sourceTable != null)
                 {
                     _sourceTable.Dispose();
                     _sourceTable   = null;
                     _sourceRowType = null;
                 }
             }
         }
         finally
         {
             if (_programStarted)
             {
                 _program.Stop(_params);
             }
         }
     }
     finally
     {
         _plan.ClearActiveCursor();
     }
 }
示例#23
0
        /// <summary>
        /// Performs a binary search among the entries in this node for the given key.  Will always return an
        /// entry index in AEntryNumber, which is the index of the entry that was found if the method returns true,
        /// otherwise it is the index where the key should be inserted if the method returns false.
        /// </summary>
        public bool NodeSearch(Schema.IRowType keyRowType, NativeRow key, out int entryNumber)
        {
            int lo     = (Node.NodeType == NativeRowTreeNodeType.Routing ? 1 : 0);
            int hi     = Node.EntryCount - 1;
            int index  = 0;
            int result = -1;

            while (lo <= hi)
            {
                index  = (lo + hi) / 2;
                result = Tree.Compare(Manager, Tree.KeyRowType, Node.Keys[index], keyRowType, key);
                if (result == 0)
                {
                    break;
                }
                else if (result > 0)
                {
                    hi = index - 1;
                }
                else                 // if (LResult < 0) unnecessary
                {
                    lo = index + 1;
                }
            }

            if (result == 0)
            {
                entryNumber = index;
            }
            else
            {
                entryNumber = lo;
            }

            return(result == 0);
        }
示例#24
0
        public int Compare(IValueManager manager, Schema.IRowType indexKeyRowType, NativeRow indexKey, Schema.IRowType compareKeyRowType, NativeRow compareKey)
        {
            // If AIndexKeyRowType is null, the index key must have the structure of an index key,
            // Otherwise, the IndexKey row could be a subset of the actual index key.
            // In that case, AIndexKeyRowType is the RowType for the IndexKey row.
            // It is the caller's responsibility to ensure that the passed IndexKey RowType
            // is a subset of the actual IndexKey with order intact.
            //Row LIndexKey = new Row(AManager, AIndexKeyRowType, AIndexKey);

            // If ACompareContext is null, the compare key must have the structure of an index key,
            // Otherwise the CompareKey could be a subset of the actual index key.
            // In that case, ACompareContext is the RowType for the CompareKey row.
            // It is the caller's responsibility to ensure that the passed CompareKey RowType
            // is a subset of the IndexKey with order intact.
            //Row LCompareKey = new Row(AManager, ACompareKeyRowType, ACompareKey);

            int result = 0;

            for (int index = 0; index < indexKeyRowType.Columns.Count; index++)
            {
                if (index >= compareKeyRowType.Columns.Count)
                {
                    break;
                }

                if ((indexKey.Values[index] != null) && (compareKey.Values[index] != null))
                {
                    if (indexKeyRowType.Columns[index].DataType is Schema.ScalarType)
                    {
                        result = manager.EvaluateSort(Key.Columns[index], indexKey.Values[index], compareKey.Values[index]);
                    }
                    else
                    {
                        using (var indexValue = DataValue.FromNative(manager, indexKey.DataTypes[index], indexKey.Values[index]))
                        {
                            using (var compareValue = DataValue.FromNative(manager, compareKey.DataTypes[index], compareKey.Values[index]))
                            {
                                result = manager.EvaluateSort(Key.Columns[index], indexValue, compareValue);
                            }
                        }
                    }
                }
                else if (indexKey.Values[index] != null)
                {
                    result = Key.Columns[index].Ascending ? 1 : -1;
                }
                else if (compareKey.Values[index] != null)
                {
                    result = Key.Columns[index].Ascending ? -1 : 1;
                }
                else
                {
                    result = 0;
                }

                if (result != 0)
                {
                    break;
                }
            }

            //LIndexKey.Dispose();
            //LCompareKey.Dispose();
            return(result);
        }
示例#25
0
 /// <summary>
 /// Searches for the given key within the index.  ARowTreeSearchPath and AEntryNumber together give the
 /// location of the key in the index.  If the search is successful, the entry exists, otherwise
 /// the EntryNumber indicates where the entry should be placed for an insert.
 /// </summary>
 /// <param name="key">The key to be found.</param>
 /// <param name="rowTreeSearchPath">A <see cref="RowTreeSearchPath"/> which will contain the set of nodes along the search path to the key.</param>
 /// <param name="entryNumber">The EntryNumber where the key either is, or should be, depending on the result of the find.</param>
 /// <returns>A boolean value indicating the success or failure of the find.</returns>
 public bool FindKey(IValueManager manager, Schema.IRowType keyRowType, NativeRow key, RowTreeSearchPath rowTreeSearchPath, out int entryNumber)
 {
     return(new RowTreeNode(manager, this, Root, LockMode.Shared).FindKey(keyRowType, key, rowTreeSearchPath, out entryNumber));
 }
示例#26
0
        public override void DetermineDataType(Plan plan)
        {
            DetermineModifiers(plan);
            Nodes[0]        = Compiler.EmitOrderNode(plan, SourceNode, new Schema.Order(_quotaOrder), true);
            _dataType       = new Schema.TableType();
            _tableVar       = new Schema.ResultTableVar(this);
            _tableVar.Owner = plan.User;
            _tableVar.InheritMetaData(SourceTableVar.MetaData);
            CopyTableVarColumns(SourceTableVar.Columns);
            DetermineRemotable(plan);

            bool quotaOrderIncludesKey = false;

            foreach (Schema.Key key in SourceNode.TableVar.Keys)
            {
                if (Compiler.OrderIncludesKey(plan, _quotaOrder, key))
                {
                    quotaOrderIncludesKey = true;
                    break;
                }
            }

            if (quotaOrderIncludesKey)
            {
                if ((Nodes[1].IsLiteral) && ((int)plan.EvaluateLiteralArgument(Nodes[1], "quota") == 1))
                {
                    Schema.Key key = new Schema.Key();
                    key.IsInherited = true;
                    TableVar.Keys.Add(key);
                }
                else
                {
                    CopyKeys(SourceTableVar.Keys);
                }
            }
            else
            {
                CopyKeys(SourceTableVar.Keys);
            }

            CopyOrders(SourceTableVar.Orders);
            if (SourceNode.Order != null)
            {
                Order = CopyOrder(SourceNode.Order);
            }

                        #if UseReferenceDerivation
                        #if UseElaborable
            if (plan.CursorContext.CursorCapabilities.HasFlag(CursorCapability.Elaborable))
                        #endif
            CopyReferences(plan, SourceTableVar);
                        #endif

            plan.EnterRowContext();
            try
            {
                                #if USENAMEDROWVARIABLES
                _quotaRowType = new Schema.RowType(_quotaOrder.Columns);
                plan.Symbols.Push(new Symbol(Keywords.Left, _quotaRowType));
                                #else
                Schema.IRowType leftRowType = new Schema.RowType(FQuotaOrder.Columns, Keywords.Left);
                APlan.Symbols.Push(new Symbol(String.Empty, leftRowType));
                                #endif
                try
                {
                                        #if USENAMEDROWVARIABLES
                    plan.Symbols.Push(new Symbol(Keywords.Right, _quotaRowType));
                                        #else
                    Schema.IRowType rightRowType = new Schema.RowType(FQuotaOrder.Columns, Keywords.Right);
                    APlan.Symbols.Push(new Symbol(String.Empty, rightRowType));
                                        #endif
                    try
                    {
                        _equalNode =
                                                        #if USENAMEDROWVARIABLES
                            Compiler.CompileExpression(plan, Compiler.BuildRowEqualExpression(plan, Keywords.Left, Keywords.Right, _quotaRowType.Columns, _quotaRowType.Columns));
                                                        #else
                            Compiler.CompileExpression(APlan, Compiler.BuildRowEqualExpression(APlan, leftRowType.Columns, rightRowType.Columns));
                                                        #endif
                    }
                    finally
                    {
                        plan.Symbols.Pop();
                    }
                }
                finally
                {
                    plan.Symbols.Pop();
                }
            }
            finally
            {
                plan.ExitRowContext();
            }
        }
示例#27
0
        // TODO: Compile row types for each index, saving column indexes to prevent the need for lookup during insert, update, and delete.
        private void Create(IValueManager manager)
        {
            TableType = TableVar.DataType;
            RowType   = TableType.RowType;

            Schema.RowType keyRowType;
            Schema.RowType dataRowType;

            // Create the indexes required to store data as described by the given table variable
            // Determine Fanout, Capacity, Clustering Key
            Schema.Order clusteringKey = manager.FindClusteringOrder(TableVar);
            keyRowType  = new Schema.RowType(clusteringKey.Columns);
            dataRowType = new Schema.RowType();
            foreach (Schema.Column column in TableVar.DataType.Columns)
            {
                if (!clusteringKey.Columns.Contains(column.Name))
                {
                    dataRowType.Columns.Add(new Schema.Column(column.Name, column.DataType));
                }
            }

            // Add an internal identifier for uniqueness of keys in nonunique indexes
                        #if USEINTERNALID
            _internalIDColumn = new Schema.TableVarColumn(new Schema.Column(InternalIDColumnName, manager.DataTypes.SystemGuid), Schema.TableVarColumnType.InternalID);
            dataRowType.Columns.Add(_internalIDColumn.Column);
                        #endif

            // Create the Clustered index
            ClusteredIndex =
                new NativeRowTree
                (
                    clusteringKey,
                    keyRowType,
                    dataRowType,
                    _fanout,
                    _capacity,
                    true
                );

            // DataLength and DataColumns for all non clustered indexes is the key length and columns of the clustered key
            dataRowType = keyRowType;

            // Create non clustered indexes for each key and order (unique sets)
            Schema.Order key;
            foreach (Schema.Key nonClusteredKey in TableVar.Keys)
            {
                if (!nonClusteredKey.IsSparse && nonClusteredKey.Enforced)
                {
                    if (!manager.OrderIncludesKey(ClusteredIndex.Key, nonClusteredKey))
                    {
                        key = manager.OrderFromKey(nonClusteredKey);
                        if (!NonClusteredIndexes.Contains(key))
                        {
                            keyRowType = new Schema.RowType(key.Columns);

                            NonClusteredIndexes.Add
                            (
                                new NativeRowTree
                                (
                                    key,
                                    keyRowType,
                                    dataRowType,
                                    _fanout,
                                    _capacity,
                                    false
                                )
                            );
                        }
                    }
                }
                else
                {
                    // This is a potentially non-unique index, so add a GUID to ensure uniqueness of the key in the BTree
                    key = manager.OrderFromKey(nonClusteredKey);
                                        #if USEINTERNALID
                    Schema.OrderColumn uniqueColumn = new Schema.OrderColumn(_internalIDColumn, key.IsAscending);
                    uniqueColumn.Sort = manager.GetUniqueSort(uniqueColumn.Column.DataType);
                    key.Columns.Add(uniqueColumn);
                                        #endif

                    if (!NonClusteredIndexes.Contains(key))
                    {
                        keyRowType = new Schema.RowType(key.Columns);

                        NonClusteredIndexes.Add
                        (
                            new NativeRowTree
                            (
                                key,
                                keyRowType,
                                dataRowType,
                                _fanout,
                                _capacity,
                                false
                            )
                        );
                    }
                }
            }

            foreach (Schema.Order order in TableVar.Orders)
            {
                // This is a potentially non-unique index, so add a GUID to ensure uniqueness of the key in the BTree
                key = new Schema.Order(order);
                                #if USEINTERNALID
                if (!manager.OrderIncludesOrder(key, clusteringKey))
                {
                    Schema.OrderColumn uniqueColumn = new Schema.OrderColumn(_internalIDColumn, order.IsAscending);
                    uniqueColumn.Sort = manager.GetUniqueSort(uniqueColumn.Column.DataType);
                    key.Columns.Add(uniqueColumn);
                }
                                #endif

                if (!NonClusteredIndexes.Contains(key))
                {
                    keyRowType = new Schema.RowType(key.Columns);

                    NonClusteredIndexes.Add
                    (
                        new NativeRowTree
                        (
                            key,
                            keyRowType,
                            dataRowType,
                            _fanout,
                            _capacity,
                            false
                        )
                    );
                }
            }
        }
示例#28
0
 // The given object[] is assumed to contains values of the appropriate type for the given data type
 public Row(IValueManager manager, Schema.IRowType dataType, NativeRow row) : base(manager, dataType)
 {
     _row        = row;
     ValuesOwned = false;
 }
示例#29
0
        public void ProcessFetchData(RemoteFetchData fetchData, Guid[] bookmarks, bool isFirst)
        {
            _buffer.BufferDirection = _bufferDirection;
            Schema.IRowType rowType = DataType.RowType;
            if (_bufferDirection == BufferDirection.Forward)
            {
                for (int index = 0; index < fetchData.Body.Length; index++)
                {
                    LocalRow row = new LocalRow(new Row(_plan._process.ValueManager, rowType), bookmarks[index]);
                    row.Row.AsPhysical = fetchData.Body[index].Data;
                    _buffer.Add(row);
                    AddBookmarkIfNotEmpty(bookmarks[index]);
                }

                if ((fetchData.Body.Length > 0) && !isFirst)
                {
                    _bufferIndex = 0;
                }
                else
                {
                    _bufferIndex = -1;
                }

                if ((fetchData.Flags & CursorGetFlags.EOF) != 0)
                {
                    _sourceCursorIndex = _buffer.Count;
                }
                else
                {
                    _sourceCursorIndex = _buffer.Count - 1;
                }
            }
            else
            {
                for (int index = 0; index < fetchData.Body.Length; index++)
                {
                    LocalRow row = new LocalRow(new Row(_plan._process.ValueManager, rowType), bookmarks[index]);
                    row.Row.AsPhysical = fetchData.Body[index].Data;
                    _buffer.Insert(0, row);
                    AddBookmarkIfNotEmpty(bookmarks[index]);
                }

                if ((fetchData.Body.Length > 0) && !isFirst)
                {
                    _bufferIndex = _buffer.Count - 1;
                }
                else
                {
                    _bufferIndex = _buffer.Count;
                }

                if ((fetchData.Flags & CursorGetFlags.BOF) != 0)
                {
                    _sourceCursorIndex = -1;
                }
                else
                {
                    _sourceCursorIndex = 0;
                }
            }

            SetFlags(fetchData.Flags);
            _bufferFull = true;
        }
示例#30
0
        public static object CopyNative(IValueManager manager, Schema.IDataType dataType, object tempValue)
        {
            // This code is duplicated in the descendent CopyNative methods for performance
            if (tempValue == null)
            {
                return(tempValue);
            }

            Schema.IScalarType scalarType = dataType as Schema.IScalarType;
            if (scalarType != null)
            {
                if (tempValue is StreamID)
                {
                    return(manager.StreamManager.Reference((StreamID)tempValue));
                }

                ICloneable cloneable = tempValue as ICloneable;
                if (cloneable != null)
                {
                    return(cloneable.Clone());
                }

                if (scalarType.IsCompound)
                {
                    return(CopyNative(manager, scalarType.CompoundRowType, tempValue));
                }

                return(tempValue);
            }

            Schema.IRowType rowType = dataType as Schema.IRowType;
            if (rowType != null)
            {
                NativeRow nativeRow = (NativeRow)tempValue;
                NativeRow newRow    = new NativeRow(rowType.Columns.Count);
                for (int index = 0; index < rowType.Columns.Count; index++)
                {
                                        #if USEDATATYPESINNATIVEROW
                    newRow.DataTypes[index] = nativeRow.DataTypes[index];
                    newRow.Values[index]    = CopyNative(manager, nativeRow.DataTypes[index], nativeRow.Values[index]);
                                        #else
                    newRow.Values[index] = CopyNative(AManager, rowType.Columns[index].DataType, nativeRow.Values[index]);
                                        #endif
                }
                return(newRow);
            }

            Schema.IListType listType = dataType as Schema.IListType;
            if (listType != null)
            {
                NativeList nativeList = (NativeList)tempValue;
                NativeList newList    = new NativeList();
                for (int index = 0; index < nativeList.DataTypes.Count; index++)
                {
                    newList.DataTypes.Add(nativeList.DataTypes[index]);
                    newList.Values.Add(CopyNative(manager, nativeList.DataTypes[index], nativeList.Values[index]));
                }
                return(newList);
            }

            Schema.ITableType tableType = dataType as Schema.ITableType;
            if (tableType != null)
            {
                NativeTable nativeTable = (NativeTable)tempValue;
                NativeTable newTable    = new NativeTable(manager, nativeTable.TableVar);
                using (Scan scan = new Scan(manager, nativeTable, nativeTable.ClusteredIndex, ScanDirection.Forward, null, null))
                {
                    scan.Open();
                    while (scan.Next())
                    {
                        using (IRow row = scan.GetRow())
                        {
                            newTable.Insert(manager, row);
                        }
                    }
                }
                return(newTable);
            }

            Schema.ICursorType cursorType = dataType as Schema.ICursorType;
            if (cursorType != null)
            {
                return(tempValue);
            }

            var runtimeType = manager.GetRuntimeType(tempValue);
            if (runtimeType != null)
            {
                return(CopyNative(manager, runtimeType, tempValue));
            }

            throw new RuntimeException(RuntimeException.Codes.InvalidValueType, dataType == null ? "<null>" : dataType.GetType().Name);
        }