MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / updateBalanceSet

Function updateBalanceSet

database/balance.go:915–949  ·  view source on GitHub ↗
(ctx context.Context, tx *sql.Tx, balances []*model.Balance)

Source from the content-addressed store, hash-verified

913}
914
915func updateBalanceSet(ctx context.Context, tx *sql.Tx, balances []*model.Balance) error {
916 seen := make(map[string]struct{}, len(balances))
917 uniqueBalances := make([]*model.Balance, 0, len(balances))
918 for _, balance := range balances {
919 if balance == nil || balance.BalanceID == "" {
920 continue
921 }
922 if _, ok := seen[balance.BalanceID]; ok {
923 continue
924 }
925 seen[balance.BalanceID] = struct{}{}
926 uniqueBalances = append(uniqueBalances, balance)
927 }
928
929 if len(uniqueBalances) == 0 {
930 return nil
931 }
932
933 for start := 0; start < len(uniqueBalances); start += maxBalancesPerUpdateChunk {
934 end := start + maxBalancesPerUpdateChunk
935 if end > len(uniqueBalances) {
936 end = len(uniqueBalances)
937 }
938
939 if err := updateBalanceChunk(ctx, tx, uniqueBalances[start:end]); err != nil {
940 return err
941 }
942 }
943
944 for _, balance := range uniqueBalances {
945 balance.Version++
946 }
947
948 return nil
949}
950
951func updateBalanceChunk(ctx context.Context, tx *sql.Tx, balances []*model.Balance) error {
952 if len(balances) == 0 {

Calls 1

updateBalanceChunkFunction · 0.85