Search in sources :

Example 6 with Get

use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.

the class TableTest method testMultiGetWithTx.

@Test
public void testMultiGetWithTx() throws Exception {
    String testMultiGet = "testMultiGet";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, testMultiGet);
    admin.create();
    try {
        Transaction tx = txClient.startShort();
        Table table = getTable(CONTEXT1, testMultiGet);
        ((TransactionAware) table).startTx(tx);
        for (int i = 0; i < 100; i++) {
            table.put(new Put(Bytes.toBytes("r" + i)).add(C1, V1).add(C2, V2));
        }
        Assert.assertTrue(txClient.canCommit(tx, ((TransactionAware) table).getTxChanges()));
        Assert.assertTrue(((TransactionAware) table).commitTx());
        Assert.assertTrue(txClient.commit(tx));
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) table).startTx(tx2);
        List<Get> gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get(Bytes.toBytes("r" + i)));
        }
        List<Row> results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx2));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            byte[] val2 = row.get(C2);
            Assert.assertNotNull(val2);
            Assert.assertArrayEquals(V2, val2);
        }
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) table).startTx(tx3);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get("r" + i).add(C1));
        }
        results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx3));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            // should have only returned column 1
            byte[] val2 = row.get(C2);
            Assert.assertNull(val2);
        }
        // retrieve different columns per row
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) table).startTx(tx4);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            Get get = new Get("r" + i);
            // evens get C1, odds get C2
            get.add(i % 2 == 0 ? C1 : C2);
            gets.add(get);
        }
        results = table.get(gets);
        Assert.assertTrue(txClient.commit(tx4));
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val1 = row.get(C1);
            byte[] val2 = row.get(C2);
            if (i % 2 == 0) {
                Assert.assertNotNull(val1);
                Assert.assertArrayEquals(V1, val1);
                Assert.assertNull(val2);
            } else {
                Assert.assertNull(val1);
                Assert.assertNotNull(val2);
                Assert.assertArrayEquals(V2, val2);
            }
        }
    } finally {
        admin.drop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Row(co.cask.cdap.api.dataset.table.Row) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 7 with Get

use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.

the class ProgramScheduleStoreDataset method addSchedules.

/**
   * Add one or more schedules to the store.
   *
   * @param schedules the schedules to add
   * @return the new schedules' last modified timestamp
   * @throws AlreadyExistsException if one of the schedules already exists
   */
public long addSchedules(Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException {
    long currentTime = System.currentTimeMillis();
    for (ProgramSchedule schedule : schedules) {
        byte[] scheduleKey = rowKeyBytesForSchedule(schedule.getProgramId().getParent().schedule(schedule.getName()));
        if (!store.get(new Get(scheduleKey)).isEmpty()) {
            throw new AlreadyExistsException(schedule.getProgramId().getParent().schedule(schedule.getName()));
        }
        Put schedulePut = new Put(scheduleKey);
        schedulePut.add(SCHEDULE_COLUMN_BYTES, GSON.toJson(schedule));
        schedulePut.add(UPDATED_COLUMN_BYTES, currentTime);
        // initially suspended
        schedulePut.add(STATUS_COLUMN_BYTES, ProgramScheduleStatus.SUSPENDED.toString());
        store.put(schedulePut);
        int count = 0;
        for (String triggerKey : extractTriggerKeys(schedule)) {
            byte[] triggerRowKey = rowKeyBytesForTrigger(scheduleKey, count++);
            store.put(new Put(triggerRowKey, TRIGGER_KEY_COLUMN_BYTES, triggerKey));
        }
    }
    return currentTime;
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) Put(co.cask.cdap.api.dataset.table.Put) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint)

Example 8 with Get

use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.

the class ProgramScheduleStoreDataset method getScheduleRecord.

/**
   * Read all information about a schedule from the store.
   *
   * @param scheduleId the id of the schedule to read
   * @return the schedule record from the store
   * @throws NotFoundException if the schedule does not exist in the store
   */
public ProgramScheduleRecord getScheduleRecord(ScheduleId scheduleId) throws NotFoundException {
    Row row = store.get(new Get(rowKeyForSchedule(scheduleId)));
    byte[] serialized = row.get(SCHEDULE_COLUMN_BYTES);
    if (serialized == null) {
        throw new NotFoundException(scheduleId);
    }
    ProgramSchedule schedule = GSON.fromJson(Bytes.toString(serialized), ProgramSchedule.class);
    ProgramScheduleMeta meta = extractMetaFromRow(scheduleId, row);
    return new ProgramScheduleRecord(schedule, meta);
}
Also used : ProgramScheduleMeta(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleMeta) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramScheduleRecord(co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) Row(co.cask.cdap.api.dataset.table.Row)

Example 9 with Get

use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.

the class ProgramScheduleStoreDataset method deleteSchedules.

/**
   * Removes one or more schedules from the store. Succeeds whether the schedules exist or not.
   *
   * @param scheduleIds the schedules to delete
   * @throws NotFoundException if one of the schedules does not exist in the store
   */
public void deleteSchedules(Iterable<? extends ScheduleId> scheduleIds) throws NotFoundException {
    for (ScheduleId scheduleId : scheduleIds) {
        String scheduleKey = rowKeyForSchedule(scheduleId);
        if (store.get(new Get(scheduleKey)).isEmpty()) {
            throw new NotFoundException(scheduleId);
        }
        store.delete(new Delete(scheduleKey));
        byte[] prefix = keyPrefixForTriggerScan(scheduleKey);
        try (Scanner scanner = store.scan(new Scan(prefix, Bytes.stopKeyForPrefix(prefix)))) {
            Row row;
            while ((row = scanner.next()) != null) {
                store.delete(row.getRow());
            }
        }
    }
}
Also used : Delete(co.cask.cdap.api.dataset.table.Delete) Scanner(co.cask.cdap.api.dataset.table.Scanner) Get(co.cask.cdap.api.dataset.table.Get) NotFoundException(co.cask.cdap.common.NotFoundException) Scan(co.cask.cdap.api.dataset.table.Scan) Row(co.cask.cdap.api.dataset.table.Row) ScheduleId(co.cask.cdap.proto.id.ScheduleId)

Example 10 with Get

use of co.cask.cdap.api.dataset.table.Get in project cdap by caskdata.

the class ProgramScheduleStoreDataset method updateScheduleStatus.

/**
   * Update the status of a schedule. This also updates the last-updated timestamp.
   * @return the updated schedule's last modified timestamp
   */
public long updateScheduleStatus(ScheduleId scheduleId, ProgramScheduleStatus newStatus) throws NotFoundException {
    long currentTime = System.currentTimeMillis();
    String scheduleKey = rowKeyForSchedule(scheduleId);
    Row row = store.get(new Get(scheduleKey));
    if (row.isEmpty()) {
        throw new NotFoundException(scheduleId);
    }
    Put updatePut = new Put(scheduleKey);
    // record current time
    updatePut.add(UPDATED_COLUMN_BYTES, currentTime);
    updatePut.add(STATUS_COLUMN_BYTES, newStatus.toString());
    store.put(updatePut);
    return currentTime;
}
Also used : Get(co.cask.cdap.api.dataset.table.Get) NotFoundException(co.cask.cdap.common.NotFoundException) Row(co.cask.cdap.api.dataset.table.Row) Put(co.cask.cdap.api.dataset.table.Put)

Aggregations

Get (co.cask.cdap.api.dataset.table.Get)31 Table (co.cask.cdap.api.dataset.table.Table)17 Row (co.cask.cdap.api.dataset.table.Row)16 Test (org.junit.Test)15 Put (co.cask.cdap.api.dataset.table.Put)13 TransactionAware (org.apache.tephra.TransactionAware)9 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)8 Transaction (org.apache.tephra.Transaction)8 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)7 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)6 TransactionExecutor (org.apache.tephra.TransactionExecutor)6 NotFoundException (co.cask.cdap.common.NotFoundException)5 IOException (java.io.IOException)4 ReadOnly (co.cask.cdap.api.annotation.ReadOnly)3 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)3 Delete (co.cask.cdap.api.dataset.table.Delete)3 Scanner (co.cask.cdap.api.dataset.table.Scanner)3 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 ApplicationManager (co.cask.cdap.test.ApplicationManager)3 FlowManager (co.cask.cdap.test.FlowManager)3