| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f3a51da commit bc6dad6
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,70 @@ | |||
| 1 | + /***************************************************************************** | ||
| 2 | + Copyright 2021 The TensorFlow.NET Authors. All Rights Reserved. | ||
| 3 | + | ||
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + you may not use this file except in compliance with the License. | ||
| 6 | + You may obtain a copy of the License at | ||
| 7 | + | ||
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + | ||
| 10 | + Unless required by applicable law or agreed to in writing, software | ||
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + See the License for the specific language governing permissions and | ||
| 14 | + limitations under the License. | ||
| 15 | + ******************************************************************************/ | ||
| 16 | + | ||
| 17 | + using System; | ||
| 18 | + using System.IO; | ||
| 19 | + using System.IO.MemoryMappedFiles; | ||
| 20 | + using System.Linq; | ||
| 21 | + using Tensorflow; | ||
| 22 | + | ||
| 23 | + namespace Tensorflow.IO | ||
| 24 | + { | ||
| 25 | + public class MemmappedFileSystem | ||
| 26 | + { | ||
| 27 | + public const string MEMMAPPED_PACKAGE_DEFAULT_NAME = "memmapped_package://."; | ||
| 28 | + | ||
| 29 | + private MemoryMappedFile _mmapFile; | ||
| 30 | + private MemmappedFileSystemDirectory _directory; | ||
| 31 | + | ||
| 32 | + public MemmappedFileSystem(string path) | ||
| 33 | + { | ||
| 34 | + using (var stream = File.OpenRead(path)) | ||
| 35 | + { | ||
| 36 | + // Read the offset for the directory | ||
| 37 | + var offsetData = new byte[sizeof(ulong)]; | ||
| 38 | + stream.Seek(-sizeof(ulong), SeekOrigin.End); | ||
| 39 | + stream.Read(offsetData, 0, sizeof(ulong)); | ||
| 40 | + var offset = BitConverter.ToUInt64(offsetData, 0); | ||
| 41 | + | ||
| 42 | + var dirLength = stream.Length - (long) offset - sizeof(ulong); | ||
| 43 | + if (dirLength < 0) | ||
| 44 | + { | ||
| 45 | + throw new InvalidDataException("Malformed mmapped filesystem!"); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + var dirData = new byte[dirLength]; | ||
| 49 | + | ||
| 50 | + stream.Seek((long) offset, SeekOrigin.Begin); | ||
| 51 | + stream.Read(dirData, 0, (int) dirLength); | ||
| 52 | + | ||
| 53 | + _directory = MemmappedFileSystemDirectory.Parser.ParseFrom(dirData); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + _mmapFile = MemoryMappedFile.CreateFromFile(path, FileMode.Open); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + public Stream OpenMemmapped(string filename) | ||
| 60 | + { | ||
| 61 | + var entry = _directory.Element.FirstOrDefault(x => x.Name == filename); | ||
| 62 | + if (entry == null) | ||
| 63 | + { | ||
| 64 | + throw new FileNotFoundException($"Missing memmaped file entry: {filename}"); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + return _mmapFile.CreateViewStream((long) entry.Offset, (long) entry.Length); | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments