• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IContentAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • Dependency Injection
        • AccessorMessages
    • Avalanche.Binding
      • Introduction
    • Avalanche.Core
      • License
    • Avalanche.DataType
      • Introduction
      • DataType
        • IDataType
        • IListType
        • IMapType
        • IRecordType
        • IFieldType
        • IOneOfType
        • IAnyType
        • IStringType
        • IValueType
        • IIntegerType
        • IEnumerationType
        • IRealType
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • Enum
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
      • Asp.Net
        • Introduction
        • Supplying localization
        • Inject to pages
        • Culture Assigned
        • Minimalistic Api
        • Diagnostics
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR40
        • Unicode.CLDR41
        • Unicode.CLDR42
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Logging
        • Validation
    • Avalanche.Service
      • Introduction
      • Service
        • Introduction
        • IService
        • IServiceDisposable
        • IServiceDecoration
        • IServiceCast
        • IServiceObservable
        • IServiceContainer
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Scope
      • Handler
        • Introduction
        • IHandler
        • IHandlerCast
        • IHandlerDecoration
        • IHandlerWithOrder
        • CancellationToken
        • Cyclicity
        • Delegates
        • Invokable
        • ExportAttribute
        • OrderAttribute
        • PrintTree
        • Recursion
      • Query
        • Introduction
        • IQuery
        • IQueryCast
        • IQueryDecoration
      • Entry
        • Introduction
        • IEntry
        • IEntryCast
        • IEntryDecoration
        • IEntryObservable
        • IEntryVisitable
        • EntryState
      • Request
        • Introduction
        • IRequest
        • IRequestFor
        • IRequestToBeCached
        • IRequestToBeDisposed
        • RequestAttribute
        • ContextParameterAttribute
        • Print Tree
      • Dependency Injection
        • Introduction
        • Asp.Net
        • ServiceRequest<T>
        • Decorating a service
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
      • HttpStatusCode
      • OpcUaStatusCode
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • ListCloner
        • DictionaryCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Pipe
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
        • IProviderEvent
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    MemoryFileSystem

    MemoryFileSystem is a memory based filesystem.

    IFileSystem filesystem = new MemoryFileSystem();
    

    Files are based on blocks. Maximum number of blocks is 2^31-1. The blockSize can be set in constructor. The default blocksize is 1024.

    IFileSystem filesystem = new MemoryFileSystem(blockSize: 4096);
    

    Files can be browsed.

    foreach (var entry in filesystem.Browse(""))
        Console.WriteLine(entry.Path);
    

    Files can be opened for reading.

    using (Stream s = filesystem.Open("file.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        Console.WriteLine(s.Length);
    }
    

    And for writing.

    using (Stream s = filesystem.Open("file.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        s.WriteByte(32);
    }
    

    Files and directories can be observed for changes.

    IObserver<Avalanche.FileSystem.IEvent> observer = new Observer();
    using (IDisposable handle = filesystem.Observe("**", observer))
    {
    }
    

    Directories can be created.

    filesystem.CreateDirectory("dir/");
    

    Directories can be created recursively.

    filesystem.CreateDirectory("dir1/dir2/dir3/");
    filesystem.PrintTreeTo(Console.Out);
    

    The root is "".

    ""
    └──"dir1"
       └──"dir2"
          └──"dir3"
    

    MemoryFileSystem can create empty directory names. For example, a slash '/' at the start of a path refers to an empty directory right under the root.

    filesystem.CreateDirectory("/tmp/dir/");
    
    ""
    └──""
       └──"tmp"
          └──"dir"
    

    Path "file://" refers to three directories; the root, "file:" and a empty-named directory between two slashes "//".

    filesystem.CreateDirectory("file://");
    
    ""
    └──"file:"
       └──""
    

    Directories can be deleted.

    filesystem.Delete("dir/", recurse: true);
    

    Files and directories can be renamed and moved.

    filesystem.CreateDirectory("dir/");
    filesystem.Move("dir/", "new-name/");
    

    Disposing

    Disposable objects can be attached to be disposed along with FileSystem.

    // Init
    object obj = new ReaderWriterLockSlim();
    IFileSystemDisposable filesystem = new FileSystem("").AttachDisposable(obj);
    
    // ... do work ...
    
    // Dispose both
    filesystem.Dispose();
    

    Delegates can be attached to be executed at dispose of FileSystem.

    IFileSystemDisposable filesystem = new FileSystem("")
        .AddDisposeAction(f => Console.WriteLine("Disposed"));
    

    .BelateDispose() creates a handle that postpones dispose on .Dispose(). Actual dispose proceeds once .Dispose() is called and all belate handles are disposed. This can be used for passing the IFileSystem to worker threads.

    MemoryFileSystem filesystem = new MemoryFileSystem();
    filesystem.CreateDirectory("/tmp/dir/");
    
    // Postpone dispose
    filesystem.TryBelateDispose(out IDisposable? belateDisposeHandle);
    // Start concurrent work
    Task.Run(() =>
    {
        // Do work
        Thread.Sleep(1000);
        filesystem.GetEntry("");
        // Release belate handle. Disposes here or below, depending which thread runs last.
        belateDisposeHandle?.Dispose();
    });
    
    // Start dispose, but postpone it until belatehandle is disposed in another thread.
    filesystem.Dispose();
    

    Size Limit

    Constructor new MemoryFileSystem(blockSize, maxSpace) creates size limited filesystem. Memory limitation applies to files only, not to directory structure.

    IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 1L << 34);
    

    Printing with PrintTree.Format.DriveFreespace | PrintTree.Format.DriveSize flags show drive size.

    IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 1L << 34);
    ms.CreateFile("file", new byte[1 << 30]);
    ms.PrintTreeTo(Console.Out, format: FileSystemPrintTreeExtensions.Format.AllWithName);
    
    "" [Freespace: 15G, Size: 1G/16G, Ram]
    └── "file" [1073741824]
    

    If filesystem runs out of space, it throws FileSystemExceptionOutOfDiskSpace.

    IFileSystem ms = new MemoryFileSystem(blockSize: 1024, maxSpace: 2048);
    ms.CreateFile("file1", new byte[1024]);
    ms.CreateFile("file2", new byte[1024]);
    
    // throws FileSystemExceptionOutOfDiskSpace
    ms.CreateFile("file3", new byte[1024]);
    

    Available space can be shared between MemoryFileSystem instances with IBlockPool.

    IBlockPool pool = new BlockPool(blockSize: 1024, maxBlockCount: 3, maxRecycleQueue: 3);
    IFileSystem ms1 = new MemoryFileSystem(pool);
    IFileSystem ms2 = new MemoryFileSystem(pool);
    
    // Reserve 2048 from shared pool
    ms1.CreateFile("file1", new byte[2048]);
    
    // Not enough for another 3072, throws FileSystemExceptionOutOfDiskSpace
    ms2.CreateFile("file2", new byte[2048]);
    

    Deleted file is returned back to pool once all open streams are closed.

    IBlockPool pool = new BlockPool(blockSize: 1024, maxBlockCount: 3, maxRecycleQueue: 3);
    IFileSystem ms = new MemoryFileSystem(pool);
    Stream s = ms.Open("file", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
    s.Write(new byte[3072], 0, 3072);
    ms.Delete("file");
    
    Console.WriteLine(pool.BytesAvailable); // Prints 0
    s.Dispose();
    Console.WriteLine(pool.BytesAvailable); // Prints 3072
    
    In This Article
    Back to top Copyright © Toni Kalajainen