public void ShrinkColumnStream()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            const int Length = 1345;
            var       data   = Any.BytesOfLength(Length);

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        stream.Write(data, 0, data.Length);
                        stream.Write(data, 0, data.Length);
                        Assert.AreEqual(Length * 2, stream.Length);

                        stream.SetLength(Length);
                        Assert.AreEqual(Length, stream.Length);

                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                Assert.AreEqual(Length, stream.Length);
                var buffer = new byte[Length];
                stream.Read(buffer, 0, buffer.Length);
                CollectionAssert.AreEqual(data, buffer);
            }
        }
        public void GrowColumnStreamByWritingPastEnd()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            const int Length   = 1345;
            const int Position = 1500;
            var       data     = Any.BytesOfLength(Length);

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        stream.Position = Position;
                        stream.Write(data, 0, data.Length);
                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                Assert.AreEqual(Length + Position, stream.Length);
                var expected = new byte[Length + Position];
                var actual   = new byte[Length + Position];
                Array.Copy(data, 0, expected, Position, Length);
                Assert.AreEqual(Length + Position, stream.Read(actual, 0, actual.Length));
                CollectionAssert.AreEqual(expected, actual);
            }
        }
        public void ExtendingColumnStream()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            var data = Any.BytesOfLength(4096);

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        // Write some of the data, rewind a bit and then overwrite the
                        // last few bytes and append some more data
                        stream.Write(data, 0, data.Length - 10);
                        stream.Seek(-10, SeekOrigin.End);
                        stream.Write(data, data.Length - 20, 20);
                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                Assert.AreEqual(data.Length, stream.Length);
                var retrieved = new byte[data.Length];
                Assert.AreEqual(retrieved.Length, stream.Read(retrieved, 0, retrieved.Length));
                CollectionAssert.AreEqual(data, retrieved);
            }
        }
        public void ReadReturnsNumberOfBytesRead()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            var data = Any.BytesOfLength(1024);

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        stream.Write(data, 0, data.Length);
                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                var retrieved = new byte[data.Length];
                stream.Seek(-1, SeekOrigin.End);
                Assert.AreEqual(1, stream.Read(retrieved, 0, retrieved.Length));
                Assert.AreEqual(data[data.Length - 1], retrieved[0]);
            }
        }
        public void ReadAtNonZeroOffset()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            var data = Any.BytesOfLength(1024);

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        stream.Write(data, 0, data.Length);
                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                var retrieved = new byte[data.Length * 2];
                stream.Read(retrieved, data.Length, data.Length);
                for (int i = data.Length; i < retrieved.Length; ++i)
                {
                    Assert.AreEqual(retrieved[i], data[i - data.Length]);
                }
            }
        }
        public void OverwriteColumnStream()
        {
            var bookmark = new byte[SystemParameters.BookmarkMost];
            int bookmarkSize;

            var       data    = Any.BytesOfLength(1024);
            var       newData = Any.BytesOfLength(128);
            const int Offset  = 10;

            using (var transaction = new Transaction(this.sesid))
                using (var update = new Update(this.sesid, this.tableid, JET_prep.Insert))
                    using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
                    {
                        stream.Write(data, 0, data.Length);
                        stream.Position = 0;
                        stream.Seek(Offset, SeekOrigin.Current);
                        stream.Write(newData, 0, newData.Length);
                        update.Save(bookmark, bookmark.Length, out bookmarkSize);
                        transaction.Commit(CommitTransactionGrbit.LazyFlush);
                    }

            Api.JetGotoBookmark(this.sesid, this.tableid, bookmark, bookmarkSize);
            using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
            {
                Assert.AreEqual(data.Length, stream.Length);
                var retrieved = new byte[data.Length];
                var expected  = new byte[data.Length];
                Array.Copy(data, 0, expected, 0, data.Length);
                Array.Copy(newData, 0, expected, Offset, newData.Length);
                Assert.AreEqual(retrieved.Length, stream.Read(retrieved, 0, retrieved.Length));
                CollectionAssert.AreEqual(expected, retrieved);
            }
        }
