Example #1
0
        public void Indexer_SetValue()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));

            attrs = attrs.SetAttribute(new Attribute("test", new StringValue("value"), AttributeSource.Custom));
            Assert.AreEqual("value", attrs.GetValue <StringValue>("test").Value);
        }
Example #2
0
        public void SetAttribute_ExistingAttribute()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(24), AttributeSource.Custom));
            Assert.IsNotNull(attrs.GetAttribute("test"));

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));
            Assert.IsNotNull(attrs.GetAttribute("test"));
            Assert.AreEqual(42, attrs.GetValue <IntValue>("test").Value);
        }
        public void StoreThumbnail_ReplaceAnExistingThumbnail()
        {
            var entity = new FileEntity("test")
                         .SetAttribute(new Attribute("attr", new IntValue(1), AttributeSource.Custom))
                         .SetAttribute(new Attribute("thumbnail", new ImageValue(new byte[] { 0x21 }), AttributeSource.Metadata));

            _storage.Store(entity);
            _storage.ApplyChanges();

            var result = _storage.Load("test");

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.GetValue <IntValue>("attr").Value);
            Assert.AreEqual(1, result.GetValue <ImageValue>("thumbnail").Value.Length);
            Assert.AreEqual(0x21, result.GetValue <ImageValue>("thumbnail").Value[0]);

            entity = entity.SetAttribute(new Attribute("thumbnail", new ImageValue(new byte[] { 0x42 }),
                                                       AttributeSource.Metadata));

            _storage.StoreThumbnail(entity);
            _storage.ApplyChanges();

            result = _storage.Load("test");
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.GetValue <IntValue>("attr").Value);
            Assert.AreEqual(1, result.GetValue <ImageValue>("thumbnail").Value.Length);
            Assert.AreEqual(0x42, result.GetValue <ImageValue>("thumbnail").Value[0]);
        }
Example #4
0
        public void Remove_OneKey()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));
            Assert.IsNotNull(attrs.GetAttribute("test"));

            attrs = attrs.RemoveAttribute("test");
            Assert.IsNull(attrs.GetAttribute("test"));
        }
Example #5
0
        public void GetAttribute_IntAttribute()
        {
            IEntity attrs = new FileEntity("test");

            attrs = attrs.SetAttribute(new Attribute("test", new IntValue(42), AttributeSource.Custom));

            var attr = attrs.GetValue <IntValue>("test").Value;

            Assert.AreEqual(42, attr.Value);
        }
Example #6
0
        /// <inheritdoc />
        /// <summary>
        /// Load attributes from given file.
        /// Read algorithm:
        /// <list type="number">
        ///     <item>
        ///         <description>
        ///         read all JPEG segments to memory (using reaader returned by
        ///         <see cref="IJpegSegmentReaderFactory"/>)
        ///         </description>
        ///     </item>
        ///     <item>
        ///         <description>
        ///         decode data in JPEG segments (using readers returned by all
        ///         <see cref="IAttributeReaderFactory"/>) to attributes
        ///         </description>
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="path">Path to a file</param>
        /// <returns>Collection of attributes read from the file</returns>
        public IEntity Load(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            try
            {
                if (_fileSystem.DirectoryExists(path))
                {
                    return(new DirectoryEntity(path));
                }

                // read all JPEG segments to memory
                IEntity entity   = new FileEntity(path);
                var     fileInfo = new FileInfo(path);
                var     segments = ReadJpegSegments(path);

                // read attributes from all sources and add them to the collection
                foreach (var factory in _attrReaderFactories)
                {
                    var attrReader = factory.CreateFromSegments(fileInfo, segments);
                    try
                    {
                        for (;;)
                        {
                            var attr = attrReader.Read();
                            if (attr == null)
                            {
                                break;
                            }
                            entity = entity.SetAttribute(attr);
                        }
                    }
                    catch (InvalidDataFormatException e)
                    {
                        Loggger.Debug(e, "While loading {0}", path);
                    }
                }

                return(entity);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }
            catch (DirectoryNotFoundException)
            {
                return(null);
            }
        }
