A CmObjectId is basically a GUID. However, GUID is a struct, and hence, every Guid variable takes 16 bytes. CmObjectIds can be shared. Also, being our own class, we can implement some common methods with CmObject. CmObjectId also implements (secretly and very incompletely) ICmObjectOrSurrogate. This allows them to occur in the main dictionary of the IdentityMap.
Наследование: ICmObjectId, ICmObjectOrIdInternal, ICmObjectOrSurrogate
Пример #1
0
 internal ICmObjectId GetCanonicalID(CmObjectId candidate)
 {
     lock (SyncRoot)
     {
         ICmObjectOrSurrogate existing;
         if (!m_IdentityMap.TryGetValue(candidate, out existing))
         {
             m_IdentityMap[candidate] = candidate;
             existing = candidate;
         }
         return(existing.Id);
     }
 }
        // IReferenceTypeHandler override
        public override void Activate(IReferenceActivationContext context)
        {
            if (m_serverOnlyMode)
            {
                ActivateServer(context);
                return;
            }

            if (m_cache == null)
            {
                throw new NullReferenceException("m_cache");
            }
            if (m_identitymap == null)
            {
                throw new NullReferenceException("m_identitymap");
            }

            var context2 = (UnmarshallingContext)context;
            var buffer   = (ByteArrayBuffer)context2.Buffer();

            // Get Guid and CmObjectId.
            // Earlier code used a special CmObject.Create() method, I think because it is faster,
            // and on the assumption that we only activate objects at startup, so none of the
            // IDs have been seen before. However, with a background thread fluffing things, we
            // COULD have seen it, even during startup, if we've fluffed some object that references
            // this one. Also, this code is called in client-server mode when we Refresh an object
            // to see whether someone else changed it, and then the ID is very likely to exist already.
            // It MAY be possible to re-introduce the faster ID creation code just during startup
            // if we're not overlapping fluffing and reading.
            var reconId = CmObjectId.FromGuid(new Guid(buffer.ReadBytes(16)), m_identitymap);
            // Get class name.
            var className = m_unicodeEnc.GetString(buffer.ReadBytes(buffer.ReadInt()));
            // Get the data.
            var dataLength = buffer.ReadInt();

            // Reconstitute the surrogate.
            ((CmObjectSurrogate)context.PersistentObject()).InitializeFromDataStore(
                m_cache,
                reconId,
                className,
                buffer.ReadBytes(dataLength));
        }
Пример #3
0
        /// <summary>
        /// This method should be used when it is desirable to have HVOs associated with
        /// Guids for objects which may not yet have been fluffed up. The ObjectOrId may be
        /// passed to GetHvoFromObjectOrId to get an HVO; anything that actually uses
        /// the HVO will result in the object being fluffed, but that can be delayed (e.g.,
        /// when persisting a pre-sorted list of guids).
        /// It will return null if the guid does not correspond to a real object (that is,
        /// for success the identity map must contain either a CmObject or a surrogate, not
        /// just a CmObjectId.)
        /// </summary>
        internal ICmObjectOrId GetObjectOrIdWithHvoFromGuid(Guid guid)
        {
            ICmObjectOrSurrogate canonicalItem;

            lock (SyncRoot)
            {
                var oldCanonicalId = CmObjectId.FromGuid(guid, this);
                canonicalItem = m_IdentityMap[oldCanonicalId];
            }
            if (canonicalItem is ICmObject)
            {
                return((ICmObject)canonicalItem);
            }
            if (canonicalItem is CmObjectSurrogate)
            {
                return(((CmObjectSurrogate)canonicalItem).ObjectOrIdWithHvo);
            }
            // If it's neither, it doesn't map to a valid object, and we don't want to
            // assign it an HVO.
            return(null);
        }
        /// <summary>
        /// returns null if there are no more CmObejtcSurroages on the stream.
        /// </summary>
        internal CmObjectSurrogate GetNextCmObjectSurrogate(GZipStream stream)
        {
            var temp = new byte[16];

            if (stream.Read(temp, 0, 16) <= 0)
            {
                return(null);
            }
            var    cmObjectId = CmObjectId.FromGuid(new Guid(temp), m_identityMap);
            string className  = m_unicodeEncoding.GetString(stream.ReadBytes(stream.ReadInt()));

            byte[] xmldata = stream.ReadBytes(stream.ReadInt());

            long id = stream.ReadLong();

            var ret = new CmObjectSurrogate(m_cache, cmObjectId, className, xmldata);

            m_mapId.Add(ret.Id, id);

            return(ret);
        }
        /// <summary>
        /// Create a surrogate from some other surrogate (or CmObject).
        /// This is used for porting from one BEP to another,
        /// and it is faster than getting all the stuff from the xml string.
        /// </summary>
        public ICmObjectSurrogate Create(ICmObjectOrSurrogate source)
        {
            var sourceSurrogate = source as ICmObjectSurrogate;

            // No! This is not what is needed in porting,
            // since it makes a new surrogate with the ICmObject from the source BEP.
            //if (sourceSurrogate == null)
            //    return Create(source as ICmObject);
            if (sourceSurrogate == null)
            {
                // Have to make a new surrogate from the extant ICmObject information,
                // since we don't even want to think of just reusing the ICmObject.
                var asCmObject = (ICmObject)source;
                var asInternal = (ICmObjectInternal)asCmObject;
                return(new CmObjectSurrogate(
                           m_cache,
                           CmObjectId.Create(asCmObject.Guid),
                           asCmObject.ClassName,
                           asInternal.ToXmlString()));
            }
            return(new CmObjectSurrogate(m_cache, sourceSurrogate));
        }
Пример #6
0
		/// <summary>
		/// object ids are equal if their guids are.
		/// </summary>
		public bool Equals(CmObjectId id)
		{
			return m_guid == id.m_guid;
		}
Пример #7
0
 ICmObjectId ICmObjectIdFactory.NewId()
 {
     return(CmObjectId.FromGuid(Guid.NewGuid(), this));
 }
Пример #8
0
 ICmObjectId ICmObjectIdFactory.FromGuid(Guid guid)
 {
     return(CmObjectId.FromGuid(guid, this));
 }
Пример #9
0
 ICmObjectId ICmObjectIdFactory.FromBase64String(string guid)
 {
     return(CmObjectId.FromGuid(GuidServices.GetGuid(guid), this));
 }
Пример #10
0
 /// <summary>
 /// object ids are equal if their guids are.
 /// </summary>
 public bool Equals(CmObjectId id)
 {
     return(m_guid == id.m_guid);
 }