protected override void LoadChildren() { TypeVersion parentObj = (TypeVersion)m_session.Open(m_parentId, false, null, false, 0, Int32.MaxValue); if (parentObj != null) { VelocityDbType vdbType; Type t = m_member.FieldType; while (t.IsArray) { t = t.GetElementType(); } TypeCode tCode = t.GetTypeCode(); bool isValueType = t.IsValueType; bool isNullableElement = t.IsGenericType && t.GetGenericTypeDefinition() == CommonTypes.s_typeOfNullable; t = isNullableElement ? t.GetGenericArguments()[0] : t.IsEnum ? Enum.GetUnderlyingType(t) : t; if (t.IsArray) { t = t.GetElementType(); } if (t.IsGenericType) { Type genericTypeDef = t.GetGenericTypeDefinition(); Type key = t.GetGenericArguments()[0]; if (genericTypeDef == CommonTypes.s_typeOfList) { t = key; } } var lookupByType = m_session.OpenSchema(false).LookupByType; if (t != null && lookupByType.TryGetValue(t, out vdbType)) { foreach (VelocityDbType vType in m_session.OpenSchema(false).TypesByName) { if (vType != null && vType.Type != null) { var type = vType.Type; if (type.IsSubclassOf(vdbType.Type) || type == t) { for (int i = 0; i < vType.TypeVersions.Length; i++) { base.Children.Add(new TypeVersionViewModel(vType.TypeVersions[i], this, _schemasViewModel)); } } } else { Trace.WriteLine("vType or vType.type is unexpectedly null for VelocityDbType: " + vType); } } } else { base.Children.Add(new NotInSchemaViewModel("Type not registered in database schema", this, m_session)); } } }
/// <summary> /// Persists this object. /// </summary> /// <param name="place">The placement rules to follow when persisting this object</param> /// <param name="session">The session managing this object</param> /// <param name="persistRefs">If true, objects referenced from this object will also be persisted</param> /// <param name="disableFlush">If true, disables possible flushing of updated pages while persisting this object; otherwise pasge flushing may occur</param> /// <returns>The object id of the persistent object</returns> public ulong Persist(SessionBase session, IOptimizedPersistable placeHint, bool persistRefs = true, bool disableFlush = false) { Placement place = new Placement(session, placeHint, this, persistRefs, UInt32.MaxValue, placeHint.FlushIfPageFull); return(session.Persist(place, this, session.OpenSchema(false), UInt16.MaxValue - 1, disableFlush)); }
protected override void LoadChildren() { if (!_session.InTransaction) { _session.BeginRead(); } var types = _session.OpenSchema(false).TypesByName.ToList(); using (System.Windows.Application.Current.Dispatcher.DisableProcessing()) { foreach (var type in types) { if (_internalTypes == _session.OpenSchema(false).IsExpandedInternalType(type.SlotNumber)) { base.Children.Add(new TypeViewModel(this, type)); } } } }
/// <summary> /// Object details as a string /// </summary> /// <param name="pObj">The object extended</param> /// <param name="session">The session managing this object</param> /// <param name="skipArrays">Indicates if string should contain detailed array data.</param> ///<returns><see cref="string"/> containing all details of this object.</returns> static public string ToStringDetails(this OptimizedPersistable pObj, SessionBase session, bool skipArrays = true) { Schema schema = session.OpenSchema(false); if (pObj.WrappedObject == null) { return(pObj.ToString() + pObj.ToStringDetails(schema, pObj.Shape, skipArrays)); } else { Array array = pObj.WrappedObject as Array; if (array != null) { return(pObj.WrappedObject.ToString() + " (" + array.Length + ") " + Oid.AsString(pObj.Id) + pObj.ToStringDetails(schema, pObj.Shape, skipArrays)); } else { return(pObj.WrappedObject.ToString() + " " + Oid.AsString(pObj.Id) + pObj.ToStringDetails(schema, pObj.Shape, skipArrays)); } } }
/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } var schemaInternalTypeFiles = new[] { "VelocityDb.TypeInfo.Schema65747.csv", "VelocityDb.TypeInfo.TypeVersion65749.csv", "VelocityDb.TypeInfo.VelocityDbType65753.csv", "VelocityDb.TypeInfo.DataMember65745.csv", "VelocityDb.Collection.BTree.BTreeSetOidShortVelocityDb.TypeInfo.VelocityDbType65623.csv", "VelocityDb.Collection.Comparer.VelocityDbTypeComparer65655.csv" }; var schemaInternalTypeFileInfo = new List <FileInfo>(); foreach (var fileName in schemaInternalTypeFiles) { schemaInternalTypeFileInfo.Add(di.GetFiles(fileName, SearchOption.TopDirectoryOnly).First()); } foreach (FileInfo info in schemaInternalTypeFileInfo) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } Database schemaDb = session.OpenDatabase(Schema.SchemaDB); Page schemaPage = schemaDb.CachedPage(1); schemaPage.FinishUpCsvImport(); var schemaTypes = new UInt32[] { 65747, 65749, 65753, 65745, 65623, 65655 }; for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if (((i == 0 && slotNumber < Schema.s_bootupTypeCountExpanded) || (i == 1 && slotNumber >= Schema.s_bootupTypeCountExpanded)) && !schemaTypes.Contains(typeShortId)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { var csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }
/// <summary> /// Persists this object. /// </summary> /// <param name="place">The placement rules to follow when persisting this object</param> /// <param name="session">The session managing this object</param> /// <param name="persistRefs">If true, objects referenced from this object will also be persisted</param> /// <param name="disableFlush">If true, disables possible flushing of updated pages while persisting this object; otherwise pasge flushing may occur</param> /// <returns>The object id of the persistent object</returns> public virtual UInt64 Persist(Placement place, SessionBase session, bool persistRefs = true, bool disableFlush = false, Queue <IOptimizedPersistable> toPersist = null) { return(session.Persist(place, this, session.OpenSchema(false), place.MaxObjectsPerPage, disableFlush, toPersist)); }
/// <summary> /// Persists this object. /// </summary> /// <param name="placeHint">Use placement as specified by this object type, see <see cref="OptimizedPersistable.PlacementDatabaseNumber"/>, <see cref="OptimizedPersistable.ObjectsPerPage()"/> and <see cref="OptimizedPersistable.PagesPerDatabase()"/></param> /// <param name="session">The session managing this object</param> /// <param name="persistRefs">Persist any referenced object now or delay until flush/commit</param> /// <param name="disableFlush">Controlls possible flushing of updated pages. Set to true if you want to prevent updated pages from being flushed to disk and setting such pages to a non updated state.</param> /// <returns>The object id of the persistent object</returns> public virtual UInt64 Persist(SessionBase session, IOptimizedPersistable placeHint, bool persistRefs = true, bool disableFlush = false) { Placement place = new Placement(session, placeHint, this, persistRefs, UInt32.MaxValue, placeHint.FlushIfPageFull); return session.Persist(place, this, session.OpenSchema(false), UInt16.MaxValue - 1, disableFlush); }
/// <summary> /// Persists this object. /// </summary> /// <param name="place">The placement rules to follow when persisting this object</param> /// <param name="session">The session managing this object</param> /// <param name="persistRefs">If true, objects referenced from this object will also be persisted</param> /// <param name="disableFlush">If true, disables possible flushing of updated pages while persisting this object; otherwise pasge flushing may occur</param> /// <returns>The object id of the persistent object</returns> public virtual UInt64 Persist(Placement place, SessionBase session, bool persistRefs = true, bool disableFlush = false, Queue<IOptimizedPersistable> toPersist = null) { return session.Persist(place, this, session.OpenSchema(false), place.MaxObjectsPerPage, disableFlush, toPersist); }
/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.CachedPage(pageNum); if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; session.UpdatePage(ref page); } pageInfoString = textReader.ReadLine(); } } for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if ((i == 0 && slotNumber < Schema.s_bootupTypeCount) || (i == 1 && slotNumber >= Schema.s_bootupTypeCount)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectBytesFromStrings(record, fileldNames, session, schema); } } } } } } } }
/// <summary> /// This is a support function for the VelocityDbBrowser. It converts a field into a string. /// </summary> /// <param name="member">A field in an object</param> /// <param name="obj">The object containing the field</param> /// <param name="page">The page of the object</param> /// <param name="skipArrays">Option to skip arrays of the object</param> /// <returns>A <see cref="string"/> containing all details of this field.</returns> public static string ToStringDetails(DataMember member, object obj, IOptimizedPersistable pObj, Page page, bool skipArrays) { SessionBase session = pObj.Page.Database.Session; IOptimizedPersistable placeHolder; Schema schema = session.OpenSchema(false); FieldInfo field = member.Field; object o = member.GetMemberValue(obj); if (member.IsGuid) { Guid guid = (Guid)o; return(guid.ToString()); } StringBuilder sb = new StringBuilder(100); if (o == null) { sb.Append(" " + member.FieldName + " : " + "null"); } else { bool foundIt = session.GlobalObjWrapperGet(o, out placeHolder); if (foundIt) { sb.Append(" " + member.FieldName + " : " + placeHolder.WrappedObject.ToString() + " " + Oid.AsString(placeHolder.Id)); } else { Array array = o as Array; if (array != null) { Type elementType = member.FieldType.GetElementType(); sb.Append(" " + member.FieldName + " " + field.FieldType.ToGenericTypeString() + " size: " + array.Length.ToString()); if (!skipArrays) { sb.Append(ArrayToString(array, false, page, elementType)); } } else { IList list = o as IList; if (list != null) { int i = 0; string listObjStr = " " + member.FieldName + " " + o.GetType().ToGenericTypeString() + " size: " + list.Count.ToString(); sb.Append(listObjStr); if (!skipArrays) { foreach (object listObj in list) { sb.AppendLine(); pObj = listObj as OptimizedPersistable; if (listObj != null && pObj != null) { sb.Append("\t[" + i.ToString() + "]\t" + Oid.AsString(pObj.Id)); } else { foundIt = session.GlobalObjWrapperGet(listObj, out placeHolder); if (foundIt) { sb.Append("\t[" + i.ToString() + "]\t" + Oid.AsString(placeHolder.Id)); } else { sb.Append("\t[" + i.ToString() + "]\t" + listObj.ToString()); } } i++; } } } else { VelocityDbType t = null; if (field.FieldType == CommonTypes.s_typeOfType) { Type fieldType = o as Type; sb.Append(" " + field.Name + " : " + fieldType.ToGenericTypeString()); } else { bool cond1 = field.FieldType.GetTypeInfo().IsPrimitive || member.HasId || field.FieldType == CommonTypes.s_typeOfString || field.FieldType.GetTypeInfo().IsEnum; if (cond1 || schema.LookupByType.TryGetValue(field.FieldType, out t) == false || (field.FieldType.GetTypeInfo().IsGenericType&& field.FieldType.GetGenericTypeDefinition() == CommonTypes.s_typeOfWeakIOptimizedPersistableReference)) { sb.Append(" " + field.Name + " : " + o.ToString()); } else { TypeVersion memberShape = t.LastShape(); bool isNullable = memberShape.Type.GetTypeInfo().IsGenericType&& memberShape.Type.GetGenericTypeDefinition() == CommonTypes.s_typeOfNullable; if (isNullable) { Type elementType = memberShape.Type.GetTypeInfo().GetGenericArguments()[0]; schema.LookupByType.TryGetValue(elementType, out t); memberShape = t.LastShape(); } sb.Append(" " + field.Name + " : " + ToStringDetails(o, schema, page, memberShape, skipArrays)); } } } } } } return(sb.ToString()); }
/// <summary> /// Object details as a string /// </summary> /// <param name="session">The session managing this object</param> /// <param name="skipArrays">Indicates if string should contain detailed array data.</param> ///<returns> /// A <see cref="string"/> containing all details of this object. ///</returns> static public string ToStringDetails(this OptimizedPersistable pObj, SessionBase session, bool skipArrays = true) { Schema schema = session.OpenSchema(false); if (pObj.WrappedObject == null) return pObj.ToString() + pObj.ToStringDetails(schema, pObj.Shape, skipArrays); else { Array array = pObj.WrappedObject as Array; if (array != null) return pObj.WrappedObject.ToString() + " (" + array.Length + ") " + Oid.AsString(pObj.Id) + pObj.ToStringDetails(schema, pObj.Shape, skipArrays); else return pObj.WrappedObject.ToString() + " " + Oid.AsString(pObj.Id) + pObj.ToStringDetails(schema, pObj.Shape, skipArrays); } }