Ejemplo n.º 1
0
        public static byte[] Serialize(this IColumnSpec columnSpec, object data)
        {
            byte[] rawData;
            switch (columnSpec.ColumnType)
            {
            case ColumnType.List:
                return(SerializeList((IList)data, value => Serialize(columnSpec.CollectionValueType, value)));

            case ColumnType.Set:
                var  colType      = columnSpec.CollectionValueType.ToType();
                Type accessorType = typeof(HashSetAccessor <>).MakeGenericType(colType);
                var  setAccessor  = (IHashSetAccessor)Activator.CreateInstance(accessorType, data);

                return(SerializeSet(setAccessor, value => Serialize(columnSpec.CollectionValueType, value)));

            case ColumnType.Map:
                return(SerializeMap((IDictionary)data, key => Serialize(columnSpec.CollectionKeyType, key), value => Serialize(columnSpec.CollectionValueType, value)));

            default:
                rawData = Serialize(columnSpec.ColumnType, data);
                break;
            }

            return(rawData);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Multiply two matrix with padding and repeating if dimensions don't match.
        /// </summary>
        /// <param name="left">The left matrix.</param>
        /// <param name="right">The right matrix.</param>
        /// <returns>The product of two matrix.</returns>
        public static INumberTable MatrixProduct(INumberTable left, INumberTable right)
        {
            int dim     = right.Rows; // The dimension of the transformation.
            int repeats = left.Columns / dim;

            if (left.Columns % dim > 0)
            {
                repeats++;
            }
            int outColumns = repeats * right.Columns;

            INumberTable prod = WaveTransforms.App.ScriptApp.New.NumberTable(left.Rows, outColumns);

            double[][] P = (double[][])prod.Matrix;
            double[][] L = (double[][])left.Matrix;
            double[][] R = (double[][])right.Matrix;

            for (int row = 0; row < prod.Rows; row++)
            {
                double[] Lrow = L[row];
                for (int col = 0; col < prod.Columns; col++)
                {
                    int    col2 = col % right.Columns;
                    int    k0   = col / right.Columns * dim;
                    double v    = 0;
                    for (int k = 0; k < dim; k++)
                    {
                        if ((k0 + k) < Lrow.Length)
                        {
                            v += Lrow[k0 + k] * R[k][col2];
                        }
                    }
                    P[row][col] = v;
                }
            }

            for (int row = 0; row < left.Rows; row++)
            {
                prod.RowSpecList[row].CopyFrom(left.RowSpecList[row]);
            }

            IList <IColumnSpec> pSpecList = prod.ColumnSpecList;
            IList <IColumnSpec> rSpecList = right.ColumnSpecList;

            for (int col = 0; col < prod.Columns; col++)
            {
                IColumnSpec cs = pSpecList[col];
                cs.CopyFrom(rSpecList[col % right.Columns]);
                int n = col / right.Columns;
                if (n > 0)
                {
                    cs.Id += "_" + n;
                }
            }
            return(prod);
        }
 internal static IObservable<object> CreateExecuteQuery(IConnection connection, byte[] id, IColumnSpec[] columnSpecs, ConsistencyLevel cl,
                                                        ExecutionFlags executionFlags, string cql,
                                                        IDataMapperFactory factory)
 {
     Action<IFrameWriter> writer = fw => WriteExecuteRequest(fw, id, columnSpecs, cl, factory);
     Func<IFrameReader, IEnumerable<object>> reader = fr => ReadRowSet(fr, factory);
     InstrumentationToken token = InstrumentationToken.Create(RequestType.Prepare, executionFlags, cql);
     Query query = new Query(writer, reader, token, connection);
     return query;
 }
Ejemplo n.º 4
0
        protected static IColumnSpec[] ReadColumnSpec(IFrameReader frameReader)
        {
            Stream        stream           = frameReader.ReadOnlyStream;
            MetadataFlags flags            = (MetadataFlags)stream.ReadInt();
            bool          globalTablesSpec = 0 != (flags & MetadataFlags.GlobalTablesSpec);

            int colCount = stream.ReadInt();

            string keyspace = null;
            string table    = null;

            if (globalTablesSpec)
            {
                keyspace = stream.ReadString();
                table    = stream.ReadString();
            }

            IColumnSpec[] columnSpecs = new IColumnSpec[colCount];
            for (int colIdx = 0; colIdx < colCount; ++colIdx)
            {
                string colKeyspace = keyspace;
                string colTable    = table;
                if (!globalTablesSpec)
                {
                    colKeyspace = stream.ReadString();
                    colTable    = stream.ReadString();
                }
                string     colName      = stream.ReadString();
                ColumnType colType      = (ColumnType)stream.ReadUShort();
                string     colCustom    = null;
                ColumnType colKeyType   = ColumnType.Custom;
                ColumnType colValueType = ColumnType.Custom;
                switch (colType)
                {
                case ColumnType.Custom:
                    colCustom = stream.ReadString();
                    break;

                case ColumnType.List:
                case ColumnType.Set:
                    colValueType = (ColumnType)stream.ReadUShort();
                    break;

                case ColumnType.Map:
                    colKeyType   = (ColumnType)stream.ReadUShort();
                    colValueType = (ColumnType)stream.ReadUShort();
                    break;
                }

                columnSpecs[colIdx] = new ColumnSpec(colIdx, colKeyspace, colTable, colName, colType, colCustom, colKeyType, colValueType);
            }

            return(columnSpecs);
        }
Ejemplo n.º 5
0
        public void Set(IColumnSpec columnSpec, object data)
        {
            if (null == data)
                return;

            if (! TrySet(columnSpec.Name, data))
            {
                if (columnSpec.Name.Contains("_"))
                {
                    string newName = columnSpec.Name.Replace("_", "");
                    TrySet(newName, data);
                }
            }
        }
        public bool Set(IColumnSpec columnSpec, object data)
        {
            if (TrySet(columnSpec.Name, data))
            {
                return true;
            }

            if (columnSpec.Name.Contains("_"))
            {
                string newName = columnSpec.Name.Replace("_", "");
                return TrySet(newName, data);
            }

            return false;
        }
        public ColumnSpec(IColumnSpec columnSpec)
        {
            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)base.ObfuscationStrategySpec != null &&
                (object)columnSpec.ObfuscationStrategySpec != null)
            {
                foreach (KeyValuePair <string, object> keyValuePair in columnSpec.ObfuscationStrategySpec)
                {
                    base.ObfuscationStrategySpec.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }

            this.ColumnName = columnSpec.ColumnName;
            this.ObfuscationStrategyType = columnSpec.ObfuscationStrategyType;
        }