Example #7
0
        public void SetEntity_NonMarkedEntityWithReplaceFalse()
        {
            var entity = new FileEntity("test");

            _entityManager.SetEntity(entity, false);

            entity.SetAttribute(new Attribute("test", new IntValue(1), AttributeSource.Custom));
            Assert.IsNotNull(entity.GetAttribute("test"));

            var modified = _entityManager.GetModified();

            Assert.AreEqual(1, modified.Count);
            Assert.AreEqual(entity.Path, modified[0].Path);
            Assert.IsNull(modified[0].GetAttribute("test"));
        }
Example #8
0
        public void SetEntity_ModifiedListIsAListOfCopies()
        {
            var entity = new FileEntity("test");

            _entityManager.SetEntity(entity, true);

            entity.SetAttribute(new Attribute("test", new IntValue(1), AttributeSource.Custom));
            Assert.IsNotNull(entity.GetAttribute("test"));

            var modified = _entityManager.GetModified();

            Assert.AreEqual(1, modified.Count);
            Assert.AreEqual(entity.Path, modified[0].Path);
            Assert.IsNull(modified[0].GetAttribute("test"));
        }
        public void Store_StoringEmptyEntityWillDeleteIt()
        {
            IEntity entity = new FileEntity("test");

            entity = entity.SetAttribute(new Attribute("attr", new IntValue(1), AttributeSource.Custom));

            _storage.Store(entity);
            _storage.ApplyChanges();

            var storedEntity = _storage.Load("test");

            Assert.IsNotNull(storedEntity);

            _storage.Store(new FileEntity("test"));
            _storage.ApplyChanges();

            storedEntity = _storage.Load("test");
            Assert.IsNull(storedEntity);
        }
Example #10
0
        public void GetEntity_LoadTheMostRecentEntityFromTheSavingList()
        {
            var entity = new FileEntity("test")
                         .SetAttribute(new Attribute("a", new IntValue(1), AttributeSource.Custom));

            // add entity to the modified list
            _entityManager.SetEntity(entity, true);

            var loaded = _entityManager.GetEntity("test");

            Assert.AreEqual(entity, loaded);

            // move entity to the saving list
            var snapshot = _entityManager.GetModified();

            Assert.AreEqual(1, snapshot.Count);
            Assert.AreEqual(entity.Path, snapshot[0].Path);
            Assert.AreEqual(1, snapshot[0].GetValue <IntValue>("a").Value);

            loaded = _entityManager.GetEntity("test");
            Assert.AreEqual(entity, loaded);

            // modify entity
            entity = entity.SetAttribute(new Attribute("a", new IntValue(2), AttributeSource.Custom));

            // add modified entity to the modified list
            _entityManager.SetEntity(entity, false);

            loaded = _entityManager.GetEntity("test");
            Assert.AreEqual(entity, loaded);

            // move modified entity to the saving list
            snapshot = _entityManager.GetModified();
            Assert.AreEqual(1, snapshot.Count);
            Assert.AreEqual(entity.Path, snapshot[0].Path);
            Assert.AreEqual(2, snapshot[0].GetValue <IntValue>("a").Value);

            loaded = _entityManager.GetEntity("test");
            Assert.AreEqual(entity, loaded);
        }
