public void TestCompareCollection6() { const string dbName = "introspectortest6.odb"; DeleteBase(dbName); var odb = OdbFactory.Open(dbName); var function = new VO.Login.Function("login"); var profile = new Profile("operator", function); var user = new User("olivier smadja", "*****@*****.**", profile); IObjectInfoComparator comparator = new ObjectInfoComparator(); var ci = ClassIntrospector.Introspect(user.GetType(), true).GetMainClassInfo(); var storageEngine = ((global::NDatabase.Odb)odb).GetStorageEngine(); var instanceInfo = (NonNativeObjectInfo) new ObjectIntrospector(storageEngine.GetClassInfoProvider()).GetMetaRepresentation(user, true, null, new InstrumentationCallbackForStore(null, false)); // Sets attributes offsets - this is normally done by reading then from // disk, but in this junit, // we must set them manually var offsets = new[] { 1L, 2L, 3L }; var ids = new[] { 1, 2, 3 }; instanceInfo.GetHeader().SetAttributesIdentification(offsets); instanceInfo.GetHeader().SetAttributesIds(ids); instanceInfo.GetHeader().SetOid(OIDFactory.BuildObjectOID(1)); var nnoiProfile = (NonNativeObjectInfo)instanceInfo.GetAttributeValueFromId(3); nnoiProfile.SetOid(OIDFactory.BuildObjectOID(2)); var nnoiFunctions = (NonNativeObjectInfo)nnoiProfile.GetAttributeValueFromId(1); nnoiFunctions.SetOid(OIDFactory.BuildObjectOID(3)); var nnoi = (NonNativeObjectInfo)instanceInfo.GetAttributeValueFromId(ci.GetAttributeId("profile")); nnoi.GetHeader().SetAttributesIdentification(offsets); nnoi.GetHeader().SetAttributesIds(ids); profile.SetName("ope"); var instanceInfo3 = (NonNativeObjectInfo) new ObjectIntrospector(storageEngine.GetClassInfoProvider()).GetMetaRepresentation(user, true, null, new InstrumentationCallbackForStore(null, false)); instanceInfo3.GetHeader().SetOid(OIDFactory.BuildObjectOID(1)); nnoiProfile = (NonNativeObjectInfo)instanceInfo3.GetAttributeValueFromId(3); nnoiProfile.SetOid(OIDFactory.BuildObjectOID(2)); nnoiFunctions = (NonNativeObjectInfo)nnoiProfile.GetAttributeValueFromId(1); nnoiFunctions.SetOid(OIDFactory.BuildObjectOID(3)); AssertTrue(comparator.HasChanged(instanceInfo, instanceInfo3)); AssertEquals(1, comparator.GetNbChanges()); odb.Close(); }
public void TestInstanceInfo() { const string dbName = "TestInstanceInfo.odb"; DeleteBase(dbName); var odb = OdbFactory.Open(dbName); var user = new User("olivier smadja", "*****@*****.**", new Profile("operator", new VO.Login.Function("login"))); var ci = ClassIntrospector.Introspect(user.GetType(), true).GetMainClassInfo(); var storageEngine = ((global::NDatabase.Odb)odb).GetStorageEngine(); var instanceInfo = (NonNativeObjectInfo) new ObjectIntrospector(storageEngine.GetClassInfoProvider()).GetMetaRepresentation(user, true, null, new InstrumentationCallbackForStore(null, false)); AssertEquals(OdbClassNameResolver.GetFullName(user.GetType()), instanceInfo.GetClassInfo().FullClassName); AssertEquals("olivier smadja", instanceInfo.GetAttributeValueFromId(ci.GetAttributeId("name")).ToString()); AssertEquals(typeof(AtomicNativeObjectInfo), instanceInfo.GetAttributeValueFromId(ci.GetAttributeId("name")).GetType()); odb.Close(); }
/// <summary> /// The database file name /// </summary> public StorageEngine(IDbIdentification parameters) { _reflectionService = DependencyContainer.Resolve <IReflectionService>(); try { var metaModelCompabilityChecker = DependencyContainer.Resolve <IMetaModelCompabilityChecker>(); DbIdentification = parameters; IsDbClosed = false; var isNewDatabase = DbIdentification.IsNew(); _session = DependencyContainer.Resolve <ISession>(this); // Object Writer must be created before object Reader _objectWriter = DependencyContainer.Resolve <IObjectWriter>(this); ObjectReader = DependencyContainer.Resolve <IObjectReader>(this); // Associate current session to the fsi -> all transaction writes // will be applied to this FileSystemInterface _session.SetFileSystemInterfaceToApplyTransaction(_objectWriter.FileSystemProcessor.FileSystemInterface); _objectIntrospectionDataProvider = new SessionDataProvider(_session); if (isNewDatabase) { _objectWriter.CreateEmptyDatabaseHeader(OdbTime.GetCurrentTimeInTicks()); } else { GetObjectReader().ReadDatabaseHeader(); } _objectWriter.AfterInit(); _objectIntrospector = new ObjectIntrospector(GetClassInfoProvider()); _triggerManager = GetLocalTriggerManager(); // This forces the initialization of the meta model var metaModel = GetMetaModel(); var shouldUpdate = metaModelCompabilityChecker.Check(ClassIntrospector.Instrospect(metaModel.GetAllClasses()), GetMetaModel()); if (shouldUpdate) { UpdateMetaModel(); } _objectWriter.SetTriggerManager(_triggerManager); _introspectionCallbackForInsert = new InstrumentationCallbackForStore(_triggerManager, false); _introspectionCallbackForUpdate = new InstrumentationCallbackForStore(_triggerManager, true); } catch { if (parameters is FileIdentification) { Monitor.Exit(string.Intern(Path.GetFullPath(parameters.FileName))); } throw; } }
public void TestGetAllFields() { var allFields = ClassIntrospector.GetAllFieldsFrom(typeof(FootballPlayer)); AssertEquals(3, allFields.Count); AssertEquals("groundName", (allFields[0]).Name); AssertEquals("name", (allFields[1]).Name); AssertEquals("role", (allFields[2]).Name); }
public virtual void TestNonNativeAttributes() { var tc = new TestClass(); var classInfo = ClassIntrospector.Introspect(tc.GetType(), true). GetMainClassInfo(); AssertEquals(0, classInfo.GetAllNonNativeAttributes().Count); }
public void TestClassInfo() { var user = new User("olivier smadja", "*****@*****.**", new Profile("operator", new VO.Login.Function("login"))); var classInfoList = ClassIntrospector.Introspect(user.GetType(), true); AssertEquals(OdbClassNameResolver.GetFullName(user.GetType()), classInfoList.GetMainClassInfo().FullClassName); AssertEquals(3, classInfoList.GetMainClassInfo().Attributes.Count); AssertEquals(3, classInfoList.GetClassInfos().Count); }
public void It_should_allow_on_getting_all_fields_based_on_class_name() { var countryFields = ClassIntrospector.GetAllFieldsFrom(typeof(EuCountry)); Assert.That(countryFields, Has.Count.EqualTo(4)); var fieldNames = countryFields.Select(field => field.Name); Assert.That(fieldNames, Contains.Item("_name")); Assert.That(fieldNames, Contains.Item("_currency")); Assert.That(fieldNames, Contains.Item("<Population>k__BackingField")); Assert.That(fieldNames, Contains.Item("<Continent>k__BackingField")); }
public void It_should_introspect_recursively_given_type() { var classInfoList = ClassIntrospector.Introspect(typeof(EuCountry), true); Assert.That(classInfoList.GetClassInfos(), Has.Count.EqualTo(2)); Assert.That(classInfoList.GetMainClassInfo().FullClassName, Is.StringStarting( "NDatabase.UnitTests.Layer1.When_we_use_class_introspector+EuCountry")); Assert.That(classInfoList.GetClassInfos().Skip(1).First().FullClassName, Is.StringStarting( "NDatabase.UnitTests.Layer1.When_we_use_class_introspector+Country+CountryName")); }
protected override void Establish_context() { _type = typeof(TypeToRefactor); _metaModelMock = new Mock <IMetaModel>(); _classInfo = ClassIntrospector.Introspect(typeof(TypeToRefactor), true).GetMainClassInfo(); var fullClassName = OdbClassNameResolver.GetFullName(_type); _metaModelMock.Setup(x => x.GetClassInfo(fullClassName, true)).Returns(_classInfo).Verifiable(); _objectWriterMock = new Mock <IObjectWriter>(); _objectWriterMock.Setup(x => x.UpdateClassInfo(_classInfo, true)).Verifiable(); }
protected override void Establish_context() { _type = typeof(TypeToRefactor); _fieldName = "NewField"; _metaModelMock = new Mock <IMetaModel>(); _classInfo = ClassIntrospector.Introspect(typeof(TypeToRefactor), true).GetMainClassInfo(); _metaModelMock.Setup(x => x.GetClassInfo(_type, true)).Returns(_classInfo).Verifiable(); _objectWriterMock = new Mock <IObjectWriter>(); _objectWriterMock.Setup(x => x.UpdateClassInfo(_classInfo, true)).Verifiable(); }
public IIndexManager IndexManagerFor <T>() where T : class { var clazz = typeof(T); var classInfo = _storageEngine.GetSession().GetMetaModel().GetClassInfo(clazz, false); if (classInfo == null) { var classInfoList = ClassIntrospector.Introspect(clazz, true); _storageEngine.GetObjectWriter().AddClasses(classInfoList); classInfo = classInfoList.GetMainClassInfo(); } return(new IndexManager(_storageEngine, classInfo)); }
protected override void Establish_context() { _object = new Employee { Name = "Object" }; _oid = new ObjectOID(1234L); var classInfoList = ClassIntrospector.Introspect(typeof(Employee), true); var mainClassInfo = classInfoList.GetMainClassInfo(); mainClassInfo.ClassInfoId = new ClassOID(12345L); _objectInfo = new NonNativeObjectInfo(_object, mainClassInfo); _objectInfo.SetOid(_oid); }
public ClassInfo GetClassInfo(Type type) { var odbType = OdbType.GetFromClass(type); if (odbType.IsNative() && !odbType.IsEnum()) { return(null); } var metaModel = _session.GetMetaModel(); if (metaModel.ExistClass(type)) { return(metaModel.GetClassInfo(type, true)); } var classInfoList = ClassIntrospector.Introspect(type, true); _session.GetObjectWriter().AddClasses(classInfoList); return(classInfoList.GetMainClassInfo()); }
public object BuildOneInstance(NonNativeObjectInfo objectInfo, IOdbCache cache) { // verify if the object is check to delete if (objectInfo.IsDeletedObject()) { throw new OdbRuntimeException( NDatabaseError.ObjectIsMarkedAsDeletedForOid.AddParameter(objectInfo.GetOid())); } // Then check if object is in cache var o = cache.GetObject(objectInfo.GetOid()); if (o != null) { return(o); } try { o = FormatterServices.GetUninitializedObject(objectInfo.GetClassInfo().UnderlyingType); } catch (Exception e) { throw new OdbRuntimeException( NDatabaseError.InstanciationError.AddParameter(objectInfo.GetClassInfo().FullClassName), e); } // This can happen if ODB can not create the instance from security reasons if (o == null) { throw new OdbRuntimeException( NDatabaseError.InstanciationError.AddParameter(objectInfo.GetClassInfo().FullClassName)); } // Keep the initial hash code. In some cases, when the class redefines // the hash code method // Hash code can return wrong values when attributes are not set (when // hash code depends on attribute values) // Hash codes are used as the key of the map, // So at the end of this method, if hash codes are different, object // will be removed from the cache and inserted back var hashCodeIsOk = true; var initialHashCode = 0; try { initialHashCode = o.GetHashCode(); } catch { hashCodeIsOk = false; } // Adds this incomplete instance in the cache to manage cyclic reference if (hashCodeIsOk) { cache.AddObject(objectInfo.GetOid(), o, objectInfo.GetHeader()); } var classInfo = objectInfo.GetClassInfo(); var fields = ClassIntrospector.GetAllFieldsFrom(classInfo.UnderlyingType); object value = null; foreach (var fieldInfo in fields) { // Gets the id of this field var attributeId = classInfo.GetAttributeId(fieldInfo.Name); if (OdbConfiguration.IsLoggingEnabled()) { DLogger.Debug(string.Concat("InstanceBuilder: ", "getting field with name ", fieldInfo.Name, ", attribute id is ", attributeId.ToString())); } var abstractObjectInfo = objectInfo.GetAttributeValueFromId(attributeId); // Check consistency // ensureClassCompatibily(field, // instanceInfo.getClassInfo().getAttributeinfo(i).getFullClassname()); if (abstractObjectInfo == null || (abstractObjectInfo.IsNull())) { continue; } if (abstractObjectInfo.IsNative()) { if (abstractObjectInfo.IsAtomicNativeObject()) { value = abstractObjectInfo.IsNull() ? null : abstractObjectInfo.GetObject(); } if (abstractObjectInfo.IsArrayObject()) { value = BuildArrayInstance((ArrayObjectInfo)abstractObjectInfo); } if (abstractObjectInfo.IsEnumObject()) { value = BuildEnumInstance((EnumNativeObjectInfo)abstractObjectInfo, fieldInfo.FieldType); } } else { if (abstractObjectInfo.IsNonNativeObject()) { if (abstractObjectInfo.IsDeletedObject()) { if (OdbConfiguration.IsLoggingEnabled()) { var warning = NDatabaseError.AttributeReferencesADeletedObject.AddParameter( objectInfo.GetClassInfo().FullClassName).AddParameter( objectInfo.GetOid()).AddParameter(fieldInfo.Name); DLogger.Warning("InstanceBuilder: " + warning); } value = null; } else { value = BuildOneInstance((NonNativeObjectInfo)abstractObjectInfo); } } } if (value == null) { continue; } if (OdbConfiguration.IsLoggingEnabled()) { DLogger.Debug(String.Format("InstanceBuilder: Setting field {0}({1}) to {2} / {3}", fieldInfo.Name, fieldInfo.GetType().FullName, value, value.GetType().FullName)); } try { fieldInfo.SetValue(o, value); } catch (Exception e) { throw new OdbRuntimeException( NDatabaseError.InstanceBuilderWrongObjectContainerType.AddParameter( objectInfo.GetClassInfo().FullClassName).AddParameter(value.GetType().FullName) .AddParameter(fieldInfo.GetType().FullName), e); } } if (o.GetType() != objectInfo.GetClassInfo().UnderlyingType) { throw new OdbRuntimeException( NDatabaseError.InstanceBuilderWrongObjectType.AddParameter( objectInfo.GetClassInfo().FullClassName).AddParameter(o.GetType().FullName)); } if (hashCodeIsOk || initialHashCode != o.GetHashCode()) { // Bug (sf bug id=1875544 )detected by glsender // This can happen when an object has redefined its own hashcode // method and depends on the field values // Then, we have to remove object from the cache and re-insert to // correct map hash code cache.RemoveObjectByOid(objectInfo.GetOid()); // re-Adds instance in the cache cache.AddObject(objectInfo.GetOid(), o, objectInfo.GetHeader()); } if (_triggerManager != null) { _triggerManager.ManageSelectTriggerAfter(objectInfo.GetClassInfo().UnderlyingType, o, objectInfo.GetOid()); } return(o); }