Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
 
namespace Microsoft.ML.OnnxRuntime
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
 
    /// <summary>
    /// Class to that stores information about the file location where an "external" initializer is stored.
    /// </summary>
    /// <see cref="OrtModelCompilationOptions.HandleInitializerDelegate"/>
    public class OrtExternalInitializerInfo : SafeHandle, IReadOnlyExternalInitializerInfo
    {
        // Set to false when constructed with an externally managed constant handle owned by ORT.
        private readonly bool _ownsHandle = true;
 
        /// <summary>
        /// Create a new OrtExternalInitializerInfo instance. 
        /// </summary>
        /// <param name="filePath">The path to the file that stores the initializer data.</param>
        /// <param name="fileOffset">The byte offset in the file where the data is stored.</param>
        /// <param name="byteSize">The size of the data (in bytes) within the file.</param>
        public OrtExternalInitializerInfo(string filePath, long fileOffset, long byteSize)
            : base(IntPtr.Zero, ownsHandle: true)
        {
            var platformFilePath = NativeOnnxValueHelper.GetPlatformSerializedString(filePath);
            NativeApiStatus.VerifySuccess(
                NativeMethods.OrtCreateExternalInitializerInfo(platformFilePath, fileOffset, (UIntPtr)byteSize, out handle));
            _ownsHandle = true;
        }
 
        /// <summary>
        /// Create a new OrtExternalInitializerInfo instance from an existing native OrtExternalInitializerInfo handle.
        /// </summary>
        /// <param name="constHandle">Native OrtExternalInitializerInfo handle.</param>
        /// <param name="ownsHandle">True if the OrtExternalInitializerInfo instance owns the native handle.
        /// Defaults to false.</param>
        internal OrtExternalInitializerInfo(IntPtr constHandle, bool ownsHandle = false)
            : base(IntPtr.Zero, ownsHandle)
        {
            Debug.Assert(constHandle != IntPtr.Zero);
            SetHandle(constHandle);
            _ownsHandle = ownsHandle;
        }
 
        /// <summary>
        /// Get the file path to the file that store's the initializer's data.
        /// </summary>
        /// <remarks>
        /// The path is relative to the filesystem directory where the ONNX model was stored.
        /// </remarks>
        /// <returns>The file path.</returns>
        public string GetFilePath()
        {
            IntPtr filePathPtr = NativeMethods.OrtExternalInitializerInfo_GetFilePath(handle);
            if (filePathPtr == IntPtr.Zero)
            {
                return string.Empty;
            }
 
            return NativeOnnxValueHelper.StringFromNativePathString(filePathPtr);
        }
 
        /// <summary>
        /// Get the byte offset within the file where the initializer's data is stored.
        /// </summary>
        /// <returns>The file offset location.</returns>
        public long GetFileOffset()
        {
            return NativeMethods.OrtExternalInitializerInfo_GetFileOffset(handle);
        }
 
        /// <summary>
        /// Get the size in bytes of the initializer's data within the file.
        /// </summary>
        /// <returns>The size in bytes of the initializer data.</returns>
        public long GetByteSize()
        {
            UIntPtr byteSize = NativeMethods.OrtExternalInitializerInfo_GetByteSize(handle);
            return checked((long)byteSize);
        }
 
        /// <summary>
        /// Indicates whether the native handle is invalid.
        /// </summary>
        public override bool IsInvalid { get { return handle == IntPtr.Zero; } }
 
        /// <summary>
        /// Release the native instance of OrtExternalInitializerInfo if we own it.
        /// </summary>
        /// <returns>true on success and false on error.</returns>
        protected override bool ReleaseHandle()
        {
            if (!_ownsHandle)
            {
                // Return false to indicate an error.
                // ReleaseHandle() should not be called on a const handle that this class does not own.
                return false;
            }
 
            NativeMethods.OrtReleaseExternalInitializerInfo(handle);
            handle = IntPtr.Zero;
            return true;
        }
    }
 
    /// <summary>
    /// Interface for all readonly methods implemented by OrtExternalInitializerInfo.
    /// </summary>
    public interface IReadOnlyExternalInitializerInfo
    {
        /// <summary>
        /// Get the file path to the file that store's the initializer's data.
        /// </summary>
        /// <remarks>
        /// The path is relative to the filesystem directory where the ONNX model was stored.
        /// </remarks>
        /// <returns>The file path.</returns>
        string GetFilePath();
 
        /// <summary>
        /// Get the byte offset within the file where the initializer's data is stored.
        /// </summary>
        /// <returns>The file offset location.</returns>
        long GetFileOffset();
 
        /// <summary>
        /// Get the size in bytes of the initializer's data within the file.
        /// </summary>
        /// <returns>The size in bytes of the initializer data.</returns>
        long GetByteSize();
    }
}