Esempio n. 1
0
        /// <summary>
        /// Returns a stream which enable storing either a new item or an update for
        /// an existing item in the store in the provided <paramref name="category"/>
        /// with the provided <paramref name="key"/>.
        /// </summary>
        /// <param name="category">The category of the item.</param>
        /// <param name="key">The key of the item.</param>
        /// <returns>A stream which must be used to store the item content.</returns>
        public Stream GetItemWriter(string category, string key)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            CheckDisposed();

            CheckSealed();

            var memoryStream = new MemoryStream();

            Interlocked.Increment(ref _openedStreamCount);

            return(new StreamWrapper(memoryStream, onDispose: () =>
            {
                byte[] bytes = memoryStream.ToArray();
                _stateStore.AddOrUpdateItem(category, key, bytes);
                Interlocked.Decrement(ref _openedStreamCount);
                Debug.Assert(Interlocked.CompareExchange(ref _sealed, 0, 0) == 0,
                             "The state was committed or rollback during the write operation.");
            }));
        }
Esempio n. 2
0
        private static InMemoryStateStore Load(XDocument doc)
        {
            var store = doc.Element("Store");
            var id    = store.Attribute("Id").Value;

            var res = new InMemoryStateStore(id);

            var categories = store.Elements("Category");

            foreach (var category in categories)
            {
                var categoryName = category.Attribute("Name").Value;

                var entries = category.Elements("Entry");
                foreach (var entry in entries)
                {
                    var keyName = entry.Attribute("Name").Value;
                    var base64  = entry.Nodes().OfType <XCData>().Single().Value;

                    var value = Convert.FromBase64String(base64);

                    res.AddOrUpdateItem(categoryName, keyName, value);
                }
            }

            return(res);
        }