📄 fastdbconnection.cs
字号:
using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace FastDbNet
{
//-------------------------------------------------------------------------
public class FastDbConnection: IDisposable {
public static readonly int DefaultInitDatabaseSize = 4*1024*1024; // Default initial db size (number of objects)
public static readonly int DefaultInitIndexSize = 512*1024; // Default initial index size (number of objects)
public static readonly int DefaultExtensionQuantum = 4*1024*1024; // Quantum of extension of allocated memory
public static readonly int MaxParallelSearchThreads = 64; // Maximal number of threads which can be spawned to perform parallel sequentila search
public static readonly int DefaultDatabasePort = 6010;
public static readonly int DefReconnectTimeoutSec = 120; // Reconnect timeout seconds
/// <summary>
/// Create a FastDb connection (without opening a database).
/// </summary>
/// <param name="DatabaseName">Database name</param>
public FastDbConnection(string DatabaseName) {
this.dbName = DatabaseName;
this.dbPath = DatabaseName + ".fdb";
}
/// <summary>
/// Destroy FastDb connection, close the database, and free session resources.
/// </summary>
~FastDbConnection() { Dispose(false); }
/// <summary>
/// Create table given its structure.
/// </summary>
/// <param name="TableName">Table name</param>
/// <param name="fields">Table fields</param>
/// <returns>Return code (int)CLI.ErrorCode</returns>
public int CreateTable(string TableName, FastDbFields fields) {
CLI.CliFieldDescriptor[] flds = (CLI.CliFieldDescriptor[])Array.CreateInstance(typeof(CLI.CliFieldDescriptor), fields.Count);
//(CLI.CliFieldDescriptor[])aFlds.ToArray(typeof(CLI.CliFieldDescriptor));
for(int i=0; i<fields.Count; i++) {
flds[i].type = fields[i].Type;
flds[i].flags = fields[i].Flags;
flds[i].name = Marshal.StringToHGlobalAnsi(fields[i].Name);
flds[i].refTableName = Marshal.StringToHGlobalAnsi(fields[i].RefTable);
flds[i].inverseRefFieldName = Marshal.StringToHGlobalAnsi(fields[i].InvRefField);
}
int rc = CLI.cli_create_table(session, TableName, fields.Count, flds);
for(int i=0; i < fields.Count; i++) {
Marshal.FreeCoTaskMem(flds[i].name);
Marshal.FreeCoTaskMem(flds[i].refTableName);
Marshal.FreeCoTaskMem(flds[i].inverseRefFieldName);
}
if (rc < 0 && rc != (int)CLI.ErrorCode.cli_table_already_exists) CLI.CliCheck(rc);
return rc;
}
/// <summary>
/// Name of the database
/// </summary>
public string DatabaseName { get { return dbName; } set { CheckConnection(false); dbName = value; } }
/// <summary>
/// Path to the database file.
/// </summary>
public string DatabasePath { get { return dbPath; } set { CheckConnection(false); dbPath = value; } }
/// <summary>
/// Initial database size.
/// </summary>
public int InitDbSize { get { return initDbSize; } set { CheckConnection(false); initDbSize = value; } }
/// <summary>
/// Initial database index size.
/// </summary>
public int InitIdxSize { get { return initIdxSize; } set { CheckConnection(false); initIdxSize = value; } }
/// <summary>
/// Memory extention quantum size
/// </summary>
public int ExtensionQuantum { get { return extQuantum; } set { CheckConnection(false); extQuantum = value; } }
/// <summary>
/// Maximum allowed size of the database file. 0 = unlimited.
/// </summary>
public int FileSizeLimit { get { return fileSizeLimit; } set { CheckConnection(false); fileSizeLimit = value; } }
/// <summary>
/// Number of attempts to establish connection
/// </summary>
public int MaxConnectRetries { get { return maxConnectRetries; } set { CheckConnection(false); maxConnectRetries = value; } }
/// <summary>
/// Timeout in seconds between connection attempts
/// </summary>
public int ReconnectTimeout { get { return reconnectTimeout; } set { CheckConnection(false); reconnectTimeout = value; } }
/// <summary>
/// If true, Open() creates a replicated node. Defaults to false.
/// </summary>
public bool EnableReplication { get { return enableReplication; } set { CheckConnection(false); enableReplication = value; } }
/// <summary>
/// Trasnaction commit delay (specify 0 to disable).
/// </summary>
public uint TransCommitDelay { get { return transCommitDelay; } set { CheckConnection(false); transCommitDelay = value; } }
/// <summary>
/// Node identifier: 0 ... NodeNames.Length (only relevant for a replicated database).
/// </summary>
public int NodeID { get { return nodeID; } set { CheckConnection(false); nodeID = value; } }
/// <summary>
/// Names of the replicated nodes (only relevant for a replicated database).
/// </summary>
public string[] NodeNames { get { return nodeNames; } set { CheckConnection(false); nodeNames = value; } }
/// <summary>
/// Internal session handle
/// </summary>
public int Session { get { return session; } }
/// <summary>
/// Controls automated calls to Attach()/Detach() methods. Disabled by default.
/// </summary>
public bool Threaded { get { return threaded; } set { threaded = value; } }
/// <summary>
/// Attributes used to open database. <seealso cref="CLI.CliOpenAttribute"/>
/// </summary>
public CLI.CliOpenAttribute OpenAttributes {
get { return openAttributes; }
set { CheckConnection(false); openAttributes = value; }
}
/// <summary>
/// Open local database.
/// </summary>
public void Open() { this.Open(true, "", 0); }
public void Open(string Host, int Port) { this.Open(false, Host, Port); }
/// <summary>
/// Commit transaction and write changed data to disk.
/// </summary>
public void Commit() { CLI.CliCheck(CLI.cli_commit(session)); }
/// <summary>
/// Commit transaction without writing changed data to disk.
/// </summary>
public void PreCommit() { CLI.CliCheck(CLI.cli_precommit(session)); }
/// <summary>
/// Roolback current transaction.
/// </summary>
public void Rollback() { CLI.CliCheck(CLI.cli_abort(session)); }
/// <summary>
/// Close database connection.
/// </summary>
public void Close()
{
for(int i=commands.Count-1; i >= 0; --i)
((FastDbCommand)commands[i]).Free();
CLI.CliCheck(CLI.cli_close(session));
session = -1;
}
/// <summary>
/// List tables in the database.
/// </summary>
/// <returns>A string array of table names</returns>
public unsafe string[] ListTables() {
bool dummy = false;
return ListTables("", ref dummy);
}
/// <summary>
/// Checks if a table exists in the database.
/// </summary>
/// <param name="TableName">Name of the table to check for existence</param>
/// <returns>true - table exists.</returns>
public bool TableExists(string TableName) {
bool exists = false;
ListTables(TableName, ref exists);
return exists;
}
public FastDbFields DescribeTable(string TableName) {
return this.DescribeTable(TableName, true); }
/// <summary>
/// Describes a table given its name.
/// </summary>
/// <param name="TableName">Name of the table to describe</param>
/// <param name="RaiseError">If true, an error check will be performed (default: true).</param>
/// <returns>A collection of fields fetched from the database's table.</returns>
public unsafe FastDbFields DescribeTable(string TableName, bool RaiseError) {
FastDbFields fields = new FastDbFields();
void* p = null;
int rc = CLI.cli_describe(session, TableName, ref p);
if (RaiseError) CLI.CliCheck(rc);
if (rc > 0) {
try {
CLI.CliFieldDescriptor* fld = (CLI.CliFieldDescriptor*)p;
for(int i=0; i<rc; i++, fld++) {
Debug.Assert(fld->name != IntPtr.Zero, "Field name is a null pointer!");
string s = Marshal.PtrToStringAnsi(fld->name);
string sr = (fld->refTableName == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(fld->refTableName);
string si = (fld->inverseRefFieldName == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(fld->inverseRefFieldName);
fields.Add(s, fld->type, fld->flags, sr, si);
}
}
finally {
CLI.cli_free_memory(session, p);
}
}
return fields;
}
/// <summary>
/// Drop a table from the database
/// </summary>
/// <param name="TableName">Name of the table</param>
public void DropTable(string TableName) {
CLI.CliCheck(CLI.cli_drop_table(session, TableName));
}
/// <summary>
/// Alter index on a field
/// </summary>
/// <param name="TableName">Name of the table</param>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -