コード例 #1
0
        public int Compare(long rowId1, long rowId2)
        {
            if (!_asc)
            {
                var tmp = rowId1;
                rowId1 = rowId2;
                rowId2 = tmp;
            }

            var part1 = RowIDUtil.ToPartitionIndex(rowId1);
            var part2 = RowIDUtil.ToPartitionIndex(rowId2);
            var lrow1 = RowIDUtil.ToLocalRowID(rowId1);
            var lrow2 = RowIDUtil.ToLocalRowID(rowId2);

            var col1 = _tx.Read(part1).ReadColumn(_filedId);
            var col2 = (part1 == part2) ? col1 : _tx.Read(part2).ReadColumn(_filedId);

            var val1 = ((ITypedColumn <TT>)col1).Get(lrow1, _tx.ReadCache);
            var val2 = ((ITypedColumn <TT>)col2).Get(lrow2, _tx.ReadCache);

            if (val1 != null)
            {
                return(((ITypedColumn <TT>)col1).Get(lrow1, _tx.ReadCache).CompareTo(
                           ((ITypedColumn <TT>)col2).Get(lrow2, _tx.ReadCache)));
            }

            if (val2 == null)
            {
                return(0);
            }
            return(-1);
        }
コード例 #2
0
        public IEnumerable <long> Filter(IEnumerable <PartitionRowIDRange> partitions,
                                         IReadTransactionContext tx, ERowIDSortDirection sortDirection)
        {
            if (_column.Indexed)
            {
                var values = GetAllMatchingValues(tx);
                if (!values.IsNone)
                {
                    if (!values.IsSigle)
                    {
                        var valueSet = values.Values.Distinct().ToList();
                        var items    = new IEnumerable <long> [valueSet.Count];
                        return(partitions.SelectMany(part =>
                        {
                            var partition = tx.Read(part.PartitionID);
                            for (int v = 0; v < valueSet.Count; v++)
                            {
                                var symbolValue = valueSet[v];
                                var rowIDs = TakeFromTo(part, partition.GetSymbolRows(_column.ColumnID, symbolValue, tx));
                                if (sortDirection == ERowIDSortDirection.Asc)
                                {
                                    // Todo: use tx.ReadContext and reuse buffers.
                                    rowIDs = rowIDs.Reverse();
                                }
                                items[v] = rowIDs;
                            }

                            if (sortDirection == ERowIDSortDirection.None)
                            {
                                return items.SelectMany(i => i);
                            }
                            return MergeSorted(items, sortDirection);
                        }));
                    }
                    else
                    {
                        return(partitions.SelectMany(part =>
                        {
                            var partition = tx.Read(part.PartitionID);
                            var rowIDs = TakeFromTo(part, partition.GetSymbolRows(_column.ColumnID, values.Value, tx));
                            if (sortDirection == ERowIDSortDirection.Asc)
                            {
                                rowIDs = rowIDs.Reverse();
                            }
                            return rowIDs;
                        }));
                    }
                }
            }

            return(partitions.SelectMany(part =>
            {
                var partition = tx.Read(part.PartitionID);
                return IsPartitionMatch(tx, part, partition, sortDirection);
            }));
        }
