github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/istructsmem/internal/vers/impl.go (about)

     1  /*
     2   * Copyright (c) 2021-present Sigma-Soft, Ltd.
     3   * @author: Nikolay Nikitin
     4   */
     5  
     6  package vers
     7  
     8  import (
     9  	"context"
    10  	"encoding/binary"
    11  
    12  	"github.com/voedger/voedger/pkg/istorage"
    13  	"github.com/voedger/voedger/pkg/istructsmem/internal/consts"
    14  	"github.com/voedger/voedger/pkg/istructsmem/internal/utils"
    15  )
    16  
    17  func newVersions() *Versions {
    18  	return &Versions{vers: make(map[VersionKey]VersionValue)}
    19  }
    20  
    21  // Returns version value for version key
    22  func (vers *Versions) Get(key VersionKey) VersionValue {
    23  	return vers.vers[key]
    24  }
    25  
    26  // Stores version value for version key into application storage
    27  func (vers *Versions) Put(key VersionKey, value VersionValue) (err error) {
    28  	vers.vers[key] = value
    29  
    30  	return vers.storage.Put(
    31  		utils.ToBytes(consts.SysView_Versions),
    32  		utils.ToBytes(uint16(key)),
    33  		utils.ToBytes(uint16(value)),
    34  	)
    35  }
    36  
    37  // Prepares cache for all versions of system views
    38  func (vers *Versions) Prepare(storage istorage.IAppStorage) (err error) {
    39  	vers.storage = storage
    40  	pKey := utils.ToBytes(consts.SysView_Versions)
    41  	return vers.storage.Read(context.Background(), pKey, nil, nil,
    42  		func(cCols, value []byte) (_ error) {
    43  			key := VersionKey(binary.BigEndian.Uint16(cCols))
    44  			val := VersionValue(binary.BigEndian.Uint16(value))
    45  			vers.vers[key] = val
    46  			return nil
    47  		})
    48  }