Ejemplo n.º 8
0
        public static object Deserialize(this IColumnSpec columnSpec, byte[] rawData)
        {
            object data;
            Type   colType;

            switch (columnSpec.ColumnType)
            {
            default:
                data = Deserialize(columnSpec.ColumnType, rawData);
                break;

            case ColumnType.List:
                colType = columnSpec.CollectionValueType.ToType();

                Type  typedColl = typeof(List <>).MakeGenericType(columnSpec.CollectionValueType.ToType());
                IList list      = (IList)Activator.CreateInstance(typedColl);

                return(DeserializeList(rawData, list, value => Deserialize(columnSpec.CollectionValueType, value)));

            case ColumnType.Map:
                colType = columnSpec.CollectionValueType.ToType();
                var colKeyType = columnSpec.CollectionKeyType.ToType();

                Type        typedDic = typeof(Dictionary <,>).MakeGenericType(colKeyType, colType);
                IDictionary dic      = (IDictionary)Activator.CreateInstance(typedDic);

                return(DeserializeMap(rawData, dic, key => Deserialize(columnSpec.CollectionKeyType, key), val => Deserialize(columnSpec.CollectionValueType, val)));

            case ColumnType.Set:
                colType = columnSpec.CollectionValueType.ToType();

                Type   typedSet = typeof(HashSet <>).MakeGenericType(colType);
                object hashSet  = Activator.CreateInstance(typedSet);

                Type accessorType = typeof(HashSetAccessor <>).MakeGenericType(colType);
                var  setAccessor  = (IHashSetAccessor)Activator.CreateInstance(accessorType, hashSet);
                DeserializeSet(rawData, setAccessor, val => Deserialize(columnSpec.CollectionValueType, val));
                return(hashSet);
            }

            return(data);
        }
