Ejemplo n.º 1
0
        private async Task ApplyChangeAsync(
            object key,
            object entity,
            EntityState entityState)
        {
            var targetPath = Path.AppendFile(key.ToString());

            switch (entityState)
            {
            case EntityState.Deleted:
                await AsyncFileSystem.DeleteAsync(targetPath);

                break;

            case EntityState.Added:
                var metadata = EntityMetadata(key, entity, EntityState.Added, null);
                using (var stream = await AsyncFileSystem.CreateFileAsync(targetPath))
                    Serialize(stream, metadata);
                break;

            case EntityState.Updated:
                var updatedMetadata = EntityMetadata(key, entity, EntityState.Updated,
                                                     await FindByIdInternalAsync(key));
                using (var stream = await AsyncFileSystem.CreateFileAsync(targetPath))
                    Serialize(stream, updatedMetadata);
                break;
            }
        }
Ejemplo n.º 2
0
 private async Task EnsureInitializedAsync()
 {
     if (!await AsyncFileSystem.ExistsAsync(Path))
     {
         await AsyncFileSystem.CreateDirectoryAsync(Path);
     }
 }
Ejemplo n.º 3
0
        public async Task EnsureInitializedAsync()
        {
            if (_initialized)
            {
                return;
            }

            using (_lock.WriteLock())
            {
                if (!await AsyncFileSystem.ExistsAsync(Path))
                {
                    _initialized = true;
                    return;
                }

                using (var stream = await AsyncFileSystem.OpenFileAsync(Path, DesiredFileAccess.Read))
                    using (var instream = new StreamReader(stream))
                    {
                        var json     = "[" + instream.ReadToEnd().Replace(Environment.NewLine, ",") + "]";
                        var entities = JsonConvert.DeserializeObject(
                            json,
                            typeof(List <>).MakeGenericType(typeof(MetadataEntity))
                            ) as IEnumerable <MetadataEntity>;

                        _entities = entities.ToDictionary(
                            key => key.Identity,
                            value => new Tuple <object, MetadataEntity>(value.EntityOfType(EntityType), value)
                            );
                        _initialized = true;
                    }
            }
        }
Ejemplo n.º 4
0
        private async Task <MetadataEntity> FindByIdInternalAsync(object key)
        {
            await EnsureInitializedAsync();

            var targetPath = Path.AppendFile(key.ToString());

            using (var stream = await AsyncFileSystem.OpenFileAsync(targetPath, DesiredFileAccess.Read))
                return(Deserialize(stream));
        }
Ejemplo n.º 5
0
        public override async Task <int> CountAsync()
        {
            using (_lock.ReadLock())
            {
                await EnsureInitializedAsync();

                return((await AsyncFileSystem.GetEntitiesAsync(Path)).Count());
            }
        }
Ejemplo n.º 6
0
 private async Task FlushAsync(List <MetadataEntity> raw)
 {
     using (var stream = await AsyncFileSystem.CreateFileAsync(Path))
         using (var outstream = new StreamWriter(stream))
         {
             var writer = new JsonTextWriter(outstream);
             _listSerializer.WriteJson(writer, raw, _serializer);
         }
 }