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;
10
11namespace BerkeleyDB {
12    /// <summary>
13    /// A class representing configuration parameters for
14    /// <see cref="QueueDatabase"/>
15    /// </summary>
16    public class QueueDatabaseConfig : DatabaseConfig {
17        /* Fields for DB->set_flags() */
18        /// <summary>
19        /// If true, modify the operation of <see cref="QueueDatabase.Consume"/>
20        /// to return key/data pairs in order. That is, they will always return
21        /// the key/data item from the head of the queue.
22        /// </summary>
23        /// <remarks>
24        /// <para>
25        /// The default behavior of queue databases is optimized for multiple
26        /// readers, and does not guarantee that record will be retrieved in the
27        /// order they are added to the queue. Specifically, if a writing thread
28        /// adds multiple records to an empty queue, reading threads may skip
29        /// some of the initial records when the next
30        /// <see cref="QueueDatabase.Consume"/> call returns.
31        /// </para>
32        /// <para>
33        /// This setting modifies <see cref="QueueDatabase.Consume"/> to verify
34        /// that the record being returned is in fact the head of the queue.
35        /// This will increase contention and reduce concurrency when there are
36        /// many reading threads.
37        /// </para>
38        /// </remarks>
39        public bool ConsumeInOrder;
40        internal new uint flags {
41            get {
42                uint ret = base.flags;
43                ret |= ConsumeInOrder ? Internal.DbConstants.DB_INORDER : 0;
44                return ret;
45            }
46        }
47
48        /// <summary>
49        /// The policy for how to handle database creation.
50        /// </summary>
51        /// <remarks>
52        /// If the database does not already exist and
53        /// <see cref="CreatePolicy.NEVER"/> is set,
54        /// <see cref="QueueDatabase.Open"/> will fail.
55        /// </remarks>
56        public CreatePolicy Creation;
57        internal new uint openFlags {
58            get {
59                uint flags = base.openFlags;
60                flags |= (uint)Creation;
61                return flags;
62            }
63        }
64
65        /// <summary>
66        /// A function to call after the record number has been selected but
67        /// before the data has been stored into the database.
68        /// </summary>
69        /// <remarks>
70        /// <para>
71        /// When using <see cref="QueueDatabase.Append"/>, it may be useful to
72        /// modify the stored data based on the generated key. If a delegate is
73        /// specified, it will be called after the record number has been
74        /// selected, but before the data has been stored.
75        /// </para>
76        /// </remarks>
77        public AppendRecordDelegate Append;
78
79        private uint len;
80        /// <summary>
81        /// Specify the length of records in the database.
82        /// </summary>
83        /// <remarks>
84        /// <para>
85        /// The record length must be enough smaller than
86        /// <see cref="DatabaseConfig.PageSize"/> that at least one record plus
87        /// the database page's metadata information can fit on each database
88        /// page.
89        /// </para>
90        /// <para>
91        /// Any records added to the database that are less than Length bytes
92        /// long are automatically padded (see <see cref="PadByte"/> for more
93        /// information).
94        /// </para>
95        /// <para>
96        /// Any attempt to insert records into the database that are greater
97        /// than Length bytes long will cause the call to fail immediately and
98        /// return an error.
99        /// </para>
100        /// <para>
101        /// If the database already exists, this setting will be ignored.
102        /// </para>
103        /// </remarks>
104        public uint Length {
105            get { return len; }
106            set { len = value; }
107        }
108
109        internal bool padIsSet;
110        private int pad;
111        /// <summary>
112        /// The padding character for short, fixed-length records.
113        /// </summary>
114        /// <remarks>
115        /// <para>
116        /// If no pad character is specified, space characters (that is, ASCII
117        /// 0x20) are used for padding.
118        /// </para>
119        /// <para>
120        /// If the database already exists, this setting will be ignored.
121        /// </para>
122        /// </remarks>
123        public int PadByte {
124            get { return pad; }
125            set {
126                padIsSet = true;
127                pad = value;
128            }
129        }
130
131        internal bool extentIsSet;
132        private uint extentSz;
133        /// <summary>
134        /// The size of the extents used to hold pages in a
135        /// <see cref="QueueDatabase"/>, specified as a number of pages.
136        /// </summary>
137        /// <remarks>
138        /// <para>
139        /// Each extent is created as a separate physical file. If no extent
140        /// size is set, the default behavior is to create only a single
141        /// underlying database file.
142        /// </para>
143        /// <para>
144        /// For information on tuning the extent size, see Selecting a extent
145        /// size in the Programmer's Reference Guide.
146        /// </para>
147        /// <para>
148        /// If the database already exists, this setting will be ignored.
149        /// </para>
150        /// </remarks>
151        public uint ExtentSize {
152            get { return extentSz; }
153            set {
154                extentIsSet = true;
155                extentSz = value;
156            }
157        }
158
159        /// <summary>
160        /// Instantiate a new QueueDatabaseConfig object
161        /// </summary>
162        public QueueDatabaseConfig() {
163            ConsumeInOrder = false;
164            Append = null;
165            padIsSet = false;
166            extentIsSet = false;
167            Creation = CreatePolicy.NEVER;
168        }
169    }
170}