package seaweedfs.hdfs;

import org.junit.Test;
import seaweed.hdfs.SeaweedRead;
import seaweedfs.client.FilerProto;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SeaweedReadTest {

    @Test
    public void testNonOverlappingVisibleIntervals() {
        List<FilerProto.FileChunk> chunks = new ArrayList<>();
        chunks.add(FilerProto.FileChunk.newBuilder()
            .setFileId("aaa")
            .setOffset(0)
            .setSize(100)
            .setMtime(1000)
            .build());
        chunks.add(FilerProto.FileChunk.newBuilder()
            .setFileId("bbb")
            .setOffset(100)
            .setSize(133)
            .setMtime(2000)
            .build());

        List<SeaweedRead.VisibleInterval> visibleIntervals = SeaweedRead.nonOverlappingVisibleIntervals(chunks);
        for (SeaweedRead.VisibleInterval visibleInterval : visibleIntervals) {
            System.out.println("visible:" + visibleInterval);
        }

        assertEquals(visibleIntervals.size(), 2);

        SeaweedRead.VisibleInterval visibleInterval = visibleIntervals.get(0);
        assertEquals(visibleInterval.start, 0);
        assertEquals(visibleInterval.stop, 100);
        assertEquals(visibleInterval.modifiedTime, 1000);
        assertEquals(visibleInterval.fileId, "aaa");

        visibleInterval = visibleIntervals.get(1);
        assertEquals(visibleInterval.start, 100);
        assertEquals(visibleInterval.stop, 233);
        assertEquals(visibleInterval.modifiedTime, 2000);
        assertEquals(visibleInterval.fileId, "bbb");

        List<SeaweedRead.ChunkView> chunkViews = SeaweedRead.viewFromVisibles(visibleIntervals, 0, 233);

        SeaweedRead.ChunkView chunkView = chunkViews.get(0);
        assertEquals(chunkView.offset, 0);
        assertEquals(chunkView.size, 100);
        assertEquals(chunkView.logicOffset, 0);
        assertEquals(chunkView.fileId, "aaa");

        chunkView = chunkViews.get(1);
        assertEquals(chunkView.offset, 0);
        assertEquals(chunkView.size, 133);
        assertEquals(chunkView.logicOffset, 100);
        assertEquals(chunkView.fileId, "bbb");


    }

}

@@ -18,16 +18,377 @@
 .hdfs;%0A%0A
+import org.apache.http.Header;%0Aimport org.apache.http.HttpEntity;%0Aimport org.apache.http.HttpHeaders;%0Aimport org.apache.http.client.methods.CloseableHttpResponse;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClientBuilder;%0Aimport org.apache.http.util.EntityUtils;%0A
 import o
@@ -472,16 +472,44 @@
 Proto;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -2579,10 +2579,1462 @@
 %0A    %7D%0A%0A
+    // test gzipped content with range requests. Conclusion: not doing this.%0A    public void testGzippedRangeRequest() throws IOException %7B%0A%0A        SeaweedRead.ChunkView chunkView = new SeaweedRead.ChunkView(%222,621a042be6e39d%22, 0, 28, 0);%0A        CloseableHttpClient client = HttpClientBuilder.create().build();%0A        String targetUrl = String.format(%22http://%25s/%25s%22, %22localhost:8080%22, chunkView.fileId);%0A        HttpGet request = new HttpGet(targetUrl);%0A        // request.removeHeaders(HttpHeaders.ACCEPT_ENCODING);%0A        request.setHeader(HttpHeaders.ACCEPT_ENCODING, %22%22);%0A        request.setHeader(HttpHeaders.RANGE, String.format(%22bytes=%25d-%25d%22, chunkView.offset, chunkView.offset + chunkView.size));%0A        System.out.println(%22request:%22);%0A        for (Header header : request.getAllHeaders()) %7B%0A            System.out.println(header.getName() + %22: %22 + header.getValue());%0A        %7D%0A%0A        int len = 29;%0A        byte%5B%5D buffer = new byte%5Blen%5D;%0A        CloseableHttpResponse response = null;%0A        try %7B%0A            response = client.execute(request);%0A            HttpEntity entity = response.getEntity();%0A            System.out.println(%22content length:%22 + entity.getContentLength());%0A            System.out.println(%22is streaming:%22 + entity.isStreaming());%0A            System.out.println(EntityUtils.toString(entity));%0A        %7D finally %7B%0A%0A            if (response != null) %7B%0A                response.close();%0A            %7D%0A        %7D%0A%0A    %7D%0A
 %7D%0A

add a local test