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

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

const DefaultMaxDocumentReadersCacheSize = 5

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 sql.RowReader objects
	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()

	tx, err := transactions.NewTransaction(ctx, opts, s.database, s.id)
	if err != nil {

Review comment (performance):
```suggestion
	documentReaders  *cache.LRUCache // track searchID to document.DocumentReader
```

Code after:
	"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()

	tx, err := transactions.NewTransaction(ctx, opts, s.database, s.id)