func benchPutItemParallel(p, c int, b *testing.B) { svc := dynamodb.New(&aws.Config{ DisableSSL: aws.Bool(true), }) av, err := dynamodbattribute.ConvertToMap(dbItem{Key: "MyKey", Data: "MyData"}) if err != nil { b.Fatal("expect no ConvertToMap errors", err) } params := &dynamodb.PutItemInput{ Item: av, TableName: aws.String(testTableName), } b.N = c b.ResetTimer() b.SetParallelism(p) b.RunParallel(func(pb *testing.PB) { for pb.Next() { _, err = svc.PutItem(params) if err != nil { b.Error("expect no request errors", err) } } }) }
func BenchmarkPutItem(b *testing.B) { cfg := aws.Config{ Region: aws.String("us-east-1"), DisableSSL: aws.Bool(true), Credentials: credentials.NewStaticCredentials("AKID", "SECRET", ""), } server := successRespServer([]byte(`{}`)) cfg.Endpoint = aws.String(server.URL) svc := dynamodb.New(&cfg) svc.Handlers.Send.Clear() svc.Handlers.Send.PushBack(func(r *request.Request) { r.HTTPResponse = &http.Response{ StatusCode: http.StatusOK, Status: http.StatusText(http.StatusOK), Body: noopBody, } }) b.ResetTimer() for i := 0; i < b.N; i++ { av, err := dynamodbattribute.ConvertToMap(dbItem{Key: "MyKey", Data: "MyData"}) if err != nil { b.Fatal("benchPutItem, expect no ConvertToMap errors", err) } params := &dynamodb.PutItemInput{ Item: av, TableName: aws.String("tablename"), } _, err = svc.PutItem(params) if err != nil { b.Error("benchPutItem, expect no request errors", err) } } }
// Use DynamoDB methods for simplicity func TestPaginationEachPage(t *testing.T) { db := dynamodb.New(nil) tokens, pages, numPages, gotToEnd := []string{}, []string{}, 0, false reqNum := 0 resps := []*dynamodb.ListTablesOutput{ {TableNames: []*string{aws.String("Table1"), aws.String("Table2")}, LastEvaluatedTableName: aws.String("Table2")}, {TableNames: []*string{aws.String("Table3"), aws.String("Table4")}, LastEvaluatedTableName: aws.String("Table4")}, {TableNames: []*string{aws.String("Table5")}}, } db.Handlers.Send.Clear() // mock sending db.Handlers.Unmarshal.Clear() db.Handlers.UnmarshalMeta.Clear() db.Handlers.ValidateResponse.Clear() db.Handlers.Build.PushBack(func(r *request.Request) { in := r.Params.(*dynamodb.ListTablesInput) if in == nil { tokens = append(tokens, "") } else if in.ExclusiveStartTableName != nil { tokens = append(tokens, *in.ExclusiveStartTableName) } }) db.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = resps[reqNum] reqNum++ }) params := &dynamodb.ListTablesInput{Limit: aws.Int64(2)} req, _ := db.ListTablesRequest(params) err := req.EachPage(func(p interface{}, last bool) bool { numPages++ for _, t := range p.(*dynamodb.ListTablesOutput).TableNames { pages = append(pages, *t) } if last { if gotToEnd { assert.Fail(t, "last=true happened twice") } gotToEnd = true } return true }) assert.Equal(t, []string{"Table2", "Table4"}, tokens) assert.Equal(t, []string{"Table1", "Table2", "Table3", "Table4", "Table5"}, pages) assert.Equal(t, 3, numPages) assert.True(t, gotToEnd) assert.Nil(t, err) }
func ExampleDynamoDB_DescribeTable() { svc := dynamodb.New(nil) params := &dynamodb.DescribeTableInput{ TableName: aws.String("TableName"), // Required } resp, err := svc.DescribeTable(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleDynamoDB_ListTables() { svc := dynamodb.New(nil) params := &dynamodb.ListTablesInput{ ExclusiveStartTableName: aws.String("TableName"), Limit: aws.Int64(1), } resp, err := svc.ListTables(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
// Use DynamoDB methods for simplicity func TestPaginationEarlyExit(t *testing.T) { db := dynamodb.New(nil) numPages, gotToEnd := 0, false reqNum := 0 resps := []*dynamodb.ListTablesOutput{ {TableNames: []*string{aws.String("Table1"), aws.String("Table2")}, LastEvaluatedTableName: aws.String("Table2")}, {TableNames: []*string{aws.String("Table3"), aws.String("Table4")}, LastEvaluatedTableName: aws.String("Table4")}, {TableNames: []*string{aws.String("Table5")}}, } db.Handlers.Send.Clear() // mock sending db.Handlers.Unmarshal.Clear() db.Handlers.UnmarshalMeta.Clear() db.Handlers.ValidateResponse.Clear() db.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = resps[reqNum] reqNum++ }) params := &dynamodb.ListTablesInput{Limit: aws.Int64(2)} err := db.ListTablesPages(params, func(p *dynamodb.ListTablesOutput, last bool) bool { numPages++ if numPages == 2 { return false } if last { if gotToEnd { assert.Fail(t, "last=true happened twice") } gotToEnd = true } return true }) assert.Equal(t, 2, numPages) assert.False(t, gotToEnd) assert.Nil(t, err) }
{TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE"), aws.String("NXT")}, LastEvaluatedTableName: aws.String("NXT")}, {TableNames: []*string{aws.String("TABLE")}}, } var benchDb = func() *dynamodb.DynamoDB { db := dynamodb.New(nil) db.Handlers.Send.Clear() // mock sending db.Handlers.Unmarshal.Clear() db.Handlers.UnmarshalMeta.Clear() db.Handlers.ValidateResponse.Clear() return db } func BenchmarkCodegenIterator(b *testing.B) { reqNum := 0 db := benchDb() db.Handlers.Unmarshal.PushBack(func(r *request.Request) { r.Data = benchResps[reqNum] reqNum++ })
func TestInterface(t *testing.T) { assert.Implements(t, (*dynamodbiface.DynamoDBAPI)(nil), dynamodb.New(nil)) }
func ExampleDynamoDB_Scan() { svc := dynamodb.New(nil) params := &dynamodb.ScanInput{ TableName: aws.String("TableName"), // Required AttributesToGet: []*string{ aws.String("AttributeName"), // Required // More values... }, ConditionalOperator: aws.String("ConditionalOperator"), ConsistentRead: aws.Bool(true), ExclusiveStartKey: map[string]*dynamodb.AttributeValue{ "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, ExpressionAttributeNames: map[string]*string{ "Key": aws.String("AttributeName"), // Required // More values... }, ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, FilterExpression: aws.String("ConditionExpression"), IndexName: aws.String("IndexName"), Limit: aws.Int64(1), ProjectionExpression: aws.String("ProjectionExpression"), ReturnConsumedCapacity: aws.String("ReturnConsumedCapacity"), ScanFilter: map[string]*dynamodb.Condition{ "Key": { // Required ComparisonOperator: aws.String("ComparisonOperator"), // Required AttributeValueList: []*dynamodb.AttributeValue{ { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, }, // More values... }, Segment: aws.Int64(1), Select: aws.String("Select"), TotalSegments: aws.Int64(1), } resp, err := svc.Scan(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleDynamoDB_BatchWriteItem() { svc := dynamodb.New(nil) params := &dynamodb.BatchWriteItemInput{ RequestItems: map[string][]*dynamodb.WriteRequest{ // Required "Key": { // Required { // Required DeleteRequest: &dynamodb.DeleteRequest{ Key: map[string]*dynamodb.AttributeValue{ // Required "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, }, PutRequest: &dynamodb.PutRequest{ Item: map[string]*dynamodb.AttributeValue{ // Required "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, }, }, // More values... }, // More values... }, ReturnConsumedCapacity: aws.String("ReturnConsumedCapacity"), ReturnItemCollectionMetrics: aws.String("ReturnItemCollectionMetrics"), } resp, err := svc.BatchWriteItem(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleDynamoDB_PutItem() { svc := dynamodb.New(nil) params := &dynamodb.PutItemInput{ Item: map[string]*dynamodb.AttributeValue{ // Required "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, TableName: aws.String("TableName"), // Required ConditionExpression: aws.String("ConditionExpression"), ConditionalOperator: aws.String("ConditionalOperator"), Expected: map[string]*dynamodb.ExpectedAttributeValue{ "Key": { // Required AttributeValueList: []*dynamodb.AttributeValue{ { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, ComparisonOperator: aws.String("ComparisonOperator"), Exists: aws.Bool(true), Value: &dynamodb.AttributeValue{ B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, }, // More values... }, ExpressionAttributeNames: map[string]*string{ "Key": aws.String("AttributeName"), // Required // More values... }, ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, ReturnConsumedCapacity: aws.String("ReturnConsumedCapacity"), ReturnItemCollectionMetrics: aws.String("ReturnItemCollectionMetrics"), ReturnValues: aws.String("ReturnValue"), } resp, err := svc.PutItem(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleDynamoDB_GetItem() { svc := dynamodb.New(nil) params := &dynamodb.GetItemInput{ Key: map[string]*dynamodb.AttributeValue{ // Required "Key": { // Required B: []byte("PAYLOAD"), BOOL: aws.Bool(true), BS: [][]byte{ []byte("PAYLOAD"), // Required // More values... }, L: []*dynamodb.AttributeValue{ { // Required // Recursive values... }, // More values... }, M: map[string]*dynamodb.AttributeValue{ "Key": { // Required // Recursive values... }, // More values... }, N: aws.String("NumberAttributeValue"), NS: []*string{ aws.String("NumberAttributeValue"), // Required // More values... }, NULL: aws.Bool(true), S: aws.String("StringAttributeValue"), SS: []*string{ aws.String("StringAttributeValue"), // Required // More values... }, }, // More values... }, TableName: aws.String("TableName"), // Required AttributesToGet: []*string{ aws.String("AttributeName"), // Required // More values... }, ConsistentRead: aws.Bool(true), ExpressionAttributeNames: map[string]*string{ "Key": aws.String("AttributeName"), // Required // More values... }, ProjectionExpression: aws.String("ProjectionExpression"), ReturnConsumedCapacity: aws.String("ReturnConsumedCapacity"), } resp, err := svc.GetItem(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func ExampleDynamoDB_CreateTable() { svc := dynamodb.New(nil) params := &dynamodb.CreateTableInput{ AttributeDefinitions: []*dynamodb.AttributeDefinition{ // Required { // Required AttributeName: aws.String("KeySchemaAttributeName"), // Required AttributeType: aws.String("ScalarAttributeType"), // Required }, // More values... }, KeySchema: []*dynamodb.KeySchemaElement{ // Required { // Required AttributeName: aws.String("KeySchemaAttributeName"), // Required KeyType: aws.String("KeyType"), // Required }, // More values... }, ProvisionedThroughput: &dynamodb.ProvisionedThroughput{ // Required ReadCapacityUnits: aws.Int64(1), // Required WriteCapacityUnits: aws.Int64(1), // Required }, TableName: aws.String("TableName"), // Required GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{ { // Required IndexName: aws.String("IndexName"), // Required KeySchema: []*dynamodb.KeySchemaElement{ // Required { // Required AttributeName: aws.String("KeySchemaAttributeName"), // Required KeyType: aws.String("KeyType"), // Required }, // More values... }, Projection: &dynamodb.Projection{ // Required NonKeyAttributes: []*string{ aws.String("NonKeyAttributeName"), // Required // More values... }, ProjectionType: aws.String("ProjectionType"), }, ProvisionedThroughput: &dynamodb.ProvisionedThroughput{ // Required ReadCapacityUnits: aws.Int64(1), // Required WriteCapacityUnits: aws.Int64(1), // Required }, }, // More values... }, LocalSecondaryIndexes: []*dynamodb.LocalSecondaryIndex{ { // Required IndexName: aws.String("IndexName"), // Required KeySchema: []*dynamodb.KeySchemaElement{ // Required { // Required AttributeName: aws.String("KeySchemaAttributeName"), // Required KeyType: aws.String("KeyType"), // Required }, // More values... }, Projection: &dynamodb.Projection{ // Required NonKeyAttributes: []*string{ aws.String("NonKeyAttributeName"), // Required // More values... }, ProjectionType: aws.String("ProjectionType"), }, }, // More values... }, StreamSpecification: &dynamodb.StreamSpecification{ StreamEnabled: aws.Bool(true), StreamViewType: aws.String("StreamViewType"), }, } resp, err := svc.CreateTable(params) if err != nil { // Print the error, cast err to awserr.Error to get the Code and // Message from an error. fmt.Println(err.Error()) return } // Pretty-print the response data. fmt.Println(resp) }
func init() { Before("@dynamodb", func() { World["client"] = dynamodb.New(nil) }) }