/*For max arrays*/ public void Write(SqlRealArrayMax a) { SqlBytes b = a.ToSqlBuffer(); this.Write(b); }
public static IEnumerable LookupDataNoSlots(SqlInt16 partsnap, SqlString phkeytable, SqlInt32 numparts, SqlString temp_partid_table_name) { //string temp_partid_table = "#temp_partid_list"; List<PosData> idPositionMap = new List<PosData>(); using (SqlConnection connection = new SqlConnection("context connection=true")) { connection.Open(); // Phkey -> (List of slots in that phkey) List<int> phkeyList = new List<int>(); string rebuildPhkeySlotMapString = "select distinct phkey from " + phkeytable.ToString(); SqlCommand rebuildPhkeySlotMapCommand = new SqlCommand(rebuildPhkeySlotMapString, connection); SqlDataReader mapReader = rebuildPhkeySlotMapCommand.ExecuteReader(); while (mapReader.Read()) { int phkey = Int32.Parse(mapReader["phkey"].ToString()); //short slot = Int16.Parse(mapReader["slot"].ToString()); phkeyList.Add(phkey); } mapReader.Close(); string getFoFGroupIDsCommandString = "select partid from " + temp_partid_table_name.ToString(); SqlCommand getFoFGroupIDsListCommand = new SqlCommand(getFoFGroupIDsCommandString, connection); SqlDataReader getFOFGroupIDsReader = getFoFGroupIDsListCommand.ExecuteReader(); List<Int64> haloPartIDs = new List<Int64>(); int numlines = 0; while (getFOFGroupIDsReader.Read()) { haloPartIDs.Add(Int64.Parse(getFOFGroupIDsReader["partid"].ToString())); ++numlines; } getFOFGroupIDsReader.Close(); string getActualDataString = "select a.phkey, a.id, a.pos from SimulationDB.dbo.snaparr a, " + phkeytable.ToString() + " b where a.snapnum = @partsnap and a.phkey = b.phkey"; SqlParameter partsnapParam2 = new SqlParameter("@partsnap", System.Data.SqlDbType.SmallInt); partsnapParam2.Value = partsnap; SqlCommand getActualDataCommand = new SqlCommand(getActualDataString, connection); getActualDataCommand.Parameters.Add(partsnapParam2); SqlDataReader dataReader = getActualDataCommand.ExecuteReader(); while (dataReader.Read()) { List<Int64> ids = new List<Int64>((new SqlBigIntArrayMax(new SqlBytes((byte[])dataReader["id"])).ToArray())); SqlRealArrayMax positions = new SqlRealArrayMax(new SqlBytes((byte[])dataReader["pos"])); int phkey = Int32.Parse(dataReader["phkey"].ToString()); float[] posArray = positions.ToArray(); for (int i = 0; i < haloPartIDs.Count; ++i) { int slot = ids.BinarySearch(haloPartIDs[i]); if (slot >= 0) { PosData currentPosComps = new PosData(ids[slot], posArray[slot * 3 + 0], posArray[slot * 3 + 1], posArray[slot * 3 + 2]); haloPartIDs.RemoveAt(i); --i; idPositionMap.Add(currentPosComps); } } } dataReader.Read(); } return idPositionMap; }
public static IEnumerable LookupData(SqlInt16 partsnap, SqlString phkeytable) { //string temp_partid_table = "#temp_partid_list"; List<PosData> idPositionMap = new List<PosData>(); using (SqlConnection connection = new SqlConnection("context connection=true")) { connection.Open(); // Phkey -> (List of slots in that phkey) Dictionary<int, List<int>> phkeySlotMap = new Dictionary<int, List<int>>(); string rebuildPhkeySlotMapString = "select phkey, slot from " + phkeytable.ToString(); SqlCommand rebuildPhkeySlotMapCommand = new SqlCommand(rebuildPhkeySlotMapString, connection); SqlDataReader mapReader = rebuildPhkeySlotMapCommand.ExecuteReader(); while (mapReader.Read()) { int phkey = Int32.Parse(mapReader["phkey"].ToString()); short slot = Int16.Parse(mapReader["slot"].ToString()); List<int> slotList = null; if (!phkeySlotMap.TryGetValue(phkey, out slotList)) { slotList = new List<int>(); } slotList.Add(slot); phkeySlotMap[phkey] = slotList; } mapReader.Close(); string getActualDataString = "select distinct a.phkey, a.id, a.pos from SimulationDB.dbo.snaparr a, " + phkeytable.ToString() + " b where a.snapnum = @partsnap and a.phkey = b.phkey"; SqlParameter partsnapParam2 = new SqlParameter("@partsnap", System.Data.SqlDbType.SmallInt); partsnapParam2.Value = partsnap; SqlCommand getActualDataCommand = new SqlCommand(getActualDataString, connection); getActualDataCommand.Parameters.Add(partsnapParam2); SqlDataReader dataReader = getActualDataCommand.ExecuteReader(); while (dataReader.Read()) { long[] ids = (new SqlBigIntArrayMax(new SqlBytes((byte[])dataReader["id"])).ToArray()); SqlRealArrayMax positions = new SqlRealArrayMax(new SqlBytes((byte[])dataReader["pos"])); int phkey = Int32.Parse(dataReader["phkey"].ToString()); //int[][] currentSlots = {(phkeySlotMap[phkey].ToArray())}; //ids.GetItems(currentSlots); //int[] lengths = { ids.Length, 3 }; //positions.Reshape(lengths); //int[] lengths[ float[] posArray = positions.ToArray(); foreach (short slot in phkeySlotMap[phkey]) { //PosData currentPosComps = new PosData(ids[slot], posArray[slot, 0], posArray[slot, 1], posArray[slot, 2]); PosData currentPosComps = new PosData(ids[slot], posArray[slot * 3 + 0], posArray[slot * 3 + 1], posArray[slot * 3 + 2]); idPositionMap.Add(currentPosComps); } } dataReader.Close(); } return idPositionMap; }
public static IEnumerable LookupDataAllTimesteps(SqlString phkeytable) { //string temp_partid_table = "#temp_partid_list"; List<PosDataWithSnap> idPositionMap = new List<PosDataWithSnap>(); using (SqlConnection connection = new SqlConnection("context connection=true")) { connection.Open(); // Phkey -> (List of slots in that phkey) List<Dictionary<int, List<int>>> phkeySlotMap = new List<Dictionary<int, List<int>>>(); // initialize dictionaries for all timesteps for (int i = 0; i < 64; ++i) { phkeySlotMap.Add(new Dictionary<int, List<int>>()); } string rebuildPhkeySlotMapString = "select snap, phkey, slot from " + phkeytable.ToString(); SqlCommand rebuildPhkeySlotMapCommand = new SqlCommand(rebuildPhkeySlotMapString, connection); SqlDataReader mapReader = rebuildPhkeySlotMapCommand.ExecuteReader(); while (mapReader.Read()) { int snap = Int32.Parse(mapReader["snap"].ToString()); int phkey = Int32.Parse(mapReader["phkey"].ToString()); short slot = Int16.Parse(mapReader["slot"].ToString()); List<int> slotList = null; if (!phkeySlotMap[snap].TryGetValue(phkey, out slotList)) { slotList = new List<int>(); } slotList.Add(slot); phkeySlotMap[snap][phkey] = slotList; } mapReader.Close(); // TODO: the faster way to do this might be to do each timestep separately, I think we will stay in memory that way??? // I should figure out what the bottleneck here is though #region one_snap_at_a_time /*string getActualDataStringRaw = "select distinct a.phkey, a.id, a.pos from SimulationDB.dbo.snaparr a, " + phkeytable.ToString() + " b where a.snapnum = b.snap and a.phkey = b.phkey and a.snapnum = {0}"; for (short currentSnap = 0; currentSnap < 64; ++currentSnap) { string getActualDataString = string.Format(getActualDataStringRaw, currentSnap); SqlCommand getActualDataCommand = new SqlCommand(getActualDataString, connection); SqlDataReader dataReader = getActualDataCommand.ExecuteReader(); while (dataReader.Read()) { long[] ids = (new SqlBigIntArrayMax(new SqlBytes((byte[])dataReader["id"])).ToArray()); SqlRealArrayMax positions = new SqlRealArrayMax(new SqlBytes((byte[])dataReader["pos"])); int phkey = Int32.Parse(dataReader["phkey"].ToString()); float[] posArray = positions.ToArray(); foreach (short slot in phkeySlotMap[currentSnap][phkey]) { //PosData currentPosComps = new PosData(ids[slot], posArray[slot, 0], posArray[slot, 1], posArray[slot, 2]); PosDataWithSnap currentPosComps = new PosDataWithSnap(currentSnap, ids[slot], posArray[slot * 3 + 0], posArray[slot * 3 + 1], posArray[slot * 3 + 2]); idPositionMap.Add(currentPosComps); } } dataReader.Close(); }*/ #endregion one_snap_at_a_time #region all_at_once string getActualDataString = "select distinct a.snapnum, a.phkey, a.id, a.pos from SimulationDB.dbo.snaparr a, " + phkeytable.ToString() + " b where a.snapnum = b.snap and a.phkey = b.phkey order by a.snapnum"; SqlCommand getActualDataCommand = new SqlCommand(getActualDataString, connection); SqlDataReader dataReader = getActualDataCommand.ExecuteReader(); while (dataReader.Read()) { short snap = Int16.Parse(dataReader["snapnum"].ToString()); long[] ids = (new SqlBigIntArrayMax(new SqlBytes((byte[])dataReader["id"])).ToArray()); SqlRealArrayMax positions = new SqlRealArrayMax(new SqlBytes((byte[])dataReader["pos"])); int phkey = Int32.Parse(dataReader["phkey"].ToString()); float[] posArray = positions.ToArray(); foreach (short slot in phkeySlotMap[snap][phkey]) { //PosData currentPosComps = new PosData(ids[slot], posArray[slot, 0], posArray[slot, 1], posArray[slot, 2]); PosDataWithSnap currentPosComps = new PosDataWithSnap(snap, ids[slot], posArray[slot * 3 + 0], posArray[slot * 3 + 1], posArray[slot * 3 + 2]); idPositionMap.Add(currentPosComps); } } dataReader.Close(); #endregion all_at_once } return idPositionMap; }