Beispiel #1
0
        /// <summary>
        /// Compact the database.
        /// </summary>
        private void CompactDatabase()
        {
            string defraggedDatabase = Path.Combine(this.databaseDirectory, "defragged.edb");

            using (var instance = this.CreateInstance())
            {
                instance.Init();
                using (var session = new Session(instance))
                {
                    // For JetCompact to work the database has to be attached, but not opened
                    Api.JetAttachDatabase(session, this.database, AttachDatabaseGrbit.None);
                    if (this.useStatusCallback)
                    {
                        this.statusCallbackWasCalled = false;
                        Api.JetCompact(session, this.database, defraggedDatabase, this.StatusCallback, null, CompactGrbit.None);
                        Assert.IsTrue(
                            this.statusCallbackWasCalled, "expected the status callback to be called during compact");
                    }
                    else
                    {
                        Api.JetCompact(session, this.database, defraggedDatabase, null, null, CompactGrbit.None);
                    }
                }
            }

            Assert.IsTrue(EseInteropTestHelper.FileExists(defraggedDatabase));
            Cleanup.DeleteFileWithRetry(this.database);
            File.Move(defraggedDatabase, this.database);
        }
Beispiel #2
0
        public void TestDeleteFileWithRetryWhenFileDoesNotExist()
        {
            string file = EseInteropTestHelper.PathGetRandomFileName();

            Assert.IsFalse(EseInteropTestHelper.FileExists(file));
            Cleanup.DeleteFileWithRetry(file);
        }
Beispiel #3
0
        public void VerifyDeleteFileWithRetryRemovesFile()
        {
            // Create a random file
            string file = EseInteropTestHelper.PathGetRandomFileName();

            EseInteropTestHelper.FileWriteAllText(file, "hello");
            Assert.IsTrue(EseInteropTestHelper.FileExists(file));

            // Delete the file
            Cleanup.DeleteFileWithRetry(file);

            // The file should no longer exist
            Assert.IsFalse(EseInteropTestHelper.FileExists(file));
        }
