Example #1
0
        /// <summary>
        /// Returns the value in binary array format (does not include a sizeOf prefix)
        /// </summary>
        /// <returns>The value in binary array format (does not include sizeOf prefix)</returns>
        public byte[] GetValueBinaryArray()
        {
            byte[] data = null;

            Debug.WriteLine(this.Value);

            if (Column.DataType.Contains("CHAR"))
            {
                data = DatabaseBinaryConverter.StringToBinary(Value, Column.DataType);
            }

            if (Column.DataType.Contains("DECIMAL") || Column.DataType.Contains("NUMERIC"))
            {
                data = DatabaseBinaryConverter.DecimalToBinary(Value, Column.DataType);
            }

            if (Column.DataType.Contains("DATETIME"))
            {
                data = DatabaseBinaryConverter.DateTimeToBinary(Value);
            }

            if (Column.DataType.Contains("BIT"))
            {
                data = DatabaseBinaryConverter.BooleanToBinary(Value);
            }

            if (Column.DataType.Equals("INT"))
            {
                data = BitConverter.GetBytes(Convert.ToInt32(Value));
            }

            return(data);
        }
Example #2
0
        /// <summary>
        /// Iterates over this page's _data and populates the supplied array with structs representing the rows
        /// </summary>
        /// <param name="rows">An array of RowStruct to populate</param>
        private void IterateOverData(ref RowStruct[] rows)
        {
            var dataSpan      = new Span <byte>(_data);
            int currentOffset = DatabaseConstants.SIZE_OF_PAGE_PREAMBLE;
            int currentRowNum = 0;

            while (currentOffset < DatabaseConstants.PAGE_SIZE)
            {
                int  rowId;
                bool isLocal;
                int  sizeOfRow;

                RowPreamble.Parse(dataSpan.Slice(currentOffset, DatabaseConstants.SIZE_OF_ROW_PREAMBLE), out rowId, out isLocal);

                // check for end of data row identifier
                if (isLocal && rowId == DatabaseConstants.END_OF_ROW_DATA_ID)
                {
                    break;
                }

                if (currentRowNum >= _totalRows)
                {
                    break;
                }

                currentOffset += DatabaseConstants.SIZE_OF_ROW_PREAMBLE;

                if (isLocal)
                {
                    var values = new RowValue2[_schema.Columns.Length];

                    // we need the size of the row to parse how far along we should go in the array (span).
                    // we will however not adjust the offset since the method LocalRowBodyFromBinary includes parsing the rowSize prefix (an int32 size (4 bytes)).

                    sizeOfRow = BitConverter.ToInt32(dataSpan.Slice(currentOffset, DatabaseConstants.SIZE_OF_ROW_SIZE));
                    int rowSize; // this isn't really needed, but it's a required param of the method below
                    Row2.LocalRowBodyFromBinary(dataSpan.Slice(currentOffset, sizeOfRow), out rowSize, ref values, _schema.Columns);

                    //rows.Add(new Row2(rowId, isLocal, _schema.Columns, _process.Id.Value, values, sizeOfRow));
                    rows[currentRowNum] = new RowStruct {
                        IsLocal = isLocal, RowId = rowId, ParticipantId = Guid.Empty, RowSize = sizeOfRow, Values = values
                    };
                    currentOffset += sizeOfRow;
                    currentRowNum++;
                }
                else
                {
                    sizeOfRow = DatabaseConstants.PARTICIPANT_ID_SIZE;
                    Guid particpantId = DatabaseBinaryConverter.BinaryToGuid(dataSpan.Slice(currentOffset, sizeOfRow));
                    //rows.Add(new Row2(rowId, isLocal, particpantId, sizeOfRow, _schema.Columns));
                    rows[currentRowNum] = new RowStruct {
                        IsLocal = isLocal, ParticipantId = particpantId, RowSize = sizeOfRow, RowId = rowId, Values = null
                    };
                    currentOffset += sizeOfRow;
                    currentRowNum++;
                }
            }
        }
