private boolean isParent(String datasetName, DatasetSpecification specification) { if (datasetName == null) { return false; } if (specification.getSpecifications().size() == 0 && specification.getName().equals(datasetName)) { return true; } if (datasetName.startsWith(specification.getName())) { return specification.getSpecifications().values().stream().anyMatch(spec -> isParent(datasetName, spec)); } return false; }
@Override public void update(DatasetSpecification oldSpec) throws IOException { // update all existing resolution tables, create all new resolutions for (Map.Entry<String, DatasetSpecification> entry : spec.getSpecifications().entrySet()) { DatasetSpecification oldSubSpec = spec.getSpecification(entry.getKey()); DatasetAdmin subAdmin = delegates.get(entry.getKey()); if (oldSubSpec != null && subAdmin instanceof Updatable) { ((Updatable) subAdmin).update(oldSubSpec); } else if (oldSubSpec == null) { subAdmin.create(); } } // TODO (CDAP-6342) delete all resolutions that were removed as part of the update } }
@Override public void update(DatasetSpecification oldSpec) throws IOException { // update all existing resolution tables, create all new resolutions for (Map.Entry<String, DatasetSpecification> entry : spec.getSpecifications().entrySet()) { DatasetSpecification oldSubSpec = spec.getSpecification(entry.getKey()); DatasetAdmin subAdmin = delegates.get(entry.getKey()); if (oldSubSpec != null && subAdmin instanceof Updatable) { ((Updatable) subAdmin).update(oldSubSpec); } else if (oldSubSpec == null) { subAdmin.create(); } } // TODO (CDAP-6342) delete all resolutions that were removed as part of the update } }
@Override public ObjectMappedTableDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException { String keyName = ObjectMappedTableProperties.getRowKeyExploreName(spec.getProperties()); DatasetSpecification tableSpec = spec.getSpecification(TABLE_NAME); // if the table spec did not have schema, this is an ObjectMappedTable from CDAP 2.8. // add the schema and row key as arguments so that explore will work // TODO: remove after CDAP-2122 is done if (!tableSpec.getProperties().containsKey(Table.PROPERTY_SCHEMA)) { tableSpec = DatasetSpecification.builder(tableSpec.getName(), tableSpec.getType()) .properties(tableSpec.getProperties()) .property(Table.PROPERTY_SCHEMA, spec.getProperty(Table.PROPERTY_SCHEMA)) .property(Table.PROPERTY_SCHEMA_ROW_FIELD, keyName) .datasets(tableSpec.getSpecifications().values()) .build(); } // reconstruct the table schema here because of backwards compatibility DatasetDefinition<Table, DatasetAdmin> tableDef = getDelegate(TABLE_NAME); Table table = tableDef.getDataset(datasetContext, tableSpec, arguments, classLoader); Map<String, String> properties = spec.getProperties(); TypeRepresentation typeRep = GSON.fromJson( ObjectMappedTableProperties.getObjectTypeRepresentation(properties), TypeRepresentation.class); Schema objSchema = ObjectMappedTableProperties.getObjectSchema(properties); return new ObjectMappedTableDataset(spec.getName(), table, typeRep, objSchema, classLoader); }
@Override public ObjectMappedTableDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException { String keyName = ObjectMappedTableProperties.getRowKeyExploreName(spec.getProperties()); DatasetSpecification tableSpec = spec.getSpecification(TABLE_NAME); // if the table spec did not have schema, this is an ObjectMappedTable from CDAP 2.8. // add the schema and row key as arguments so that explore will work // TODO: remove after CDAP-2122 is done if (!tableSpec.getProperties().containsKey(Table.PROPERTY_SCHEMA)) { tableSpec = DatasetSpecification.builder(tableSpec.getName(), tableSpec.getType()) .properties(tableSpec.getProperties()) .property(Table.PROPERTY_SCHEMA, spec.getProperty(Table.PROPERTY_SCHEMA)) .property(Table.PROPERTY_SCHEMA_ROW_FIELD, keyName) .datasets(tableSpec.getSpecifications().values()) .build(); } // reconstruct the table schema here because of backwards compatibility DatasetDefinition<Table, DatasetAdmin> tableDef = getDelegate(TABLE_NAME); Table table = tableDef.getDataset(datasetContext, tableSpec, arguments, classLoader); Map<String, String> properties = spec.getProperties(); TypeRepresentation typeRep = GSON.fromJson( ObjectMappedTableProperties.getObjectTypeRepresentation(properties), TypeRepresentation.class); Schema objSchema = ObjectMappedTableProperties.getObjectSchema(properties); return new ObjectMappedTableDataset(spec.getName(), table, typeRep, objSchema, classLoader); }
@Test public void testSimpleSpec() { String name = "name"; String type = "type"; String propKey = "prop1"; String propVal = "val1"; DatasetSpecification spec = DatasetSpecification.builder(name, type) .property(propKey, propVal) .build(); Assert.assertEquals(name, spec.getName()); Assert.assertEquals(type, spec.getType()); Assert.assertEquals(propVal, spec.getProperty(propKey)); Assert.assertEquals(1, spec.getProperties().size()); Assert.assertTrue(spec.getSpecifications().isEmpty()); }
@Test public void testNamespacing() { DatasetSpecification innerSpec = DatasetSpecification.builder("inner", "table") .build(); DatasetSpecification outerSpec = DatasetSpecification.builder("kv", "kvtable") .datasets(innerSpec) .build(); Assert.assertEquals("kv", outerSpec.getName()); Assert.assertEquals("kvtable", outerSpec.getType()); DatasetSpecification actualInner = outerSpec.getSpecification("inner"); Assert.assertEquals("kv.inner", actualInner.getName()); Assert.assertEquals("table", actualInner.getType()); Assert.assertTrue(actualInner.getSpecifications().isEmpty()); }