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 representing a SecondaryQueueDatabase. The Queue format supports
15    /// fast access to fixed-length records accessed sequentially or by logical
16    /// record number.
17    /// </summary>
18    public class SecondaryQueueDatabase : SecondaryDatabase {
19
20        #region Constructors
21        internal SecondaryQueueDatabase(DatabaseEnvironment env, uint flags)
22            : base(env, flags) { }
23
24        private void Config(SecondaryQueueDatabaseConfig cfg) {
25            base.Config((SecondaryDatabaseConfig)cfg);
26            db.set_flags(cfg.flags);
27            if (cfg.lengthIsSet)
28                db.set_re_len(cfg.Length);
29            if (cfg.padIsSet)
30                db.set_re_pad(cfg.PadByte);
31            if (cfg.extentIsSet)
32                db.set_q_extentsize(cfg.ExtentSize);
33        }
34
35        /// <summary>
36        /// Instantiate a new SecondaryQueueDatabase object, open the
37        /// database represented by <paramref name="Filename"/> and associate
38        /// the database with the
39        /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
40        /// </summary>
41        /// <remarks>
42        /// <para>
43        /// If <paramref name="Filename"/> is null, the database is strictly
44        /// temporary and cannot be opened by any other thread of control, thus
45        /// the database can only be accessed by sharing the single database
46        /// object that created it, in circumstances where doing so is safe.
47        /// </para>
48        /// <para>
49        /// If <see cref="DatabaseConfig.AutoCommit"/> is set, the operation
50        /// will be implicitly transaction protected. Note that transactionally
51        /// protected operations on a datbase object requires the object itself
52        /// be transactionally protected during its open.
53        /// </para>
54        /// </remarks>
55        /// <param name="Filename">
56        /// The name of an underlying file that will be used to back the
57        /// database. In-memory databases never intended to be preserved on disk
58        /// may be created by setting this parameter to null.
59        /// </param>
60        /// <param name="cfg">The database's configuration</param>
61        /// <returns>A new, open database object</returns>
62        public static SecondaryQueueDatabase Open(
63            string Filename, SecondaryQueueDatabaseConfig cfg) {
64            return Open(Filename, cfg, null);
65        }
66        /// <summary>
67        /// Instantiate a new SecondaryQueueDatabase object, open the
68        /// database represented by <paramref name="Filename"/> and associate
69        /// the database with the
70        /// <see cref="SecondaryDatabaseConfig.Primary">primary index</see>.
71        /// </summary>
72        /// <remarks>
73        /// <para>
74        /// If <paramref name="Filename"/> is null, the database is strictly
75        /// temporary and cannot be opened by any other thread of control, thus
76        /// the database can only be accessed by sharing the single database
77        /// object that created it, in circumstances where doing so is safe.
78        /// </para>
79        /// <para>
80        /// If <paramref name="txn"/> is null, but
81        /// <see cref="DatabaseConfig.AutoCommit"/> is set, the operation will
82        /// be implicitly transaction protected. Note that transactionally
83        /// protected operations on a datbase object requires the object itself
84        /// be transactionally protected during its open. Also note that the
85        /// transaction must be committed before the object is closed.
86        /// </para>
87        /// </remarks>
88        /// <param name="Filename">
89        /// The name of an underlying file that will be used to back the
90        /// database. In-memory databases never intended to be preserved on disk
91        /// may be created by setting this parameter to null.
92        /// </param>
93        /// <param name="cfg">The database's configuration</param>
94        /// <param name="txn">
95        /// If the operation is part of an application-specified transaction,
96        /// <paramref name="txn"/> is a Transaction object returned from
97        /// <see cref="DatabaseEnvironment.BeginTransaction"/>; if
98        /// the operation is part of a Berkeley DB Concurrent Data Store group,
99        /// <paramref name="txn"/> is a handle returned from
100        /// <see cref="DatabaseEnvironment.BeginCDSGroup"/>; otherwise null.
101        /// </param>
102        /// <returns>A new, open database object</returns>
103        public static SecondaryQueueDatabase Open(string Filename,
104            SecondaryQueueDatabaseConfig cfg, Transaction txn) {
105            SecondaryQueueDatabase ret = new SecondaryQueueDatabase(cfg.Env, 0);
106            ret.Config(cfg);
107            ret.db.open(Transaction.getDB_TXN(txn),
108                Filename, null, DBTYPE.DB_QUEUE, cfg.openFlags, 0);
109            ret.isOpen = true;
110            ret.doAssocRef =
111                new BDB_AssociateDelegate(SecondaryDatabase.doAssociate);
112            cfg.Primary.db.associate(Transaction.getDB_TXN(null),
113                ret.db, ret.doAssocRef, cfg.assocFlags);
114
115            if (cfg.ForeignKeyDatabase != null) {
116                if (cfg.OnForeignKeyDelete == ForeignKeyDeleteAction.NULLIFY)
117                    ret.doNullifyRef =
118                        new BDB_AssociateForeignDelegate(doNullify);
119                else
120                    ret.doNullifyRef = null;
121                cfg.ForeignKeyDatabase.db.associate_foreign(
122                    ret.db, ret.doNullifyRef, cfg.foreignFlags);
123            }
124            return ret;
125        }
126        #endregion Constructors
127
128        #region Properties
129        /// <summary>
130        /// The size of the extents used to hold pages in a
131        /// <see cref="QueueDatabase"/>, specified as a number of pages.
132        /// </summary>
133        public uint ExtentSize {
134            get {
135                uint ret = 0;
136                db.get_q_extentsize(ref ret);
137                return ret;
138            }
139        }
140        /// <summary>
141        /// The length of records in the database.
142        /// </summary>
143        public uint Length {
144            get {
145                uint ret = 0;
146                db.get_re_len(ref ret);
147                return ret;
148            }
149        }
150        /// <summary>
151        /// The padding character for short, fixed-length records.
152        /// </summary>
153        public int PadByte {
154            get {
155                int ret = 0;
156                db.get_re_pad(ref ret);
157                return ret;
158            }
159        }
160        #endregion Properties
161    }
162}
163