You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@fluo.apache.org by gi...@git.apache.org on 2017/07/21 18:52:16 UTC

[GitHub] keith-turner commented on a change in pull request #884: fixes #867 - Stream support for Scanners

keith-turner commented on a change in pull request #884: fixes #867 - Stream support for Scanners
URL: https://github.com/apache/incubator-fluo/pull/884#discussion_r128834632
 
 

 ##########
 File path: modules/api/src/test/java/org/apache/fluo/api/client/scanner/ScannerStreamTest.java
 ##########
 @@ -0,0 +1,219 @@
+/*
+ * 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.fluo.api.client.scanner;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.fluo.api.data.Bytes;
+import org.apache.fluo.api.data.Column;
+import org.apache.fluo.api.data.ColumnValue;
+import org.apache.fluo.api.data.RowColumnValue;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @since 1.2.0
+ */
+public class ScannerStreamTest {
+
+  @Test
+  public void testCellScannerStream() {
+    Set<RowColumnValue> rowCols = new HashSet<>();
+    Set<RowColumnValue> empty = new HashSet<>();
+
+    rowCols.add(new RowColumnValue("r1", new Column("f1", "q1"), "v1"));
+    rowCols.add(new RowColumnValue("r1", new Column("f2", "q3"), "v2"));
+    rowCols.add(new RowColumnValue("r2", new Column("f1", "q1"), "v3"));
+    rowCols.add(new RowColumnValue("r2", new Column("f1", "q2"), "v4"));
+    rowCols.add(new RowColumnValue("r4", new Column("f2", "q5"), "v5"));
+
+    CellScannerImpl cellScanStream = new CellScannerImpl(rowCols);
+
+    rowCols.stream().filter(rcv -> rcv.getColumn().getsFamily().equals("f2"))
+        .collect(Collectors.toSet());
+    cellScanStream.stream().filter(rcv -> rcv.getColumn().getsFamily().equals("f2"))
+        .collect(Collectors.toSet());
+
+    Assert.assertNotEquals(empty, cellScanStream.stream().collect(Collectors.toSet()));
+    Assert.assertEquals(rowCols, cellScanStream.stream().collect(Collectors.toSet()));
+    Assert.assertEquals(rowCols.size(), cellScanStream.stream().count());
+  }
+
+  @Test
+  public void testColumnScannerStream() {
+    Set<ColumnValue> colsVal = new HashSet<>();
+    Set<ColumnValue> empty = new HashSet<>();
+
+    Bytes row = Bytes.of("123");
+    colsVal.add(new ColumnValue(new Column("f1", "q1"), Bytes.of("v1")));
+    colsVal.add(new ColumnValue(new Column("f2", "q3"), Bytes.of("v2")));
+    colsVal.add(new ColumnValue(new Column("f1", "q1"), Bytes.of("v3")));
+    colsVal.add(new ColumnValue(new Column("f1", "q2"), Bytes.of("v4")));
+    colsVal.add(new ColumnValue(new Column("f2", "q5"), Bytes.of("v5")));
+
+    ColumnScanner colScanStream = new ColumnScannerImpl(row, colsVal);
+
+    colsVal.stream().filter(cv -> cv.getColumn().getsFamily().equals("f2"))
+        .collect(Collectors.toSet());
+    colScanStream.stream().filter(cv -> cv.getColumn().getsFamily().equals("f2"))
+        .collect(Collectors.toSet());
+
+    Assert.assertNotEquals(empty, colScanStream.stream().collect(Collectors.toSet()));
+    Assert.assertEquals(colsVal, colScanStream.stream().collect(Collectors.toSet()));
+    Assert.assertEquals(colsVal.size(), colScanStream.stream().count());
+  }
+
+  @Test
+  public void testRowScannerStream() {
+    List<ColumnScanner> rows = new ArrayList<>();
+    Set<ColumnValue> cv1 = new HashSet<>();
+    Set<ColumnValue> cv2 = new HashSet<>();
+
+    Bytes row1 = Bytes.of("555555555");
+    cv1.add(new ColumnValue(new Column("firstname"), Bytes.of("Chris")));
+    cv1.add(new ColumnValue(new Column("lastname"), Bytes.of("McTague")));
+    cv1.add(new ColumnValue(new Column("age"), Bytes.of("21")));
+
+    rows.add(new ColumnScannerImpl(row1, cv1));
+
+    Bytes row2 = Bytes.of("55234234");
+    cv2.add(new ColumnValue(new Column("firstname"), Bytes.of("Hulk")));
+    cv2.add(new ColumnValue(new Column("lastname"), Bytes.of("Hogan")));
+    cv2.add(new ColumnValue(new Column("age"), Bytes.of("60")));
+
+    rows.add(new ColumnScannerImpl(row2, cv2));
+
+    RowScannerImpl rsi = new RowScannerImpl(rows);
+
+    Set<Person> people = rsi.stream().map(cs -> toPerson(cs)).collect(Collectors.toSet());
+
+    Set<Person> expected = new HashSet<>();
+    expected.add(new Person("Chris", "McTague", 21, 555555555));
+    expected.add(new Person("Hulk", "Hogan", 60, 55234234));
+
+    Assert.assertEquals(expected.hashCode(), people.hashCode());
+    Assert.assertEquals(expected.size(), people.size());
+  }
+
+  private static Person toPerson(ColumnScanner cs) {
+    Person p = new Person();
+    p.id = Integer.parseInt(cs.getsRow());
+
+    for (ColumnValue cv : cs) {
+      switch (cv.getColumn().getsFamily()) {
+        case "firstname": {
+          p.firstname = cv.getsValue();
+          break;
+        }
+        case "lastname": {
+          p.lastname = cv.getsValue();
+          break;
+        }
+        case "age": {
+          p.age = Integer.parseInt(cv.getsValue());
+          break;
+        }
+        default: {
+          throw new IllegalArgumentException("unknown column " + cv.getColumn());
+        }
+      }
+    }
+    return p;
+  }
+
+  private static class CellScannerImpl implements CellScanner {
+    private Collection<RowColumnValue> rcv;
+
+    CellScannerImpl(Collection<RowColumnValue> rcv) {
+      this.rcv = rcv;
+    }
+
+    @Override
+    public Iterator<RowColumnValue> iterator() {
+      return rcv.iterator();
+    }
+  }
+
+  private static class RowScannerImpl implements RowScanner {
+    private Collection<ColumnScanner> scan;
+
+    RowScannerImpl(Collection<ColumnScanner> scan) {
+      this.scan = scan;
+    }
+
+    @Override
+    public Iterator<ColumnScanner> iterator() {
+      return scan.iterator();
+    }
+  }
+
+  private static class ColumnScannerImpl implements ColumnScanner {
+    private Collection<ColumnValue> scan;
+    private Bytes row;
+
+    ColumnScannerImpl(Bytes row, Collection<ColumnValue> scan) {
+      this.row = row;
+      this.scan = scan;
+    }
+
+    @Override
+    public Iterator<ColumnValue> iterator() {
+      return scan.iterator();
+    }
+
+    @Override
+    public Bytes getRow() {
+      return row;
+    }
+
+    @Override
+    public String getsRow() {
+      return row.toString();
+    }
+  }
+
+  private static class Person {
+    private String firstname;
+    private String lastname;
+    private int age;
+    private int id;
+
+    Person() {
+      this.firstname = "";
+      this.lastname = "";
+      this.age = 0;
+      this.id = 0;
+    }
+
+    Person(String f, String l, int a, int id) {
+      this.firstname = f;
+      this.lastname = l;
+      this.age = a;
+      this.id = id;
+    }
+
 
 Review comment:
   Since these are being placed in a set, also need to implement equals method.
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services