Ejemplo n.º 9
0
        public object Get(IColumnSpec columnSpec)
        {
            object res;
            string name = columnSpec.Name;
            if (TryGet(name, out res))
            {
                return res;
            }

            if (name.Contains("_"))
            {
                name = name.Replace("_", "");
                if (TryGet(name, out res))
                {
                    return res;
                }
            }

            throw new ArgumentException("Can't find requested member", columnSpec.Name);
        }
        public object GetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec columnSpec, IField field, object originalFieldValue)
        {
            IColumnSpec <TObfuscationStrategySpec> _columnSpec;
            object value;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)originalFieldValue == DBNull.Value)
            {
                originalFieldValue = null;
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            _columnSpec = new ColumnSpec <TObfuscationStrategySpec>(columnSpec);

            if ((object)_columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(_columnSpec.ObfuscationStrategySpec)));
            }

            value = this.CoreGetObfuscatedValue(obfuscationContext, _columnSpec, field, originalFieldValue);

            return(value);
        }
Ejemplo n.º 11
0
        protected static IColumnSpec[] ReadColumnSpecs(int colCount, string keyspace, string table, bool globalTablesSpec, Stream stream)
        {
            IColumnSpec[] columnSpecs = new IColumnSpec[colCount];
            for (int colIdx = 0; colIdx < colCount; ++colIdx)
            {
                string colKeyspace = keyspace;
                string colTable    = table;
                if (!globalTablesSpec)
                {
                    colKeyspace = stream.ReadString();
                    colTable    = stream.ReadString();
                }

                string     colName      = stream.ReadString();
                ColumnType colType      = (ColumnType)stream.ReadUShort();
                string     colCustom    = null;
                ColumnType colKeyType   = ColumnType.Custom;
                ColumnType colValueType = ColumnType.Custom;
                switch (colType)
                {
                case ColumnType.Custom:
                    colCustom = stream.ReadString();
                    break;

                case ColumnType.List:
                case ColumnType.Set:
                    colValueType = (ColumnType)stream.ReadUShort();
                    break;

                case ColumnType.Map:
                    colKeyType   = (ColumnType)stream.ReadUShort();
                    colValueType = (ColumnType)stream.ReadUShort();
                    break;
                }

                columnSpecs[colIdx] = new ColumnSpec(colIdx, colKeyspace, colTable, colName, colType, colCustom, colKeyType, colValueType);
            }
            return(columnSpecs);
        }
        private static IEnumerable<object> ReadRows(IFrameReader frameReader, IColumnSpec[] columnSpecs, IDataMapperFactory mapperFactory)
        {
            Stream stream = frameReader.ReadOnlyStream;
            int rowCount = stream.ReadInt();
            for (int rowIdx = 0; rowIdx < rowCount; ++rowIdx)
            {
                IInstanceBuilder instanceBuilder = mapperFactory.CreateBuilder();
                foreach (ColumnSpec colSpec in columnSpecs)
                {
                    byte[] rawData = stream.ReadByteArray();
                    object data = null != rawData
                                          ? colSpec.Deserialize(rawData)
                                          : null;
                    instanceBuilder.Set(colSpec, data);
                }

                yield return instanceBuilder.Build();
            }
        }
        private static void WriteExecuteRequest(IFrameWriter frameWriter, byte[] id, IColumnSpec[] columnSpecs, ConsistencyLevel cl, IDataMapperFactory factory)
        {
            Stream stream = frameWriter.WriteOnlyStream;
            stream.WriteShortByteArray(id);
            stream.WriteShort((short) columnSpecs.Length);

            IDataSource dataSource = factory.DataSource;
            foreach (IColumnSpec columnSpec in columnSpecs)
            {
                object data = dataSource.Get(columnSpec);
                byte[] rawData = columnSpec.Serialize(data);
                stream.WriteByteArray(rawData);
            }

            stream.WriteShort((short) cl);
            frameWriter.SetMessageType(MessageOpcodes.Execute);
        }
