/// <summary> /// Adds a property type to the cache. /// </summary> internal void AddPropTypeToCache(int ID, string name, PropDataType propType, PropTypeFlags flags) { if (ID < 0 || ID > 65536) { throw new BadIndexesException("Invalid property type ID " + ID); } lock ( _propTypeCache ) { PropTypeItem propTypeItem = new PropTypeItem(ID, name, propType, flags); while (_propTypeCache.Count < ID) { _propTypeCache.Add(null); } if (_propTypeCache.Count == ID) { _propTypeCache.Add(propTypeItem); } else { _propTypeCache [ID] = propTypeItem; } _propTypeNameCache [name] = propTypeItem; } }
internal PropTypeCollection(MyPalStorage storage, ITable propTypeTable) { _storage = storage; _propTypeTable = propTypeTable; _pseudoProps [0] = new PropTypeItem(ResourceProps.Id, "", PropDataType.Int, PropTypeFlags.Normal); _pseudoProps [1] = new PropTypeItem(ResourceProps.DisplayName, "DisplayName", PropDataType.String, PropTypeFlags.Normal); _pseudoProps [1].SetDisplayNames("Name", null); _pseudoProps [2] = new PropTypeItem(ResourceProps.Type, "Type", PropDataType.String, PropTypeFlags.Normal); }
private PropTypeItem FindPropTypeItem(int propID) { if (propID < 0 || propID >= _propTypeCache.Count) { throw new StorageException("Invalid property type ID " + propID); } PropTypeItem item = (PropTypeItem)_propTypeCache [propID]; if (item == null) { throw new StorageException("Invalid property type ID " + propID); } return(item); }
/** * Returns the display name for a property or link. */ public string GetPropDisplayName(int propId) { bool reverse = false; bool directedLink = false; PropTypeItem ptItem = (PropTypeItem)this [propId]; if (ptItem.DataType == PropDataType.Link && ptItem.HasFlag(PropTypeFlags.DirectedLink)) { directedLink = true; if (propId < 0) { propId = -propId; reverse = true; } } string result; if (reverse) { result = ptItem.ReverseDisplayName; if (result == null) { result = ptItem.Name + " Target"; } } else { result = ptItem.DisplayName; if (result == null) { if (directedLink) { result = ptItem.Name + " Source"; } else { result = ptItem.Name; } } } return(result); }
/** * When a PropType resource is changed, updates the DB data for it. */ internal void UpdatePropType(Resource res) { string propName = res.GetStringProp("Name"); if (propName == null) { MyPalStorage.Storage.OnIndexCorruptionDetected("UpdatePropType: Name property missing on PropType resource"); return; } int propId = this [propName].Id; UpdatePropTypeRecord(propName, res); PropTypeItem propTypeItem = FindPropTypeItem(propId); Debug.Assert(propTypeItem.Name == propName); propTypeItem.SetFlags((PropTypeFlags)res.GetIntProp("Flags")); }
internal void UpdatePropTypeFlags(int propId, PropTypeFlags flags) { PropTypeItem propType = (PropTypeItem)_propTypeCache [propId]; propType.SetFlags(flags); IResource res = _storage.FindUniqueResource("PropType", "ID", propId); if (res != null) { res.SetProp("Flags", (int)flags); UpdatePropTypeRecord(propType.Name, res); } else { MyPalStorage.Storage.OnIndexCorruptionDetected("Could not find PropType resource with ID " + propId); } }
/** * Unregisters the specified property type and deletes all properties * of that type. */ public void Delete(int id) { PropTypeItem item = FindPropTypeItem(id); if (item.DataType == PropDataType.Link) { _storage.DeleteLinksOfType(id); } else { _storage.DeletePropsOfType(id); } ResourceRestrictions.DeletePropRestrictions(id); IResultSet rs = _propTypeTable.CreateModifiableResultSet(0, id); try { SafeRecordEnumerator enumerator = new SafeRecordEnumerator(rs, "PropTypes.Delete"); using ( enumerator ) { if (!enumerator.MoveNext()) { MyPalStorage.Storage.OnIndexCorruptionDetected("PropTypeCollection.Delete: Attempt to delete non-existing property type " + id); } else { IRecord rec = enumerator.Current; _storage.SafeDeleteRecord(rec, "PropTypes.Delete"); } } } finally { rs.Dispose(); } IResource propTypeRes = _storage.FindUniqueResource("PropType", "ID", id); Debug.Assert(propTypeRes != null); propTypeRes.Delete(); }
public IPropType this [int propID] { get { if (propID < 0) { PropTypeItem revItem = FindPropTypeItem(-propID); if (revItem.HasFlag(PropTypeFlags.DirectedLink)) { return(revItem); } throw new StorageException("Invalid property type ID " + propID); } if (propID >= ResourceProps.Id) { return(_pseudoProps [propID - ResourceProps.Id]); } return(FindPropTypeItem(propID)); } }
internal void RegisterPropDisplayNameInternal(int propID, string fromDisplayName, string toDisplayName) { PropTypeItem propTypeItem = (PropTypeItem)_propTypeCache [propID]; IResource propTypeRes = _storage.FindUniqueResource("PropType", "ID", propID); if (propTypeRes == null) { MyPalStorage.Storage.OnIndexCorruptionDetected("Property type not found in RegisterPropDisplayNameInternal"); return; } propTypeRes.SetProp("PropDisplayName", fromDisplayName); if (toDisplayName == null) { propTypeRes.DeleteProp("ReverseDisplayName"); } else { propTypeRes.SetProp("ReverseDisplayName", toDisplayName); } propTypeItem.SetDisplayNames(fromDisplayName, toDisplayName); }
/** * Adds a record for the specified prop type to the DB. */ internal int RegisterPropTypeInternal(string name, PropDataType propType, PropTypeFlags flags, bool forceType, out bool newPropType) { _storage.CheckOwnerThread(); IRecord rec = _propTypeTable.GetRecordByEqual(1, name); if (rec != null) { if (!_propTypeNameCache.ContainsKey(name)) { throw new BadIndexesException("Property type " + name + " found in PropTypes table but missing in name cache"); } bool recordChanged = false; if (rec.GetIntValue(2) != (int)propType) { if (forceType) { rec.SetValue(2, IntInternalizer.Intern((int)propType)); ((PropTypeItem)this[name]).SetDataType(propType); recordChanged = true; } else { throw new StorageException("Inconsistent registration for property type " + name + ": old type " + (PropDataType)rec.GetIntValue(2) + ", new type " + propType); } } int propId = rec.GetIntValue(0); PropTypeFlags newFlags = flags | this [propId].Flags; if (rec.GetIntValue(3) != (int)newFlags) { rec.SetValue(3, (int)newFlags); recordChanged = true; } if (recordChanged) { rec.Commit(); } newPropType = false; PropTypeItem propTypeItem = (PropTypeItem)_propTypeCache [propId]; propTypeItem.SetFlags(newFlags); return(propId); } if ((flags & (PropTypeFlags.DirectedLink | PropTypeFlags.CountUnread)) != 0 && propType != PropDataType.Link) { throw new StorageException("DirectedLink and CountUnread flags can be used only on Link properties"); } int ID; lock ( _propTypeTable ) { IRecord propertyType = _propTypeTable.NewRecord(); propertyType.SetValue(1, name); propertyType.SetValue(2, IntInternalizer.Intern((int)propType)); propertyType.SetValue(3, (int)flags); _storage.SafeCommitRecord(propertyType, "PropTypeCollection.RegisterPropTypeInternal"); ID = propertyType.GetID(); if (ID > 65536) { MyPalStorage.Storage.OnIndexCorruptionDetected("Invalid next ID in property type table"); } } AddPropTypeToCache(ID, name, propType, flags); newPropType = true; return(ID); }