Repository: dolthub/dolt
File: go/libraries/doltcore/sqle/statspro/worker.go (Go)

Code before:
			return nil, nil, 0, err
		}
		buckets = append(buckets, newBucket)
	}

	return buckets, lowerBound, writes, nil
}

// execWithOptionalRateLimit executes the given function either directly or through the rate limiter
// depending on the bypassRateLimit flag
func (sc *StatsController) execWithOptionalRateLimit(ctx *sql.Context, bypassRateLimit bool, f func() error) error {
	if bypassRateLimit {
		return f()
	}
	return sc.rateLimiter.execute(ctx, f)
}

func (sc *StatsController) updateTable(ctx *sql.Context, newStats *rootStats, tableName string, sqlDb dsess.SqlDatabase, gcKv *memStats, bypassRateLimit bool, openSessionCmds bool) error {
	var err error
	var sqlTable *sqle.DoltTable
	var dTab *doltdb.Table
	if err := sc.execWithOptionalRateLimit(ctx, bypassRateLimit, func() (err error) {
		if openSessionCmds {
			sql.SessionCommandBegin(ctx.Session)
			defer sql.SessionCommandEnd(ctx.Session)
		}
		sqlTable, dTab, err = GetLatestTable(ctx, tableName, sqlDb)
		return err
	}); err != nil {
		return err
	}

	schemaName := sqlTable.DatabaseSchema().SchemaName()

	tableKey := tableIndexesKey{
		db:     strings.ToLower(sqlDb.AliasedName()),
		branch: strings.ToLower(sqlDb.Revision()),
		table:  strings.ToLower(tableName),
		schema: strings.ToLower(schemaName),
	}

	tableHash, err := dTab.HashOf()
	if err != nil {
		return err
	}
	if gcKv == nil {
		if stats, ok := sc.preexistingStats(tableKey, tableHash); ok {
			newStats.stats[tableKey] = stats
			newStats.hashes[tableKey] = tableHash
			newStats.TablesSkipped++
			return nil

Review comment (suggestion):
This pattern make me cringe. It forces us to be careful about returning 1-1 so we don't end up starting the session twice. Could we call updateTable/collectIndexNodes in a wrapper method which does nothing but begin and end the session if openSessionCmds is true.

At the least could we just do it once at the beginning of each method? If my misgiving or the suggestion isn't clear, DM me.

Code after:
}

// execWithOptionalRateLimit executes the given function either directly or through the rate limiter
// depending on the bypassRateLimit flag
func (sc *StatsController) execWithOptionalRateLimit(ctx *sql.Context, bypassRateLimit, openSessionCmds bool, f func() error) error {
	if openSessionCmds {
		sql.SessionCommandBegin(ctx.Session)
		defer sql.SessionCommandEnd(ctx.Session)
	}
	if bypassRateLimit {
		return f()
	}
	return sc.rateLimiter.execute(ctx, f)
}

func (sc *StatsController) updateTable(ctx *sql.Context, newStats *rootStats, tableName string, sqlDb dsess.SqlDatabase, gcKv *memStats, bypassRateLimit bool, openSessionCmds bool) error {
	var err error
	var sqlTable *sqle.DoltTable
	var dTab *doltdb.Table
	if err := sc.execWithOptionalRateLimit(ctx, bypassRateLimit, openSessionCmds, func() (err error) {
		sqlTable, dTab, err = GetLatestTable(ctx, tableName, sqlDb)
		return err
	}); err != nil {
		return err
	}

	schemaName := sqlTable.DatabaseSchema().SchemaName()

	tableKey := tableIndexesKey{
		db:     strings.ToLower(sqlDb.AliasedName()),
		branch: strings.ToLower(sqlDb.Revision()),
		table:  strings.ToLower(tableName),
		schema: strings.ToLower(schemaName),
	}

	tableHash, err := dTab.HashOf()
	if err != nil {
		return err
	}
	if gcKv == nil {
		if stats, ok := sc.preexistingStats(tableKey, tableHash); ok {
			newStats.stats[tableKey] = stats
			newStats.hashes[tableKey] = tableHash
			newStats.TablesSkipped++
			return nil
		}
	}

	var indexes []sql.Index
	if err := sc.execWithOptionalRateLimit(ctx, bypassRateLimit, openSessionCmds, func() (err error) {
		indexes, err = sqlTable.GetIndexes(ctx)