Example #1
0
        public static byte[] BuildRowPutChangeWithHeader(RowPutChange rowChange)
        {
            List <PlainBufferCell> pkCells = new List <PlainBufferCell>();

            if (rowChange.GetPrimaryKey() == null)
            {
                throw new ArgumentException("Primary Key is NULL");
            }

            foreach (PrimaryKeyColumn column in rowChange.GetPrimaryKey().GetPrimaryKeyColumns())
            {
                pkCells.Add(PlainBufferConversion.ToPlainBufferCell(column));
            }

            List <PlainBufferCell> cells = new List <PlainBufferCell>();

            foreach (Column column in rowChange.GetColumnsToPut())
            {
                cells.Add(PlainBufferConversion.ToPlainBufferCell(column, false, false, false, (byte)0x0));
            }

            PlainBufferRow row = new PlainBufferRow(pkCells, cells, false);

            int size = ComputePlainBufferRowWithHeader(row);
            PlainBufferOutputStream      output      = new PlainBufferOutputStream(size);
            PlainBufferCodedOutputStream codedOutput = new PlainBufferCodedOutputStream(output);

            codedOutput.WriteRowWithHeader(row);
            if (!output.IsFull())
            {
                throw new IOException("Bug: serialize row put change failed. Buffer remains " + output.Remain());
            }

            return(output.GetBuffer());
        }
Example #2
0
        public static byte[] BuildRowDeleteChangeWithHeader(RowDeleteChange rowChange)
        {
            List <PlainBufferCell> pkCells = new List <PlainBufferCell>();

            foreach (var primaryKeyColumn in rowChange.GetPrimaryKey().GetPrimaryKeyColumns())
            {
                pkCells.Add(PlainBufferConversion.ToPlainBufferCell(primaryKeyColumn));
            }

            List <PlainBufferCell> cells = new List <PlainBufferCell>();
            PlainBufferRow         row   = new PlainBufferRow(pkCells, cells, true);

            int size = ComputePlainBufferRowWithHeader(row);
            PlainBufferOutputStream      output      = new PlainBufferOutputStream(size);
            PlainBufferCodedOutputStream codedOutput = new PlainBufferCodedOutputStream(output);

            codedOutput.WriteRowWithHeader(row);

            if (!output.IsFull())
            {
                throw new IOException("Bug: serialize row delete change failed.");
            }

            return(output.GetBuffer());
        }
Example #3
0
        public static byte[] BuildPrimaryKeyWithHeader(PrimaryKey primaryKey)
        {
            int size = ComputePrimaryKeyWithHeader(primaryKey);
            PlainBufferOutputStream output = new PlainBufferOutputStream(size);

            output.WriteRawLittleEndian32(PlainBufferConsts.HEADER);
            output.WriteRawByte(PlainBufferConsts.TAG_ROW_PK);

            byte rowChecksum = (byte)0x0, cellChecksum;

            foreach (var key in primaryKey)
            {
                var column = new Column(key.Key, key.Value);
                cellChecksum = PlainBufferCrc8.crc8((byte)0x0, column.GetNameRawData());
                cellChecksum = column.Value.GetChecksum(cellChecksum);
                WritePrimaryKeyColumn(column, output, cellChecksum);
                rowChecksum = PlainBufferCrc8.crc8(rowChecksum, cellChecksum);
            }

            // 没有deleteMarker, 要与0x0做crc.
            rowChecksum = PlainBufferCrc8.crc8(rowChecksum, (byte)0x0);

            output.WriteRawByte(PlainBufferConsts.TAG_ROW_CHECKSUM);
            output.WriteRawByte(rowChecksum);

            if (!output.IsFull())
            {
                throw new IOException("Bug: serialize primary key failed.");
            }

            return(output.GetBuffer());
        }
