//-----------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.
// 
//-----------------------------------------------------------------------
namespace Microsoft.Isam.Esent.Interop.Windows8
{
    using System;
    using Microsoft.Isam.Esent.Interop.Vista;
    using Win81 = Microsoft.Isam.Esent.Interop.Windows81;
    /// 
    /// Api calls introduced in Windows 8.
    /// 
    public static class Windows8Api
    {
        #region Init / Term
        /// 
        /// Prepares an instance for termination. Can also be used to resume a previous quiescing.
        /// 
        /// The (running) instance to use.
        /// The options to stop or resume the instance.
        public static void JetStopServiceInstance2(
            JET_INSTANCE instance,
            StopServiceGrbit grbit)
        {
            Api.Check(Api.Impl.JetStopServiceInstance2(instance, grbit));
        }
        #endregion
        #region Transactions
        /// 
        /// Causes a session to enter a transaction or create a new save point in an existing
        /// transaction.
        /// 
        /// The session to begin the transaction for.
        /// An optional identifier supplied by the user for identifying the transaction.
        /// Transaction options.
        /// Introduced in Windows 8.
        public static void JetBeginTransaction3(JET_SESID sesid, long userTransactionId, BeginTransactionGrbit grbit)
        {
            Api.Check(Api.Impl.JetBeginTransaction3(sesid, userTransactionId, grbit));
        }
        /// 
        /// Commits the changes made to the state of the database during the current save point
        /// and migrates them to the previous save point. If the outermost save point is committed
        /// then the changes made during that save point will be committed to the state of the
        /// database and the session will exit the transaction.
        /// 
        /// The session to commit the transaction for.
        /// Commit options.
        /// Duration to commit lazy transaction.
        /// Commit-id associated with this commit record.
        public static void JetCommitTransaction2(
            JET_SESID sesid,
            CommitTransactionGrbit grbit,
            TimeSpan durableCommit,
            out JET_COMMIT_ID commitId)
        {
            Api.Check(Api.Impl.JetCommitTransaction2(sesid, grbit, durableCommit, out commitId));
        }
        #endregion
        /// 
        /// Gets extended information about an error.
        /// 
        /// The error code about which to retrieve information.
        /// Information about the specified error code.
        public static void JetGetErrorInfo(
            JET_err error,
            out JET_ERRINFOBASIC errinfo)
        {
            Api.Check(Api.Impl.JetGetErrorInfo(error, out errinfo));
        }
        /// 
        /// Resizes a currently open database. Windows 8: Only supports growing a database file.
        /// Windows 8.1: When  is set to
        /// , and if the
        /// file system supports Sparse Files, then space may be freed up in the middle of the
        /// file.
        /// 
        /// 
        /// Many APIs return the logical size of the file, not how many bytes it takes up on disk.
        /// Win32's GetCompressedFileSize returns the correct on-disk size.
        /// 
        /// returns the on-disk size when used with
        /// 
        /// 
        /// The session to use.
        /// The database to grow.
        /// The desired size of the database, in pages.
        /// The size of the database, in pages, after the call. 
        /// Resize options.
        public static void JetResizeDatabase(
            JET_SESID sesid, 
            JET_DBID dbid, 
            int desiredPages, 
            out int actualPages,
            ResizeDatabaseGrbit grbit)
        {
            Api.Check(Api.Impl.JetResizeDatabase(sesid, dbid, desiredPages, out actualPages, grbit));
        }
        #region DDL
        /// 
        /// Creates indexes over data in an ESE database.
        /// 
        /// 
        /// When creating multiple indexes (i.e. with numIndexCreates
        /// greater than 1) this method MUST be called
        /// outside of any transactions and with exclusive access to the
        /// table. The JET_TABLEID returned by "Api.JetCreateTable"
        /// will have exlusive access or the table can be opened for
        /// exclusive access by passing 
        /// to .
        /// 
        /// The session to use.
        /// The table to create the index on.
        /// Array of objects describing the indexes to be created.
        /// Number of index description objects.
        public static void JetCreateIndex4(
            JET_SESID sesid,
            JET_TABLEID tableid,
            JET_INDEXCREATE[] indexcreates,
            int numIndexCreates)
        {
            Api.Check(Api.Impl.JetCreateIndex4(sesid, tableid, indexcreates, numIndexCreates));            
        }
        /// 
        /// Creates a temporary table with a single index. A temporary table
        /// stores and retrieves records just like an ordinary table created
        /// using JetCreateTableColumnIndex. However, temporary tables are
        /// much faster than ordinary tables due to their volatile nature.
        /// They can also be used to very quickly sort and perform duplicate
        /// removal on record sets when accessed in a purely sequential manner.
        /// Also see
        /// ,
        /// "Api.JetOpenTempTable2",
        /// .
        /// .
        /// 
        /// 
        /// Use 
        /// for earlier versions of Esent.
        /// 
        /// The session to use.
        /// 
        /// Description of the temporary table to create on input. After a
        /// successful call, the structure contains the handle to the temporary
        /// table and column identifications. Use 
        /// to free the temporary table when finished.
        /// 
        public static void JetOpenTemporaryTable2(JET_SESID sesid, JET_OPENTEMPORARYTABLE temporarytable)
        {
            Api.Check(Api.Impl.JetOpenTemporaryTable2(sesid, temporarytable));
        }
        /// 
        /// Creates a table, adds columns, and indices on that table.
        /// 
        /// 
        /// The session to use.
        /// The database to which to add the new table.
        /// Object describing the table to create.
        /// 
        public static void JetCreateTableColumnIndex4(
            JET_SESID sesid,
            JET_DBID dbid,
            JET_TABLECREATE tablecreate)
        {
            Api.Check(Api.Impl.JetCreateTableColumnIndex4(sesid, dbid, tablecreate));
        }
        #endregion
        #region Session Parameters
        /// 
        /// Gets a parameter on the provided session state, used for the lifetime of this session or until reset.
        /// 
        /// The session to set the parameter on.
        /// The ID of the session parameter to set, see
        ///  and .
        /// A 32-bit integer to retrieve.
        public static void JetGetSessionParameter(
            JET_SESID sesid,
            JET_sesparam sesparamid,
            out int value)
        {
            Api.Check(Api.Impl.JetGetSessionParameter(sesid, sesparamid, out value));
        }
        /// 
        /// Gets a parameter on the provided session state, used for the lifetime of this session or until reset.
        /// 
        /// The session to set the parameter on.
        /// The ID of the session parameter to set, see
        ///  and .
        /// A byte array to retrieve.
        /// AThe length of the data array.
        /// The actual size of the data field.
        public static void JetGetSessionParameter(
            JET_SESID sesid,
            JET_sesparam sesparamid,
            byte[] data,
            int length,
            out int actualDataSize)
        {
            Api.Check(Api.Impl.JetGetSessionParameter(sesid, sesparamid, data, length, out actualDataSize));
        }
        /// 
        /// Sets a parameter on the provided session state, used for the lifetime of this session or until reset.
        /// 
        /// The session to set the parameter on.
        /// The ID of the session parameter to set.
        /// A 32-bit integer to set.
        public static void JetSetSessionParameter(JET_SESID sesid, JET_sesparam sesparamid, int value)
        {
            Api.Check(Api.Impl.JetSetSessionParameter(sesid, sesparamid, value));
        }
        /// 
        /// Sets a parameter on the provided session state, used for the lifetime of this session or until reset.
        /// 
        /// The session to set the parameter on.
        /// The ID of the session parameter to set.
        /// Data to set in this session parameter.
        /// Size of the data provided.
        public static void JetSetSessionParameter(
            JET_SESID sesid,
            JET_sesparam sesparamid,
            byte[] data,
            int dataSize)
        {
            Api.Check(Api.Impl.JetSetSessionParameter(sesid, sesparamid, data, dataSize));
        }
        #endregion
        #region Misc
        /// 
        /// If the records with the specified key ranges are not in the buffer
        /// cache, then start asynchronous reads to bring the records into the
        /// database buffer cache.
        /// 
        /// The session to use.
        /// The table to issue the prereads against.
        /// The key ranges to preread.
        /// The index of the first key range in the array to read.
        /// The maximum number of key ranges to preread.
        /// Returns the number of keys actually preread.
        /// List of column ids for long value columns to preread.
        /// Preread options. Used to specify the direction of the preread.
        /// true if some preread done, false otherwise.
        public static bool JetTryPrereadIndexRanges(
            JET_SESID sesid,
            JET_TABLEID tableid,
            JET_INDEX_RANGE[] indexRanges,
            int rangeIndex,
            int rangeCount,
            out int rangesPreread,
            JET_COLUMNID[] columnsPreread,
            PrereadIndexRangesGrbit grbit)
        {
            JET_err err = (JET_err)Api.Impl.JetPrereadIndexRanges(sesid, tableid, indexRanges, rangeIndex, rangeCount, out rangesPreread, columnsPreread, grbit);
            return err >= JET_err.Success;
        }
        /// 
        /// If the records with the specified key ranges are not in the buffer
        /// cache, then start asynchronous reads to bring the records into the
        /// database buffer cache.
        /// 
        /// The session to use.
        /// The table to issue the prereads against.
        /// The key ranges to preread.
        /// The index of the first key range in the array to read.
        /// The maximum number of key ranges to preread.
        /// Returns the number of keys actually preread.
        /// List of column ids for long value columns to preread.
        /// Preread options. Used to specify the direction of the preread.
        public static void JetPrereadIndexRanges(
            JET_SESID sesid,
            JET_TABLEID tableid,
            JET_INDEX_RANGE[] indexRanges,
            int rangeIndex,
            int rangeCount,
            out int rangesPreread,
            JET_COLUMNID[] columnsPreread,
            PrereadIndexRangesGrbit grbit)
        {
            Api.Check(Api.Impl.JetPrereadIndexRanges(sesid, tableid, indexRanges, rangeIndex, rangeCount, out rangesPreread, columnsPreread, grbit));
        }
        /// 
        /// If the records with the specified key ranges are not in the
        /// buffer cache then start asynchronous reads to bring the records
        /// into the database buffer cache.
        /// 
        /// The session to use.
        /// The table to issue the prereads against.
        /// The start of key ranges to preread.
        /// The lengths of the start keys to preread.
        /// The end of key rangess to preread.
        /// The lengths of the end keys to preread.
        /// The index of the first key range in the array to read.
        /// The maximum number of key ranges to preread.
        /// Returns the number of keys actually preread.
        /// List of column ids for long value columns to preread.
        /// Preread options. Used to specify the direction of the preread.
        public static void PrereadKeyRanges(
            JET_SESID sesid,
            JET_TABLEID tableid,
            byte[][] keysStart,
            int[] keyStartLengths,
            byte[][] keysEnd,
            int[] keyEndLengths,
            int rangeIndex,
            int rangeCount,
            out int rangesPreread,
            JET_COLUMNID[] columnsPreread,
            PrereadIndexRangesGrbit grbit)
        {
            Api.Check(Api.Impl.JetPrereadKeyRanges(sesid, tableid, keysStart, keyStartLengths, keysEnd, keyEndLengths, rangeIndex, rangeCount, out rangesPreread, columnsPreread, grbit));
        }
        /// 
        /// Set an array of simple filters for .
        /// 
        /// The session to use for the call.
        /// The cursor to position.
        /// Simple record filters.
        /// Move options.
        public static void JetSetCursorFilter(JET_SESID sesid, JET_TABLEID tableid, JET_INDEX_COLUMN[] filters, CursorFilterGrbit grbit)
        {
            Api.Check(Api.Impl.JetSetCursorFilter(sesid, tableid, filters, grbit));
        }
        #endregion
    }
}