Repository: codenotary/immudb
File: pkg/server/sessions/session.go (Go)

Code before:
	"github.com/codenotary/immudb/pkg/errors"
	"github.com/codenotary/immudb/pkg/logger"
	"github.com/codenotary/immudb/pkg/server/sessions/internal/transactions"
	"google.golang.org/grpc/metadata"
)

var (
	ErrPaginatedDocumentReaderNotFound = errors.New("document reader not found")
)

type PaginatedDocumentReader struct {
	Reader         document.DocumentReader // reader to read from
	Query          *protomodel.Query
	LastPageNumber uint32 // last read page number
	LastPageSize   uint32 // number of items per page
}

type Session struct {
	mux                      sync.RWMutex
	id                       string
	user                     *auth.User
	database                 database.DB
	creationTime             time.Time
	lastActivityTime         time.Time
	transactions             map[string]transactions.Transaction
	paginatedDocumentReaders *cache.LRUCache // track searchID to sql.RowReader objects
	log                      logger.Logger
}

func NewSession(sessionID string, user *auth.User, db database.DB, maxActiveSnapshots int, log logger.Logger) *Session {
	if maxActiveSnapshots == 0 {
		maxActiveSnapshots = store.DefaultMaxActiveTransactions
	}

	now := time.Now()
	lruCache, _ := cache.NewLRUCache(maxActiveSnapshots)

	return &Session{
		id:                       sessionID,
		user:                     user,
		database:                 db,
		creationTime:             now,
		lastActivityTime:         now,
		transactions:             make(map[string]transactions.Transaction),
		log:                      log,
		paginatedDocumentReaders: lruCache,
	}
}

func (s *Session) NewTransaction(ctx context.Context, opts *sql.TxOptions) (transactions.Transaction, error) {
	s.mux.Lock()

Review comment (performance):
`documentReaders *cache.LRUCache // track searchID to document.DocumentReader objects` ?

here is doesn't need to know it's a paginated reader


Code after:
	"github.com/codenotary/immudb/pkg/logger"
	"github.com/codenotary/immudb/pkg/server/sessions/internal/transactions"
	"google.golang.org/grpc/metadata"
)

// DefaultMaxDocumentReadersCacheSize is the default maximum number of document readers to keep in cache
const DefaultMaxDocumentReadersCacheSize = 1

var (
	ErrPaginatedDocumentReaderNotFound = errors.New("document reader not found")
)

type PaginatedDocumentReader struct {
	Reader         document.DocumentReader // reader to read from
	Query          *protomodel.Query
	LastPageNumber uint32 // last read page number
	LastPageSize   uint32 // number of items per page
}

type Session struct {
	mux              sync.RWMutex
	id               string
	user             *auth.User
	database         database.DB
	creationTime     time.Time
	lastActivityTime time.Time
	transactions     map[string]transactions.Transaction
	documentReaders  *cache.LRUCache // track searchID to document.DocumentReader
	log              logger.Logger
}

func NewSession(sessionID string, user *auth.User, db database.DB, log logger.Logger) *Session {
	now := time.Now()
	lruCache, _ := cache.NewLRUCache(DefaultMaxDocumentReadersCacheSize)

	return &Session{
		id:               sessionID,
		user:             user,
		database:         db,
		creationTime:     now,
		lastActivityTime: now,
		transactions:     make(map[string]transactions.Transaction),
		log:              log,
		documentReaders:  lruCache,
	}
}

func (s *Session) NewTransaction(ctx context.Context, opts *sql.TxOptions) (transactions.Transaction, error) {
	s.mux.Lock()
	defer s.mux.Unlock()