Ejemplo n.º 14
0
        public bool ImportFile0(string fileName)
        {
            IVisuMap app = CustomImporter.App.ScriptApp;

            if (!fileName.ToLower().EndsWith(".fcs"))
            {
                return(false);  // Let other import handle it.
            }

            INumberTable dataTable   = null;
            IFreeTable   textTable   = null;
            bool         compensated = false;
            bool         logScaled   = false;

            using (StreamReader sr = new StreamReader(fileName)) {
                //
                // Read the text segment.
                //
                char[] header = new char[42];
                sr.ReadBlock(header, 0, header.Length);
                int textBegin = int.Parse(new string(header, 10, 8));
                int textEnd   = int.Parse(new string(header, 18, 8));
                int beginData = int.Parse(new string(header, 26, 8));
                int endData   = int.Parse(new string(header, 34, 8));

                char[] line = new char[textEnd + 4];
                sr.ReadBlock(line, header.Length, line.Length - header.Length);
                string textSeg   = new string(line, textBegin + 1, textEnd - textBegin);
                char   delimiter = line[textBegin];

                textTable = app.New.FreeTable();
                textTable.AddColumn("Value", false);

                string[] textFields = textSeg.Split(delimiter);
                textTable.AddRows("P", textFields.Length / 2);
                IList <IRowSpec> rowSpecList = textTable.RowSpecList;
                for (int row = 0; row < textTable.Rows; row++)
                {
                    rowSpecList[row].Id      = textFields[2 * row];
                    textTable.Matrix[row][0] = textFields[2 * row + 1];
                }

                //
                // Read in the data segement
                //
                fcsInfo = new FcsInfo(textTable, header);
                if ((beginData == 0) && (textTable.IndexOfRow("$BEGINDATA") > 0))
                {
                    beginData = int.Parse(textTable.Matrix[textTable.IndexOfRow("$BEGINDATA")][0]);
                }
                if ((endData == 0) && (textTable.IndexOfRow("$ENDDATA") > 0))
                {
                    endData = int.Parse(textTable.Matrix[textTable.IndexOfRow("$ENDDATA")][0]);
                }
                dataTable = app.New.NumberTable(fcsInfo.Rows, fcsInfo.Columns);

                using (BinaryReader br = new BinaryReader(sr.BaseStream)) {
                    br.BaseStream.Seek(beginData, SeekOrigin.Begin);

                    Byte[] buf4 = new byte[4];
                    Byte[] buf8 = new byte[8];

                    int bitOffset = 0;
                    for (int row = 0; row < fcsInfo.Rows; row++)
                    {
                        for (int col = 0; col < fcsInfo.Columns; col++)
                        {
                            ColumnInfo ci   = fcsInfo.ColumnInfo[col];
                            Byte[]     data = ReadBits(br, ci.Bits, ref bitOffset);
                            if (data.Length < ci.Bytes)
                            {
                                row = fcsInfo.Rows; // enforce premature loop-end.
                                break;
                            }
                            Byte[] buf = (fcsInfo.DataType == FcsInfo.DataTypes.Double) ? buf8 : buf4;
                            Array.Clear(buf, 0, buf.Length);
                            Array.Copy(data, 0, buf, 0, ci.Bytes);
                            if (!fcsInfo.BigEndian)
                            {
                                // Intel CPU expects BigEndian format.
                                Array.Reverse(buf, 0, ci.Bytes);
                            }

                            switch (fcsInfo.DataType)
                            {
                            case FcsInfo.DataTypes.Integer:
                                dataTable.Matrix[row][col] = (BitConverter.ToUInt32(buf, 0) & ci.RangeMask);
                                break;

                            case FcsInfo.DataTypes.Float:
                                dataTable.Matrix[row][col] = BitConverter.ToSingle(buf, 0);
                                break;

                            case FcsInfo.DataTypes.Double:
                                dataTable.Matrix[row][col] = BitConverter.ToDouble(buf, 0);
                                break;
                            }
                        }
                    }
                }
            }

            // Post processing
            for (int col = 0; col < fcsInfo.Columns; col++)
            {
                IColumnSpec cs    = dataTable.ColumnSpecList[col];
                ColumnInfo  cInfo = fcsInfo.ColumnInfo[col];
                cs.Id = cInfo.ShortName;
                //cs.Name = cInfo.Name + ( cInfo.IsLinear ? ".Lin" : ".Log");
                cs.Name = cInfo.Name;

                if ((cs.Id == "TIME") || (cs.Id == "TIME1"))
                {
                    int timeStepIdx = textTable.IndexOfRow("$TIMESTEP");
                    if (timeStepIdx >= 0)
                    {
                        double timeStep = double.Parse(textTable.Matrix[timeStepIdx][0]);
                        for (int row = 0; row < fcsInfo.Rows; row++)
                        {
                            dataTable.Matrix[row][col] *= timeStep;
                        }
                    }
                }
            }

            IList <IRowSpec> rSpecList = dataTable.RowSpecList;

            for (int row = 0; row < fcsInfo.Rows; row++)
            {
                rSpecList[row].Id = row.ToString();
            }


            FileInfo fInfo     = new FileInfo(fileName);
            string   shortName = fInfo.Name.Substring(0, fInfo.Name.LastIndexOf('.'));

            if (saveMetaInfo >= 1)
            {
                SaveParameterTable(textTable, shortName);
                if (saveMetaInfo >= 2)
                {
                    textTable.SaveAsDataset(shortName + " (Text Seg)", "Text segement of data table " + shortName);
                }
            }

            if (autoCompensation)
            {
                try {
                    string sMatrix = null;
                    for (int i = 0; i < textTable.Rows; i++)
                    {
                        string id = textTable.RowSpecList[i].Id.ToLower();
                        if (id.StartsWith("$"))
                        {
                            id = id.Substring(1);
                        }
                        if ((id == "spill") || (id == "spillover"))
                        {
                            sMatrix = textTable.Matrix[i][0];
                            break;
                        }
                    }

                    if (sMatrix == null)
                    {
                        throw new Exception("");                   // silently ignore compensation as no compensation matrix available.
                    }
                    string[] fs        = sMatrix.Split(',');
                    int      dimension = int.Parse(fs[0]);

                    if (fs.Length != (dimension * dimension + dimension + 1))
                    {
                        throw new Exception("Invalid spill over matrix.");
                    }

                    INumberTable  cMatrix = app.New.NumberTable(dimension, dimension);
                    List <string> parList = fs.Skip(1).Take(dimension).ToList();

                    int idx;
                    if (parList.Count(id => int.TryParse(id, out idx)) == parList.Count)
                    {
                        // The columns are specified by a list of indexes. We convert them here to ids
                        for (int i = 0; i < parList.Count; i++)
                        {
                            parList[i] = dataTable.ColumnSpecList[int.Parse(parList[i]) - 1].Id;   // index starts with 1 !
                        }
                    }

                    for (int col = 0; col < cMatrix.Columns; col++)
                    {
                        cMatrix.ColumnSpecList[col].Id = parList[col];
                    }
                    int fsIdx = dimension + 1;
                    for (int row = 0; row < cMatrix.Rows; row++)
                    {
                        for (int col = 0; col < cMatrix.Columns; col++)
                        {
                            cMatrix.Matrix[row][col] = double.Parse(fs[fsIdx++]);
                        }
                    }

                    var cData = dataTable.SelectColumnsById(parList);
                    if (cData.Columns != parList.Count)
                    {
                        if (dataTable.ColumnSpecList.Count(cl => cl.Id.Equals("<" + parList[0] + ">")) == 1)
                        {
                            // siliently ignore aready compensated data.
                            throw new Exception("");
                        }
                        else
                        {
                            throw new Exception("Invalid spill over matrix: unknown names.");
                        }
                    }

                    var math = CustomImporter.App.GetMathAdaptor();
                    var m    = math.InvertMatrix((double[][])cMatrix.Matrix);
                    for (int row = 0; row < cMatrix.Rows; row++)
                    {
                        cMatrix.Matrix[row] = m[row];
                    }

                    cData = cData.Multiply(cMatrix);  // perform the comensation with the inverse matrix of the spill over matrix.
                    cData.CopyValuesTo(dataTable);
                    compensated = true;
                } catch (Exception ex) {
                    if (ex.Message != "")
                    {
                        MessageBox.Show("Cannot perform compensation: " + ex.Message);
                    }
                }
            }

            if (logTransform)
            {
                double[][] m = (double[][])dataTable.Matrix;

                double   T        = 262144;
                double   W        = 1.0;
                double   M        = 4.5;
                string[] settings = app.GetProperty(
                    "CustomImporter.Logicle.Settings", "262144; 1.0; 4.5").Split(';');
                if (settings.Length == 3)
                {
                    double.TryParse(settings[0], out T);
                    double.TryParse(settings[1], out W);
                    double.TryParse(settings[2], out M);
                }
                var    fastLogicle = new VisuMap.DataCleansing.FastLogicle(T, W, M);
                double maxVal      = fastLogicle.MaxValue;
                double minVal      = fastLogicle.MinValue;

                for (int col = 0; col < fcsInfo.Columns; col++)
                {
                    if (fcsInfo.ColumnInfo[col].LogTrans)
                    {
                        for (int row = 0; row < fcsInfo.Rows; row++)
                        {
                            m[row][col] = M * fastLogicle.scale(Math.Min(maxVal, Math.Max(minVal, m[row][col])));
                        }
                        logScaled = true;
                    }
                }
            }

            string msg = "Dataset imported from " + fileName + ". Version: " + fcsInfo.version;

            if (compensated)
            {
                msg += ", compensated";
            }
            if (logScaled)
            {
                msg += ", log-scaled";
            }
            string dsName = dataTable.SaveAsDataset(shortName, msg);

            app.Folder.OpenDataset(dsName);
            INumberTable nTable = app.GetNumberTable();


            List <IColumnSpec> csList = nTable.ColumnSpecList as List <IColumnSpec>;
            int fsc = csList.FindIndex(cs => cs.Id == "FSC-A");
            int ssc = csList.FindIndex(cs => cs.Id == "SSC-A");

            if ((fsc >= 0) && (ssc >= 0))
            {
                var xy = app.New.XyPlot(nTable);
                xy.Show();
                xy.XAxisIndex  = fsc;
                xy.YAxisIndex  = ssc;
                xy.AutoScaling = true;
                xy.Redraw();
                xy.CaptureMap();
                xy.Close();
                app.Map.Name = "FSC/SSC";
            }
            else
            {
                // We just do create a simple PCA view.
                IPcaView pca = nTable.ShowPcaView();
                pca.ResetView();
                pca.CaptureMap();
                app.Map.Redraw();
                pca.Close();
                app.Map.Name = "PCA-All";
            }

            app.Map.GlyphType = glyphName;
            app.Map.Redraw();

            if (showResult)
            {
                app.New.SpectrumBand(nTable).Show();
            }

            fcsInfo = null;
            return(true);
        }
