use of org.pentaho.di.shared.SharedObjectInterface in project pentaho-kettle by pentaho.
the class KettleFileRepository method save.
public void save(RepositoryElementInterface repositoryElement, String versionComment, ProgressMonitorListener monitor, ObjectId parentId, boolean used) throws KettleException {
try {
if (!(repositoryElement instanceof XMLInterface) && !(repositoryElement instanceof SharedObjectInterface)) {
throw new KettleException("Class [" + repositoryElement.getClass().getName() + "] needs to implement the XML Interface in order to save it to disk");
}
if (!Utils.isEmpty(versionComment)) {
insertLogEntry("Save repository element : " + repositoryElement.toString() + " : " + versionComment);
}
ObjectId objectId = new StringObjectId(calcObjectId(repositoryElement));
FileObject fileObject = getFileObject(repositoryElement);
String xml = ((XMLInterface) repositoryElement).getXML();
OutputStream os = KettleVFS.getOutputStream(fileObject, false);
os.write(xml.getBytes(Const.XML_ENCODING));
os.close();
if (repositoryElement instanceof ChangedFlagInterface) {
((ChangedFlagInterface) repositoryElement).clearChanged();
}
//
if (repositoryElement.getObjectId() != null && !repositoryElement.getObjectId().equals(objectId)) {
delObject(repositoryElement.getObjectId());
}
repositoryElement.setObjectId(objectId);
//
if (repositoryElement instanceof TransMeta) {
((TransMeta) repositoryElement).saveMetaStoreObjects(this, metaStore);
}
if (repositoryElement instanceof JobMeta) {
((JobMeta) repositoryElement).saveMetaStoreObjects(this, metaStore);
}
} catch (Exception e) {
throw new KettleException("Unable to save repository element [" + repositoryElement + "] to XML file : " + calcFilename(repositoryElement), e);
}
}
use of org.pentaho.di.shared.SharedObjectInterface in project pentaho-kettle by pentaho.
the class PurRepository_SharedObjects_IT method loadAllShared.
@Test
public void loadAllShared() throws Exception {
ClusterSchema cluster = cluster();
DatabaseMeta database = database();
SlaveServer slaveServer = slaveServer();
PartitionSchema partition = partition();
purRepository.save(cluster, null, null);
purRepository.save(database, null, null);
purRepository.save(slaveServer, null, null);
purRepository.save(partition, null, null);
Map<RepositoryObjectType, List<? extends SharedObjectInterface>> map = map();
purRepository.readSharedObjects(map, RepositoryObjectType.CLUSTER_SCHEMA, RepositoryObjectType.DATABASE, RepositoryObjectType.SLAVE_SERVER, RepositoryObjectType.PARTITION_SCHEMA);
RepositoryElementInterface[] saved = new RepositoryElementInterface[] { cluster, database, slaveServer, partition };
assertEquals(saved.length, map.size());
for (RepositoryElementInterface sharedObject : saved) {
List<? extends SharedObjectInterface> list = map.get(sharedObject.getRepositoryElementType());
assertEquals(1, list.size());
assertEquals(sharedObject, list.get(0));
}
}
use of org.pentaho.di.shared.SharedObjectInterface in project pentaho-kettle by pentaho.
the class PurRepository_SharedObjects_IT method testLoadSharedObjects.
private void testLoadSharedObjects(RepositoryElementInterface sharedObject) throws Exception {
purRepository.save(sharedObject, null, null);
Map<RepositoryObjectType, List<? extends SharedObjectInterface>> map = map();
purRepository.readSharedObjects(map, sharedObject.getRepositoryElementType());
List<? extends SharedObjectInterface> objects = map.get(sharedObject.getRepositoryElementType());
assertNotNull(objects);
assertEquals(1, objects.size());
SharedObjectInterface object = objects.get(0);
assertEquals(sharedObject, object);
}
use of org.pentaho.di.shared.SharedObjectInterface in project pentaho-kettle by pentaho.
the class PurRepositoryIT method testNewNonAmbiguousNaming.
@Test
public void testNewNonAmbiguousNaming() throws Exception {
PurRepository repo = (PurRepository) repository;
System.setProperty("KETTLE_COMPATIBILITY_PUR_OLD_NAMING_MODE", "N");
// $NON-NLS-1$
PartitionSchema partSchema1 = createPartitionSchema("find.me");
// $NON-NLS-1$
PartitionSchema partSchema2 = createPartitionSchema("find|me");
repository.save(partSchema1, VERSION_COMMENT_V1, null);
repository.save(partSchema2, VERSION_COMMENT_V1, null);
Map<RepositoryObjectType, List<? extends SharedObjectInterface>> sharedObjectsByType = new HashMap<RepositoryObjectType, List<? extends SharedObjectInterface>>();
repo.readSharedObjects(sharedObjectsByType, RepositoryObjectType.PARTITION_SCHEMA);
List<PartitionSchema> partitionSchemas = (List<PartitionSchema>) sharedObjectsByType.get(RepositoryObjectType.PARTITION_SCHEMA);
assertEquals(2, partitionSchemas.size());
System.setProperty("KETTLE_COMPATIBILITY_PUR_OLD_NAMING_MODE", "Y");
// $NON-NLS-1$
PartitionSchema partSchema3 = createPartitionSchema("another.one");
// $NON-NLS-1$
PartitionSchema partSchema4 = createPartitionSchema("another|one");
repository.save(partSchema3, VERSION_COMMENT_V1, null);
repository.save(partSchema4, VERSION_COMMENT_V1, null);
sharedObjectsByType = new HashMap<RepositoryObjectType, List<? extends SharedObjectInterface>>();
repo.readSharedObjects(sharedObjectsByType, RepositoryObjectType.PARTITION_SCHEMA);
partitionSchemas = (List<PartitionSchema>) sharedObjectsByType.get(RepositoryObjectType.PARTITION_SCHEMA);
assertEquals(3, partitionSchemas.size());
}
use of org.pentaho.di.shared.SharedObjectInterface in project pentaho-kettle by pentaho.
the class PurRepository method readSharedObjects.
/**
* Read shared objects of the types provided from the repository. Every {@link SharedObjectInterface} that is read
* will be fully loaded as if it has been loaded through {@link #loadDatabaseMeta(ObjectId, String)},
* {@link #loadClusterSchema(ObjectId, List, String)}, etc.
* <p>
* This method was introduced to reduce the number of server calls for loading shared objects to a constant number:
* {@code 2 + n, where n is the number of types requested}.
* </p>
*
* @param sharedObjectsByType
* Map of type to shared objects. Each map entry will contain a non-null {@link List} of
* {@link RepositoryObjectType}s for every type provided. Only entries for types provided will be altered.
* @param types
* Types of repository objects to read from the repository
* @throws KettleException
*/
protected void readSharedObjects(Map<RepositoryObjectType, List<? extends SharedObjectInterface>> sharedObjectsByType, RepositoryObjectType... types) throws KettleException {
// Overview:
// 1) We will fetch RepositoryFile, NodeRepositoryFileData, and VersionSummary for all types provided.
// 2) We assume that unless an exception is thrown every RepositoryFile returned by getFilesByType(..) have a
// matching NodeRepositoryFileData and VersionSummary.
// 3) With all files, node data, and versions in hand we will iterate over them, merging them back into usable
// shared objects
List<RepositoryFile> allFiles = new ArrayList<RepositoryFile>();
// Since type is not preserved in the RepositoryFile we fetch files by type so we don't rely on parsing the name to
// determine type afterward
// Map must be ordered or we can't match up files with data and version summary
LinkedHashMap<RepositoryObjectType, List<RepositoryFile>> filesByType = getFilesByType(allFiles, types);
try {
List<NodeRepositoryFileData> data = pur.getDataForReadInBatch(allFiles, NodeRepositoryFileData.class);
List<VersionSummary> versions = pur.getVersionSummaryInBatch(allFiles);
// Only need one iterator for all data and versions. We will work through them as we process the files by type, in
// order.
Iterator<NodeRepositoryFileData> dataIter = data.iterator();
Iterator<VersionSummary> versionsIter = versions.iterator();
// Assemble into completely loaded SharedObjectInterfaces by type
for (Entry<RepositoryObjectType, List<RepositoryFile>> entry : filesByType.entrySet()) {
SharedObjectAssembler<?> assembler = sharedObjectAssemblerMap.get(entry.getKey());
if (assembler == null) {
throw new UnsupportedOperationException(// $NON-NLS-1$
String.format("Cannot assemble shared object of type [%s]", entry.getKey()));
}
// For all files of this type, assemble them from the pieces of data pulled from the repository
Iterator<RepositoryFile> filesIter = entry.getValue().iterator();
List<SharedObjectInterface> sharedObjects = new ArrayList<SharedObjectInterface>(entry.getValue().size());
// (no need to check for next on all iterators)
while (filesIter.hasNext()) {
RepositoryFile file = filesIter.next();
NodeRepositoryFileData repoData = dataIter.next();
VersionSummary version = versionsIter.next();
// TODO: inexistent db types can cause exceptions assembling; prevent total failure
try {
sharedObjects.add(assembler.assemble(file, repoData, version));
} catch (Exception ex) {
// TODO i18n
getLog().logError("Unable to load shared objects", ex);
}
}
sharedObjectsByType.put(entry.getKey(), sharedObjects);
}
} catch (Exception ex) {
// $NON-NLS-1$
throw new KettleException("Unable to load shared objects", ex);
}
}
Aggregations