• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.benchmark.marshaller.dynamodb;
17 
18 import com.amazonaws.util.ImmutableMapParameter;
19 import java.nio.ByteBuffer;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Random;
25 
26 abstract class AbstractItemFactory<T> {
27     private static final String ALPHA = "abcdefghijklmnopqrstuvwxyz";
28 
29     private static final Random RNG = new Random();
30 
tiny()31     final Map<String, T> tiny() {
32         return ImmutableMapParameter.<String, T>builder()
33             .put("stringAttr", av(randomS()))
34             .build();
35     }
36 
small()37     final Map<String, T> small() {
38         return ImmutableMapParameter.<String, T>builder()
39             .put("stringAttr", av(randomS()))
40             .put("binaryAttr", av(randomB()))
41             .put("listAttr", av(Arrays.asList(
42                 av(randomS()),
43                 av(randomB()),
44                 av(randomS())
45             )))
46             .build();
47     }
48 
huge()49     final Map<String, T> huge() {
50         return ImmutableMapParameter.<String, T>builder()
51             .put("hashKey", av(randomS()))
52             .put("stringAttr", av(randomS()))
53             .put("binaryAttr", av(randomB()))
54             .put("listAttr", av(
55                 Arrays.asList(
56                     av(randomS()),
57                     av(randomS()),
58                     av(randomS()),
59                     av(randomS()),
60                     av(randomS()),
61                     av(randomS()),
62                     av(randomS()),
63                     av(randomS()),
64                     av(randomS()),
65                     av(randomS()),
66                     av(randomS()),
67                     av(randomS()),
68                     av(randomS()),
69                     av(randomS()),
70                     av(randomB()),
71                     av(Collections.singletonList(av(randomS()))),
72                     av(ImmutableMapParameter.of(
73                         "attrOne", av(randomS())
74                     )),
75                     av(Arrays.asList(
76                         av(randomS()),
77                         av(randomS()),
78                         av(randomS()),
79                         av(randomS()),
80                         av(randomS()),
81                         av(randomS()),
82                         av(randomS()),
83                         av(randomS()),
84                         av(randomS()),
85                         av(randomS()),
86                         av(randomS()),
87                         av(randomS()),
88                         av(randomS()),
89                         av(randomS()),
90                         av(randomB()),
91                         (av(randomS())),
92                         av(ImmutableMapParameter.of(
93                             "attrOne",
94                             av(randomS())
95                         ))
96                     ))
97                 )
98             ))
99             .put("mapAttr", av(
100                 ImmutableMapParameter.<String, T>builder()
101                     .put("attrOne", av(randomS()))
102                     .put("attrTwo", av(randomB()))
103                     .put("attrThree", av(
104                         Arrays.asList(
105                             av(randomS()),
106                             av(randomS()),
107                             av(randomS()),
108                             av(randomS()),
109                             av(ImmutableMapParameter.<String, T>builder()
110                                    .put("attrOne", av(randomS()))
111                                    .put("attrTwo", av(randomB()))
112                                    .put("attrThree",
113                                         av(Arrays.asList(
114                                             av(randomS()),
115                                             av(randomS()),
116                                             av(randomS()),
117                                             av(randomS())
118                                         ))
119                                    )
120                                    .build())
121                         ))
122                     )
123                     .build()))
124             .build();
125     }
126 
127 
av(String val)128     protected abstract T av(String val);
129 
av(ByteBuffer val)130     protected abstract T av(ByteBuffer val);
131 
av(List<T> val)132     protected abstract T av(List<T> val);
133 
av(Map<String, T> val)134     protected abstract T av(Map<String, T> val);
135 
randomS(int len)136     private String randomS(int len) {
137         StringBuilder sb = new StringBuilder(len);
138         for (int i = 0; i < len; ++i) {
139             sb.append(ALPHA.charAt(RNG.nextInt(ALPHA.length())));
140         }
141         return sb.toString();
142     }
143 
randomS()144     private String randomS() {
145         return randomS(16);
146     }
147 
randomB(int len)148     private ByteBuffer randomB(int len) {
149         byte[] b = new byte[len];
150         RNG.nextBytes(b);
151         return ByteBuffer.wrap(b);
152     }
153 
randomB()154     private ByteBuffer randomB() {
155         return randomB(16);
156     }
157 }
158