Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit d383a76

Browse files
authored
Commit to metadata table for state analysis (#106)
* Commit to metadata table * Fix schema * Fix schema * Fix schema
1 parent de1d7fe commit d383a76

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

tools/cmd/seidb/operations/state_size.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func StateSizeCmd() *cobra.Command {
2525

2626
// DynamoDB export flags
2727
cmd.PersistentFlags().Bool("export-dynamodb", false, "Export results to DynamoDB instead of printing")
28-
cmd.PersistentFlags().String("dynamodb-table", "state-size-analysis", "DynamoDB table name")
28+
cmd.PersistentFlags().String("dynamodb-table", "state_size_analysis", "DynamoDB table name")
2929
cmd.PersistentFlags().String("aws-region", "us-east-2", "AWS region for DynamoDB")
3030

3131
return cmd
@@ -218,7 +218,9 @@ func exportResultsToDynamoDB(moduleResults map[string]*ModuleResult, height int6
218218
return fmt.Errorf("failed to export analyses to DynamoDB: %w", err)
219219
}
220220

221-
return nil
221+
metadataTableName := tableName + "_metadata"
222+
_, err = dynamoClient.UpdateLatestHeightIfGreater(metadataTableName, height)
223+
return err
222224
}
223225

224226
// printResultsToConsole prints the collected results to console

tools/utils/dynamo.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package utils
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/awserr"
79
"github.com/aws/aws-sdk-go/aws/session"
810
"github.com/aws/aws-sdk-go/service/dynamodb"
911
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
@@ -50,6 +52,7 @@ type ContractSizeEntry struct {
5052
KeyCount uint64 `json:"key_count"`
5153
}
5254

55+
// PrefixSize is a helper structure kept for completeness (unused here)
5356
type PrefixSize struct {
5457
KeySize uint64 `json:"key_size"`
5558
ValueSize uint64 `json:"value_size"`
@@ -88,3 +91,32 @@ func (d *DynamoDBClient) ExportMultipleAnalyses(analyses []*StateSizeAnalysis) e
8891
}
8992
return nil
9093
}
94+
95+
// UpdateLatestHeightIfGreater keeps exactly one item in the metadata table using the schema:
96+
// Partition key: keyname (S). Numeric attribute: height (N) stores the latest height.
97+
// Upserts the row keyname = "latest_height" and sets height = :h only if missing or lower.
98+
func (d *DynamoDBClient) UpdateLatestHeightIfGreater(metadataTable string, height int64) (bool, error) {
99+
input := &dynamodb.UpdateItemInput{
100+
TableName: aws.String(metadataTable),
101+
Key: map[string]*dynamodb.AttributeValue{
102+
"keyname": {S: aws.String("latest_height")},
103+
},
104+
UpdateExpression: aws.String("SET height = :h"),
105+
ConditionExpression: aws.String("attribute_not_exists(height) OR height < :h"),
106+
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
107+
":h": {N: aws.String(fmt.Sprintf("%d", height))},
108+
},
109+
ReturnValues: aws.String("NONE"),
110+
}
111+
112+
_, err := d.client.UpdateItem(input)
113+
if err != nil {
114+
var aerr awserr.Error
115+
if errors.As(err, &aerr) && aerr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
116+
return false, nil
117+
}
118+
return false, fmt.Errorf("failed to update latest height: %w", err)
119+
}
120+
fmt.Printf("Updated Dynamodb with latest height %d\n", height)
121+
return true, nil
122+
}

0 commit comments

Comments
 (0)