Skip to content

Commit

Permalink
feat: add logs memento
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 7a23797 commit 9af9ddb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
16 changes: 8 additions & 8 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (l *Log) ComputeHash(previous *Log) {
}

payload := l.Data.(any)
if hv, ok := payload.(hashValuer); ok {
payload = hv.hashValue()
if hv, ok := payload.(Memento); ok {
payload = hv.GetMemento()
}

if err := enc.Encode(struct {
Expand Down Expand Up @@ -181,8 +181,8 @@ type LogPayload interface {
Type() LogType
}

type hashValuer interface {
hashValue() any
type Memento interface {
GetMemento() any
}

type AccountMetadata map[string]metadata.Metadata
Expand All @@ -198,7 +198,7 @@ func (p CreatedTransaction) Type() LogType {

var _ LogPayload = (*CreatedTransaction)(nil)

func (p CreatedTransaction) hashValue() any {
func (p CreatedTransaction) GetMemento() any {
// Exclude postCommitVolumes and postCommitEffectiveVolumes fields from transactions.
// We don't want those fields to be part of the hash as they are not part of the decision-making process.
return struct {
Expand All @@ -210,7 +210,7 @@ func (p CreatedTransaction) hashValue() any {
}
}

var _ hashValuer = (*CreatedTransaction)(nil)
var _ Memento = (*CreatedTransaction)(nil)

type SavedMetadata struct {
TargetType string `json:"targetType"`
Expand Down Expand Up @@ -315,7 +315,7 @@ func (r RevertedTransaction) Type() LogType {

var _ LogPayload = (*RevertedTransaction)(nil)

func (r RevertedTransaction) hashValue() any {
func (r RevertedTransaction) GetMemento() any {
return struct {
RevertedTransactionID int `json:"revertedTransactionID"`
RevertTransaction Transaction `json:"transaction"`
Expand All @@ -325,7 +325,7 @@ func (r RevertedTransaction) hashValue() any {
}
}

var _ hashValuer = (*RevertedTransaction)(nil)
var _ Memento = (*RevertedTransaction)(nil)

func HydrateLog(_type LogType, data []byte) (LogPayload, error) {
var payload any
Expand Down
2 changes: 2 additions & 0 deletions internal/storage/bucket/migrations/29-logs-add-memento.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "{{.Bucket}}".logs
add column memento bytea;
20 changes: 16 additions & 4 deletions internal/storage/ledger/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Log struct {

Ledger string `bun:"ledger,type:varchar"`
Data RawMessage `bun:"data,type:jsonb"`
Memento RawMessage `bun:"memento,type:bytea"`
}

func (log Log) toCore() ledger.Log {
Expand Down Expand Up @@ -72,17 +73,28 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error {
}

_, err := tracing.TraceWithLatency(ctx, "InsertLog", tracing.NoResult(func(ctx context.Context) error {
data, err := json.Marshal(log.Data)
payloadData, err := json.Marshal(log.Data)
if err != nil {
return fmt.Errorf("failed to marshal log data: %w", err)
}

mementoObject := log.Data.(any)
if memento, ok := mementoObject.(ledger.Memento); ok {
mementoObject = memento
}

mementoData, err := json.Marshal(mementoObject)
if err != nil {
return err
}

_, err = s.db.
NewInsert().
Model(&Log{
Log: log,
Ledger: s.ledger.Name,
Data: data,
Log: log,
Ledger: s.ledger.Name,
Data: payloadData,
Memento: mementoData,
}).
ModelTableExpr(s.GetPrefixedRelationName("logs")).
Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"log_id_%d"`, s.ledger.ID))).
Expand Down

0 comments on commit 9af9ddb

Please sign in to comment.