Ejemplo n.º 1
0
        public MergeResult AddPTable(PTable tableToAdd,
                                     long prepareCheckpoint,
                                     long commitCheckpoint,
                                     Func <IndexEntry, bool> recordExistsAt,
                                     IIndexFilenameProvider filenameProvider)
        {
            Ensure.Nonnegative(prepareCheckpoint, "prepareCheckpoint");
            Ensure.Nonnegative(commitCheckpoint, "commitCheckpoint");

            var tables = CopyFrom(_map);

            CreateIfNeeded(0, tables);
            tables[0].Add(tableToAdd);

            var toDelete = new List <PTable>();

            for (int level = 0; level < tables.Count; level++)
            {
                if (tables[level].Count >= _maxTablesPerLevel)
                {
                    var    filename = filenameProvider.GetFilenameNewTable();
                    PTable table    = PTable.MergeTo(tables[level], filename, recordExistsAt);
                    CreateIfNeeded(level + 1, tables);
                    tables[level + 1].Add(table);
                    toDelete.AddRange(tables[level]);
                    tables[level].Clear();
                }
            }

            var indexMap = new IndexMap(Version, tables, prepareCheckpoint, commitCheckpoint, _maxTablesPerLevel);

            return(new MergeResult(indexMap, toDelete));
        }
Ejemplo n.º 2
0
        public MergeResult AddPTableForManualMerge(long prepareCheckpoint,
                                                   long commitCheckpoint,
                                                   Func <string, ulong, ulong> upgradeHash,
                                                   Func <IndexEntry, bool> existsAt,
                                                   Func <IndexEntry, Tuple <string, bool> > recordExistsAt,
                                                   IIndexFilenameProvider filenameProvider,
                                                   byte version,
                                                   int indexCacheDepth  = 16,
                                                   bool skipIndexVerify = false)
        {
            Ensure.Nonnegative(prepareCheckpoint, "prepareCheckpoint");
            Ensure.Nonnegative(commitCheckpoint, "commitCheckpoint");

            var tables = CopyFrom(_map);

            if (tables.Count < _maxTableLevelsForAutomaticMerge)
            {
                return(new MergeResult(this, new List <PTable>()));
            }

            var toDelete      = new List <PTable>();
            var tablesToMerge = tables.Skip(_maxTableLevelsForAutomaticMerge).SelectMany(a => a).ToList();

            if (tablesToMerge.Count == 1)
            {
                return(new MergeResult(this, new List <PTable>()));
            }

            var    filename    = filenameProvider.GetFilenameNewTable();
            PTable mergedTable = PTable.MergeTo(tablesToMerge, filename, upgradeHash, existsAt, recordExistsAt,
                                                version, ESConsts.PTableInitialReaderCount, _pTableMaxReaderCount,
                                                indexCacheDepth, skipIndexVerify);

            for (int i = tables.Count - 1; i > _maxTableLevelsForAutomaticMerge; i--)
            {
                tables.RemoveAt(i);
            }

            tables[_maxTableLevelsForAutomaticMerge].Clear();
            AddTableToTables(tables, _maxTableLevelsForAutomaticMerge + 1, mergedTable);
            toDelete.AddRange(tablesToMerge);

            var indexMap = new IndexMap(Version, tables, prepareCheckpoint, commitCheckpoint, _maxTablesPerLevel,
                                        _maxTableLevelsForAutomaticMerge, _pTableMaxReaderCount);

            return(new MergeResult(indexMap, toDelete));
        }
Ejemplo n.º 3
0
        public MergeResult TryManualMerge <TStreamId>(
            Func <TStreamId, ulong, ulong> upgradeHash,
            Func <IndexEntry, bool> existsAt,
            Func <IndexEntry, Tuple <TStreamId, bool> > recordExistsAt,
            IIndexFilenameProvider filenameProvider,
            byte version,
            int indexCacheDepth  = 16,
            bool skipIndexVerify = false,
            bool useBloomFilter  = true,
            int lruCacheSize     = 1_000_000)
        {
            var tables = CopyFrom(_map);

            if (tables.Count <= _maxTableLevelsForAutomaticMerge)
            {
                return(new MergeResult(this, new List <PTable>(), false, false));
            }

            var toDelete      = new List <PTable>();
            var tablesToMerge = tables.Skip(_maxTableLevelsForAutomaticMerge).SelectMany(a => a).ToList();

            if (tablesToMerge.Count == 1)
            {
                return(new MergeResult(this, new List <PTable>(), false, false));
            }

            var    filename    = filenameProvider.GetFilenameNewTable();
            PTable mergedTable = PTable.MergeTo(tablesToMerge, filename, upgradeHash, existsAt, recordExistsAt,
                                                version, ESConsts.PTableInitialReaderCount, _pTableMaxReaderCount,
                                                indexCacheDepth, skipIndexVerify,
                                                useBloomFilter, lruCacheSize);

            for (int i = tables.Count - 1; i > _maxTableLevelsForAutomaticMerge; i--)
            {
                tables.RemoveAt(i);
            }

            tables[_maxTableLevelsForAutomaticMerge].Clear();
            AddTableToTables(tables, _maxTableLevelsForAutomaticMerge + 1, mergedTable);
            toDelete.AddRange(tablesToMerge);

            var indexMap = new IndexMap(Version, tables, PrepareCheckpoint, CommitCheckpoint, _maxTablesPerLevel,
                                        _maxTableLevelsForAutomaticMerge, _pTableMaxReaderCount);

            return(new MergeResult(indexMap, toDelete, true, false));
        }
