Esempio n. 1
0
        public ITypedStream GetTypedStream(OpenMode mode, Type t)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                if (mode != OpenMode.Read)
                {
                    AssertCurrentVersion();
                }

                // We first check streams in cache, then try to load.
                ManagedTypedStream stream = GetCachedTypedStream(t.FullName, mode);
                if (stream != null)
                {
                    return(stream);
                }

                // We can also check in base node.
                ManagedNode b = CacheBase();
                if (b != null)
                {
                    return(b.GetTypedStream(mode, t));
                }
                return(null);
            }
        }
        public void CopyTo([NotNull] ITypedStream s)
        {
            AssertNotDisposed();
            ManagedTypedStream stream = s as ManagedTypedStream;

            lock (Inner)
            {
                stream.AssertNotDisposed();
                lock (stream.Inner)
                {
                    // Implementation notes: should be journalled in future.
                    uint[] locations = Inner.ObjectLocations;
                    for (int i = 0; i < locations.Length; i++)
                    {
                        string type = Inner.GetObjectType(locations[i]);

                        if (usesRaw)
                        {
                            byte[] data = Inner.Read(locations[i]) as byte[];
                            stream.Inner.Write(locations[i], type, data);
                        }
                        else
                        {
                            object data = Inner.Read(locations[i]);
                            stream.Inner.Write(locations[i], type, data);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Helper; creates cached TS.
        /// </summary>
        private ManagedTypedStream GetCachedTypedStream(string t, OpenMode mode)
        {
            // First update all unused typed streams containers.
            typedStreams.RemoveAll(delegate(TypedStreamContainer c) {
                if (!c.IsAlive)
                {
                    return(true);
                }
                return(false);
            });

            // Foreach stream.
            foreach (TypedStreamContainer c in typedStreams)
            {
                if (c.Type == t)
                {
                    ManagedTypedStream stream = c.Create(mode);
                    if (stream != null)
                    {
                        return(stream);
                    }
                    break;
                }
            }

            // We have to create it.


            // We ask node to provide TS.
            IDriverTypedStream s = node.GetTypedStream(OpenMode.ReadWrite, t);

            if (s == null)
            {
                return(null);
            }

            TypedStreamContainer cont = new TypedStreamContainer(t, s);

            // We add it in front, to make sure we first check this new type.
            typedStreams.Insert(0, cont);
            return(cont.Create(mode));
        }
Esempio n. 4
0
        public void AddTypedStream(Type t, StreamOptions flags)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                AssertCurrentVersion();

                // We first check that it does not exist.
                ManagedTypedStream stream = GetCachedTypedStream(t.FullName, OpenMode.Read);
                if (stream != null)
                {
                    stream.Dispose();
                    throw new TypedStreamAlreadyExistsException("Typed stream " +
                                                                t.FullName + " already exists.");
                }

                // We can add it.
                node.AddTypedStream(t.FullName, flags);
            }
        }
Esempio n. 5
0
        public ITypedStream OpenDefaultStream(OpenMode mode)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                if (mode != OpenMode.Read)
                {
                    AssertCurrentVersion();
                }

                if (node == null)
                {
                    throw new TypedStreamNotFoundException("Default typed stream does " +
                                                           " not exist on transparent nodes.");
                }

                // We obtain the stream.
                ManagedTypedStream stream = GetCachedTypedStream(node.DefaultType, mode);
                return(stream);
            }
        }
Esempio n. 6
0
        public void ChangeDefaultStream(Type t)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                AssertCurrentVersion();

                // We make sure the typed stream is valid.
                ManagedTypedStream ts = GetCachedTypedStream(t.FullName, OpenMode.Read);
                if (ts == null)
                {
                    throw new TypedStreamNotFoundException("The typed stream of type " +
                                                           t.FullName + " does not exist in this node.");
                }

                // We are not using it anymore.
                ts.Dispose();

                // Change it.
                node.ChangeDefaultStream(t.FullName);
            }
        }
Esempio n. 7
0
        public void RemoveTypedStream(Type t)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                AssertCurrentVersion();

                // We first make sure it exists.
                ManagedTypedStream stream = GetCachedTypedStream(t.FullName, OpenMode.Read);
                if (stream == null)
                {
                    throw new TypedStreamNotFoundException("Cannot remove unexisting typed stream.");
                }

                // We check it's usage, there must only one read stream opened.
                TypedStreamContainer container = typedStreams.Find(
                    delegate(TypedStreamContainer c)
                {
                    if (c.Type == t.FullName)
                    {
                        return(true);
                    }
                    return(false);
                });

                // We obtained the container, make sure only one stream is using it.
                if (container.ReadCount != 1)
                {
                    stream.Dispose();
                    throw new InvalidOperationException("Cannot delete typed stream in usage.");
                }

                // We must dispose it, invalidating the container.
                stream.Dispose();

                // We can delete it.
                node.RemoveTypedStream(t.FullName);
            }
        }