3636
3737import javax .xml .bind .DatatypeConverter ;
3838import org .apache .commons .codec .digest .DigestUtils ;
39- import org .apache .hadoop . hdds . client . DefaultReplicationConfig ;
39+ import org .apache .commons . lang3 . StringUtils ;
4040import org .apache .hadoop .hdds .client .RatisReplicationConfig ;
4141import org .apache .hadoop .hdds .client .ReplicationConfig ;
4242import org .apache .hadoop .hdds .client .ReplicationFactor ;
4343import org .apache .hadoop .hdds .client .ReplicationType ;
44- import org .apache .hadoop .hdds .protocol .StorageType ;
4544import org .apache .hadoop .hdds .protocol .proto .HddsProtos ;
4645import org .apache .hadoop .hdds .scm .storage .ByteBufferStreamOutput ;
4746import org .apache .hadoop .ozone .OzoneAcl ;
5554import org .apache .hadoop .ozone .om .helpers .OmMultipartInfo ;
5655import org .apache .hadoop .ozone .om .helpers .OmMultipartUploadCompleteInfo ;
5756import org .apache .hadoop .util .Time ;
57+ import org .slf4j .Logger ;
58+ import org .slf4j .LoggerFactory ;
5859
5960import static org .apache .hadoop .ozone .OzoneConsts .ETAG ;
6061import static org .apache .hadoop .ozone .OzoneConsts .MD5_HASH ;
6364/**
6465 * In-memory ozone bucket for testing.
6566 */
66- public class OzoneBucketStub extends OzoneBucket {
67+ public final class OzoneBucketStub extends OzoneBucket {
68+
69+ private static final Logger LOG = LoggerFactory .getLogger (OzoneBucketStub .class );
6770
6871 private Map <String , OzoneKeyDetails > keyDetails = new HashMap <>();
6972
@@ -80,7 +83,7 @@ public static Builder newBuilder() {
8083 return new Builder ();
8184 }
8285
83- public OzoneBucketStub (Builder b ) {
86+ private OzoneBucketStub (Builder b ) {
8487 super (b );
8588 this .replicationConfig = super .getReplicationConfig ();
8689 }
@@ -93,43 +96,6 @@ public static final class Builder extends OzoneBucket.Builder {
9396 private Builder () {
9497 }
9598
96- @ Override
97- public Builder setVolumeName (String volumeName ) {
98- super .setVolumeName (volumeName );
99- return this ;
100- }
101-
102- @ Override
103- public Builder setName (String name ) {
104- super .setName (name );
105- return this ;
106- }
107-
108- @ Override
109- public Builder setDefaultReplicationConfig (
110- DefaultReplicationConfig defaultReplicationConfig ) {
111- super .setDefaultReplicationConfig (defaultReplicationConfig );
112- return this ;
113- }
114-
115- @ Override
116- public Builder setStorageType (StorageType storageType ) {
117- super .setStorageType (storageType );
118- return this ;
119- }
120-
121- @ Override
122- public Builder setVersioning (Boolean versioning ) {
123- super .setVersioning (versioning );
124- return this ;
125- }
126-
127- @ Override
128- public Builder setCreationTime (long creationTime ) {
129- super .setCreationTime (creationTime );
130- return this ;
131- }
132-
13399 @ Override
134100 public OzoneBucketStub build () {
135101 return new OzoneBucketStub (this );
@@ -149,31 +115,16 @@ public OzoneOutputStream createKey(String key, long size,
149115 ReplicationFactor factor ,
150116 Map <String , String > metadata )
151117 throws IOException {
152- ByteArrayOutputStream byteArrayOutputStream =
153- new ByteArrayOutputStream ((int ) size ) {
154- @ Override
155- public void close () throws IOException {
156- keyContents .put (key , toByteArray ());
157- keyDetails .put (key , new OzoneKeyDetails (
158- getVolumeName (),
159- getName (),
160- key ,
161- size ,
162- System .currentTimeMillis (),
163- System .currentTimeMillis (),
164- new ArrayList <>(), replicationConfig , metadata , null ,
165- () -> readKey (key ), true
166- ));
167- super .close ();
168- }
169- };
170- return new OzoneOutputStream (byteArrayOutputStream , null );
118+ ReplicationConfig replication = ReplicationConfig .fromTypeAndFactor (type , factor );
119+ return createKey (key , size , replication , metadata );
171120 }
172121
173122 @ Override
174123 public OzoneOutputStream createKey (String key , long size ,
175124 ReplicationConfig rConfig , Map <String , String > metadata )
176125 throws IOException {
126+ assertDoesNotExist (key + "/" );
127+
177128 final ReplicationConfig repConfig ;
178129 if (rConfig == null ) {
179130 repConfig = getReplicationConfig ();
@@ -208,6 +159,8 @@ public OzoneDataStreamOutput createStreamKey(String key, long size,
208159 ReplicationConfig rConfig ,
209160 Map <String , String > keyMetadata )
210161 throws IOException {
162+ assertDoesNotExist (key + "/" );
163+
211164 ByteBufferStreamOutput byteBufferStreamOutput =
212165 new KeyMetadataAwareByteBufferStreamOutput (keyMetadata ) {
213166
@@ -611,6 +564,9 @@ public ReplicationConfig getReplicationConfig() {
611564
612565 @ Override
613566 public void createDirectory (String keyName ) throws IOException {
567+ assertDoesNotExist (StringUtils .stripEnd (keyName , "/" ));
568+
569+ LOG .info ("createDirectory({})" , keyName );
614570 keyDetails .put (keyName , new OzoneKeyDetails (
615571 getVolumeName (),
616572 getName (),
@@ -622,6 +578,12 @@ public void createDirectory(String keyName) throws IOException {
622578 () -> readKey (keyName ), false ));
623579 }
624580
581+ private void assertDoesNotExist (String keyName ) throws OMException {
582+ if (keyDetails .get (keyName ) != null ) {
583+ throw new OMException ("already exists" , ResultCodes .FILE_ALREADY_EXISTS );
584+ }
585+ }
586+
625587 /**
626588 * ByteArrayOutputStream stub with metadata.
627589 */
0 commit comments