コード例 #3
0
        private List <long> GetLatestByIndexedSymbolByKeys(IEnumerable <PartitionRowIDRange> partitions, IReadTransactionContext tx)
        {
            var latestRowIDs = new List <long>(_keys.Count);

            foreach (var part in partitions)
            {
                var   partition = tx.Read(part.PartitionID);
                int[] keysMap   = new int[_keys.Count];

                // Key mapping.
                for (int i = 0; i < _keys.Count; i++)
                {
                    keysMap[i] = partition.GetSymbolKey(_column.ColumnID, _keys[i], tx);
                }

                var allFound = true;
                for (int i = 0; i < _keys.Count; i++)
                {
                    if (latestRowIDs[i] == 0)
                    {
                        // Symbol D file key.
                        var key = keysMap[i];
                        if (key != MetadataConstants.SYMBOL_NOT_FOUND_VALUE)
                        {
                            var rowIDs = partition.GetSymbolRowsByKey(_column.ColumnID, key, tx);

                            foreach (var rowID in rowIDs)
                            {
                                if (rowID >= part.Low && rowID <= part.High)
                                {
                                    // Stop search the key.
                                    latestRowIDs[i] = RowIDUtil.ToRowID(part.PartitionID, rowID);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            // Stop search the invalid value.
                            latestRowIDs[i] = MetadataConstants.SYMBOL_NOT_FOUND_VALUE;
                        }
                    }
                    allFound &= latestRowIDs[i] != 0;
                }

                // Early partition scan termination.
                if (allFound)
                {
                    break;
                }
            }

            return(latestRowIDs);
        }
コード例 #4
0
        public int Compare(long rowId1, long rowId2)
        {
            if (!_asc)
            {
                var tmp = rowId1;
                rowId1 = rowId2;
                rowId2 = tmp;
            }

            var part1 = RowIDUtil.ToPartitionIndex(rowId1);
            var part2 = RowIDUtil.ToPartitionIndex(rowId2);
            var lrow1 = RowIDUtil.ToLocalRowID(rowId1);
            var lrow2 = RowIDUtil.ToLocalRowID(rowId2);

            var col1 = _tx.Read(part1).ReadColumn(_filedId);
            var col2 = (part1 == part2) ? col1 : _tx.Read(part2).ReadColumn(_filedId);

            return(Compare(((ITypedColumn <byte[]>)col1).Get(lrow1, _tx.ReadCache),
                           ((ITypedColumn <byte[]>)col2).Get(lrow2, _tx.ReadCache)));
        }
コード例 #5
0
        public override object Execute(Expression expression)
        {
            var lambda = expression as LambdaExpression;

            if (lambda == null && _cache != null && expression.NodeType != ExpressionType.Constant)
            {
                return(_cache.Execute(expression));
            }

            var result = GetExecutionPlan(expression);
            var res    = result.Build();

            if (res.IsSingle)
            {
                var rowID = res.RowID;

                // First / Last OrDefault can return null.
                if (rowID == 0)
                {
                    return(null);
                }
                int  partitionID         = RowIDUtil.ToPartitionIndex(rowID);
                var  lastPartitionReader = _tx.Read(partitionID);
                long localRowID          = RowIDUtil.ToLocalRowID(rowID);

                // ReSharper disable once PossibleNullReferenceException
                return(lastPartitionReader.Read <T>(localRowID, _tx.ReadCache));
            }

            switch (res.PostExpression)
            {
            case EJournalExpressionType.None:
                return(new ResultSet <T>(res.Rowids, _tx));

            case EJournalExpressionType.LongCount:
                return(res.Rowids.LongCount());

            case EJournalExpressionType.Count:
                return(res.Rowids.Count());

            default:
                throw QueryExceptionExtensions.ExpressionNotSupported(
                          string.Format("Post expression {0} is not supported at this level", res.PostExpression), expression);
            }
        }
コード例 #6
0
        private IEnumerable <long> FilterRowsRange(IReadTransactionContext tx, List <PartitionRowIDRange> intervals, ERowIDSortDirection sort)
        {
            for (int i = 0; i < intervals.Count; i++)
            {
                var  partitionRowIDRange = intervals[i];
                var  paritition          = tx.Read(partitionRowIDRange.PartitionID);
                long from      = sort == ERowIDSortDirection.Asc ? partitionRowIDRange.Low : partitionRowIDRange.High;
                long to        = sort == ERowIDSortDirection.Asc ? partitionRowIDRange.High: partitionRowIDRange.Low;
                long increment = sort == ERowIDSortDirection.Asc ? 1 : -1;

                for (long rowID = from; rowID < to; rowID += increment)
                {
                    if (MatchFilters(paritition, tx.ReadCache, rowID))
                    {
                        yield return(RowIDUtil.ToRowID(partitionRowIDRange.PartitionID, rowID));
                    }
                }
            }
        }
コード例 #7
0
ファイル: ResultSet.cs プロジェクト: ideoma/nfsdb-csharp
        private IEnumerable <T> Read()
        {
            int lastPartitionID = -1;
            IPartitionReader lastPartitionReader = null;

            foreach (var rowID in _rowIDs)
            {
                int partitionID = RowIDUtil.ToPartitionIndex(rowID);
                if (partitionID != lastPartitionID)
                {
                    lastPartitionReader = _tx.Read(partitionID);
                    lastPartitionID     = partitionID;
                }
                long localRowID = RowIDUtil.ToLocalRowID(rowID);

                // ReSharper disable once PossibleNullReferenceException
                yield return(lastPartitionReader.Read <T>(localRowID, _tx.ReadCache));
            }
        }
コード例 #8
0
        private List <long> GetAllLatestByIndexedSymbolByKeys(IEnumerable <PartitionRowIDRange> partitions, IReadTransactionContext tx)
        {
            var allKeys      = new ObjIntHashMap();
            var latestRowIDs = new List <long>();

            foreach (var part in partitions)
            {
                var partition    = tx.Read(part.PartitionID);
                var symbolColumn = (ISymbolMapColumn)partition.ReadColumn(Column.ColumnID);

                // Key mapping.
                var partitionTxData = tx.GetPartitionTx(part.PartitionID);
                var distinctCount   = symbolColumn.GetDistinctCount(partitionTxData);

                for (int i = 0; i < distinctCount; i++)
                {
                    var key         = i;
                    var symbolValue = symbolColumn.GetKeyValue(key, partitionTxData);
                    if (symbolValue == null)
                    {
                        key = MetadataConstants.NULL_SYMBOL_VALUE;
                    }
                    var existing = allKeys.Get(symbolValue);
                    if (existing == MetadataConstants.SYMBOL_NOT_FOUND_VALUE)
                    {
                        var rowIDs = partition.GetSymbolRowsByKey(_column.ColumnID, key, tx);

                        foreach (var rowID in rowIDs)
                        {
                            if (rowID >= part.Low && rowID <= part.High)
                            {
                                // Stop search the key.
                                latestRowIDs.Add(RowIDUtil.ToRowID(part.PartitionID, rowID));
                                allKeys.Put(symbolValue, 1);
                                break;
                            }
                        }
                    }
                }
            }

            return(latestRowIDs);
        }
コード例 #9
0
        private IEnumerable <long> ApplyFilters(IEnumerable <long> rowIDs, IReadTransactionContext tx)
        {
            int partitionIndex         = -1;
            IPartitionReader partition = null;

            foreach (var globalRowID in rowIDs)
            {
                var rowPartitionIndex = RowIDUtil.ToPartitionIndex(globalRowID);
                if (rowPartitionIndex != partitionIndex)
                {
                    partition      = tx.Read(rowPartitionIndex);
                    partitionIndex = rowPartitionIndex;
                }

                if (MatchFilters(partition, tx.ReadCache, RowIDUtil.ToLocalRowID(globalRowID)))
                {
                    yield return(globalRowID);
                }
            }
        }
コード例 #10
0
        private IEnumerable <long> GetLatestFromDescending(IEnumerable <PartitionRowIDRange> partitions, IReadTransactionContext tx)
        {
            var         latest   = new HashSet <T>();
            HashSet <T> contains = _keys != null ? new HashSet <T>(_keys) : null;

            foreach (var partition in partitions)
            {
                var readPartition = tx.Read(partition.PartitionID);
                var col           = (ITypedColumn <T>)readPartition.ReadColumn(_column.ColumnID);
                for (long r = partition.High; r >= partition.Low; r--)
                {
                    var val = col.Get(r, tx.ReadCache);
                    if ((contains == null || contains.Contains(val)) && !latest.Contains(val))
                    {
                        latest.Add(val);
                        yield return(RowIDUtil.ToRowID(partition.PartitionID, r));
                    }
                }
            }
        }
コード例 #11
0
 private T UpdatePartition <T>(int partitionID, long localRowID, int columnIndex)
 {
     _lastPartitionReader = _tx.Read(partitionID);
     _lastPartitionID     = partitionID;
     return(((ITypedColumn <T>)_lastPartitionReader.ReadColumn(columnIndex)).Get(localRowID, _tx.ReadCache));
 }