Beispiel #1
0
    /// <summary>
    /// 序列化
    /// </summary>
    public static void Serialize(MemoryStream stream, CsvFile csv)
    {
        // 初始化缓存
        if (mSerBuffer == null)
        {
            mSerBuffer = new byte[1024 * 1024];
        }

        FileMgr.WriteInt(stream, CsvFileMgr.Version);

        FileMgr.WriteString(stream, csv.Name);

        FileMgr.WriteString(stream, csv.primaryKey);

        // 写入主key类型
        LPC.LPCValue.ValueType pkeyType = LPCValue.ValueType.INT;
        if (csv.rows.Length > 0)
        {
            var row  = csv.rows[0];
            var pkey = row.Query <LPCValue>(csv.primaryKey);
            pkeyType = pkey.type;
            FileMgr.WriteInt(stream, (int)pkeyType);
        }

        // 列名
        FileMgr.WriteInt(stream, csv.columns.Count);
        foreach (var kv in csv.columns)
        {
            FileMgr.WriteString(stream, kv.Key);
            FileMgr.WriteInt(stream, kv.Value);
        }

        // 行数
        FileMgr.WriteInt(stream, csv.rows.Length);

        // 写入每行的主key
        for (int i = 0; i < csv.rows.Length; i++)
        {
            var row = csv.rows[i];
            if (pkeyType == LPCValue.ValueType.INT)
            {
                var pkey = row.Query <int>(csv.primaryKey);
                FileMgr.WriteInt(stream, pkey);
            }
            else
            {
                var pkey = row.Query <string>(csv.primaryKey);
                FileMgr.WriteString(stream, pkey);
            }
        }

        // 写入行长度和内容
        for (int i = 0; i < csv.rows.Length; i++)
        {
            var row = csv.rows[i];
            int len = 0;

            for (var idx = 0; idx < csv.columns.Count; idx++)
            {
                len += LPCValue.SaveToBuffer(mSerBuffer, len, row.properties[idx]);
            }

            FileMgr.WriteInt(stream, len);
            stream.Write(mSerBuffer, 0, len);
        }

        // 释放
        mSerBuffer = null;
    }