Example #11
0
        public void SetEntity_MarkedEntityWithReplaceFalse()
        {
            var entity1 = new FileEntity("test1")
                          .SetAttribute(new Attribute("a", new IntValue(1), AttributeSource.Custom));
            var entity2 = new FileEntity("test1")
                          .SetAttribute(new Attribute("a", new IntValue(2), AttributeSource.Custom));

            _entityManager.SetEntity(entity1, true);
            _entityManager.SetEntity(entity2, false);

            entity1.SetAttribute(new Attribute("test", new IntValue(1), AttributeSource.Custom));
            Assert.IsNotNull(entity1.GetAttribute("test"));

            entity2.SetAttribute(new Attribute("test", new IntValue(1), AttributeSource.Custom));
            Assert.IsNotNull(entity2.GetAttribute("test"));

            var modified = _entityManager.GetModified();

            Assert.AreEqual(1, modified.Count);
            Assert.AreEqual(entity1.Path, modified[0].Path);
            Assert.IsNull(modified[0].GetAttribute("test"));
            Assert.AreEqual(1, modified[0].GetValue <IntValue>("a").Value);
        }
        public void Store_RewriteEntity()
        {
            IEntity entity = new FileEntity("ěščřžýáíé");

            entity = entity.SetAttribute(new Attribute("attr", new IntValue(1), AttributeSource.Custom));

            _storage.Store(entity);
            _storage.ApplyChanges();

            var storedEntity = _storage.Load("ěščřžýáíé");

            Assert.AreEqual(1, storedEntity.GetValue <IntValue>("attr").Value);

            IEntity newEntity = new FileEntity("ĚŠČŘŽÝÁÍÉ");

            newEntity = newEntity.SetAttribute(new Attribute("attr2", new IntValue(2), AttributeSource.Custom));
            _storage.Store(newEntity);
            _storage.ApplyChanges();

            storedEntity = _storage.Load("ěščřžýáíé");
            Assert.IsNull(storedEntity.GetAttribute("attr"));
            Assert.AreEqual(2, storedEntity.GetValue <IntValue>("attr2").Value);
        }
Example #13
0
        /// <summary>
        /// Load an entity at <paramref name="path"/> using query returned from
        /// <paramref name="queryGetter"/>. The query is disposed iff <paramref name="dispose"/>
        /// is true.
        /// </summary>
        /// <param name="path">Path to load</param>
        /// <param name="queryGetter">Function which returns load query</param>
        /// <param name="dispose">
        /// ture iff we should dispose the query returned from <paramref name="queryGetter"/>
        /// </param>
        /// <returns>Loaded entity of null</returns>
        private IEntity LoadImpl(string path, Func <LoadEntityQuery> queryGetter, bool dispose)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            path = PathUtils.NormalizePath(path);

            // check if there is a pending change in main memory
            lock (_requests)
            {
                if (_requests.TryGetValue(path, out var req))
                {
                    if (req is StoreRequest store)
                    {
                        return(store.Entity);
                    }

                    if (req is DeleteRequest)
                    {
                        return(null);
                    }

                    if (req is TouchRequest)
                    {
                        _requests[path] = new TouchRequest(DateTime.Now);
                    }
                }
                else
                {
                    _requests[path] = new TouchRequest(DateTime.Now);
                }
            }

            var     fi            = new FileInfo(path);
            var     lastWriteTime = fi.LastWriteTime;
            IEntity entity        = new FileEntity(path);

            // Get file metadata attributes. These are the only attributes which can change
            // even if the LastWriteTime has not changed.
            var fileAttributes = new List <Attribute>();

            try
            {
                var attributes = _fileAttributeReaderFactory.CreateFromSegments(fi, Enumerable.Empty <JpegSegment>());
                for (;;)
                {
                    var attr = attributes.Read();
                    if (attr == null)
                    {
                        break;
                    }
                    fileAttributes.Add(attr);
                }
            }
            catch (FileNotFoundException)
            {
                return(null);
            }

            // otherwise, load the entity from the database
            var query = queryGetter();

            try
            {
                // load valid attributes
                int count = 0;
                foreach (var attr in query.Fetch(entity.Path, lastWriteTime))
                {
                    entity.SetAttribute(attr);
                    ++count;
                }

                // update file attributes
                var result = count > 0 ? entity : null;
                if (result != null)
                {
                    foreach (var attr in fileAttributes)
                    {
                        result.SetAttribute(attr);
                    }
                }

                return(result);
            }
            finally
            {
                if (dispose)
                {
                    query.Dispose();
                }
            }
        }
Example #14
0
        public void SetAttribute_NullAttributeName()
        {
            var attrs = new FileEntity("test");

            attrs.SetAttribute(new Attribute(null, new IntValue(42), AttributeSource.Custom));
        }