summaryrefslogtreecommitdiff
path: root/lang/csharp/src/DatabaseException.cs
blob: 48026bb1f7d36f0796493e7ed922bfded42f29fc (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */
using System;
using System.Collections.Generic;
using System.Text;
using BerkeleyDB.Internal;

namespace BerkeleyDB {
    /// <summary>
    /// Represents errors that occur during Berkley DB operations.
    /// </summary>
    public class DatabaseException : Exception {
        /// <summary>
        /// The underlying error code from the Berkeley DB C library.
        /// </summary>
        public int ErrorCode;

        /// <summary>
        /// Throw an exception which corresponds to the specified Berkeley DB
        /// error code.
        /// </summary>
        /// <param name="err">The Berkeley DB error code</param>
        public static void ThrowException(int err) {
            switch (err) {
                case 0:
                    return;
                case ErrorCodes.DB_NOTFOUND:
                    throw new NotFoundException();
                case ErrorCodes.DB_BUFFER_SMALL:
                    throw new MemoryException();
                case ErrorCodes.DB_FOREIGN_CONFLICT:
                    throw new ForeignConflictException();
                case ErrorCodes.DB_HEAP_FULL:
                    throw new HeapFullException();
                case ErrorCodes.DB_KEYEMPTY:
                    throw new KeyEmptyException();
                case ErrorCodes.DB_KEYEXIST:
                    throw new KeyExistException();
                case ErrorCodes.DB_LOCK_DEADLOCK:
                    throw new DeadlockException();
                case ErrorCodes.DB_LOCK_NOTGRANTED:
                    throw new LockNotGrantedException();
                case ErrorCodes.DB_LOG_BUFFER_FULL:
                    throw new FullLogBufferException();
                case ErrorCodes.DB_META_CHKSUM_FAIL:
                    throw new MetaCheckSumFailException();
                case ErrorCodes.DB_OLD_VERSION:
                    throw new OldVersionException();
                case ErrorCodes.DB_PAGE_NOTFOUND:
                    throw new PageNotFoundException();
                case ErrorCodes.DB_REP_DUPMASTER:
                case ErrorCodes.DB_REP_HOLDELECTION:
                case ErrorCodes.DB_REP_IGNORE:
                case ErrorCodes.DB_REP_ISPERM:
                case ErrorCodes.DB_REP_JOIN_FAILURE:
                case ErrorCodes.DB_REP_NEWSITE:
                case ErrorCodes.DB_REP_NOTPERM:
                    return;
                case ErrorCodes.DB_REP_LEASE_EXPIRED:
                    throw new LeaseExpiredException();
                case ErrorCodes.DB_RUNRECOVERY:
                    throw new RunRecoveryException();
                case ErrorCodes.DB_SECONDARY_BAD:
                    throw new BadSecondaryException();
                case ErrorCodes.DB_VERIFY_BAD:
                    throw new VerificationException();
                case ErrorCodes.DB_VERSION_MISMATCH:
                    throw new VersionMismatchException();
                default:
                    throw new DatabaseException(err);
            }
        }

        /// <summary>
        /// Create a new DatabaseException, encapsulating a specific error code.
        /// </summary>
        /// <param name="err">The error code to encapsulate.</param>
        public DatabaseException(int err)
            : base(libdb_csharp.db_strerror(err)) {
            ErrorCode = err;
        }
    }

    /// <summary>
    /// A secondary index has been corrupted. This is likely the result of an
    /// application operating on related databases without first associating
    /// them.
    /// </summary>
    public class BadSecondaryException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the BadSecondaryException
        /// </summary>
        public BadSecondaryException() : base(ErrorCodes.DB_SECONDARY_BAD) { }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ForeignConflictException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the ForeignConflictException
        /// </summary>
        public ForeignConflictException()
            : base(ErrorCodes.DB_FOREIGN_CONFLICT) { }
    }

    /// <summary>
    /// In-memory logs are configured and no more log buffer space is available.
    /// </summary>
    public class FullLogBufferException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the FullLogBufferException
        /// </summary>
        public FullLogBufferException()
            : base(ErrorCodes.DB_LOG_BUFFER_FULL) { }
    }

    /// <summary>
    /// An attempt was made to add or update a record in a
    /// <see cref="HeapDatabase"/>. However, the size of the database was
    /// constrained using <see cref="HeapDatabaseConfig.MaxSize"/>, and that
    /// limit has been reached.
    /// </summary>
    public class HeapFullException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the HeapFullException
        /// </summary>
        public HeapFullException() : base(ErrorCodes.DB_HEAP_FULL) { }
    }

    /// <summary>
    /// The requested key/data pair logically exists but was never explicitly
    /// created by the application, or that the requested key/data pair was
    /// deleted and never re-created. In addition, the Queue access method 
    /// throws a KeyEmptyException for records that were created as part of a
    /// transaction that was later aborted and never re-created.
    /// </summary>
    /// <remarks>
    /// The Recno and Queue access methods automatically create key/data
    /// pairs under some circumstances.
    /// </remarks>
    public class KeyEmptyException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the KeyEmptyException
        /// </summary>
        public KeyEmptyException() : base(ErrorCodes.DB_KEYEMPTY) { }
    }

    /// <summary>
    /// A key/data pair was inserted into the database using
    /// <see cref="Database.PutNoOverwrite"/> and the key already
    /// exists in the database, or using
    /// <see cref="BTreeDatabase.PutNoDuplicate"/> or
    /// <see cref="HashDatabase.PutNoDuplicate"/> and the key/data
    /// pair already exists in the database.
    /// </summary>
    public class KeyExistException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the KeyExistException
        /// </summary>
        public KeyExistException() : base(ErrorCodes.DB_KEYEXIST) { }
    }

    /// <summary>
    /// When multiple threads of control are modifying the database, there is
    /// normally the potential for deadlock. In Berkeley DB, deadlock is
    /// signified by a DeadlockException thrown from the Berkeley DB function.
    /// Whenever a Berkeley DB function throws a DeadlockException, the
    /// enclosing transaction should be aborted.
    /// </summary>
    public class DeadlockException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the DeadlockException
        /// </summary>
        public DeadlockException() : base(ErrorCodes.DB_LOCK_DEADLOCK) { }
    }

    /// <summary>
    /// The site's replication master lease has expired.
    /// </summary>
    public class LeaseExpiredException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the LeaseExpiredException
        /// </summary>
        public LeaseExpiredException()
            : base(ErrorCodes.DB_REP_LEASE_EXPIRED) { }
    }

    /// <summary>
    /// If <see cref="DatabaseEnvironmentConfig.TimeNotGranted"/> is true,
    /// database calls timing out based on lock or transaction timeout values
    /// throw a LockNotGrantedException, instead of a DeadlockException.
    /// </summary>
    public class LockNotGrantedException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the LockNotGrantedException
        /// </summary>
        public LockNotGrantedException()
            : base(ErrorCodes.DB_LOCK_NOTGRANTED) { }
    }

    internal class MemoryException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the MemoryException
        /// </summary>
        internal MemoryException() : base(ErrorCodes.DB_BUFFER_SMALL) { }
    }

    /// <summary>
    /// A checksum mismatch was detected on a database metadata page.  Either
    /// the database is corrupted or the file is not a Berkeley DB database
    /// file. 
    /// </summary>
    public class MetaCheckSumFailException : DatabaseException {
        /// <summary
        /// Initialize a new instance of the MetaCheckSumFailException
        /// </summary>
        public MetaCheckSumFailException()
            : base(ErrorCodes.DB_META_CHKSUM_FAIL) { }
    }

    /// <summary>
    /// The requested key/data pair did not exist in the database or that
    /// start-of- or end-of-file has been reached by a cursor.
    /// </summary>
    public class NotFoundException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the NotFoundException
        /// </summary>
        public NotFoundException() : base(ErrorCodes.DB_NOTFOUND) { }
    }

    /// <summary>
    /// This version of Berkeley DB is unable to upgrade a given database.
    /// </summary>
    public class OldVersionException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the OldVersionException
        /// </summary>
        public OldVersionException() : base(ErrorCodes.DB_OLD_VERSION) { }
    }

    /// <summary>
    /// 
    /// </summary>
    public class PageNotFoundException : DatabaseException {
        /// <summary>
        /// 
        /// </summary>
        public PageNotFoundException() : base(ErrorCodes.DB_PAGE_NOTFOUND) { }
    }

    /// <summary>
    /// Berkeley DB has encountered an error it considers fatal to an entire
    /// environment. Once a RunRecoveryException has been thrown by any
    /// interface, it is returned from all subsequent Berkeley DB calls
    /// made by any threads of control participating in the environment.
    /// </summary>
    /// <remarks>
    /// An example of this type of fatal error is a corrupted database page. The
    /// only way to recover from this type of error is to have all threads of
    /// control exit the Berkeley DB environment, run recovery of the
    /// environment, and re-enter Berkeley DB. (It is not strictly necessary
    /// that the processes exit, although that is the only way to recover system
    /// resources, such as file descriptors and memory, allocated by
    /// Berkeley DB.)
    /// </remarks>
    public class RunRecoveryException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the RunRecoveryException
        /// </summary>
        public RunRecoveryException() : base(ErrorCodes.DB_RUNRECOVERY) { }
    }

    /// <summary>
    /// Thrown by <see cref="Database.Verify"/> if a database is
    /// corrupted, and by <see cref="Database.Salvage"/> if all
    /// key/data pairs in the file may not have been successfully output.
    /// </summary>
    public class VerificationException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the VerificationException
        /// </summary>
        public VerificationException() : base(ErrorCodes.DB_VERIFY_BAD) { }
    }

    /// <summary>
    /// The version of the Berkeley DB library does not match the version that
    /// created the database environment.
    /// </summary>
    public class VersionMismatchException : DatabaseException {
        /// <summary>
        /// Initialize a new instance of the VersionMismatchException
        /// </summary>
        public VersionMismatchException()
            : base(ErrorCodes.DB_VERSION_MISMATCH) { }
    }
}