Пример #1
0
        public void Commit(ITransactionContext tx, int partitionTtl)
        {
            if (Access != EFileAccess.ReadWrite)
            {
                throw new NFSdbCommitFailedException(
                          "Journal opened in readonly mode. Transaction commit is not allowed");
            }

            bool isUpdated = false;

            try
            {
                PartitionTxData lastAppendPartition = tx.GetPartitionTx();
                // Non-empty commit.
                if (lastAppendPartition != null)
                {
                    var lastPartitionID = lastAppendPartition.PartitionID;
                    var rowIdFrom       = _lastTxRec != null ? _lastTxRec.JournalMaxRowID : 0;

                    for (int i = tx.Partitions.Count - 1; i >= 0; i--)
                    {
                        var partition = tx.Partitions[i];
                        if (partition != null)
                        {
                            if (tx.IsParitionUpdated(partition.PartitionID, _lastTransactionLog))
                            {
                                partition.Commit(tx.GetPartitionTx(partition.PartitionID));
                                tx.RemoveRef(partitionTtl);
                                isUpdated = true;
                            }
                        }
                    }

                    if (isUpdated)
                    {
                        var lastPartition = GetPartitionByID(tx, lastPartitionID);
                        var rec           = new TxRec();
                        lastPartition.SetTxRec(tx, rec);
                        // _symbolTxSupport.SetTxRec(tx, rec);
                        _txLog.Create(rec);
                        _lastTxRec = rec;

                        var onCommited = OnCommited;
                        if (onCommited != null)
                        {
                            onCommited(rowIdFrom, _lastTxRec.JournalMaxRowID);
                        }
                    }
                    tx.SetCommited();
                }
            }
            catch (Exception ex)
            {
                throw new NFSdbCommitFailedException(
                          "Error commiting transaction. See InnerException for details.", ex);
            }
            _lastTransactionLog = tx;
        }
Пример #2
0
        public void Append(object item)
        {
            var dateTime = _writerState.GetTimestampDelegate(item);
            var p        = _partitionManager.GetAppendPartition(dateTime, _transaction);

            _transaction.LastAppendTimestamp = dateTime;
            p.Append(item, _transaction.GetPartitionTx());
        }
Пример #3
0
        public unsafe void SetBytes(long rowID, byte *value, int length, ITransactionContext tx)
        {
            var offset = rowID * MetadataConstants.STRING_INDEX_FILE_RECORD_SIZE;

            if (value != null)
            {
                var writeOffset = tx.GetPartitionTx().AppendOffset[_data.FileID];
                _index.WriteInt64(offset, writeOffset);

                WriteLength(writeOffset, length);
                _data.WriteBytes(writeOffset + MetadataConstants.LARGE_VAR_COL_HEADER_LENGTH, value, length);
                tx.GetPartitionTx().AppendOffset[_data.FileID] = writeOffset + MetadataConstants.LARGE_VAR_COL_HEADER_LENGTH + length;
            }
            else
            {
                _index.WriteInt64(offset, MetadataConstants.INDEX_NULL_DATA_VALUE);
            }
        }
Пример #4
0
        private void SwitchWritePartitionTo(ITransactionContext tx, int partitionID)
        {
            var prevPartitionTx = tx.GetPartitionTx();

            tx.SetCurrentPartition(partitionID);
            tx.AddRef(partitionID);

            if (prevPartitionTx != null)
            {
                // Write append offsets in all the files.
                var pp = GetPartitionByID(tx, prevPartitionTx.PartitionID);
                pp.Commit(tx.GetPartitionTx(prevPartitionTx.PartitionID));

                var deref = tx.Partitions[prevPartitionTx.PartitionID];
                if (deref != null)
                {
                    tx.RemoveRef(prevPartitionTx.PartitionID);
                    pp.SaveConfig(tx);
                }
            }
        }
Пример #5
0
        public IPartition GetAppendPartition(DateTime dateTime, ITransactionContext tx)
        {
            var lastUsedPartition = tx.GetPartitionTx();

            if (tx.LastAppendTimestamp <= dateTime && lastUsedPartition != null)
            {
                var part = GetPartitionByID(tx, lastUsedPartition.PartitionID);
                if (part != null && dateTime >= part.StartDate && dateTime < part.EndDate)
                {
                    return(part);
                }
            }

            return(GetAppendPartition0(dateTime, tx));
        }
