Ejemplo n.º 1
0
        private static void WriteFieldValue <TType>(int Id, BitWriter r, FieldMetaData fieldMeta, ColumnMetaData columnMeta, List <Value32[]> palletData, Dictionary <int, Value32> commonData, object value) where TType : unmanaged
        {
            switch (columnMeta.CompressionType)
            {
            case CompressionType.None:
            {
                int bitSize = 32 - fieldMeta.Bits;
                if (bitSize <= 0)
                {
                    bitSize = columnMeta.Immediate.BitWidth;
                }

                r.Write((TType)value, bitSize);
                break;
            }

            case CompressionType.Immediate:
            case CompressionType.SignedImmediate:
            {
                r.Write((TType)value, columnMeta.Immediate.BitWidth);
                break;
            }

            case CompressionType.Common:
            {
                if (!columnMeta.Common.DefaultValue.GetValue <TType>().Equals(value))
                {
                    commonData.Add(Id, Value32.Create((TType)value));
                }
                break;
            }

            case CompressionType.Pallet:
            {
                Value32[] array = new[] { Value32.Create((TType)value) };

                int palletIndex = palletData.FindIndex(x => Value32Comparer.Equals(array, x));
                if (palletIndex == -1)
                {
                    palletIndex = palletData.Count;
                    palletData.Add(array);
                }

                r.Write(palletIndex, columnMeta.Pallet.BitWidth);
                break;
            }
            }
        }
Ejemplo n.º 2
0
        private static void WriteFieldValueArray <TType>(BitWriter r, FieldMetaData fieldMeta, ColumnMetaData columnMeta, List <Value32[]> palletData, Dictionary <int, Value32> commonData, Array value) where TType : unmanaged
        {
            switch (columnMeta.CompressionType)
            {
            case CompressionType.None:
            {
                int bitSize = 32 - fieldMeta.Bits;
                if (bitSize <= 0)
                {
                    bitSize = columnMeta.Immediate.BitWidth;
                }

                for (int i = 0; i < value.Length; i++)
                {
                    r.Write((TType)value.GetValue(i), bitSize);
                }

                break;
            }

            case CompressionType.PalletArray:
            {
                // get data
                Value32[] array = new Value32[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    array[i] = Value32.Create((TType)value.GetValue(i));
                }

                int palletIndex = palletData.FindIndex(x => Value32Comparer.Equals(array, x));
                if (palletIndex == -1)
                {
                    palletIndex = palletData.Count;
                    palletData.Add(array);
                }

                r.Write(palletIndex, columnMeta.Pallet.BitWidth);
                break;
            }
            }
        }
Ejemplo n.º 3
0
        public void Serialize(int id, T row)
        {
            BitWriter bitWriter = new BitWriter(m_writer.RecordSize);

            int indexFieldOffSet = 0;

            for (int i = 0; i < m_writer.FieldCache.Length; i++)
            {
                FieldCache <T> info = m_writer.FieldCache[i];

                if (i == m_writer.IdFieldIndex && m_writer.Flags.HasFlagExt(DB2Flags.Index))
                {
                    indexFieldOffSet++;
                    continue;
                }

                int fieldIndex = i - indexFieldOffSet;

                // common data fields
                if (fieldIndex >= m_writer.FieldsCount)
                {
                    m_writer.CommonData[fieldIndex - m_writer.FieldsCount].Add(id, Value32.Create(info.Getter(row)));
                    continue;
                }

                if (info.IsArray)
                {
                    if (arrayWriters.TryGetValue(info.Field.FieldType, out var writer))
                    {
                        writer(bitWriter, m_writer, m_fieldMeta[fieldIndex], (Array)info.Getter(row));
                    }
                    else
                    {
                        throw new Exception("Unhandled array type: " + typeof(T).Name);
                    }
                }
                else
                {
                    if (simpleWriters.TryGetValue(info.Field.FieldType, out var writer))
                    {
                        writer(bitWriter, m_writer, m_fieldMeta[fieldIndex], info.Getter(row));
                    }
                    else
                    {
                        throw new Exception("Unhandled field type: " + typeof(T).Name);
                    }
                }
            }

            // pad to record size
            if (!m_writer.Flags.HasFlagExt(DB2Flags.Sparse))
            {
                bitWriter.Resize(m_writer.RecordSize);
            }
            else
            {
                bitWriter.ResizeToMultiple(4);
            }

            Records[id] = bitWriter;
        }