Exemplo n.º 1
0
        /**
         * Adds a record for the specified resource type to the DB.
         */

        internal int RegisterResourceTypeInternal(string name, string displayNameTemplate, ResourceTypeFlags flags,
                                                  bool skipChecks, out bool newType)
        {
            _storage.CheckOwnerThread();
            IRecord rec = _resourceTypeTable.GetRecordByEqual(1, name);

            if (rec != null)
            {
                if (!_resourceTypeNameCache.ContainsKey(name))
                {
                    throw new BadIndexesException("Resource type " + name + " found in ResourceTypes table but missing in name cache");
                }
                string oldDisplayNameTemplate = rec.GetStringValue(2);
                if (!skipChecks && String.Compare(oldDisplayNameTemplate, displayNameTemplate, true, CultureInfo.InvariantCulture) != 0)
                {
                    if (oldDisplayNameTemplate.Length == 0)
                    {
                        rec.SetValue(2, displayNameTemplate);
                        _storage.SafeCommitRecord(rec, "ResourceTypeCollection.RegisterResourceTypeInternal");
                    }
                    else
                    {
                        throw new StorageException("Inconsistent display name template for resource type " + name +
                                                   "\nOld: " + oldDisplayNameTemplate + " New: " + displayNameTemplate);
                    }
                }
                newType = false;
                return(rec.GetIntValue(0));
            }

            int ID;

            lock ( _resourceTypeTable )
            {
                IRecord resourceType = _resourceTypeTable.NewRecord();
                resourceType.SetValue(1, name);
                resourceType.SetValue(2, displayNameTemplate);
                _storage.SafeCommitRecord(resourceType, "ResourceTypeCollection.RegisterResourceTypeInternal");
                ID = resourceType.GetID();
                if (ID > 65536)
                {
                    MyPalStorage.Storage.OnIndexCorruptionDetected("Invalid next ID in property type table");
                }
            }

            AddResourceTypeToCache(ID, name, displayNameTemplate, flags);

            newType = true;
            return(ID);
        }
Exemplo n.º 2
0
        /**
         * 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);
        }