/// <summary> /// Create a RealDataCache object, and laod it with metadata and real data. /// </summary> /// <param name="metadataPathname"></param> /// <param name="realDataPathname"></param> /// <param name="objects"></param> /// <returns></returns> public RealDataCache LoadCache(string metadataPathname, string realDataPathname, Dictionary<int, uint> objects) { CheckDisposed(); m_realDataCache = new RealDataCache(); m_realDataCache.CheckWithMDC = false; try { #if DEBUG //Process objectBrowser = Process.GetCurrentProcess(); //long memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (start load): {0}.", memory.ToString())); DateTime start = DateTime.Now; #endif m_metaDataCache = new MetaDataCache(); m_metaDataCache.InitXml(metadataPathname, true); m_realDataCache.MetaDataCache = m_metaDataCache; #if DEBUG DateTime end = DateTime.Now; TimeSpan span = new TimeSpan(end.Ticks - start.Ticks); string totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load MDC: " + totalTime); start = end; //memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (loaded MDC load): {0}.", memory.ToString())); #endif XmlDocument doc = new XmlDocument(); doc.Load(realDataPathname); #if DEBUG end = DateTime.Now; span = new TimeSpan(end.Ticks - start.Ticks); totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load XML: " + totalTime); start = end; //memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (loaded data XML): {0}.", memory.ToString())); #endif // Load Writing Systems first. int ord = 0; int hvo; uint clid = 0; { XmlNodeList wsNodes = doc.DocumentElement.SelectNodes("LgWritingSystem"); uint flid = m_metaDataCache.GetFieldId("LgWritingSystem", "ICULocale", false); // We need a full list of ints and strings for Wses, // before we can load string data, // so cache the barebones first. foreach (XmlNode wsNode in wsNodes) { hvo = BootstrapWs(wsNode, flid, out clid, objects); } foreach (XmlNode wsNode in wsNodes) { string uid = wsNode.Attributes["id"].Value.Substring(1); hvo = m_realDataCache.get_ObjFromGuid(new Guid(uid)); LoadObject(wsNode, hvo, clid, objects); } } // Now load other ownerless objects, except LangProject and Wses. foreach (XmlNode otherOwnerlessNode in doc.DocumentElement.ChildNodes) { if (otherOwnerlessNode.Name != "LangProject" && otherOwnerlessNode.Name != "LgWritingSystem") { hvo = LoadCmObjectProperties(otherOwnerlessNode, 0, 0, ord, out clid, objects); LoadObject(otherOwnerlessNode, hvo, clid, objects); } } // Now load LangProject XmlNode langProjectNode = doc.DocumentElement.SelectSingleNode("LangProject"); hvo = LoadCmObjectProperties(langProjectNode, 0, 0, ord, out clid, objects); LoadObject(langProjectNode, hvo, clid, objects); // Set references // Set atomic references foreach (KeyValuePair<HvoFlidKey, XmlNode> kvp in m_delayedAtomicReferences) { string uid = kvp.Value.Attributes["target"].Value.Substring(1); try { int hvoTarget = m_realDataCache.get_ObjFromGuid(new Guid(uid)); m_realDataCache.CacheObjProp(kvp.Key.Hvo, (int)kvp.Key.Flid, hvoTarget); } catch { // Invalid reference. Just clear the cache in case there is a save. m_realDataCache.SetObjProp(kvp.Key.Hvo, (int)kvp.Key.Flid, 0); } } //// Remove all items from m_delayedAtomicReferences that are in handledRefs. //// Theory has it that m_delayedAtomicReferences should then be empty. m_delayedAtomicReferences.Clear(); // Set vector (col or seq) references. foreach (KeyValuePair<HvoFlidKey, List<XmlNode>> kvp in m_delayedVecterReferences) { List<int> hvos = new List<int>(); foreach (XmlNode obj in kvp.Value) { string uid = obj.Attributes["target"].Value.Substring(1); try { int ownedHvo = m_realDataCache.get_ObjFromGuid(new Guid(uid)); hvos.Add(ownedHvo); } catch { // Invalid reference. Just remove the bogus hvo. // Since the id is added after the exception, it is effectively 'removed'. Debug.WriteLine("Bogus Id found."); } } m_realDataCache.CacheVecProp(kvp.Key.Hvo, (int)kvp.Key.Flid, hvos.ToArray(), hvos.Count); } m_delayedVecterReferences.Clear(); #if DEBUG end = DateTime.Now; span = new TimeSpan(end.Ticks - start.Ticks); totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load main Cache: " + totalTime); start = end; Debug.WriteLine(String.Format("Number of objects cached: {0}", (m_realDataCache.NextHvo - 1).ToString())); //memory = objectBrowser..PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (cache loaded): {0}.", memory.ToString())); #endif } finally { m_realDataCache.CheckWithMDC = true; } return m_realDataCache; }
/// <summary> /// Load the list (given by owner and sFieldName) from the given TextReader. /// </summary> public void ImportList(ICmObject owner, string sFieldName, TextReader reader, IProgress progress) { m_cache = owner.Cache; m_mdc = m_cache.MetaDataCache; m_wsf = m_cache.WritingSystemFactory; m_progress = progress; NonUndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW(m_cache.ActionHandlerAccessor, () => { int flidList = m_mdc.GetFieldId(owner.ClassName, sFieldName, true); if (flidList == 0) { throw new Exception(String.Format("Invalid list fieldname (programming error): {0}", sFieldName)); } using (var xrdr = XmlReader.Create(reader)) { xrdr.MoveToContent(); if (xrdr.Name != owner.ClassName) { throw new Exception(String.Format("Unexpected outer element: {0}", xrdr.Name)); } if (!xrdr.ReadToDescendant(sFieldName)) { throw new Exception(String.Format("Unexpected second element: {0}", xrdr.Name)); } if (!xrdr.ReadToDescendant("CmPossibilityList")) { throw new Exception(String.Format("Unexpected third element: {0}", xrdr.Name)); } ICmPossibilityList list; int hvo = m_cache.MainCacheAccessor.get_ObjectProp(owner.Hvo, flidList); if (hvo == 0) { hvo = m_cache.MainCacheAccessor.MakeNewObject(CmPossibilityListTags.kClassId, owner.Hvo, flidList, -2); } ICmPossibilityListRepository repo = m_cache.ServiceLocator.GetInstance <ICmPossibilityListRepository>(); list = repo.GetObject(hvo); string sItemClassName = "CmPossibility"; xrdr.Read(); while (xrdr.Depth == 3) { xrdr.MoveToContent(); if (xrdr.Depth < 3) { break; } switch (xrdr.Name) { case "Description": SetMultiStringFromXml(xrdr, list.Description); break; case "Name": SetMultiUnicodeFromXml(xrdr, list.Name); break; case "Abbreviation": SetMultiUnicodeFromXml(xrdr, list.Abbreviation); break; case "Depth": list.Depth = ReadIntFromXml(xrdr); break; case "DisplayOption": list.DisplayOption = ReadIntFromXml(xrdr); break; case "HelpFile": list.HelpFile = ReadUnicodeFromXml(xrdr); break; case "IsClosed": list.IsClosed = ReadBoolFromXml(xrdr); break; case "IsSorted": list.IsSorted = ReadBoolFromXml(xrdr); break; case "IsVernacular": list.IsVernacular = ReadBoolFromXml(xrdr); break; case "ItemClsid": list.ItemClsid = ReadIntFromXml(xrdr); sItemClassName = m_mdc.GetClassName(list.ItemClsid); break; case "ListVersion": list.ListVersion = ReadGuidFromXml(xrdr); break; case "PreventChoiceAboveLevel": list.PreventChoiceAboveLevel = ReadIntFromXml(xrdr); break; case "PreventDuplicates": list.PreventDuplicates = ReadBoolFromXml(xrdr); break; case "PreventNodeChoices": list.PreventNodeChoices = ReadBoolFromXml(xrdr); break; case "UseExtendedFields": list.UseExtendedFields = ReadBoolFromXml(xrdr); break; case "WsSelector": list.WsSelector = ReadIntFromXml(xrdr); break; case "Possibilities": LoadPossibilitiesFromXml(xrdr, list, sItemClassName); break; case "HeaderFooterSets": throw new Exception("We don't (yet?) handle HeaderFooterSets for CmPossibilityList (programming issue)"); case "Publications": throw new Exception("We don't (yet?) handle Publications for CmPossibilityList (programming issue)"); default: throw new Exception(String.Format("Unknown field element in CmPossibilityList: {0}", xrdr.Name)); } } xrdr.Close(); if (m_mapRelatedDomains.Count > 0) { SetRelatedDomainsLinks(); } } }); }
/// <summary> /// Load the list (given by owner and sFieldName) from the given TextReader. /// </summary> public void ImportList(ICmObject owner, string sFieldName, TextReader reader, IProgress progress) { m_cache = owner.Cache; m_mdc = m_cache.MetaDataCache; m_wsf = m_cache.WritingSystemFactory; m_progress = progress; NonUndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW(m_cache.ActionHandlerAccessor, () => { int flidList = m_mdc.GetFieldId(owner.ClassName, sFieldName, true); if (flidList == 0) throw new Exception(String.Format("Invalid list fieldname (programming error): {0}", sFieldName)); using (var xrdr = XmlReader.Create(reader)) { xrdr.MoveToContent(); if (xrdr.Name != owner.ClassName) throw new Exception(String.Format("Unexpected outer element: {0}", xrdr.Name)); if (!xrdr.ReadToDescendant(sFieldName)) throw new Exception(String.Format("Unexpected second element: {0}", xrdr.Name)); if (!xrdr.ReadToDescendant("CmPossibilityList")) throw new Exception(String.Format("Unexpected third element: {0}", xrdr.Name)); ICmPossibilityList list; int hvo = m_cache.MainCacheAccessor.get_ObjectProp(owner.Hvo, flidList); if (hvo == 0) hvo = m_cache.MainCacheAccessor.MakeNewObject(CmPossibilityListTags.kClassId, owner.Hvo, flidList, -2); ICmPossibilityListRepository repo = m_cache.ServiceLocator.GetInstance<ICmPossibilityListRepository>(); list = repo.GetObject(hvo); string sItemClassName = "CmPossibility"; xrdr.Read(); while (xrdr.Depth == 3) { xrdr.MoveToContent(); if (xrdr.Depth < 3) break; switch (xrdr.Name) { case "Description": SetMultiStringFromXml(xrdr, list.Description); break; case "Name": SetMultiUnicodeFromXml(xrdr, list.Name); break; case "Abbreviation": SetMultiUnicodeFromXml(xrdr, list.Abbreviation); break; case "Depth": list.Depth = ReadIntFromXml(xrdr); break; case "DisplayOption": list.DisplayOption = ReadIntFromXml(xrdr); break; case "HelpFile": list.HelpFile = ReadUnicodeFromXml(xrdr); break; case "IsClosed": list.IsClosed = ReadBoolFromXml(xrdr); break; case "IsSorted": list.IsSorted = ReadBoolFromXml(xrdr); break; case "IsVernacular": list.IsVernacular = ReadBoolFromXml(xrdr); break; case "ItemClsid": list.ItemClsid = ReadIntFromXml(xrdr); sItemClassName = m_mdc.GetClassName(list.ItemClsid); break; case "ListVersion": list.ListVersion = ReadGuidFromXml(xrdr); break; case "PreventChoiceAboveLevel": list.PreventChoiceAboveLevel = ReadIntFromXml(xrdr); break; case "PreventDuplicates": list.PreventDuplicates = ReadBoolFromXml(xrdr); break; case "PreventNodeChoices": list.PreventNodeChoices = ReadBoolFromXml(xrdr); break; case "UseExtendedFields": list.UseExtendedFields = ReadBoolFromXml(xrdr); break; case "WsSelector": list.WsSelector = ReadIntFromXml(xrdr); break; case "Possibilities": LoadPossibilitiesFromXml(xrdr, list, sItemClassName); break; case "HeaderFooterSets": throw new Exception("We don't (yet?) handle HeaderFooterSets for CmPossibilityList (programming issue)"); case "Publications": throw new Exception("We don't (yet?) handle Publications for CmPossibilityList (programming issue)"); default: throw new Exception(String.Format("Unknown field element in CmPossibilityList: {0}", xrdr.Name)); } } xrdr.Close(); if (m_mapRelatedDomains.Count > 0) SetRelatedDomainsLinks(); } }); }
/// <summary> /// Create a RealDataCache object, and laod it with metadata and real data. /// </summary> /// <param name="metadataPathname"></param> /// <param name="realDataPathname"></param> /// <param name="objects"></param> /// <returns></returns> public RealDataCache LoadCache(string metadataPathname, string realDataPathname, Dictionary <int, uint> objects) { CheckDisposed(); m_realDataCache = new RealDataCache(); m_realDataCache.CheckWithMDC = false; try { #if DEBUG //Process objectBrowser = Process.GetCurrentProcess(); //long memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (start load): {0}.", memory.ToString())); DateTime start = DateTime.Now; #endif m_metaDataCache = new MetaDataCache(); m_metaDataCache.InitXml(metadataPathname, true); m_realDataCache.MetaDataCache = m_metaDataCache; #if DEBUG DateTime end = DateTime.Now; TimeSpan span = new TimeSpan(end.Ticks - start.Ticks); string totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load MDC: " + totalTime); start = end; //memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (loaded MDC load): {0}.", memory.ToString())); #endif XmlDocument doc = new XmlDocument(); doc.Load(realDataPathname); #if DEBUG end = DateTime.Now; span = new TimeSpan(end.Ticks - start.Ticks); totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load XML: " + totalTime); start = end; //memory = objectBrowser.PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (loaded data XML): {0}.", memory.ToString())); #endif // Load Writing Systems first. int ord = 0; int hvo; uint clid = 0; { XmlNodeList wsNodes = doc.DocumentElement.SelectNodes("LgWritingSystem"); uint flid = m_metaDataCache.GetFieldId("LgWritingSystem", "ICULocale", false); // We need a full list of ints and strings for Wses, // before we can load string data, // so cache the barebones first. foreach (XmlNode wsNode in wsNodes) { hvo = BootstrapWs(wsNode, flid, out clid, objects); } foreach (XmlNode wsNode in wsNodes) { string uid = wsNode.Attributes["id"].Value.Substring(1); hvo = m_realDataCache.get_ObjFromGuid(new Guid(uid)); LoadObject(wsNode, hvo, clid, objects); } } // Now load other ownerless objects, except LangProject and Wses. foreach (XmlNode otherOwnerlessNode in doc.DocumentElement.ChildNodes) { if (otherOwnerlessNode.Name != "LangProject" && otherOwnerlessNode.Name != "LgWritingSystem") { hvo = LoadCmObjectProperties(otherOwnerlessNode, 0, 0, ord, out clid, objects); LoadObject(otherOwnerlessNode, hvo, clid, objects); } } // Now load LangProject XmlNode langProjectNode = doc.DocumentElement.SelectSingleNode("LangProject"); hvo = LoadCmObjectProperties(langProjectNode, 0, 0, ord, out clid, objects); LoadObject(langProjectNode, hvo, clid, objects); // Set references // Set atomic references foreach (KeyValuePair <HvoFlidKey, XmlNode> kvp in m_delayedAtomicReferences) { string uid = kvp.Value.Attributes["target"].Value.Substring(1); try { int hvoTarget = m_realDataCache.get_ObjFromGuid(new Guid(uid)); m_realDataCache.CacheObjProp(kvp.Key.Hvo, (int)kvp.Key.Flid, hvoTarget); } catch { // Invalid reference. Just clear the cache in case there is a save. m_realDataCache.SetObjProp(kvp.Key.Hvo, (int)kvp.Key.Flid, 0); } } //// Remove all items from m_delayedAtomicReferences that are in handledRefs. //// Theory has it that m_delayedAtomicReferences should then be empty. m_delayedAtomicReferences.Clear(); // Set vector (col or seq) references. foreach (KeyValuePair <HvoFlidKey, List <XmlNode> > kvp in m_delayedVecterReferences) { List <int> hvos = new List <int>(); foreach (XmlNode obj in kvp.Value) { string uid = obj.Attributes["target"].Value.Substring(1); try { int ownedHvo = m_realDataCache.get_ObjFromGuid(new Guid(uid)); hvos.Add(ownedHvo); } catch { // Invalid reference. Just remove the bogus hvo. // Since the id is added after the exception, it is effectively 'removed'. Debug.WriteLine("Bogus Id found."); } } m_realDataCache.CacheVecProp(kvp.Key.Hvo, (int)kvp.Key.Flid, hvos.ToArray(), hvos.Count); } m_delayedVecterReferences.Clear(); #if DEBUG end = DateTime.Now; span = new TimeSpan(end.Ticks - start.Ticks); totalTime = String.Format("Hours: {0}, Minutes: {1}, Seconds: {2}, Millseconds: {3}", span.Hours.ToString(), span.Minutes.ToString(), span.Seconds.ToString(), span.Milliseconds.ToString()); Debug.WriteLine("Time to load main Cache: " + totalTime); start = end; Debug.WriteLine(String.Format("Number of objects cached: {0}", (m_realDataCache.NextHvo - 1).ToString())); //memory = objectBrowser..PrivateMemorySize64; //Debug.WriteLine(String.Format("Memory used (cache loaded): {0}.", memory.ToString())); #endif } finally { m_realDataCache.CheckWithMDC = true; } return(m_realDataCache); }