Beispiel #4
0
        /// <summary>
        /// Recovery to an alternate path with JetInit3.
        /// </summary>
        private void RecoverAlternatePathWithJetInit3()
        {
            using (var instance = this.CreateInstance())
            {
                instance.Init();
                using (var session = new Session(instance))
                {
                    Api.JetAttachDatabase(session, this.database, AttachDatabaseGrbit.None);
                    JET_DBID dbid;
                    Api.JetOpenDatabase(session, this.database, string.Empty, out dbid, OpenDatabaseGrbit.None);
                }
            }

#if !MANAGEDESENT_ON_WSA // The File model in Windows Store Apps has changed.
            // Delete the database and checkpoint
            File.Delete(this.database);
            File.Delete(Path.Combine(this.databaseDirectory, "edb.chk"));
#endif

            // Recovery to a different database
            string newDatabaseName = this.database + ".moved";
            var    recoveryOptions = new JET_RSTINFO
            {
                crstmap   = 1,
                pfnStatus = this.StatusCallback,
                rgrstmap  = new[]
                {
                    new JET_RSTMAP {
                        szDatabaseName = this.database, szNewDatabaseName = newDatabaseName
                    },
                },
            };

            JET_INSTANCE recoveryInstance;
            Api.JetCreateInstance(out recoveryInstance, "JetInit3");
            Api.JetSetSystemParameter(recoveryInstance, JET_SESID.Nil, JET_param.LogFileSize, 128, null);
            Api.JetSetSystemParameter(recoveryInstance, JET_SESID.Nil, JET_param.LogFilePath, 0, this.databaseDirectory);
            Api.JetSetSystemParameter(recoveryInstance, JET_SESID.Nil, JET_param.TempPath, 0, this.databaseDirectory);
            Api.JetSetSystemParameter(recoveryInstance, JET_SESID.Nil, JET_param.SystemPath, 0, this.databaseDirectory);
            VistaApi.JetInit3(ref recoveryInstance, recoveryOptions, InitGrbit.None);
            Api.JetTerm(recoveryInstance);

#if !MANAGEDESENT_ON_WSA // The File model in Windows Store Apps has changed.
            Assert.IsTrue(EseInteropTestHelper.FileExists(newDatabaseName), "New database ({0}) doesn't exist", newDatabaseName);
            Assert.IsFalse(EseInteropTestHelper.FileExists(this.database), "Old database ({0}) still exists", this.database);
            File.Move(newDatabaseName, this.database);
#endif
        }
        public void CreateDatabaseWithUnicodePath()
        {
            if (!EsentVersion.SupportsUnicodePaths)
            {
                return;
            }

            using (var instance = new Instance("unicodedbcreate"))
            {
                SetupHelper.SetLightweightConfiguration(instance);
                instance.Parameters.CreatePathIfNotExist = true;
                instance.Init();
                using (var session = new Session(instance))
                {
                    JET_DBID dbid;
                    Api.JetCreateDatabase(session, this.database, string.Empty, out dbid, CreateDatabaseGrbit.None);
                    Assert.IsTrue(EseInteropTestHelper.FileExists(this.database));
                }
            }
        }
        public void TestDotNetGuidSortOrder()
        {
            if (!EsentVersion.SupportsWindows8Features)
            {
                return;
            }

            // Create table with GUID column and index over GUID column.
            this.CreatePopulateAndTestTable();

            Api.JetCloseDatabase(this.sesId, this.databaseId, CloseDatabaseGrbit.None);
            Api.JetDetachDatabase(this.sesId, this.database);

#if !MANAGEDESENT_ON_WSA // Not exposed in MSDK
            EseInteropTestHelper.ConsoleWriteLine("Compact database.");
            Api.JetAttachDatabase(this.sesId, this.database, Windows8Grbits.PurgeCacheOnAttach);
            Api.JetCompact(this.sesId, this.database, Path.Combine(this.directory, "defragged.edb"), null, null, CompactGrbit.None);
            Api.JetDetachDatabase(this.sesId, this.database);

            this.database = Path.Combine(this.directory, "defragged.edb");
            Assert.IsTrue(EseInteropTestHelper.FileExists(this.database));
#endif // !MANAGEDESENT_ON_WSA
            Api.JetAttachDatabase(this.sesId, this.database, Windows8Grbits.PurgeCacheOnAttach);

            Api.JetOpenDatabase(this.sesId, this.database, null, out this.databaseId, OpenDatabaseGrbit.None);
            Api.JetOpenTable(this.sesId, this.databaseId, this.tableName, null, 0, OpenTableGrbit.None, out this.tableId);

            Api.JetBeginTransaction(this.sesId);
            EseInteropTestHelper.ConsoleWriteLine("Insert more values in index.");
            for (int i = 0; i < 10000; i++)
            {
                if ((i % 2000) == 0)
                {
                    EseInteropTestHelper.ConsoleWriteLine("Added another 2000 Guids.");
                }

                Api.JetCommitTransaction(this.sesId, CommitTransactionGrbit.None);
                Api.JetBeginTransaction(this.sesId);
                this.InsertRecord(this.tableId, System.Guid.NewGuid());
            }

            Api.JetCommitTransaction(this.sesId, CommitTransactionGrbit.None);
            EseInteropTestHelper.ConsoleWriteLine("Finished inserting more values in index.");

            // validate order after having closed the database and restarted
            Guid guidPrev;
            Guid guidCur;
            Api.JetMove(this.sesId, this.tableId, JET_Move.First, MoveGrbit.None);
            int    bytesRead;
            byte[] data = new byte[16];
            Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, 0, null);
            guidPrev = new System.Guid(data);
            for (int i = 1; i < 10000; i++)
            {
                Api.JetMove(this.sesId, this.tableId, JET_Move.Next, MoveGrbit.None);
                Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, 0, null);
                guidCur = new System.Guid(data);
                Assert.IsTrue(guidCur.CompareTo(guidPrev) > 0);
                guidPrev = guidCur;
            }

            EseInteropTestHelper.ConsoleWriteLine("Validated order.");

            // retrieve newly inserted GUID from index and compare values
            // to test denormalization logic
            Guid guidT = System.Guid.NewGuid();
            EseInteropTestHelper.ConsoleWriteLine("Allocate random GUID...");
            EseInteropTestHelper.ConsoleWriteLine(guidT.ToString());
            this.InsertRecord(this.tableId, guidT);
            Api.JetSetCurrentIndex(this.sesId, this.tableId, this.secIndexName);
            EseInteropTestHelper.ConsoleWriteLine("Guid inserted is....");
            EseInteropTestHelper.ConsoleWriteLine("{0}", guidT);
            byte[] keyArray = guidT.ToByteArray();
            Api.JetMakeKey(this.sesId, this.tableId, keyArray, keyArray.Length, MakeKeyGrbit.NewKey);
            Api.JetSeek(this.sesId, this.tableId, SeekGrbit.SeekEQ);

            Api.JetSetCurrentIndex(this.sesId, this.tableId, this.secIndexName);
            keyArray = guidT.ToByteArray();
            Api.JetMakeKey(this.sesId, this.tableId, keyArray, keyArray.Length, MakeKeyGrbit.NewKey);
            Api.JetSeek(this.sesId, this.tableId, SeekGrbit.SeekEQ);
            JET_wrn err = Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, RetrieveColumnGrbit.RetrieveFromIndex, null);
            Assert.AreEqual(data.Length, bytesRead);
            EseInteropTestHelper.ConsoleWriteLine("Found random GUID in index...");
            Guid guidTT = new System.Guid(data);
            Assert.AreEqual(guidT, guidTT);
            EseInteropTestHelper.ConsoleWriteLine("Found specific GUID in index");

            // check retrieve from index for denormalization
            // by comparing guid inserted.  They should match.
            Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, RetrieveColumnGrbit.RetrieveFromIndex, null);
            guidCur = new System.Guid(data);
            EseInteropTestHelper.ConsoleWriteLine("Retrieved Guid is:");
            EseInteropTestHelper.ConsoleWriteLine(guidCur.ToString());
            Assert.IsTrue(guidCur.CompareTo(guidT) == 0);
            EseInteropTestHelper.ConsoleWriteLine("Retrieve from index matches inserted GUID");

            Api.JetCloseTable(this.sesId, this.tableId);

            this.TestTempTableWithGuidDotNetSortOrder();
        }
        public void ULongLongSortOrder()
        {
            // Create table with ULongLong column and index over ULongLong column.
            if (EsentVersion.SupportsWindows10Features)
            {
                this.CreatePopulateAndTestTable();
            }
            else
            {
                try
                {
                    this.CreatePopulateAndTestTable();
                }
                catch (EsentInvalidColumnTypeException)
                {
                    return;
                }
            }

            Api.JetCloseDatabase(this.sesId, this.databaseId, CloseDatabaseGrbit.None);
            Api.JetDetachDatabase(this.sesId, this.database);

#if !MANAGEDESENT_ON_WSA // Not exposed in MSDK
            EseInteropTestHelper.ConsoleWriteLine("Compact database.");
            Api.JetAttachDatabase(this.sesId, this.database, Windows8Grbits.PurgeCacheOnAttach);
            Api.JetCompact(this.sesId, this.database, Path.Combine(this.directory, "defragged.edb"), null, null, CompactGrbit.None);
            Api.JetDetachDatabase(this.sesId, this.database);

            this.database = Path.Combine(this.directory, "defragged.edb");
            Assert.IsTrue(EseInteropTestHelper.FileExists(this.database));
#endif // !MANAGEDESENT_ON_WSA
            Api.JetAttachDatabase(this.sesId, this.database, Windows8Grbits.PurgeCacheOnAttach);

            Api.JetOpenDatabase(this.sesId, this.database, null, out this.databaseId, OpenDatabaseGrbit.None);
            Api.JetOpenTable(this.sesId, this.databaseId, this.tableName, null, 0, OpenTableGrbit.None, out this.tableId);

            // validate order after having closed the database and restarted
            ulong ulongPrev;
            ulong ulongCur;
            Api.JetMove(this.sesId, this.tableId, JET_Move.First, MoveGrbit.None);
            int    bytesRead;
            byte[] data = new byte[8];
            Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, 0, null);
            ulongPrev = BitConverter.ToUInt64(data, 0);
            for (int i = 1; i < 3; i++)
            {
                Api.JetMove(this.sesId, this.tableId, JET_Move.Next, MoveGrbit.None);
                Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, 0, null);
                ulongCur = BitConverter.ToUInt64(data, 0);
                Assert.IsTrue(ulongCur.CompareTo(ulongPrev) > 0);
                ulongPrev = ulongCur;
            }

            EseInteropTestHelper.ConsoleWriteLine("Validated order.");

            // retrieve newly inserted ULongLong from index and compare values
            // to test denormalization logic
            ulong  ulongT   = 0;
            byte[] keyArray = BitConverter.GetBytes(ulongT);
            Api.JetSetCurrentIndex(this.sesId, this.tableId, this.secIndexName);
            Api.JetMakeKey(this.sesId, this.tableId, keyArray, keyArray.Length, MakeKeyGrbit.NewKey);
            Api.JetSeek(this.sesId, this.tableId, SeekGrbit.SeekEQ);
            JET_wrn err = Api.JetRetrieveColumn(this.sesId, this.tableId, this.columnIdKey1, data, data.Length, out bytesRead, RetrieveColumnGrbit.RetrieveFromIndex, null);
            Assert.AreEqual(data.Length, bytesRead);
            ulongCur = BitConverter.ToUInt64(data, 0);
            Assert.IsTrue(ulongCur.CompareTo(ulongT) == 0);

            EseInteropTestHelper.ConsoleWriteLine("Retrieved ulong is:");
            EseInteropTestHelper.ConsoleWriteLine(ulongCur.ToString());
            Assert.IsTrue(ulongCur.CompareTo(ulongT) == 0);
            EseInteropTestHelper.ConsoleWriteLine("Retrieve from index matches inserted ULongLong");

            Api.JetCloseTable(this.sesId, this.tableId);
        }