@Test
  public void testGetMulitipleLayouts() throws Exception {
    final KijiTableLayout layout1 =
        KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));
    final KijiTableLayout layout2 =
        KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));
    final KijiTableLayout layout3 =
        KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));

    layout1.getDesc().setVersion("layout-1.0");
    layout2.getDesc().setVersion("layout-1.0.1");
    layout3.getDesc().setVersion("layout-1.1");

    final Get expectedGet =
        new Get(Bytes.toBytes(layout1.getDesc().getName()))
            .addColumn(
                Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT))
            .setMaxVersions(2);

    final List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(
        new KeyValue(
            Bytes.toBytes(layout3.getDesc().getName()),
            Bytes.toBytes(mFamily),
            Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT),
            3L,
            encode(layout3.getDesc())));
    kvs.add(
        new KeyValue(
            Bytes.toBytes(layout2.getDesc().getName()),
            Bytes.toBytes(mFamily),
            Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT),
            2L,
            encode(layout2.getDesc())));
    Result cannedResult = new Result(kvs);
    expect(mHTable.get(eqGet(expectedGet))).andReturn(cannedResult);

    replay(mHTable);

    NavigableMap<Long, KijiTableLayout> timedLayouts =
        mDb.getTimedTableLayoutVersions(layout1.getDesc().getName().toString(), 2);
    Set<Long> timestamps = timedLayouts.keySet();
    Iterator<Long> iterator = timestamps.iterator();

    assertEquals(2, timedLayouts.size());
    long time2 = iterator.next();
    long time3 = iterator.next();
    assertEquals(2L, time2);
    assertEquals(layout2, timedLayouts.get(time2));
    assertEquals(3L, time3);
    assertEquals(layout3, timedLayouts.get(time3));

    verify(mHTable);
  }
  @Test
  public void testRetrieveAll() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
    final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji());
    manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen());
    manager.storePolicy("user", "info:visits", TestProducer.class, new NeverFreshen());

    LOG.info(KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString());

    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(),
            "--do=retrieve-all"));

    assertEquals(
        "Freshness policy attached to column: info:visits\n"
            + "  Freshness policy class: org.kiji.scoring.lib.NeverFreshen\n"
            + "  Freshness policy state: \n"
            + "  Producer class: org.kiji.scoring.tools.TestFreshTool$TestProducer\n"
            + "Freshness policy attached to column: info:name\n"
            + "  Freshness policy class: org.kiji.scoring.lib.NeverFreshen\n"
            + "  Freshness policy state: \n"
            + "  Producer class: org.kiji.scoring.tools.TestFreshTool$TestProducer\n",
        mToolOutputStr);
  }
  @Test
  public void testGetLayout() throws Exception {
    final KijiTableLayout version1 =
        KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));

    final Get expectedGet =
        new Get(Bytes.toBytes(version1.getDesc().getName()))
            .addColumn(
                Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT))
            .setMaxVersions(1);
    final List<KeyValue> kvs = new ArrayList<KeyValue>();
    kvs.add(
        new KeyValue(
            Bytes.toBytes(version1.getDesc().getName()),
            Bytes.toBytes(mFamily),
            Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT),
            1L,
            encode(version1.getDesc())));
    final Result cannedResult = new Result(kvs);
    expect(mHTable.get(eqGet(expectedGet))).andReturn(cannedResult);

    replay(mHTable);

    assertEquals(version1, mDb.getTableLayout(version1.getDesc().getName().toString()));

    verify(mHTable);
  }
  @Test
  public void testSetLayout() throws Exception {
    final TableLayoutDesc layoutDesc = KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE);
    final KijiTableLayout layout = KijiTableLayout.newLayout(layoutDesc);

    final Get expectedGet =
        new Get(Bytes.toBytes(layout.getDesc().getName()))
            .addColumn(
                Bytes.toBytes(mFamily), Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT))
            .setMaxVersions(1);
    final Result expectedGetResult = new Result(Collections.<KeyValue>emptyList());
    expect(mHTable.get(eqGet(expectedGet))).andReturn(expectedGetResult);

    final Put expectedPut =
        new Put(Bytes.toBytes(layout.getDesc().getName()))
            .add(
                Bytes.toBytes(mFamily),
                Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_UPDATE),
                encode(layoutDesc))
            .add(
                Bytes.toBytes(mFamily),
                Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT),
                encode(layout.getDesc()))
            .add(
                Bytes.toBytes(mFamily),
                Bytes.toBytes(HBaseTableLayoutDatabase.QUALIFIER_LAYOUT_ID),
                Bytes.toBytes("1"));
    mHTable.put(eqPut(expectedPut));

    replay(mHTable);

    mDb.updateTableLayout(layout.getDesc().getName(), layoutDesc);

    verify(mHTable);
  }
  @Test
  public void testBuilder() throws Exception {
    final KijiTableLayout layout =
        KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));

    final Kiji kiji =
        new InstanceBuilder()
            .withTable("table", layout)
            .withRow("row1")
            .withFamily("family")
            .withQualifier("column")
            .withValue(1, "foo1")
            .withValue(2, "foo2")
            .withRow("row2")
            .withFamily("family")
            .withQualifier("column")
            .withValue(100, "foo3")
            .build();

    final KijiTable table = kiji.openTable("table");
    final KijiTableReader reader = table.openTableReader();

    // Verify the first row.
    final KijiDataRequest req = KijiDataRequest.create("family", "column");
    final KijiRowData row1 = reader.get(table.getEntityId("row1"), req);
    assertEquals("foo2", row1.getValue("family", "column", 2).toString());

    // Verify the second row.
    final KijiRowData row2 = reader.get(table.getEntityId("row2"), req);
    assertEquals("foo3", row2.getValue("family", "column", 100).toString());

    ResourceUtils.closeOrLog(reader);
    ResourceUtils.releaseOrLog(table);
    ResourceUtils.releaseOrLog(kiji);
  }
  @Before
  public void readLayout() throws Exception {
    final KijiTableLayout tableLayout =
        KijiTableLayout.newLayout(
            KijiTableLayouts.getLayout(KijiTableLayouts.FULL_FEATURED_IDENTITY));

    mTranslator = HBaseColumnNameTranslator.from(tableLayout);
  }
 @Test
 public void testTableExists() throws IOException {
   final KijiTableLayout simple =
       KijiTableLayout.newLayout(KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE));
   final Kiji kiji = new InstanceBuilder(getKiji()).withTable("table", simple).build();
   assertTrue(kiji.getMetaTable().tableExists(simple.getDesc().getName()));
   assertFalse(kiji.getMetaTable().tableExists("faketablename"));
 }
 @Test
 public void testRetrieveAllEmpty() throws Exception {
   getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
   assertEquals(
       BaseTool.SUCCESS,
       runTool(
           new FreshTool(),
           KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(),
           "--do=retrieve-all"));
   assertEquals(
       "There are no freshness policies attached to columns in table: user", mToolOutputStr);
 }