Example #7
0
 public void ReadThrowsExceptionWhenBufferIsNull()
 {
     using (var t = new Transaction(this.sesid))
         using (var u = new Update(this.sesid, this.tableid, JET_prep.Insert))
             using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
             {
                 stream.Read(null, 0, 0);
             }
 }
Example #8
0
        public bool Initialize(String filePath, int pageSize)
        {
            this._databaseFile   = filePath;
            DatabasePageSize     = pageSize;
            VersionStorePageSize = pageSize * 2;
            _instance            = CreateEsentInstance();

            _primarySessionId = new Session(_instance);
            InitializeDatabaseAndTables();
            JET_TABLEID tableid;

            bool result = Api.TryOpenTable(_primarySessionId, _primaryDatabaseId, "Containers", OpenTableGrbit.None, out tableid);

            if (result == false)
            {
                return(false);
            }
            this.tableNameDict = new Dictionary <string, string>();

            using (var trx = new Transaction(_primarySessionId))
            {
                if (Api.TryMoveFirst(_primarySessionId, tableid))
                {
                    do
                    {
                        JET_COLUMNBASE colBaseName;
                        Api.JetGetColumnInfo(_primarySessionId, _primaryDatabaseId, "Containers", "Name", out colBaseName);
                        ColumnStream streamName = new ColumnStream(_primarySessionId, tableid, colBaseName.columnid);
                        Byte[]       dataName   = new Byte[streamName.Length];
                        streamName.Read(dataName, 0, (int)streamName.Length);
                        String tableConvertName = Encoding.Unicode.GetString(dataName).Replace("\0", string.Empty);

                        JET_COLUMNBASE colBasePartition;
                        Api.JetGetColumnInfo(_primarySessionId, _primaryDatabaseId, "Containers", "PartitionId", out colBasePartition);
                        ColumnStream streamPartition = new ColumnStream(_primarySessionId, tableid, colBasePartition.columnid);
                        Byte[]       dataPartition   = new Byte[streamPartition.Length];
                        streamPartition.Read(dataPartition, 0, (int)streamPartition.Length);
                        String tableConvertPartition = Encoding.Unicode.GetString(dataPartition).Replace("\0", string.Empty);
                        tableConvertName += "(" + tableConvertPartition + ")";

                        JET_COLUMNBASE colBaseID;
                        Api.JetGetColumnInfo(_primarySessionId, _primaryDatabaseId, "Containers", "ContainerId", out colBaseID);
                        ColumnStream streamID = new ColumnStream(_primarySessionId, tableid, colBaseID.columnid);
                        Byte[]       dataID   = new Byte[streamID.Length];
                        streamID.Read(dataID, 0, (int)streamID.Length);
                        String tableConvertID = BitConverter.ToUInt64(dataID, 0).ToString();

                        tableNameDict[tableConvertName] = tableConvertID;
                    }while (Api.TryMoveNext(_primarySessionId, tableid));
                }
                Api.JetCloseTable(_primarySessionId, tableid);
            }


            return(true);
        }
Example #9
0
 public void ReadThrowsExceptionWhenNumberOfBytesIsTooLarge()
 {
     using (var t = new Transaction(this.sesid))
         using (var u = new Update(this.sesid, this.tableid, JET_prep.Insert))
             using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
             {
                 var buffer = new byte[10];
                 stream.Read(buffer, 1, buffer.Length);
             }
 }
Example #10
0
 public void ReadThrowsExceptionWhenBufferOffsetIsNegative()
 {
     using (var t = new Transaction(this.sesid))
         using (var u = new Update(this.sesid, this.tableid, JET_prep.Insert))
             using (var stream = new ColumnStream(this.sesid, this.tableid, this.columnidLongText))
             {
                 var buffer = new byte[10];
                 stream.Read(buffer, -1, 1);
             }
 }
