openinx commented on a change in pull request #3376:
URL: https://github.com/apache/iceberg/pull/3376#discussion_r808832299



##########
File path: 
dell/src/main/java/org/apache/iceberg/dell/ecs/EcsAppendOutputStream.java
##########
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.dell.ecs;
+
+import com.emc.object.s3.S3Client;
+import com.emc.object.s3.request.PutObjectRequest;
+import java.io.ByteArrayInputStream;
+import java.nio.ByteBuffer;
+import org.apache.iceberg.io.PositionOutputStream;
+
+/**
+ * Use ECS append API to write data.
+ */
+class EcsAppendOutputStream extends PositionOutputStream {
+
+  private final S3Client client;
+
+  private final EcsURI key;
+
+  /**
+   * Local bytes cache that avoid too many requests
+   * <p>
+   * Use {@link ByteBuffer} to maintain offset.
+   */
+  private final ByteBuffer localCache;
+
+  /**
+   * A marker for data file to put first part instead of append first part.
+   */
+  private boolean firstPart = true;
+
+  /**
+   * Pos for {@link PositionOutputStream}
+   */
+  private long pos;
+
+  private EcsAppendOutputStream(S3Client client, EcsURI key, byte[] 
localCache) {
+    this.client = client;
+    this.key = key;
+    this.localCache = ByteBuffer.wrap(localCache);
+  }
+
+  /**
+   * Use built-in 1 KiB byte buffer
+   */
+  static EcsAppendOutputStream create(S3Client client, EcsURI uri) {
+    return createWithBufferSize(client, uri, 1024);
+  }
+
+  /**
+   * Create {@link PositionOutputStream} with specific buffer size.
+   */
+  static EcsAppendOutputStream createWithBufferSize(S3Client client, EcsURI 
uri, int size) {
+    return new EcsAppendOutputStream(client, uri, new byte[size]);
+  }
+
+  /**
+   * Write a byte. If buffer is full, upload the buffer.
+   */
+  @Override
+  public void write(int b) {
+    if (!checkBuffer(1)) {
+      flush();
+    }
+
+    localCache.put((byte) b);
+    pos += 1;
+  }
+
+  /**
+   * Write a byte.
+   * If buffer is full, upload the buffer.
+   * If buffer size &lt; input bytes, upload input bytes.
+   */
+  @Override
+  public void write(byte[] b, int off, int len) {
+    if (!checkBuffer(len)) {
+      flush();
+    }
+
+    if (checkBuffer(len)) {
+      localCache.put(b, off, len);
+    } else {
+      // if content > cache, directly flush itself.
+      flushBuffer(b, off, len);
+    }
+
+    pos += len;
+  }
+
+  private boolean checkBuffer(int nextWrite) {
+    return localCache.remaining() >= nextWrite;
+  }
+
+  private void flushBuffer(byte[] buffer, int offset, int length) {
+    if (firstPart) {
+      client.putObject(new PutObjectRequest(key.bucket(), key.name(),
+          new ByteArrayInputStream(buffer, offset, length)));
+      firstPart = false;
+    } else {
+      client.appendObject(key.bucket(), key.name(), new 
ByteArrayInputStream(buffer, offset, length));

Review comment:
       Never mind, the current implementation for me.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to