/// <summary> /// Save the DTT square to batch. /// </summary> /// <param name="tableName">Name of the table.</param> /// <param name="square">The square.</param> private void SaveDTTSquareToBatch(string tableName, DTTSquare square) { try { byte[] block = new byte[square.DttValues.Length * sizeof(int)]; Buffer.BlockCopy(square.DttValues, 0, block, 0, block.Length); var stream = new MemoryStream(); using (Stream ds = new GZipStream(stream, CompressionMode.Compress)) { ds.Write(block, 0, square.DttValues.Count() * sizeof(int)); } byte[] compressed = stream.ToArray(); DynamicTableEntity entity = new DynamicTableEntity(); entity.Properties.Add("DataRecord", EntityProperty.GeneratePropertyForByteArray(compressed)); entity.Properties.Add("Easting", EntityProperty.GeneratePropertyForInt(square.Easting)); entity.Properties.Add("Northing", EntityProperty.GeneratePropertyForInt(square.Northing)); entity.RowKey = square.Northing.ToString().PadLeft(7, '0'); entity.PartitionKey = square.Easting.ToString().PadLeft(7, '0'); if (!batches.ContainsKey(entity.PartitionKey)) { batches[entity.PartitionKey] = new TableBatchOperation(); } TableBatchOperation batchOperations = batches[entity.PartitionKey]; batchOperations.Add(TableOperation.Insert(entity)); if (batchOperations.Count >= 50) { this.FlushBatch(tableName, batchOperations); batchOperations.Clear(); } } catch (Exception ex) { this.Logger.Log(TraceEventType.Error, LoggingMessageId.DttSyncGenericMessage, string.Format("Exception Occured in Table: {0}, Exception :{1}", tableName, ex.ToString())); } }
/// <summary> /// Decompresses the DTT data. /// </summary> /// <param name="dttblockdata">The DTT block data.</param> /// <returns>returns DTTSquare.</returns> public static DTTSquare DecompressDTTData(DTTDataAvailability dttblockdata) { DTTSquare dttdata = null; using (MemoryStream ms = new MemoryStream()) { int msgLength = BitConverter.ToInt32(dttblockdata.DataRecord, 0); ms.Write(dttblockdata.DataRecord, 0, dttblockdata.DataRecord.Length - 0); byte[] buffer = new byte[msgLength]; ms.Position = 0; GZipStream zip = new GZipStream(ms, CompressionMode.Decompress); zip.Read(buffer, 0, buffer.Length); // Convert byte to integer array int[] block = new int[buffer.Length / sizeof(int)]; Buffer.BlockCopy(buffer, 0, block, 0, block.Length); dttdata = new DTTSquare(dttblockdata.Easting, dttblockdata.Northing, block); } return(dttdata); }
/// <summary> /// Loads the DTT synchronize file. /// </summary> /// <param name="dataStream">The data stream.</param> /// <param name="fileName">Name of the file.</param> /// <param name="tableName">Name of the table.</param> public void ProcessDttSyncFile(Stream dataStream, string fileName, string tableName) { this.Logger.Log(TraceEventType.Information, LoggingMessageId.DttSyncGenericMessage, string.Format("Enter Method Name: {0}", "ProcessDttSyncFile")); try { int count = 0; Dictionary <string, DTTSquare> squares = new Dictionary <string, DTTSquare>(); using (StreamReader sr = new StreamReader(dataStream)) { // First line should be header string line = sr.ReadLine(); // Make sure that the header has "easting" if (!line.ToLower().Contains("easting")) { throw new Exception("First line does not contain the word 'easting'"); } while (sr.Peek() >= 0) { count++; line = sr.ReadLine(); ////string[] numbers = line.Split(','); int[] dttValues = line.Split(new[] { ',' }).Select(obj => obj.ToInt32()).ToArray(); int squareStartEasting = (dttValues[0] / DTTSquare.NumberOfEastings) * DTTSquare.NumberOfEastings; int squareStartNorthing = (dttValues[1] / DTTSquare.NumberOfNorthings) * DTTSquare.NumberOfNorthings; string key = DTTSquare.FormatEastingNorthingName(squareStartEasting, squareStartNorthing); DTTSquare square; if (!squares.Keys.Contains(key)) { square = new DTTSquare(squareStartEasting, squareStartNorthing); squares.Add(key, square); } else { square = squares[key]; } square.SetDTT(dttValues[0], dttValues[1], dttValues); if (square.AllDTTFieldsSet()) { // Write square out to be completed this.SaveDTTSquareToBatch(tableName, square); squares.Remove(key); } } if (squares.Count() > 0) { throw new Exception("Didn't write out all squares."); } // Flush any remaining batches foreach (string paritionKey in batches.Keys) { TableBatchOperation batchOperation = batches[paritionKey]; this.FlushBatch(tableName, batchOperation); } } } catch (Exception e) { this.Logger.Log(TraceEventType.Error, LoggingMessageId.DttSyncGenericMessage, string.Format("Exception Occured in file : - {0}, Exception :{1}", fileName, e.ToString())); } this.Logger.Log(TraceEventType.Information, LoggingMessageId.DttSyncGenericMessage, string.Format("Exit Method Name: {0}", "ProcessDttSyncFile")); }