Example #3
0
        /// <summary>
        /// Parses a byte array to a RowValue2 for this column data type. This method assumes the byte array passed in is the value. If the column length is variable, you must ensure the array size passed in
        /// is the array for the total value (do not include the size prefix for variable length columns.)
        /// </summary>
        /// <param name="span">The byte array to parse. This array must contain the value to be parsed. Ensure that it does not contain the size prefix for variable length columns.</param>
        /// <returns>A RowValue2</returns>
        public RowValue2 Parse(Span <byte> span)
        {
            var value = new RowValue2();

            value.Column = this;

            if (!isVariableLengthColumn())
            {
                if (DataType.Equals("DATETIME"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToDateTime(span).ToString();
                }

                if (DataType.Equals("BIT"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToBoolean(span).ToString();
                }

                if (DataType.Equals("INT"))
                {
                    value.Value = DatabaseBinaryConverter.BinaryToInt(span).ToString();
                }

                if (DataType.Contains("CHAR"))
                {
                    // need to get the fixed character width, then parse
                    // CHAR always pads any values less with whitespace
                    throw new NotImplementedException();
                }
            }
            else
            {
                // if it's varaible length, then the array has prefix'ed the actual value with the size (length) of the item. The prefix is the size of an INT32.
                // example VARCHAR(10)
                // byte format is 2 <byte array>
                // in the above if the value was "XX" it means that we only used 2 characters out of a potential maximum of 10 characters wide

                if (DataType.Contains("VARCHAR"))
                {
                    throw new NotImplementedException();
                }

                if (DataType.Contains("DECIMAL"))
                {
                    throw new NotImplementedException();
                }

                if (DataType.Contains("NUMERIC"))
                {
                    throw new NotImplementedException();
                }
            }

            return(value);
        }
Example #4
0
        /// <summary>
        /// Converts a binary array to the body of a local row. The array should include the row size prefix.
        /// </summary>
        /// <param name="span">The bytes to parse. Include the row size prefix in this array.</param>
        /// <param name="sizeOfRow">The total size of the row (parsed from the bytes)</param>
        /// <param name="values">A list of values parse from the array</param>
        /// <param name="schema">The schema of the table</param>
        public static void LocalRowBodyFromBinary(Span <byte> span, out int sizeOfRow, ref RowValue2[] values, ColumnSchema[] schema)
        {
            int       colIdx        = 0;
            int       currentOffset = 0;
            RowValue2 item          = null;

            schema.OrderByByteFormat();

            sizeOfRow = BitConverter.ToInt32(span.Slice(0, DatabaseConstants.SIZE_OF_ROW_SIZE));

            currentOffset += DatabaseConstants.SIZE_OF_ROW_SIZE;

            foreach (var column in schema)
            {
                if (!column.IsVariableLength)
                {
                    item           = column.Parse(span.Slice(currentOffset, column.Size));
                    values[colIdx] = item;
                    colIdx++;
                    currentOffset += column.Size;
                }
                else
                {
                    int sizeOfValue = DatabaseBinaryConverter.BinaryToInt(span.Slice(currentOffset, DatabaseConstants.SIZE_OF_INT));
                    currentOffset += DatabaseConstants.SIZE_OF_INT;
                    item           = column.Parse(span.Slice(currentOffset, sizeOfValue));
                    currentOffset += sizeOfValue;
                }
            }


            /*
             * Row Byte Array Layout:
             * RowId IsLocal {{SizeOfRow | ParticipantId} | RowData}
             * RowId IsLocal - preamble (used in inital load of the Row)
             *
             * if IsLocal == true, then need to request the rest of the byte array
             *
             * if IsLocal == false, then need to request the rest of the byte array, i.e. the size of the ParticipantId
             *
             * SizeOfRow is the size of the rest of the row in bytes minus the preamble.
             * For a remote row, this is just the size of the ParticipantId (a guid)
             * For a local row, this is the total size of all the data
             *
             * If IsLocal == true, format is as follows -
             * [data_col1] [data_col2] [data_colX] - fixed size columns first
             * [SizeOfVar] [varData] [SizeOfVar] [varData] - variable size columns
             * [ -1 preamble] - signals the end of row data (a preamble whose RowId == -1 and IsLocal == true)
             */

            throw new NotImplementedException();
        }
Example #5
0
        /// <summary>
        /// Returns the byte array in the data file at the specified linenumber
        /// </summary>
        /// <param name="lineNumber">The line number of the data file</param>
        /// <returns>The binary data at the specified line number</returns>
        private byte[] GetBinaryPageDataFromDisk(int lineNumber)
        {
            string line = string.Empty;

            lock (_fileLock)
            {
                using (Stream stream = File.Open(FileName(), FileMode.Open))
                {
                    stream.Seek(DatabaseConstants.PAGE_SIZE * (lineNumber - 1), SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        line = reader.ReadLine();
                    }
                }
            }
            return(DatabaseBinaryConverter.StringToBinary(line));
        }
Example #6
0
 /// <summary>
 /// Saves the participantId to the supplied array.
 /// </summary>
 /// <param name="array">The array to save the values to</param>
 /// <param name="participantId">The participant Id</param>
 public static void ParticipantToBinaryFormat(ref byte[] array, Guid?participantId)
 {
     // just save off the participant id (the GUID)
     byte[] item = DatabaseBinaryConverter.GuidToBinary(participantId.Value);
     Array.Copy(item, 0, array, 0, item.Length);
 }