Example #11
0
        public int UnsafeBufferClass()
        {
            var read = 0;

            for (var i = 0; i < WROTE; i += READ)
            {
                read += _csUnsafeClass.Read(_rData, READ);
            }
            return(read);
        }
Example #12
0
        public override async Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            int result = 0;
            await _session.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();
                result = _inlinedStream.Read(buffer, offset, count);
            });

            return(result);
        }
Example #13
0
        public void readRecordsDoWork(object sender, DoWorkEventArgs e)
        {
            Tuple <String, List <ColumnInformation> > tableData = (Tuple <String, List <ColumnInformation> >)e.Argument;
            String tableName = tableData.Item1;
            List <ColumnInformation> columnInformationList = tableData.Item2;
            List <String>            coulumnDataList       = new List <String>();
            JET_TABLEID         tableid;
            List <ListViewItem> listViewItemList = new List <ListViewItem>();

            using (var trx = new Transaction(_primarySessionId))
            {
                String originalTableName = "Container_" + tableNameDict[tableName];
                Api.JetOpenTable(_primarySessionId, _primaryDatabaseId, originalTableName, null, 0, OpenTableGrbit.None, out tableid);
                int count;
                int index = 0;
                Api.JetIndexRecordCount(_primarySessionId, tableid, out count, Int32.MaxValue);
                this.formWait.setPrgress(0);
                this.formWait.maxProgress(count);
                this.formWait.end("- load table : " + tableName);
                if (Api.TryMoveFirst(_primarySessionId, tableid))
                {
                    do
                    {
                        if (backgroudWoker.CancellationPending == true)
                        {
                            break;
                        }
                        ListViewItem listViewItem = new ListViewItem();
                        foreach (ColumnInformation columnInformation in columnInformationList)
                        {
                            if (columnInformation.getId() == 256 && columnInformation.getName().Equals("Url"))
                            {
                                JET_COLUMNBASE colBase;
                                Api.JetGetColumnInfo(_primarySessionId, _primaryDatabaseId, originalTableName, columnInformation.getName(), out colBase);
                                ColumnStream stream = new ColumnStream(_primarySessionId, tableid, colBase.columnid);
                                Byte[]       data   = new Byte[stream.Length];
                                stream.Read(data, 0, (int)stream.Length);
                                String url = convertValue(data, columnInformation);
                                while (url.Contains('%'))
                                {
                                    url = HttpUtility.UrlDecode(url);
                                }
                                listViewItem.SubItems.Add(url);
                            }
                            else if (columnInformation.getId() == 999)
                            {
                                if (tableName.Contains("History"))
                                {
                                    String responseHeader = listViewItem.SubItems[22].Text;
                                    int    startOffset    = responseHeader.IndexOf("000000001F000000");
                                    if (startOffset == -1)
                                    {
                                        listViewItem.SubItems.Add("");
                                    }
                                    else
                                    {
                                        startOffset += 24;
                                        int len = BitConverter.ToInt32(hextoByte(responseHeader.Substring(startOffset - 8, 8)), 0) * 4;
                                        listViewItem.SubItems.Add(Encoding.Unicode.GetString(hextoByte(responseHeader.Substring(startOffset, len))));
                                    }
                                }
                                else if (tableName.Contains("Content"))
                                {
                                    String responseHeader = listViewItem.SubItems[22].Text;
                                    int    startOffset    = responseHeader.IndexOf("48545450");
                                    if (startOffset == -1)
                                    {
                                        listViewItem.SubItems.Add("");
                                    }
                                    else
                                    {
                                        int    len = responseHeader.Length - 4;
                                        String url = Encoding.UTF8.GetString(hextoByte(responseHeader.Substring(startOffset, len)));
                                        while (url.Contains('%'))
                                        {
                                            url = HttpUtility.UrlDecode(url);
                                        }
                                        listViewItem.SubItems.Add(url);
                                    }
                                }
                                else if (tableName.Contains("iecompatua"))
                                {
                                    String responseHeader = listViewItem.SubItems[22].Text;
                                    listViewItem.SubItems.Add(responseHeader);
                                }
                                else if (tableName.Contains("feedplat"))
                                {
                                    String responseHeader = listViewItem.SubItems[22].Text;
                                    int    len            = responseHeader.IndexOf("00000000");
                                    if (len == -1)
                                    {
                                        listViewItem.SubItems.Add("");
                                    }
                                    else
                                    {
                                        if (len % 2 == 1)
                                        {
                                            len++;
                                        }
                                        listViewItem.SubItems.Add(Encoding.UTF8.GetString(hextoByte(responseHeader.Substring(0, len))));
                                    }
                                }
                                else if (tableName.Contains("iedownload"))
                                {
                                    String responseHeader = listViewItem.SubItems[22].Text;
                                    int    startOffset    = responseHeader.LastIndexOf("00000000");
                                    if (startOffset == -1)
                                    {
                                        listViewItem.SubItems.Add("");
                                    }
                                    else
                                    {
                                        if (startOffset % 2 == 1)
                                        {
                                            startOffset++;
                                        }

                                        int    j        = 0;
                                        byte[] bytes    = hextoByte(responseHeader.Substring(startOffset + 8));
                                        byte[] newBytes = new byte[bytes.Length + 100];
                                        for (int i = 0; i < bytes.Length; i += 2)
                                        {
                                            if (bytes.Length > i + 1)
                                            {
                                                newBytes[j]     = bytes[i];
                                                newBytes[j + 1] = bytes[i + 1];
                                                if (bytes[i] == 0x00 && bytes[i + 1] == 0x00)
                                                {
                                                    newBytes[j]     = 0x0D;
                                                    newBytes[j + 1] = 0x00;
                                                    newBytes[j + 2] = 0x0A;
                                                    newBytes[j + 3] = 0x00;
                                                    newBytes[j + 4] = 0x0D;
                                                    newBytes[j + 5] = 0x00;
                                                    newBytes[j + 6] = 0x0A;
                                                    newBytes[j + 7] = 0x00;
                                                    j += 6;
                                                }
                                                j += 2;
                                            }
                                        }
                                        newBytes[j]     = 0x00;
                                        newBytes[j + 1] = 0x00;

                                        listViewItem.SubItems.Add(Encoding.Unicode.GetString(newBytes));
                                    }
                                }
                            }
                            else
                            {
                                JET_COLUMNBASE colBase;
                                Api.JetGetColumnInfo(_primarySessionId, _primaryDatabaseId, originalTableName, columnInformation.getName(), out colBase);
                                ColumnStream stream = new ColumnStream(_primarySessionId, tableid, colBase.columnid);
                                Byte[]       data   = new Byte[stream.Length];
                                stream.Read(data, 0, (int)stream.Length);
                                listViewItem.SubItems.Add(convertValue(data, columnInformation));
                            }
                        }
                        listViewItemList.Add(listViewItem);
                        this.formWait.setPrgress((++index));
                    }while (Api.TryMoveNext(_primarySessionId, tableid));
                }
                Api.JetCloseTable(_primarySessionId, tableid);
            }
            foreach (ColumnInformation columnInformation in columnInformationList)
            {
                coulumnDataList.Add(columnInformation.getName());
            }
            this.formWait.end("");

            if (isSaveData)
            {
                this.formWait.setLabel("Save table : " + tableName);
            }
            else
            {
                this.formWait.setLabel("Show table : " + tableName);
            }


            var result = new Tuple <String, List <String>, List <ListViewItem> >(tableName, coulumnDataList, listViewItemList);

            e.Result = result;
        }