/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2011, 2015 Oracle and/or its affiliates. All rights reserved.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using BerkeleyDB.Internal;
namespace BerkeleyDB {
///
/// A class representing a HeapDatabase.
///
public class HeapDatabase : Database {
#region Constructors
private HeapDatabase(DatabaseEnvironment env, uint flags)
: base(env, flags) { }
internal HeapDatabase(BaseDatabase clone) : base(clone) { }
private void Config(HeapDatabaseConfig cfg) {
base.Config(cfg);
/*
* Database.Config calls set_flags, but that does not get the Heap
* specific flags. No harm in calling it again.
*/
db.set_flags(cfg.flags);
if (cfg.BlobDir != null && cfg.Env == null)
db.set_blob_dir(cfg.BlobDir);
if (cfg.blobThresholdIsSet)
db.set_blob_threshold(cfg.BlobThreshold, 0);
if (cfg.maxSizeIsSet)
db.set_heapsize(cfg.MaxSizeGBytes, cfg.MaxSizeBytes);
if (cfg.regionszIsSet)
db.set_heap_regionsize(cfg.RegionSize);
}
///
/// Instantiate a new HeapDatabase object and open the database
/// represented by .
///
///
///
/// If is null, the database is strictly
/// temporary and cannot be opened by any other thread of control, thus
/// the database can only be accessed by sharing the single database
/// object that created it, in circumstances where doing so is safe.
///
///
/// If is set, the operation
/// is implicitly transaction protected. Transactionally
/// protected operations on a database object requires the object itself
/// be transactionally protected during its open.
///
///
///
/// The name of an underlying file used to back up the
/// database. In-memory databases never intended to be preserved on disk
/// may be created by setting this parameter to null.
///
/// The database's configuration
/// A new, open database object
public static HeapDatabase Open(
string Filename, HeapDatabaseConfig cfg) {
return Open(Filename, cfg, null);
}
///
/// Instantiate a new HeapDatabase object and open the database
/// represented by .
///
///
///
/// If is null, the database is strictly
/// temporary and cannot be opened by any other thread of control, thus
/// the database can only be accessed by sharing the single database
/// object that created it, in circumstances where doing so is safe.
///
///
/// If is null, but
/// is set, the operation
/// is implicitly transaction protected. Transactionally
/// protected operations on a database object requires the object itself
/// be transactionally protected during its open. The
/// transaction must be committed before the object is closed.
///
///
///
/// The name of an underlying file used to back up the
/// database. In-memory databases never intended to be preserved on disk
/// may be created by setting this parameter to null.
///
/// The database's configuration
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
/// A new, open database object
public static HeapDatabase Open(
string Filename, HeapDatabaseConfig cfg, Transaction txn) {
HeapDatabase ret = new HeapDatabase(cfg.Env, 0);
ret.Config(cfg);
ret.db.open(Transaction.getDB_TXN(txn),
Filename, null, DBTYPE.DB_HEAP, cfg.openFlags, 0);
ret.isOpen = true;
return ret;
}
#endregion Constructors
#region Properties
///
/// The path of the directory where blobs are stored.
///
public string BlobDir {
get {
string dir;
db.get_blob_dir(out dir);
return dir;
}
}
internal string BlobSubDir {
get {
string dir;
db.get_blob_sub_dir(out dir);
return dir;
}
}
///
/// The threshold value in bytes beyond which data items are stored as
/// blobs.
///
/// Any data item that is equal to or larger in size than the
/// threshold value is automatically stored as a blob.
///
///
/// A value of 0 indicates that blobs are not used by the database.
///
///
public uint BlobThreshold {
get {
uint ret = 0;
db.get_blob_threshold(ref ret);
return ret;
}
}
///
/// The gigabytes component of the maximum on-disk database file size.
///
public uint MaxSizeGBytes {
get {
uint gbytes = 0;
uint bytes = 0;
db.get_heapsize(ref gbytes, ref bytes);
return gbytes;
}
}
///
/// The bytes component of the maximum on-disk database file size.
///
public uint MaxSizeBytes {
get {
uint gbytes = 0;
uint bytes = 0;
db.get_heapsize(ref gbytes, ref bytes);
return bytes;
}
}
///
/// The number of pages in a region of the database. It is set by
/// when the heap database
/// is opened.
///
public uint RegionSize {
get {
uint ret = 0;
db.get_heap_regionsize(ref ret);
return ret;
}
}
#endregion Properties
#region Methods
///
/// Append the data item to the end of the database.
///
/// The data item to store in the database
/// The heap record id allocated to the record
public HeapRecordId Append(DatabaseEntry data) {
return Append(data, null);
}
///
/// Append the data item to the end of the database.
///
/// The data item to store in the database
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
/// The heap record id allocated to the record
public HeapRecordId Append(DatabaseEntry data, Transaction txn) {
DatabaseEntry key = new DatabaseEntry();
Put(key, data, txn, DbConstants.DB_APPEND);
return HeapRecordId.fromArray(key.Data);
}
///
/// Return the database statistical information which does not require
/// traversal of the database.
///
///
/// The database statistical information which does not require
/// traversal of the database.
///
public HeapStats FastStats() {
return Stats(null, true, Isolation.DEGREE_THREE);
}
///
/// Return the database statistical information which does not require
/// traversal of the database.
///
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
///
/// The database statistical information which does not require
/// traversal of the database.
///
public HeapStats FastStats(Transaction txn) {
return Stats(txn, true, Isolation.DEGREE_THREE);
}
///
/// Return the database statistical information which does not require
/// traversal of the database.
///
///
///
/// Among other things, this method makes it possible for applications
/// to request key and record counts without incurring the performance
/// penalty of traversing the entire database.
///
///
/// The statistical information is described by the
/// , ,
/// , , and
/// classes.
///
///
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
///
/// The level of isolation for database reads.
/// is silently ignored for
/// databases which did not specify
/// .
///
///
/// The database statistical information which does not require
/// traversal of the database.
///
public HeapStats FastStats(Transaction txn, Isolation isoDegree) {
return Stats(txn, true, isoDegree);
}
///
/// Return the database statistical information for this database.
///
/// Database statistical information.
public HeapStats Stats() {
return Stats(null, false, Isolation.DEGREE_THREE);
}
///
/// Return the database statistical information for this database.
///
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
/// Database statistical information.
public HeapStats Stats(Transaction txn) {
return Stats(txn, false, Isolation.DEGREE_THREE);
}
///
/// Return the database statistical information for this database.
///
///
/// The statistical information is described by
/// .
///
///
/// If the operation is part of an application-specified transaction,
/// is a Transaction object returned from
/// ; if
/// the operation is part of a Berkeley DB Concurrent Data Store group,
/// is a handle returned from
/// ; otherwise null.
///
///
/// The level of isolation for database reads.
/// is silently ignored for
/// databases which did not specify
/// .
///
/// Database statistical information.
public HeapStats Stats(Transaction txn, Isolation isoDegree) {
return Stats(txn, false, isoDegree);
}
private HeapStats Stats(Transaction txn, bool fast, Isolation isoDegree) {
uint flags = 0;
flags |= fast ? DbConstants.DB_FAST_STAT : 0;
switch (isoDegree) {
case Isolation.DEGREE_ONE:
flags |= DbConstants.DB_READ_UNCOMMITTED;
break;
case Isolation.DEGREE_TWO:
flags |= DbConstants.DB_READ_COMMITTED;
break;
}
HeapStatStruct st = db.stat_heap(Transaction.getDB_TXN(txn), flags);
return new HeapStats(st);
}
#endregion Methods
}
}