Example #4
0
        public static void WritePrimaryKeyValue(ColumnValue value, PlainBufferOutputStream output)
        {
            if (value.IsInfMin())
            {
                output.WriteRawLittleEndian32(1);
                output.WriteRawByte(PlainBufferConsts.VT_INF_MIN);
                return;
            }

            if (value.IsInfMax())
            {
                output.WriteRawLittleEndian32(1);
                output.WriteRawByte(PlainBufferConsts.VT_INF_MAX);
                return;
            }

            if (value.IsPlaceHolderForAutoIncr())
            {
                output.WriteRawLittleEndian32(1);
                output.WriteRawByte(PlainBufferConsts.VT_AUTO_INCREMENT);
                return;
            }

            switch (value.Type)
            {
            case ColumnValueType.String:
            {
                byte[] rawData      = value.AsStringInBytes();
                int    prefixLength = PlainBufferOutputStream.LITTLE_ENDIAN_32_SIZE + 1; // length + type + length
                output.WriteRawLittleEndian32(prefixLength + rawData.Length);            // length + type + value
                output.WriteRawByte(PlainBufferConsts.VT_STRING);
                output.WriteRawLittleEndian32(rawData.Length);
                output.WriteBytes(rawData);
                break;
            }

            case ColumnValueType.Integer:
            {
                output.WriteRawLittleEndian32(1 + PlainBufferOutputStream.LITTLE_ENDIAN_64_SIZE);
                output.WriteRawByte(PlainBufferConsts.VT_INTEGER);
                output.WriteRawLittleEndian64(value.IntegerValue);
                break;
            }

            case ColumnValueType.Binary:
            {
                byte[] rawData      = value.BinaryValue;
                int    prefixLength = PlainBufferOutputStream.LITTLE_ENDIAN_32_SIZE + 1; // length + type + length
                output.WriteRawLittleEndian32(prefixLength + rawData.Length);            // length + type + value
                output.WriteRawByte(PlainBufferConsts.VT_BLOB);
                output.WriteRawLittleEndian32(rawData.Length);
                output.WriteBytes(rawData);
                break;
            }

            default:
                throw new IOException("Bug: unsupported primary key type: " + value.GetType());
            }
        }
Example #5
0
 public static void WritePrimaryKeyColumn(Column column, PlainBufferOutputStream output, byte checksum)
 {
     output.WriteRawByte(PlainBufferConsts.TAG_CELL);
     output.WriteRawByte(PlainBufferConsts.TAG_CELL_NAME);
     byte[] rawData = column.GetNameRawData();
     output.WriteRawLittleEndian32(rawData.Length);
     output.WriteBytes(rawData);
     output.WriteRawByte(PlainBufferConsts.TAG_CELL_VALUE);
     WritePrimaryKeyValue(column.Value, output);
     output.WriteRawByte(PlainBufferConsts.TAG_CELL_CHECKSUM);
     output.WriteRawByte(checksum);
 }
Example #6
0
        public static byte[] BuildRowUpdateChangeWithHeader(RowUpdateChange rowChange)
        {
            List <PlainBufferCell> pkCells = new List <PlainBufferCell>();

            foreach (var column in rowChange.GetPrimaryKey().GetPrimaryKeyColumns())
            {
                pkCells.Add(PlainBufferConversion.ToPlainBufferCell(column));
            }

            List <PlainBufferCell> cells = new List <PlainBufferCell>();

            if (rowChange.GetColumnsToUpdate().Count > 0)
            {
                foreach (Tuple <Column, RowChangeType> column in rowChange.GetColumnsToUpdate())
                {
                    switch (column.Item2)
                    {
                    case RowChangeType.PUT:
                        cells.Add(PlainBufferConversion.ToPlainBufferCell(column.Item1, false, false, false, (byte)0x0));
                        break;

                    case RowChangeType.DELETE:
                        cells.Add(PlainBufferConversion.ToPlainBufferCell(column.Item1, true, false, true, PlainBufferConsts.DELETE_ONE_VERSION));
                        break;

                    case RowChangeType.DELETE_ALL:
                        cells.Add(PlainBufferConversion.ToPlainBufferCell(column.Item1, true, true, true, PlainBufferConsts.DELETE_ALL_VERSION));
                        break;

                    case RowChangeType.INCREMENT:
                        cells.Add(PlainBufferConversion.ToPlainBufferCell(column.Item1, false, true, true, PlainBufferConsts.INCREMENT));
                        break;
                    }
                }
            }

            PlainBufferRow row = new PlainBufferRow(pkCells, cells, false);

            int size = ComputePlainBufferRowWithHeader(row);
            PlainBufferOutputStream      output      = new PlainBufferOutputStream(size);
            PlainBufferCodedOutputStream codedOutput = new PlainBufferCodedOutputStream(output);

            codedOutput.WriteRowWithHeader(row);

            if (!output.IsFull())
            {
                throw new IOException("Bug: serialize row update change failed.");
            }

            return(output.GetBuffer());
        }
Example #7
0
        public static byte[] BuildColumnValueWithoutLengthPrefix(ColumnValue value)
        {
            int size = ComputeColumnValueWithoutLengthPrefix(value);
            PlainBufferOutputStream      output      = new PlainBufferOutputStream(size);
            PlainBufferCodedOutputStream codedOutput = new PlainBufferCodedOutputStream(output);

            codedOutput.WriteColumnValueWithoutLengthPrefix(value);

            if (!output.IsFull())
            {
                throw new IOException("Bug: serialize column value failed. Buffer remains " + output.Remain());
            }

            return(output.GetBuffer());
        }
 public PlainBufferCodedOutputStream(PlainBufferOutputStream output)
 {
     this.output = output;
 }