Skip to content

Commit

Permalink
feat: combine logs before insertion (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored and flemzord committed May 12, 2023
1 parent dfc70d5 commit 45730e8
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 304 deletions.
6 changes: 4 additions & 2 deletions cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ func TestContainers(t *testing.T) {
}
errCh := make(chan error, 1)
go func() {
waitAndPostProcess := l.SaveMeta(ctx, core.MetaTargetTypeAccount, "world", core.Metadata{"foo": []byte(`"bar"`)})
err := waitAndPostProcess(ctx)
logs, err := l.SaveMeta(ctx, core.MetaTargetTypeAccount, "world", core.Metadata{"foo": []byte(`"bar"`)})
if err == nil {
err = logs.Wait(ctx)
}
if err != nil {
errCh <- err
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/api/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ func (ctl *AccountController) PostAccountMetadata(w http.ResponseWriter, r *http
return
}

waitAndPostProcess := l.SaveMeta(r.Context(),
logs, err := l.SaveMeta(r.Context(),
core.MetaTargetTypeAccount, chi.URLParam(r, "address"), m)
if err := waitAndPostProcess(r.Context()); err != nil {
if err != nil {
apierrors.ResponseError(w, r, err)
return
}

if err := logs.Wait(r.Context()); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand Down
37 changes: 29 additions & 8 deletions pkg/api/controllers/transaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,13 @@ func (ctl *TransactionController) PostTransaction(w http.ResponseWriter, r *http
Reference: payload.Reference,
Metadata: payload.Metadata,
}
res, waitAndPostProcess := l.ExecuteScript(r.Context(), preview, core.TxToScriptData(txData))
if err := waitAndPostProcess(r.Context()); err != nil {
res, logs, err := l.ProcessScript(r.Context(), true, preview, core.TxToScriptData(txData))
if err != nil {
apierrors.ResponseError(w, r, err)
return
}

if err := logs.Wait(r.Context()); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand All @@ -216,8 +221,14 @@ func (ctl *TransactionController) PostTransaction(w http.ResponseWriter, r *http
Reference: payload.Reference,
Metadata: payload.Metadata,
}
res, waitAndPostProcess := l.ExecuteScript(r.Context(), preview, script)
if err := waitAndPostProcess(r.Context()); err != nil {

res, logs, err := l.ProcessScript(r.Context(), true, preview, script)
if err != nil {
apierrors.ResponseError(w, r, err)
return
}

if err := logs.Wait(r.Context()); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand Down Expand Up @@ -252,8 +263,13 @@ func (ctl *TransactionController) RevertTransaction(w http.ResponseWriter, r *ht
return
}

tx, waitAndPostProcess := l.RevertTransaction(r.Context(), txId)
if err := waitAndPostProcess(r.Context()); err != nil {
tx, logs, err := l.RevertTransaction(r.Context(), txId)
if err != nil {
apierrors.ResponseError(w, r, err)
return
}

if err := logs.Wait(r.Context()); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand Down Expand Up @@ -282,9 +298,14 @@ func (ctl *TransactionController) PostTransactionMetadata(w http.ResponseWriter,
return
}

waitAndPostProcess := l.SaveMeta(r.Context(),
logs, err := l.SaveMeta(r.Context(),
core.MetaTargetTypeTransaction, txId, m)
if err := waitAndPostProcess(r.Context()); err != nil {
if err != nil {
apierrors.ResponseError(w, r, err)
return
}

if err := logs.Wait(r.Context()); err != nil {
apierrors.ResponseError(w, r, err)
return
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/ledger/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func BenchmarkLedger_PostTransactions_Scripts_Single_FixedAccounts(b *testing.B)
b.StopTimer()
script := txToScriptData(txData)
b.StartTimer()
_res, waitAndPostProcess := l.ExecuteScript(context.Background(), true, script)
require.NoError(b, waitAndPostProcess(context.Background()))
_res, logs, err := l.ProcessScript(context.Background(), true, true, script)
require.NoError(b, err)
require.NoError(b, logs.Wait(context.Background()))
require.Len(b, res.Postings, nbPostings)
res = _res
}
Expand Down Expand Up @@ -70,8 +71,9 @@ func BenchmarkLedger_PostTransactions_Postings_Single_FixedAccounts(b *testing.B
_, err := txData.Postings.Validate()
require.NoError(b, err)

tx, waitAndPostProcess := l.ExecuteScript(context.Background(), true, core.TxToScriptData(txData))
require.NoError(b, waitAndPostProcess(context.Background()))
tx, logs, err := l.ProcessScript(context.Background(), true, true, core.TxToScriptData(txData))
require.NoError(b, err)
require.NoError(b, logs.Wait(context.Background()))
require.Len(b, res, 1)
require.Len(b, res[0].Postings, nbPostings)
res = append(res, tx)
Expand Down Expand Up @@ -99,8 +101,9 @@ func BenchmarkLedger_PostTransactions_Postings_Batch_FixedAccounts(b *testing.B)
require.NoError(b, err)
}
for _, script := range core.TxsToScriptsData(txsData...) {
tx, waitAndPostProcess := l.ExecuteScript(context.Background(), true, script)
require.NoError(b, waitAndPostProcess(context.Background()))
tx, logs, err := l.ProcessScript(context.Background(), true, true, script)
require.NoError(b, err)
require.NoError(b, logs.Wait(context.Background()))
res = append(res, tx)
}

Expand Down Expand Up @@ -144,8 +147,9 @@ func BenchmarkLedger_PostTransactions_Postings_Batch_VaryingAccounts(b *testing.
require.NoError(b, err)
}
for _, script := range core.TxsToScriptsData(txsData...) {
tx, waitAndPostProcess := l.ExecuteScript(context.Background(), true, script)
require.NoError(b, waitAndPostProcess(context.Background()))
tx, logs, err := l.ProcessScript(context.Background(), true, true, script)
require.NoError(b, err)
require.NoError(b, logs.Wait(context.Background()))
res = append(res, tx)
}
require.Len(b, res, 7)
Expand Down
Loading

0 comments on commit 45730e8

Please sign in to comment.