use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class DefaultPreviewStore method get.
@Override
public Map<String, List<JsonElement>> get(ApplicationId applicationId, String tracerName) {
// PreviewStore is a singleton and we have to create gson for each operation since gson is not thread safe.
Gson gson = new GsonBuilder().registerTypeAdapter(Schema.class, new SchemaTypeAdapter()).create();
byte[] startRowKey = new MDSKey.Builder().add(applicationId.getNamespace()).add(applicationId.getApplication()).add(tracerName).build().getKey();
byte[] stopRowKey = new MDSKey(Bytes.stopKeyForPrefix(startRowKey)).getKey();
Map<String, List<JsonElement>> result = new HashMap<>();
try (Scanner scanner = table.scan(startRowKey, stopRowKey, null, null, null)) {
Row indexRow;
while ((indexRow = scanner.next()) != null) {
Map<byte[], byte[]> columns = indexRow.getColumns();
String propertyName = Bytes.toString(columns.get(PROPERTY));
JsonElement value = gson.fromJson(Bytes.toString(columns.get(VALUE)), JsonElement.class);
List<JsonElement> values = result.get(propertyName);
if (values == null) {
values = new ArrayList<>();
result.put(propertyName, values);
}
values.add(value);
}
} catch (IOException e) {
String message = String.format("Error while reading preview data for application '%s' and tracer '%s'.", applicationId, tracerName);
throw new RuntimeException(message, e);
}
return result;
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class ProfileStore method add.
/**
* Add the profile to the profile store.
*
* @param profileId the id of the profile to add
* @param profile the information of the profile
* @throws IOException if there was an IO error adding the profile
* @throws AlreadyExistsException if the profile already exists
*/
public void add(ProfileId profileId, Profile profile) throws IOException, AlreadyExistsException {
Transactionals.execute(transactional, context -> {
MetadataStoreDataset mds = getMDS(context);
// make sure that a profile doesn't exist
MDSKey rowKey = getRowKey(profileId);
Profile value = mds.get(rowKey, Profile.class);
if (value != null) {
throw new AlreadyExistsException(profileId, String.format("Profile '%s' already exists.", profile.getName()));
}
mds.write(rowKey, profile);
}, IOException.class, AlreadyExistsException.class);
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class ProfileStore method delete.
/**
* Deletes the profile from the profile store
*
* @param profileId the id of the profile to delete
* @throws IOException if there was an IO error deleting the profile
* @throws NotFoundException if the profile is not found
*/
public void delete(ProfileId profileId) throws IOException, NotFoundException {
Transactionals.execute(transactional, context -> {
MetadataStoreDataset mds = getMDS(context);
MDSKey rowKey = getRowKey(profileId);
Profile value = getMDS(context).get(rowKey, Profile.class);
if (value == null) {
throw new NotFoundException(profileId);
}
mds.delete(getRowKey(profileId));
}, IOException.class, NotFoundException.class);
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MdsKey method getNamespacedIdFromKey.
static NamespacedEntityId getNamespacedIdFromKey(String type, byte[] rowKey) {
MDSKey.Splitter keySplitter = new MDSKey(rowKey).split();
// The rowkey is
// [rowPrefix][targetType][targetId][key] for value rows and
// [rowPrefix][targetType][targetId][key][index] for value index rows
// so skip the first two.
keySplitter.skipBytes();
keySplitter.skipString();
return EntityIdKeyHelper.getTargetIdIdFromKey(keySplitter, type);
}
use of io.cdap.cdap.data2.dataset2.lib.table.MDSKey in project cdap by caskdata.
the class MetadataDataset method deleteIndexes.
/**
* Deletes all indexes associated with a metadata key
*
* @param targetId the {@link NamespacedEntityId} for which keys are to be removed
* @param metadataKey the key to remove from the metadata of the specified {@link NamespacedEntityId}
*/
private void deleteIndexes(NamespacedEntityId targetId, String metadataKey) {
MDSKey mdsKey = MdsKey.getMDSIndexKey(targetId, metadataKey, null);
byte[] startKey = mdsKey.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
deleteIndexRow(next);
}
}
}
Aggregations