Ejemplo n.º 15
0
 public void Set(IColumnSpec columnSpec, object data)
 {
     _data[columnSpec.Name] = data;
 }
Ejemplo n.º 16
0
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            Type   valueType;
            string _columnValue;

            long   surrogateKey;
            object surrogateValue;

            IDictionarySpec dictionarySpec;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            if ((object)columnValue == null)
            {
                return(null);
            }

            valueType = columnValue.GetType();

            if (valueType != typeof(string))
            {
                return(null);
            }

            _columnValue = (string)columnValue;
            _columnValue = _columnValue.Trim();

            if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(_columnValue))
            {
                return(_columnValue);
            }

            dictionarySpec = columnSpec.ObfuscationStrategySpec.DictionarySpec;

            if ((object)dictionarySpec == null)
            {
                throw new InvalidOperationException(string.Format("Failed to obtain dictionary."));
            }

            if ((dictionarySpec.RecordCount ?? 0L) <= 0L)
            {
                return(null);
            }

            surrogateKey = obfuscationContext.GetValueHash(dictionarySpec.RecordCount, columnValue);

            if (!obfuscationContext.TryGetSurrogateValue(dictionarySpec, surrogateKey, out surrogateValue))
            {
                throw new InvalidOperationException(string.Format("Failed to obtain surrogate value."));
            }

            return(surrogateValue);
        }