Пример #6
0
        public void SetTxRec(ITransactionContext tx, TxRec rec)
        {
            rec.PrevTxAddress = tx.PrevTxAddress;
            rec.Command       = TxRec.TX_NORMAL;
            var pd = tx.GetPartitionTx(_partitionID);

            var columCount = _metadata.Columns.Count();

            rec.LastPartitionTimestamp = DateUtils.DateTimeToUnixTimeStamp(tx.LastAppendTimestamp);

            // Java NFSdb Journal has paritions from 0 with no reserved id for symbol parition.
            // Max row ID is rowcount + 1 for compatibility
            rec.JournalMaxRowID = RowIDUtil.ToRowID(_partitionID - 1, pd.NextRowID - 1) + 1;

            rec.IndexPointers = new long[columCount];
            var symbolTableSize          = new List <int>();
            var symbolTableIndexPointers = new List <long>();

            for (int i = 0; i < _storage.OpenFileCount; i++)
            {
                var f = _storage.GetOpenedFileByID(i);
                if (f != null && f.DataType == EDataType.Datak)
                {
                    rec.IndexPointers[f.ColumnID] = pd.SymbolData[f.FileID].KeyBlockOffset;
                }
                else
                {
                    var file = _storage.GetOpenedFileByID(i);
                    if (file == null)
                    {
                        continue;
                    }

                    if (file.DataType == EDataType.Symi)
                    {
                        var indexSize = (int)(pd.AppendOffset[file.FileID] / 8);
                        symbolTableSize.Add(indexSize);
                    }
                    else if (file.DataType == EDataType.Symrk)
                    {
                        var sd = pd.SymbolData[file.FileID];
                        symbolTableIndexPointers.Add(sd.KeyBlockOffset);
                    }
                }
            }
            rec.SymbolTableIndexPointers = symbolTableIndexPointers.ToArray();
            rec.SymbolTableSizes         = symbolTableSize.ToArray();
        }
Пример #7
0
        public static void WriteItem(object obj,
                                     ByteArray bitset,
                                     IFixedWidthColumn[] fixedCols, long rowid,
                                     IRefTypeColumn[] stringColumns, ITransactionContext readContext)
        {
            var q = (Quote)obj;

            bitset.Set(0, !q.__isset.timestamp);
            fixedCols[0].SetInt64(rowid, q.Timestamp);

            bitset.Set(1, !q.__isset.bid);
            fixedCols[1].SetDouble(rowid, q.Bid);

            bitset.Set(2, !q.__isset.bid);
            stringColumns[0].SetValue(rowid, q.Mode, readContext.GetPartitionTx());
        }
Пример #8
0
            public static void WriteItemPoco(
                object obj,
                ByteArray bitset,
                IFixedWidthColumn[] fixedCols,
                long rowid,
                IRefTypeColumn[] stringColumns,
                ITransactionContext readContext)
            {
                var q = (QuotePoco)obj;

                fixedCols[0].SetInt64(rowid, q._timestamp);

                var pd = readContext.GetPartitionTx();

                bitset.Set(0, q._sym == null);
                stringColumns[0].SetValue(rowid, q._sym, pd);

                bool isnull = !q._ask.hasValue;

                bitset.Set(1, isnull);
                if (!isnull)
                {
                    fixedCols[0].SetDouble(rowid, q._ask.value);
                }

                isnull = !q._bid.hasValue;
                bitset.Set(2, isnull);
                if (!isnull)
                {
                    fixedCols[1].SetDouble(rowid, q._bid.value);
                }

                fixedCols[2].SetInt32(rowid, q._bidSize);
                fixedCols[3].SetInt32(rowid, q._askSize);

                bitset.Set(2, q._mode == null);
                stringColumns[1].SetValue(rowid, q._mode, pd);

                bitset.Set(3, q._ex == null);
                stringColumns[1].SetValue(rowid, q._ex, pd);
            }
Пример #9
0
        public bool IsParitionUpdated(int partitionID, ITransactionContext lastTransactionLog)
        {
            var thisPd      = PartitionTx[partitionID];
            var partitionTx = lastTransactionLog.GetPartitionTx(partitionID);

            if (partitionTx == null || PartitionTx.Count <= partitionID)
            {
                return(true);
            }
            var lastPd = PartitionTx[partitionID];

            for (int i = 0; i < thisPd.AppendOffset.Length; i++)
            {
                if (thisPd.AppendOffset[i] != lastPd.AppendOffset[i] ||
                    thisPd.SymbolData[i].KeyBlockOffset != lastPd.SymbolData[i].KeyBlockOffset ||
                    thisPd.SymbolData[i].KeyBlockSize != lastPd.SymbolData[i].KeyBlockSize)
                {
                    return(true);
                }
            }

            return(false);
        }