summaryrefslogtreecommitdiff
path: root/csharp/BaseCursor.cs
blob: 2eaa0fbf77e5531e9fdf4cf5ef65fca1cadf7f9b (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009 Oracle.  All rights reserved.
 *
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using BerkeleyDB.Internal;

namespace BerkeleyDB {
    /// <summary>
    /// <para>
    /// The abstract base class from which all cursor classes inherit.
    /// </para>
    /// <para>
    /// Cursors may span threads, but only serially, that is, the application
    /// must serialize access to the cursor handle.
    /// </para>
    /// </summary>
    public abstract class BaseCursor :
        IDisposable, IEnumerable<KeyValuePair<DatabaseEntry, DatabaseEntry>> {
        /// <summary>
        /// The underlying DBC handle
        /// </summary>
        internal DBC dbc;
        private bool isOpen;
        static internal DBC getDBC(BaseCursor curs) {
            return curs == null ? null : curs.dbc;
        }

        internal BaseCursor(DBC dbc) {
            this.dbc = dbc;
            isOpen = true;
        }

        /// <summary>
        /// Compare this cursor's position to another's.
        /// </summary>
        /// <param name="compareTo">The cursor with which to compare.</param>
        /// <returns>
        /// True if both cursors point to the same item, false otherwise.
        /// </returns>
        public bool Compare(Cursor compareTo) {
            int ret = 0;
            dbc.cmp(compareTo.dbc, ref ret, 0);
            return (ret == 0);
        }

        /// <summary>
        /// Returns a count of the number of data items for the key to which the
        /// cursor refers. 
        /// </summary>
        /// <returns>
        /// A count of the number of data items for the key to which the cursor
        /// refers.
        /// </returns>
        public uint Count() {
            int ret;
            uint count = 0;
            ret = dbc.count(ref count, 0);
            return count;
        }

        /// <summary>
        /// <para>
        /// Discard the cursor.
        /// </para>
        /// <para>
        /// It is possible for the Close() method to throw a
        /// <see cref="DeadlockException"/>, signaling that any enclosing
        /// transaction should be aborted. If the application is already
        /// intending to abort the transaction, this error should be ignored,
        /// and the application should proceed.
        /// </para>
        /// <para>
        /// After Close has been called, regardless of its result, the object
        /// may not be used again. 
        /// </para>
        /// </summary>
        /// <exception cref="DeadlockException"></exception>
        public void Close() {
            dbc.close();

            isOpen = false;
        }

        /// <summary>
        /// Release the resources held by this object, and close the cursor if
        /// it's still open.
        /// </summary>
        public void Dispose() {
            try {
                if (isOpen)
                    Close();
            } catch {
                /* 
                 * Errors here are likely because our db has been closed out
                 * from under us.  Not much we can do, just move on. 
                 */
            }
            dbc.Dispose();
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// <para>
        /// Delete the key/data pair to which the cursor refers.
        /// </para>
        /// <para>
        /// When called on a SecondaryCursor, delete the key/data pair from the
        /// primary database and all secondary indices.
        /// </para>
        /// <para>
        /// The cursor position is unchanged after a delete, and subsequent
        /// calls to cursor functions expecting the cursor to refer to an
        /// existing key will fail.
        /// </para>
        /// </summary>
        /// <exception cref="KeyEmptyException">
        /// Thrown if the element has already been deleted.
        /// </exception>
        public void Delete() {
            dbc.del(0);
        }
        IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
        /// <summary>
        /// Returns an enumerator that iterates through the cursor.
        /// </summary>
        /// <returns>An enumerator for the cursor.</returns>
        public virtual IEnumerator<KeyValuePair<DatabaseEntry, DatabaseEntry>>
            GetEnumerator() { return null; }
    }
}