A data store that is backed by a file located at the path given.
Наследование: IStoreData
Пример #1
0
        public void Open(bool readOnly)
        {
            lock (objectLock) {
                // Does the file exist?
                string f            = SliceFileName(0);
                bool   openExisting = FileSystem.FileExists(f);

                // If the file already exceeds the threshold and there isn't a secondary
                // file then we need to convert the file.
                if (openExisting && f.Length > MaxFileSlice)
                {
                    string f2 = SliceFileName(1);
                    if (FileSystem.FileExists(f2))
                    {
                        throw new IOException("File length exceeds maximum slice size setting.");
                    }

                    // We need to scatter the file.
                    if (readOnly)
                    {
                        throw new IOException("Unable to convert to a scattered store because Read-only.");
                    }
                }

                // Setup the first file slice
                var slice = new FileStoreData(f);
                slice.Open(readOnly);

                fileSlices.Add(slice);
                long runningLength = slice.Length;

                // If we are opening a store that exists already, there may be other
                // slices we need to setup.
                if (openExisting)
                {
                    int    i         = 1;
                    string slicePart = SliceFileName(i);
                    while (FileSystem.FileExists(slicePart))
                    {
                        // Create the new slice information for this part of the file.
                        slice = new FileStoreData(slicePart);
                        slice.Open(readOnly);

                        fileSlices.Add(slice);
                        runningLength += slice.Length;

                        ++i;
                        slicePart = SliceFileName(i);
                    }
                }

                trueFileLength = runningLength;
                IsOpen         = true;
                IsReadOnly     = readOnly;
            }
        }
Пример #2
0
        public bool Delete()
        {
            // The number of files
            int countFiles = FileCount;

            // Delete each file from back to front
            for (int i = countFiles - 1; i >= 0; --i)
            {
                string f             = SliceFileName(i);
                bool   deleteSuccess = new FileStoreData(f).Delete();
                if (!deleteSuccess)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        public void SetLength(long value)
        {
            lock (objectLock) {
                // The size we need to grow the data area
                long totalSizeToGrow = value - trueFileLength;
                // Assert that we aren't shrinking the data area size.
                if (totalSizeToGrow < 0)
                {
                    throw new IOException("Unable to make the data area size " +
                                          "smaller for this type of store.");
                }

                while (totalSizeToGrow > 0)
                {
                    // Grow the last slice by this size
                    int  last           = fileSlices.Count - 1;
                    var  slice          = fileSlices[last];
                    long oldSliceLength = slice.Length;
                    long toGrow         = System.Math.Min(totalSizeToGrow, (MaxFileSlice - oldSliceLength));

                    // Flush the buffer and set the length of the file
                    slice.SetLength(oldSliceLength + toGrow);
                    slice.Flush();

                    totalSizeToGrow -= toGrow;
                    // Create a new empty slice if we need to extend the data area
                    if (totalSizeToGrow > 0)
                    {
                        string sliceFile = SliceFileName(last + 1);

                        slice = new FileStoreData(sliceFile);
                        slice.Open(false);

                        fileSlices.Add(slice);
                    }
                }
                trueFileLength = value;
            }
        }
Пример #4
0
        public void SetLength(long value)
        {
            lock (objectLock) {
                // The size we need to grow the data area
                long totalSizeToGrow = value - trueFileLength;
                // Assert that we aren't shrinking the data area size.
                if (totalSizeToGrow < 0) {
                    throw new IOException("Unable to make the data area size " +
                                          "smaller for this type of store.");
                }

                while (totalSizeToGrow > 0) {
                    // Grow the last slice by this size
                    int last = fileSlices.Count - 1;
                    var slice = fileSlices[last];
                    long oldSliceLength = slice.Length;
                    long toGrow = System.Math.Min(totalSizeToGrow, (MaxFileSlice - oldSliceLength));

                    // Flush the buffer and set the length of the file
                    slice.SetLength(oldSliceLength + toGrow);
                    slice.Flush();

                    totalSizeToGrow -= toGrow;
                    // Create a new empty slice if we need to extend the data area
                    if (totalSizeToGrow > 0) {
                        string sliceFile = SliceFileName(last + 1);

                        slice = new FileStoreData(sliceFile);
                        slice.Open(false);

                        fileSlices.Add(slice);
                    }
                }
                trueFileLength = value;
            }
        }
Пример #5
0
        public void Open(bool readOnly)
        {
            lock (objectLock) {
                // Does the file exist?
                string f = SliceFileName(0);
                bool openExisting = File.Exists(f);

                // If the file already exceeds the threshold and there isn't a secondary
                // file then we need to convert the file.
                if (openExisting && f.Length > MaxFileSlice) {
                    string f2 = SliceFileName(1);
                    if (File.Exists(f2))
                        throw new IOException("File length exceeds maximum slice size setting.");

                    // We need to scatter the file.
                    if (readOnly)
                        throw new IOException("Unable to convert to a scattered store because Read-only.");
                }

                // Setup the first file slice
                var slice = new FileStoreData(f);
                slice.Open(readOnly);

                fileSlices.Add(slice);
                long runningLength = slice.Length;

                // If we are opening a store that exists already, there may be other
                // slices we need to setup.
                if (openExisting) {
                    int i = 1;
                    string slicePart = SliceFileName(i);
                    while (File.Exists(slicePart)) {
                        // Create the new slice information for this part of the file.
                        slice = new FileStoreData(slicePart);
                        slice.Open(readOnly);

                        fileSlices.Add(slice);
                        runningLength += slice.Length;

                        ++i;
                        slicePart = SliceFileName(i);
                    }
                }

                trueFileLength = runningLength;
                IsOpen = true;
                IsReadOnly = readOnly;
            }
        }
Пример #6
0
 public bool Delete()
 {
     // The number of files
     int countFiles = FileCount;
     // Delete each file from back to front
     for (int i = countFiles - 1; i >= 0; --i) {
         string f = SliceFileName(i);
         bool deleteSuccess = new FileStoreData(f).Delete();
         if (!deleteSuccess)
             return false;
     }
     return true;
 }