Ejemplo n.º 17
0
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            return(columnValue);
        }
        private static IColumnSpec[] ReadColumnSpec(IFrameReader frameReader)
        {
            Stream stream = frameReader.ReadOnlyStream;
            MetadataFlags flags = (MetadataFlags) stream.ReadInt();
            bool globalTablesSpec = 0 != (flags & MetadataFlags.GlobalTablesSpec);

            int colCount = stream.ReadInt();

            string keyspace = null;
            string table = null;
            if (globalTablesSpec)
            {
                keyspace = stream.ReadString();
                table = stream.ReadString();
            }

            IColumnSpec[] columnSpecs = new IColumnSpec[colCount];
            for (int colIdx = 0; colIdx < colCount; ++colIdx)
            {
                string colKeyspace = keyspace;
                string colTable = table;
                if (!globalTablesSpec)
                {
                    colKeyspace = stream.ReadString();
                    colTable = stream.ReadString();
                }
                string colName = stream.ReadString();
                ColumnType colType = (ColumnType) stream.ReadShort();
                string colCustom = null;
                ColumnType colKeyType = ColumnType.Custom;
                ColumnType colValueType = ColumnType.Custom;
                switch (colType)
                {
                    case ColumnType.Custom:
                        colCustom = stream.ReadString();
                        break;

                    case ColumnType.List:
                    case ColumnType.Set:
                        colValueType = (ColumnType) stream.ReadShort();
                        break;

                    case ColumnType.Map:
                        colKeyType = (ColumnType) stream.ReadShort();
                        colValueType = (ColumnType) stream.ReadShort();
                        break;
                }

                columnSpecs[colIdx] = new ColumnSpec(colIdx, colKeyspace, colTable, colName, colType, colCustom, colKeyType, colValueType);
            }

            return columnSpecs;
        }
Ejemplo n.º 19
0
 public PreparedQuery(IConnection connection, byte[] id, IColumnSpec[] columnSpecs)
 {
     _connection = connection;
     _id = id;
     _columnSpecs = columnSpecs;
 }
Ejemplo n.º 20
0
 public object Get(IColumnSpec columnSpec)
 {
     return _dataSource[columnSpec.Index];
 }
Ejemplo n.º 21
0
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            object value;
            double maskingFactor;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            maskingFactor = (columnSpec.ObfuscationStrategySpec.MaskingPercentValue.GetValueOrDefault() / 100.0);

            value = GetMask(maskingFactor, columnValue);

            return(value);
        }
Ejemplo n.º 22
0
 public ColumnData(IColumnSpec spec, byte[] rawData)
 {
     ColumnSpec = spec;
     RawData = rawData;
 }
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            long   signHash, valueHash;
            object value;
            double varianceFactor;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            signHash       = obfuscationContext.GetSignHash(columnValue);
            valueHash      = obfuscationContext.GetValueHash(columnSpec.ObfuscationStrategySpec.VariancePercentValue, columnValue);
            varianceFactor = ((((valueHash <= 0 ? 1 : valueHash)) * ((signHash % 2 == 0 ? 1.0 : -1.0))) / 100.0);

            value = GetVariance(varianceFactor, columnValue);

            return(value);
        }
Ejemplo n.º 24
0
 public ColumnData(IColumnSpec spec, byte[] rawData)
 {
     ColumnSpec = spec;
     RawData    = rawData;
 }
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            object value;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            //value = GetDefault(field.IsOptional && (columnSpec.ObfuscationStrategySpec.DefaultingCanBeNull ?? false), field.FieldType);
            value = null;
            return(value);
        }
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            long   valueHash;
            object value;
            long   randomSeed;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            valueHash  = obfuscationContext.GetValueHash(null, columnValue);
            randomSeed = valueHash;

            value = GetShuffle(randomSeed, columnValue);

            return(value);
        }
Ejemplo n.º 27
0
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            long   signHash, valueHash;
            object value;
            string sharedSecret;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            signHash     = obfuscationContext.GetSignHash(columnValue);
            valueHash    = obfuscationContext.GetValueHash(null, columnValue);
            sharedSecret = ((valueHash <= 0 ? 1 : valueHash) * (signHash % 2 == 0 ? -1 : 1)).ToString("X");

            value = GetCipher(sharedSecret, columnValue);

            return(value);
        }
        protected override object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <Spec> columnSpec, IField field, object columnValue)
        {
            long   valueHash;
            object value;
            long   randomSeed;

            if ((object)obfuscationContext == null)
            {
                throw new ArgumentNullException(nameof(obfuscationContext));
            }

            if ((object)columnSpec == null)
            {
                throw new ArgumentNullException(nameof(columnSpec));
            }

            if ((object)field == null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            if ((object)columnSpec.ObfuscationStrategySpec == null)
            {
                throw new InvalidOperationException(string.Format("Specification missing: '{0}'.", nameof(columnSpec.ObfuscationStrategySpec)));
            }

            valueHash  = obfuscationContext.GetValueHash(null, field.FieldName);
            randomSeed = valueHash;

            // create a new repeatable yet random-ish math funcion using the random seed, then executes for column value
            value = GetSurrogateKey(randomSeed, columnValue);

            return(value);
        }
Ejemplo n.º 29
0
 public bool Set(IColumnSpec columnSpec, object data)
 {
     _data[columnSpec.Name] = data;
     return true;
 }
 protected abstract object CoreGetObfuscatedValue(IObfuscationContext obfuscationContext, IColumnSpec <TObfuscationStrategySpec> columnSpec, IField field, object columnValue);