private ProfileDataset(Table table) { this.table = new MetadataStoreDataset(table, GSON); }
@Override public void run(DatasetContext context) throws Exception { String desc = Optional.fromNullable(description).orNull(); getMetadataStore(context).write(getKey(streamId), createStreamSpec(streamId, desc)); } }, Exception.class);
/** * Delete all values in the given prefix row key * * @param id prefix row key */ public void deleteAll(MDSKey id) { deleteAll(id, x -> true); }
/** * Add an assignment to the profile. Assignment can only be added if the profile is ENABLED * * @param profileId the profile id * @param entityId the entity to add to the assgiment */ public void addProfileAssignment(ProfileId profileId, EntityId entityId) throws ProfileConflictException, NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } if (profile.getStatus() == ProfileStatus.DISABLED) { throw new ProfileConflictException( String.format("Profile %s is DISABLED. No entity can be assigned to it.", profileId.getProfile()), profileId); } table.write(getEntityIndexRowKey(profileId, entityId), entityId); }
final MetadataStoreDataset metadataStoreDataset = new MetadataStoreDataset(table); TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) table); for (int i = 0; i < 5; ++i) { MDSKey mdsKey = new MDSKey.Builder().add(i).build(); metadataStoreDataset.write(mdsKey, i); expectedMap.put(mdsKey, i); if ((i % 2) == 0) { final int fv = i; txnl.execute(() -> { Map<MDSKey, Integer> val = metadataStoreDataset.listKV(new MDSKey.Builder().add(0).build(), new MDSKey.Builder().add(5).build(), Integer.class, 1, final int fv = i; txnl.execute(() -> { Map<MDSKey, Integer> val = metadataStoreDataset.listKV(new MDSKey.Builder().add(0).build(), new MDSKey.Builder().add(5).build(), Integer.class, 2, Map<MDSKey, Integer> val = metadataStoreDataset.listKV(expectedMap.keySet(), Integer.class, 5); Assert.assertEquals(expectedMap, val); }); Map<MDSKey, Integer> valHalf = metadataStoreDataset.listKV(expectedMapHalf.keySet(), Integer.class, 5); Assert.assertEquals(expectedMapHalf, valHalf); });
final MetadataStoreDataset metadataStoreDataset = new MetadataStoreDataset(table); TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) table); for (int i = 0; i < 25; ++i) { MDSKey mdsKey = new MDSKey.Builder().add(i).build(); metadataStoreDataset.write(mdsKey, i); expected.add(i); final ScanFunction function = new ScanFunction(scanLimit); final MDSKey finalStart = start; txnl.execute(() -> metadataStoreDataset.scan(finalStart, end, Integer.class, function)); if (function.getNumProcessed() == 0) { break;
/** * Returns mapping of all that has same first id parts for default COLUMN * * @param id prefix row key * @param typeOfT the type of the result * @return map of row key to result */ public <T> Map<MDSKey, T> listKV(MDSKey id, Type typeOfT) { return listKV(id, typeOfT, Integer.MAX_VALUE); }
/** * Lists all that has same first id parts for default COLUMN, with a limit * * @param id prefix row key * @param typeOfT the type of the result * @param limit limit number of result * @return a list of the deserialized value of the result */ public <T> List<T> list(MDSKey id, Type typeOfT, int limit) { return list(id, null, typeOfT, limit, x -> true); }
/** * Get assignments with the profile. * * @param profileId the profile id * @return the set of entities that the profile is assigned to * @throws NotFoundException if the profile is not found */ public Set<EntityId> getProfileAssignments(ProfileId profileId) throws NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } return Collections.unmodifiableSet(new HashSet<>(table.list(getEntityIndexRowKeyPrefix(profileId), EntityId.class))); }
/** * Remove an assignment from the profile. * * @param profileId the profile id * @param entityId the entity to remove from the assignment * @throws NotFoundException if the profile is not found */ public void removeProfileAssignment(ProfileId profileId, EntityId entityId) throws NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } table.delete(getEntityIndexRowKey(profileId, entityId)); }
private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); // Key Filter doesn't pass if (keyFilter != null && !keyFilter.test(key)) { continue; } // If Value Filter doesn't pass if (valueFilter != null && !valueFilter.test(value)) { continue; } map.put(key, value); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Scans for program run information * * @param limit maximum number of task info to return * @param lastProgramRunId an optional {@link ProgramRunId} returned from previous scan to continue the scanning * @return a {@link List} of {@link ProgramRunId} to the corresponding {@link ProgramOptions} for each run */ public List<Map.Entry<ProgramRunId, ProgramOptions>> scan(int limit, @Nullable ProgramRunId lastProgramRunId) { MDSKey startKey = new MDSKey.Builder().add(KEY_PREFIX).build(); MDSKey stopKey = new MDSKey(Bytes.stopKeyForPrefix(startKey.getKey())); if (lastProgramRunId != null) { // If there is a start program run id, scan from that id instead of from the beginning startKey = getKey(lastProgramRunId); } List<Map.Entry<ProgramRunId, ProgramOptions>> result = new LinkedList<>(); super.<ProgramOptions>scan(startKey, stopKey, ProgramOptions.class, keyValue -> { if (result.size() >= limit) { return false; } ProgramRunId programRunId = getProgramRunId(keyValue.getKey()); // Skip the lastProgramRunId if (lastProgramRunId != null && lastProgramRunId.equals(programRunId)) { return true; } result.add(Maps.immutableEntry(programRunId, keyValue.getValue())); return true; }); return result; }
/** * Clear bootstrap state. This should only be called in tests. */ @VisibleForTesting void clear() { Transactionals.execute(transactional, dsContext -> { MetadataStoreDataset ds = get(dsContext, datasetFramework); ds.delete(KEY); }); }
/** * @return whether the CDAP instance is bootstrapped. */ public boolean isBootstrapped() { return Transactionals.execute(transactional, dsContext -> { MetadataStoreDataset ds = get(dsContext, datasetFramework); return ds.exists(KEY); }); }
/** * Add an assignment to the profile. Assignment can only be added if the profile is ENABLED * * @param profileId the profile id * @param entityId the entity to add to the assgiment */ public void addProfileAssignment(ProfileId profileId, EntityId entityId) throws ProfileConflictException, NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } if (profile.getStatus() == ProfileStatus.DISABLED) { throw new ProfileConflictException( String.format("Profile %s is DISABLED. No entity can be assigned to it.", profileId.getProfile()), profileId); } table.write(getEntityIndexRowKey(profileId, entityId), entityId); }
/** * Returns mapping of all that match the given keySet for default COLUMN * * @param keySet row key set * @param typeOfT the type of the result * @param limit limit of the result * @return map of row key to result */ public <T> Map<MDSKey, T> listKV(Set<MDSKey> keySet, Type typeOfT, int limit) { return listKV(keySet, typeOfT, limit, x -> true); }
/** * Lists all that has same first id parts for default COLUMN * * @param id prefix row key * @param typeOfT the type of the result * @return a list of the deserialized value of the result */ public <T> List<T> list(MDSKey id, Type typeOfT) { return list(id, typeOfT, Integer.MAX_VALUE); }
/** * Get assignments with the profile. * * @param profileId the profile id * @return the set of entities that the profile is assigned to * @throws NotFoundException if the profile is not found */ public Set<EntityId> getProfileAssignments(ProfileId profileId) throws NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } return Collections.unmodifiableSet(new HashSet<>(table.list(getEntityIndexRowKeyPrefix(profileId), EntityId.class))); }
/** * Remove an assignment from the profile. * * @param profileId the profile id * @param entityId the entity to remove from the assignment * @throws NotFoundException if the profile is not found */ public void removeProfileAssignment(ProfileId profileId, EntityId entityId) throws NotFoundException { Profile profile = table.get(getProfileRowKey(profileId), Profile.class); if (profile == null) { throw new NotFoundException(profileId); } table.delete(getEntityIndexRowKey(profileId, entityId)); }
private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); // Key Filter doesn't pass if (keyFilter != null && !keyFilter.test(key)) { continue; } // If Value Filter doesn't pass if (valueFilter != null && !valueFilter.test(value)) { continue; } map.put(key, value); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }