1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009 Oracle.  All rights reserved.
5 *
6 */
7using System;
8using System.Collections.Generic;
9using System.Text;
10using BerkeleyDB.Internal;
11
12namespace BerkeleyDB {
13    /// <summary>
14    /// A class to represent what lock request(s) should be rejected during
15    /// deadlock resolution.
16    /// </summary>
17    public class DeadlockPolicy {
18        private uint flag;
19        internal uint policy { get { return flag; } }
20
21        private DeadlockPolicy(uint val) {
22            flag = val;
23        }
24
25        internal static DeadlockPolicy fromPolicy(uint val) {
26            switch (val) {
27                case DbConstants.DB_LOCK_DEFAULT:
28                    return DEFAULT;
29                case DbConstants.DB_LOCK_EXPIRE:
30                    return EXPIRE;
31                case DbConstants.DB_LOCK_MAXLOCKS:
32                    return MAX_LOCKS;
33                case DbConstants.DB_LOCK_MAXWRITE:
34                    return MAX_WRITE;
35                case DbConstants.DB_LOCK_MINLOCKS:
36                    return MIN_LOCKS;
37                case DbConstants.DB_LOCK_MINWRITE:
38                    return MIN_WRITE;
39                case DbConstants.DB_LOCK_OLDEST:
40                    return OLDEST;
41                case DbConstants.DB_LOCK_RANDOM:
42                    return RANDOM;
43                case DbConstants.DB_LOCK_YOUNGEST:
44                    return YOUNGEST;
45            }
46            throw new ArgumentException("Invalid deadlock policy.");
47        }
48
49        /// <summary>
50        /// If no DeadlockPolicy has yet been specified, use
51        /// <see cref="RANDOM"/>.
52        /// </summary>
53        public static DeadlockPolicy DEFAULT =
54            new DeadlockPolicy(DbConstants.DB_LOCK_DEFAULT);
55        /// <summary>
56        /// Reject lock requests which have timed out. No other deadlock
57        /// detection is performed.
58        /// </summary>
59        public static DeadlockPolicy EXPIRE =
60            new DeadlockPolicy(DbConstants.DB_LOCK_EXPIRE);
61        /// <summary>
62        /// Reject the lock request for the locker ID with the most locks.
63        /// </summary>
64        public static DeadlockPolicy MAX_LOCKS =
65            new DeadlockPolicy(DbConstants.DB_LOCK_MAXLOCKS);
66        /// <summary>
67        /// Reject the lock request for the locker ID with the most write locks.
68        /// </summary>
69        public static DeadlockPolicy MAX_WRITE =
70            new DeadlockPolicy(DbConstants.DB_LOCK_MAXWRITE);
71        /// <summary>
72        /// Reject the lock request for the locker ID with the fewest locks.
73        /// </summary>
74        public static DeadlockPolicy MIN_LOCKS =
75            new DeadlockPolicy(DbConstants.DB_LOCK_MINLOCKS);
76        /// <summary>
77        /// Reject the lock request for the locker ID with the fewest write
78        /// locks.
79        /// </summary>
80        public static DeadlockPolicy MIN_WRITE =
81            new DeadlockPolicy(DbConstants.DB_LOCK_MINWRITE);
82        /// <summary>
83        /// Reject the lock request for the locker ID with the oldest lock.
84        /// </summary>
85        public static DeadlockPolicy OLDEST =
86            new DeadlockPolicy(DbConstants.DB_LOCK_OLDEST);
87        /// <summary>
88        /// Reject the lock request for a random locker ID.
89        /// </summary>
90        public static DeadlockPolicy RANDOM =
91            new DeadlockPolicy(DbConstants.DB_LOCK_RANDOM);
92        /// <summary>
93        /// Reject the lock request for the locker ID with the youngest lock.
94        /// </summary>
95        public static DeadlockPolicy YOUNGEST =
96            new DeadlockPolicy(DbConstants.DB_LOCK_YOUNGEST);
97    }
98}
99