Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// <copyright file="VirtualFileSystem.fs" company="Brick Abode">
//
// PL/.NET (pldotnet) - PostgreSQL support for .NET C# and F# as
//                      procedural languages (PL)
//
//
// Copyright (c) 2023 Brick Abode
//
// This code is subject to the terms of the PostgreSQL License.
// The full text of the license can be found in the LICENSE file
// at the top level of the pldotnet repository.
//
// </copyright>
 
#if INTERACTIVE
#r "FSharp.Compiler.Service.dll"
#endif
 
namespace PlDotNET.FSharp
 
open System
open System.IO
open System.Text
open System.Collections.Generic
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.IO
 
/// <summary>
/// A virtual file system that allows reading and writing to files in memory.
/// </summary>
/// <remarks>
/// This class implements the `IFileSystem` interface and provides a virtual file system
/// that allows reading and writing to files stored in memory. It also delegates to a
/// default file system for all other file system operations.
/// </remarks>
/// <param name="container">A list of tuples representing the initial files in the virtual file system.
/// Each tuple consists of the file name and its contents.</param>
/// <param name="defaultFileSystem">The default file system to delegate to for all other file system operations.</param>
type VirtualFileSystem(container: (string * string) [], defaultFileSystem: IFileSystem) =
 
    /// <summary>
    /// A dictionary of the virtual files in the file system, keyed by file name.
    /// </summary>
    /// <remarks>
    /// The dictionary stores the contents of the virtual files as strings.
    /// </remarks>
    let files = dict container
 
    /// <summary>
    /// A dictionary of the virtual files in the file system, keyed by file name.
    /// </summary>
    /// <remarks>
    /// The dictionary stores the contents of the virtual files as `MemoryStream` objects.
    /// </remarks>
    let streams =
        let d = new Dictionary<string, MemoryStream>()
        for (key, value) in container do
            d.Add(key, new MemoryStream(Encoding.UTF8.GetBytes(value)))
        d
 
    /// <summary>
    /// An interface for file system operations.
    /// </summary>
    /// <remarks>
    /// This interface provides methods for performing various file system operations,
    /// such as reading and writing to files, checking file existence, and deleting files.
    /// </remarks>
    interface IFileSystem with
        /// <summary>
        /// Opens a file for reading.
        /// </summary>
        /// <param name="fileName">The name of the file to open.</param>
        /// <param name="useMemoryMappedFile">An optional flag indicating whether to use a memory-mapped file.</param>
        /// <param name="shouldShadowCopy">An optional flag indicating whether to shadow copy the file.</param>
        /// <returns>A stream representing the opened file.</returns>
        member _.OpenFileForReadShim(fileName, ?useMemoryMappedFile: bool, ?shouldShadowCopy: bool) =
            match files.TryGetValue fileName with
            | true, text -> streams.[fileName] :> Stream
            | _ ->
                defaultFileSystem.OpenFileForReadShim(
                    fileName,
                    ?useMemoryMappedFile = useMemoryMappedFile,
                    ?shouldShadowCopy = shouldShadowCopy
                )
 
        /// <summary>
        /// Opens a file for writing.
        /// </summary>
        /// <param name="fileName">The name of the file to open.</param>
        /// <param name="fileMode">An optional flag indicating the mode to open the file in.</param>
        /// <param name="fileAccess">An optional flag indicating the access to grant to the file.</param>
        /// <param name="fileShare">An optional flag indicating the type of sharing to allow for the file.</param>
        /// <returns>A stream representing the opened file.</returns>
        member _.OpenFileForWriteShim(fileName, ?fileMode: FileMode, ?fileAccess: FileAccess, ?fileShare: FileShare) =
            defaultFileSystem.OpenFileForWriteShim(
                fileName,
                ?fileMode = fileMode,
                ?fileAccess = fileAccess,
                ?fileShare = fileShare
            )
 
        /// <summary>
        /// Check if a file exists.
        /// </summary>
        /// <param name="fileName">The name of the file to check.</param>
        /// <returns>`true` if the file exists, `false` otherwise.</returns>
        member _.FileExistsShim(fileName) =
            files.ContainsKey(fileName)
            || defaultFileSystem.FileExistsShim(fileName)
 
        /// <summary>
        /// Get the path to the system's temporary directory.
        /// </summary>
        /// <returns>The path to the temporary directory.</returns>
        member _.GetTempPathShim() = defaultFileSystem.GetTempPathShim()
 
        /// <summary>
        /// Get the last write time of a file.
        /// </summary>
        /// <param name="fileName">The name of the file to check.</param>
        /// <returns>The last write time of the file.</returns>
        member _.GetLastWriteTimeShim(fileName) =
            defaultFileSystem.GetLastWriteTimeShim(fileName)
 
        /// <summary>
        /// Get the full path to a file.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <returns>The full path to the file.</returns>
        member _.GetFullPathShim(fileName) =
            defaultFileSystem.GetFullPathShim(fileName)
 
        /// <summary>
        /// Check if a path is invalid.
        /// </summary>
        /// <param name="fileName">The path to check.</param>
        /// <returns>`true` if the path is invalid, `false` otherwise.</returns>
        member _.IsInvalidPathShim(fileName) =
            defaultFileSystem.IsInvalidPathShim(fileName)
 
        /// <summary>
        /// Check if a path is rooted.
        /// </summary>
        /// <param name="fileName">The path to check.</param>
        /// <returns>`true` if the path is rooted, `false` otherwise.</returns>
        member _.IsPathRootedShim(fileName) =
            defaultFileSystem.IsPathRootedShim(fileName)
 
        /// <summary>
        /// Delete a file.
        /// </summary>
        /// <param name="fileName">The name of the file to delete.</param>
        member _.FileDeleteShim(fileName) =
            defaultFileSystem.FileDeleteShim(fileName)
 
        /// <summary>
        /// Get the assembly loader to use.
        /// </summary>
        member _.AssemblyLoader = defaultFileSystem.AssemblyLoader
 
        /// <summary>
        /// Get the full path to a file in a directory.
        /// </summary>
        /// <param name="dir">The directory containing the file.</param>
        /// <param name="fileName">The name of the file.</param>
        /// <returns>The full path to the file in the directory.</returns>
        member _.GetFullFilePathInDirectoryShim dir fileName =
            defaultFileSystem.GetFullFilePathInDirectoryShim dir fileName
 
        /// <summary>
        /// Normalize a path.
        /// </summary>
        /// <param name="path">The path to normalize.</param>
        /// <returns>The normalized path.</returns>
        member _.NormalizePathShim(path) =
            defaultFileSystem.NormalizePathShim(path)
 
        /// <summary>
        /// Get the directory name from a path.
        /// </summary>
        /// <param name="path">The path to get the directory name from.</param>
        /// <returns>The directory name from the path.</returns>
        member _.GetDirectoryNameShim(path) =
            defaultFileSystem.GetDirectoryNameShim(path)
 
        /// <summary>
        /// Get the creation time of a file.
        /// </summary>
        /// <param name="path">The path to the file.</param>
        /// <returns>The creation time of the file.</returns>
        member _.GetCreationTimeShim(path) =
            defaultFileSystem.GetCreationTimeShim(path)
 
        /// <summary>
        /// Copy a file.
        /// </summary>
        /// <param name="src">The path to the source file.</param>
        /// <param name="dest">The path to the destination file.</param>
        /// <param name="overwrite">A flag indicating whether to overwrite the destination file if it exists.</param>
        member _.CopyShim(src, dest, overwrite) =
            defaultFileSystem.CopyShim(src, dest, overwrite)
 
        /// <summary>
        /// Create a directory.
        /// </summary>
        /// <param name="path">The path to the directory to create.</param>
        member _.DirectoryCreateShim(path) =
            defaultFileSystem.DirectoryCreateShim(path)
 
        /// <summary>
        /// Check if a directory exists.
        /// </summary>
        /// <param name="path">The path to the directory to check.</param>
        /// <returns>`true` if the directory exists, `false` otherwise.</returns>
        member _.DirectoryExistsShim(path) =
            defaultFileSystem.DirectoryExistsShim(path)
 
        /// <summary>
        /// Delete a directory.
        /// </summary>
        /// <param name="path">The path to the directory to delete.</param>
        member _.DirectoryDeleteShim(path) =
            defaultFileSystem.DirectoryDeleteShim(path)
 
        /// <summary>
        /// Enumerate the files in a directory.
        /// </summary>
        /// <param name="path">The path to the directory to enumerate.</param>
        /// <param name="pattern">The search pattern to use for the enumeration.</param>
        /// <returns>An enumerable containing the names of the files in the directory.</returns>
        member _.EnumerateFilesShim(path, pattern) =
            defaultFileSystem.EnumerateFilesShim(path, pattern)
 
        /// <summary>
        /// Enumerate the directories in a directory.
        /// </summary>
        /// <param name="path">The path to the directory to enumerate.</param>
        /// <returns>An enumerable containing the names of the directories in the directory.</returns>
        member _.EnumerateDirectoriesShim(path) =
            defaultFileSystem.EnumerateDirectoriesShim(path)
 
        /// <summary>
        /// Check if a file is stable using a heuristic.
        /// </summary>
        /// <param name="path">The path to the file to check.</param>
        /// <returns>`true` if the file is stable according to the heuristic, `false` otherwise.</returns>
        member _.IsStableFileHeuristic(path) =
            defaultFileSystem.IsStableFileHeuristic(path)