Example #9
0
 @Test
 public void testTableNoFamilies() throws Exception {
   final Kiji kiji =
       new InstanceBuilder(getKiji())
           .withTable(KijiTableLayouts.getLayout(KijiTableLayouts.NOFAMILY))
           .build();
   final KijiTable table = kiji.openTable("nofamily");
   try {
     assertEquals(BaseTool.SUCCESS, runTool(new LsTool(), table.getURI().toString()));
   } finally {
     table.release();
   }
 }
Example #10
0
 /** Sets up one table to freshen and creates a file for a key value store. */
 @Before
 public void setup() throws IOException {
   TableLayoutDesc userLayout = KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST);
   TableLayoutDesc tableLayout = KijiTableLayouts.getLayout(KijiTableLayouts.SIMPLE);
   new InstanceBuilder(getKiji())
       .withTable(userLayout)
       .withRow("felix")
       .withFamily("info")
       .withQualifier("name")
       .withValue("Felis")
       .build();
   // Set up a file for a key value store as well.
   final File file = new File(getLocalTempDir(), KV_FILENAME);
   final BufferedWriter writer =
       new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
   try {
     writer.append("Jennyanydots\tOld Gumbie Cat\n");
     writer.append("Skimbleshanks\tRailway Cat\n");
   } finally {
     writer.close();
   }
 }
  @Test
  public void testCreateTableWithInvalidSchemaClassInLayout() throws Exception {
    final TableLayoutDesc layout = KijiTableLayouts.getLayout(KijiTableLayouts.INVALID_SCHEMA);
    final File layoutFile = getTempLayoutFile(layout);
    final KijiURI tableURI =
        KijiURI.newBuilder(getKiji().getURI()).withTableName(layout.getName()).build();

    assertEquals(
        BaseTool.FAILURE,
        runTool(new CreateTableTool(), "--table=" + tableURI, "--layout=" + layoutFile));
    assertEquals(2, mToolOutputLines.length);
    assertTrue(
        mToolOutputLines[1].startsWith(
            "Error: Schema with type 'class' must be a valid Java identifier."));
  }
  @Test
  public void testUnregisterAll() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
    final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji());
    manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen());
    manager.storePolicy("user", "info:visits", TestProducer.class, new NeverFreshen());

    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(),
            "--do=unregister-all"));
    assertEquals(0, manager.retrievePolicies("user").size());
    assertEquals("All freshness policies removed from table: user", mToolOutputStr);
  }
 @Test
 public void testRetrieveEmpty() throws Exception {
   getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
   assertEquals(
       BaseTool.SUCCESS,
       runTool(
           new FreshTool(),
           KijiURI.newBuilder(getKiji().getURI())
               .withTableName("user")
               .withColumnNames(Lists.newArrayList("info:name"))
               .build()
               .toString(),
           "--do=retrieve"));
   assertEquals(
       "There is no freshness policy attached to column: info:name in table: user",
       mToolOutputStr);
 }
  @Test
  public void testValidate() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
    final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji());
    manager.storePolicy("user", "info:name", TestProducer.class, new AlwaysFreshen());
    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(),
            "--do=validate-all"));

    KijiFreshnessPolicyRecord record =
        KijiFreshnessPolicyRecord.newBuilder()
            .setRecordVersion(ProtocolVersion.parse("policyrecord-0.1").toCanonicalString())
            .setProducerClass(TestProducer.class.getName())
            .setFreshnessPolicyClass(AlwaysFreshen.class.getName())
            .setFreshnessPolicyState("")
            .build();

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final EncoderFactory encoderFactory = EncoderFactory.get();
    Encoder encoder = encoderFactory.directBinaryEncoder(outputStream, null);
    final DatumWriter<KijiFreshnessPolicyRecord> recordWriter =
        new SpecificDatumWriter<KijiFreshnessPolicyRecord>(KijiFreshnessPolicyRecord.SCHEMA$);
    recordWriter.write(record, encoder);
    getKiji()
        .getMetaTable()
        .putValue("user", "kiji.scoring.fresh.columnName", outputStream.toByteArray());

    assertEquals(
        BaseTool.FAILURE,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI()).withTableName("user").build().toString(),
            "--do=validate-all"));
    assertEquals(
        "Freshness policy attached to column: columnName is not valid.", mToolOutputLines[1]);
    assertEquals(
        "NO_FAMILY_IN_TABLE: java.lang.IllegalArgumentException: Table: user does not "
            + "contain family: columnName",
        mToolOutputLines[2]);
  }
  @Test
  public void testUnregister() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
    final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji());
    manager.storePolicy("user", "info:name", TestProducer.class, new NeverFreshen());

    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI())
                .withTableName("user")
                .withColumnNames(Lists.newArrayList("info:name"))
                .build()
                .toString(),
            "--do=unregister"));
    assertEquals(null, manager.retrievePolicy("user", "info:name"));
    assertEquals("Freshness policy removed from column: info:name in table user", mToolOutputStr);
  }
  @Test
  public void testIllegalRegister() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));

    runTool(
        new FreshTool(),
        KijiURI.newBuilder(getKiji().getURI())
            .withTableName("user")
            .withColumnNames(Lists.newArrayList("info:name"))
            .build()
            .toString(),
        "--do=register",
        "--policy-class=org.kiji..badname",
        "--policy-state={\"shelfLife\":10}",
        "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer",
        "--interactive=false");

    assertEquals(
        "BAD_POLICY_NAME: java.lang.IllegalArgumentException: Policy class name: "
            + "org.kiji..badname is not a valid Java class identifier.",
        mToolOutputLines[0]);

    runTool(
        new FreshTool(),
        KijiURI.newBuilder(getKiji().getURI())
            .withTableName("user")
            .withColumnNames(Lists.newArrayList("info:name"))
            .build()
            .toString(),
        "--do=register",
        "--policy-class=org.kiji.class",
        "--policy-state={\"shelfLife\":10}",
        "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer.",
        "--interactive=false");

    assertEquals(
        "BAD_PRODUCER_NAME: java.lang.IllegalArgumentException: Producer class name: org."
            + "kiji.scoring.tools.TestFreshTool$TestProducer. is not a valid Java class identifier.",
        mToolOutputLines[0]);
  }
  @Test
  public void testRegister() throws Exception {
    getKiji().createTable(KijiTableLayouts.getLayout(KijiTableLayouts.COUNTER_TEST));
    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            KijiURI.newBuilder(getKiji().getURI())
                .withTableName("user")
                .withColumnNames(Lists.newArrayList("info:name"))
                .build()
                .toString(),
            "--do=register",
            "--policy-class=org.kiji.scoring.lib.ShelfLife",
            "--policy-state={\"shelfLife\":10}",
            "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer"));

    final KijiFreshnessPolicyRecord record =
        KijiFreshnessPolicyRecord.newBuilder()
            .setRecordVersion("policyrecord-0.1.0")
            .setProducerClass(TestProducer.class.getName())
            .setFreshnessPolicyClass(
                Class.forName("org.kiji.scoring.lib.ShelfLife")
                    .asSubclass(KijiFreshnessPolicy.class)
                    .getName())
            .setFreshnessPolicyState("{\"shelfLife\":10}")
            .build();
    final KijiFreshnessManager manager = KijiFreshnessManager.create(getKiji());
    assertEquals(record, manager.retrievePolicy("user", "info:name"));
    assertEquals(
        "Freshness policy: org.kiji.scoring.lib.ShelfLife with state: {\"shelfLife\":10} "
            + "and producer: org.kiji.scoring.tools.TestFreshTool$TestProducer\n"
            + "attached to column: info:name in table: user",
        mToolOutputStr);

    // Test another ordering for arguments
    assertEquals(
        BaseTool.SUCCESS,
        runTool(
            new FreshTool(),
            "--do=register",
            KijiURI.newBuilder(getKiji().getURI())
                .withTableName("user")
                .withColumnNames(Lists.newArrayList("info:visits"))
                .build()
                .toString(),
            "--policy-state={\"shelfLife\":10}",
            "--producer-class=org.kiji.scoring.tools.TestFreshTool$TestProducer",
            "--policy-class=org.kiji.scoring.lib.ShelfLife"));

    final KijiFreshnessPolicyRecord record2 =
        KijiFreshnessPolicyRecord.newBuilder()
            .setRecordVersion("policyrecord-0.1.0")
            .setProducerClass(TestProducer.class.getName())
            .setFreshnessPolicyClass(
                Class.forName("org.kiji.scoring.lib.ShelfLife")
                    .asSubclass(KijiFreshnessPolicy.class)
                    .getName())
            .setFreshnessPolicyState("{\"shelfLife\":10}")
            .build();
    assertEquals(record2, manager.retrievePolicy("user", "info:visits"));
    assertEquals(
        "Freshness policy: org.kiji.scoring.lib.ShelfLife with state: {\"shelfLife\":10} "
            + "and producer: org.kiji.scoring.tools.TestFreshTool$TestProducer\n"
            + "attached to column: info:visits in table: user",
        mToolOutputStr);
  }