/*-
* 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 {
///
///
/// The abstract base class from which all cursor classes inherit.
///
///
/// Cursors may span threads, but only serially, that is, the application
/// must serialize access to the cursor handle.
///
///
public abstract class BaseCursor :
IDisposable, IEnumerable> {
///
/// The underlying DBC handle
///
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;
}
///
/// Compare this cursor's position to another's.
///
/// The cursor with which to compare.
///
/// True if both cursors point to the same item, false otherwise.
///
public bool Compare(Cursor compareTo) {
int ret = 0;
dbc.cmp(compareTo.dbc, ref ret, 0);
return (ret == 0);
}
///
/// Returns a count of the number of data items for the key to which the
/// cursor refers.
///
///
/// A count of the number of data items for the key to which the cursor
/// refers.
///
public uint Count() {
int ret;
uint count = 0;
ret = dbc.count(ref count, 0);
return count;
}
///
///
/// Discard the cursor.
///
///
/// It is possible for the Close() method to throw a
/// , 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.
///
///
/// After Close has been called, regardless of its result, the object
/// may not be used again.
///
///
///
public void Close() {
dbc.close();
isOpen = false;
}
///
/// Release the resources held by this object, and close the cursor if
/// it's still open.
///
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);
}
///
///
/// Delete the key/data pair to which the cursor refers.
///
///
/// When called on a SecondaryCursor, delete the key/data pair from the
/// primary database and all secondary indices.
///
///
/// 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.
///
///
///
/// Thrown if the element has already been deleted.
///
public void Delete() {
dbc.del(0);
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
///
/// Returns an enumerator that iterates through the cursor.
///
/// An enumerator for the cursor.
public virtual IEnumerator>
GetEnumerator() { return null; }
}
}