Ejemplo n.º 4
0
        public MergeResult TryMergeOneLevel <TStreamId>(
            Func <TStreamId, ulong, ulong> upgradeHash,
            Func <IndexEntry, bool> existsAt,
            Func <IndexEntry, Tuple <TStreamId, bool> > recordExistsAt,
            IIndexFilenameProvider filenameProvider,
            byte version,
            int indexCacheDepth  = 16,
            bool skipIndexVerify = false,
            bool useBloomFilter  = true,
            int lruCacheSize     = 1_000_000)
        {
            var tables = CopyFrom(_map);

            var hasMergedAny          = false;
            var canMergeAny           = false;
            var toDelete              = new List <PTable>();
            var maxTableLevelsToMerge = Math.Min(tables.Count, _maxTableLevelsForAutomaticMerge);

            for (int level = 0; level < maxTableLevelsToMerge; level++)
            {
                if (tables[level].Count >= _maxTablesPerLevel)
                {
                    if (hasMergedAny)
                    {
                        canMergeAny = true;
                        break;
                    }
                    var    filename    = filenameProvider.GetFilenameNewTable();
                    PTable mergedTable = PTable.MergeTo(tables[level], filename, upgradeHash, existsAt, recordExistsAt,
                                                        version,
                                                        ESConsts.PTableInitialReaderCount, _pTableMaxReaderCount,
                                                        indexCacheDepth, skipIndexVerify,
                                                        useBloomFilter, lruCacheSize);
                    hasMergedAny = true;

                    AddTableToTables(tables, level + 1, mergedTable);
                    toDelete.AddRange(tables[level]);
                    tables[level].Clear();
                }
            }

            var indexMap = new IndexMap(Version, tables, PrepareCheckpoint, CommitCheckpoint, _maxTablesPerLevel,
                                        _maxTableLevelsForAutomaticMerge, _pTableMaxReaderCount);

            return(new MergeResult(indexMap, toDelete, hasMergedAny, canMergeAny));
        }
Ejemplo n.º 5
0
        public MergeResult AddPTableForAutomaticMerge(PTable tableToAdd,
                                                      long prepareCheckpoint,
                                                      long commitCheckpoint,
                                                      Func <string, ulong, ulong> upgradeHash,
                                                      Func <IndexEntry, bool> existsAt,
                                                      Func <IndexEntry, Tuple <string, bool> > recordExistsAt,
                                                      IIndexFilenameProvider filenameProvider,
                                                      byte version,
                                                      int indexCacheDepth  = 16,
                                                      bool skipIndexVerify = false)
        {
            Ensure.Nonnegative(prepareCheckpoint, "prepareCheckpoint");
            Ensure.Nonnegative(commitCheckpoint, "commitCheckpoint");

            var tables = CopyFrom(_map);

            AddTableToTables(tables, 0, tableToAdd);

            var toDelete = new List <PTable>();
            var maxTableLevelsToMerge = Math.Min(tables.Count, _maxTableLevelsForAutomaticMerge);

            for (int level = 0; level < maxTableLevelsToMerge; level++)
            {
                if (tables[level].Count >= _maxTablesPerLevel)
                {
                    var    filename    = filenameProvider.GetFilenameNewTable();
                    PTable mergedTable = PTable.MergeTo(tables[level], filename, upgradeHash, existsAt, recordExistsAt,
                                                        version,
                                                        ESConsts.PTableInitialReaderCount, _pTableMaxReaderCount,
                                                        indexCacheDepth, skipIndexVerify);

                    AddTableToTables(tables, level + 1, mergedTable);
                    toDelete.AddRange(tables[level]);
                    tables[level].Clear();
                }
            }

            var indexMap = new IndexMap(Version, tables, prepareCheckpoint, commitCheckpoint, _maxTablesPerLevel,
                                        _maxTableLevelsForAutomaticMerge, _pTableMaxReaderCount);

            return(new MergeResult(indexMap, toDelete));
        }