Пример #1
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());
        }
        public void WriteRow(PlainBufferRow row)
        {
            WriteTag(PlainBufferConsts.TAG_ROW_PK);
            foreach (PlainBufferCell cell in row.GetPrimaryKey())
            {
                WriteCell(cell);
            }

            if (row.GetCells().Count > 0)
            {
                WriteTag(PlainBufferConsts.TAG_ROW_DATA);
                foreach (PlainBufferCell cell in row.GetCells())
                {
                    WriteCell(cell);
                }
            }
            if (row.HasDeleteMarker())
            {
                WriteTag(PlainBufferConsts.TAG_DELETE_ROW_MARKER);
            }

            if (row.HasExtension())
            {
                WriteExtension(row.GetExtension());
            }

            WriteTag(PlainBufferConsts.TAG_ROW_CHECKSUM);
            output.WriteRawByte(row.GetChecksum());
        }
Пример #3
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());
        }
Пример #4
0
        public static int ComputePlainBufferRow(PlainBufferRow row)
        {
            int size = 0;

            size += 1; // TAG_ROW_PK
            foreach (PlainBufferCell cell in row.GetPrimaryKey())
            {
                size += ComputePlainBufferCell(cell);
            }
            if (row.GetCells().Count > 0)
            {
                size += 1; // TAG_ROW_DATA
                foreach (PlainBufferCell cell in row.GetCells())
                {
                    size += ComputePlainBufferCell(cell);
                }
            }
            if (row.HasDeleteMarker())
            {
                size += 1; // TAG_DELETE_MARKER
            }
            if (row.HasExtension())
            {
                size += ComputePlainBufferExtension(row.GetExtension());
            }
            size += 2; // TAG_ROW_CHECKSUM + checksum
            return(size);
        }
Пример #5
0
        public static int ComputePlainBufferRowWithHeader(PlainBufferRow row)
        {
            int size = PlainBufferOutputStream.LITTLE_ENDIAN_32_SIZE; // header

            size += ComputePlainBufferRow(row);
            return(size);
        }
        private Response.OTSResponse DecodeGetRange(byte[] body, out IMessage _message)
        {
            var builder = PB.GetRangeResponse.CreateBuilder();

            builder.MergeFrom(body);
            var message  = builder.Build();
            var response = new Response.GetRangeResponse
            {
                ConsumedCapacityUnit = ParseCapacityUnit(message.Consumed.CapacityUnit)
            };

            if (!message.HasNextStartPrimaryKey)
            {
                response.NextPrimaryKey = null;
            }
            else
            {
                var inputStream = new PB.PlainBufferCodedInputStream(message.NextStartPrimaryKey.CreateCodedInput());
                var rows        = inputStream.ReadRowsWithHeader();
                if (rows.Count != 1)
                {
                    throw new IOException("Expect only one row return. Row count: " + rows.Count);
                }

                PB.PlainBufferRow row = rows[0];
                if (row.HasDeleteMarker() || row.HasCells())
                {
                    throw new IOException("The next primary key should only have primary key: " + row);
                }

                response.NextPrimaryKey = PB.PlainBufferConversion.ToPrimaryKey(row.GetPrimaryKey());
            }


            if (message.HasRows && !message.Rows.IsEmpty)
            {
                List <DataModel.Row> rows = new List <DataModel.Row>();
                var inputStream           = new PB.PlainBufferCodedInputStream(message.Rows.CreateCodedInput());

                List <PB.PlainBufferRow> pbRows = inputStream.ReadRowsWithHeader();
                foreach (var pbRow in pbRows)
                {
                    rows.Add((DataModel.Row)PB.PlainBufferConversion.ToRow(pbRow));
                }

                response.RowDataList = rows;
            }

            if (message.HasNextToken)
            {
                response.NextToken = message.NextToken.ToByteArray();
            }

            _message = message;
            return(response);
        }
Пример #7
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());
        }
        public PlainBufferRow ReadRow()
        {
            List <PlainBufferCell> columns    = new List <PlainBufferCell>();
            List <PlainBufferCell> primaryKey = new List <PlainBufferCell>();
            bool hasDeleteMarker = false;

            if (CheckLastTagWas(PlainBufferConsts.TAG_ROW_PK))
            {
                primaryKey = ReadRowPK();
                if (primaryKey.Count <= 0)
                {
                    throw new IOException("The primary key of row is empty.");
                }
            }

            if (CheckLastTagWas(PlainBufferConsts.TAG_ROW_DATA))
            {
                columns = ReadRowData();
            }

            if (CheckLastTagWas(PlainBufferConsts.TAG_DELETE_ROW_MARKER))
            {
                hasDeleteMarker = true;
                ReadTag();
            }

            PlainBufferRow row = new PlainBufferRow(primaryKey, columns, hasDeleteMarker);

            row.SetExtension(ReadExtension());

            if (CheckLastTagWas(PlainBufferConsts.TAG_ROW_CHECKSUM))
            {
                byte checksum = this.inputStream.ReadRawByte();
                ReadTag();
                if (row.GetChecksum() != checksum)
                {
                    throw new IOException("Checksum is mismatch.Row: " + row + ". Checksum: " + checksum + ". PlainBuffer: " + this.inputStream);
                }
            }
            else
            {
                throw new IOException("Expect TAG_ROW_CHECKSUM but it was " + PlainBufferConsts.PrintTag(GetLastTag()));
            }

            return(row);
        }
Пример #9
0
        public static IRow ToRow(PlainBufferRow plainBufferRow)
        {
            if (plainBufferRow.HasDeleteMarker())
            {
                throw new IOException("Row could not has delete marker: " + plainBufferRow);
            }

            if (plainBufferRow.GetPrimaryKey() == null)
            {
                throw new IOException("Row has no primary key: " + plainBufferRow);
            }

            List <Column> columns = new List <Column>(plainBufferRow.GetCells().Count);

            foreach (PlainBufferCell cell in plainBufferRow.GetCells())
            {
                columns.Add(ToColumn(cell));
            }

            return(new Row(ToPrimaryKey(plainBufferRow.GetPrimaryKey()), columns));
        }
        public static byte GetChecksum(byte crc, PlainBufferRow row)
        {
            foreach (PlainBufferCell cell in row.GetPrimaryKey())
            {
                crc = crc8(crc, cell.GetChecksum());
            }

            foreach (PlainBufferCell cell in row.GetCells())
            {
                crc = crc8(crc, cell.GetChecksum());
            }

            byte del = 0;

            if (row.HasDeleteMarker())
            {
                del = (byte)0x1;
            }
            crc = crc8(crc, del);

            return(crc);
        }
        public List <PlainBufferRow> ReadRowsWithHeader()
        {
            List <PlainBufferRow> rows = new List <PlainBufferRow>();

            if (ReadHeader() != PlainBufferConsts.HEADER)
            {
                throw new IOException("Invalid header from plain buffer: " + this.inputStream);
            }

            ReadTag();
            while (!this.inputStream.IsAtEnd)
            {
                PlainBufferRow row = ReadRow();
                rows.Add(row);
            }

            if (!this.inputStream.IsAtEnd)
            {
                throw new IOException("");
            }

            return(rows);
        }
 public void WriteRowWithHeader(PlainBufferRow row)
 {
     WriteHeader();
     WriteRow(row);
 }