Package com.comsol.api.database.result

Result types returned by read operations in the Model Manager database API.

A get operation in the Model Manager database API typically involves reading multiple data values for a database object using a single method invocation — thereby offering better performance than invoking multiple methods that each return a single data value. The effect is most notable when connecting to a Model Manager server database with network latencies. In the following example, the single invocation of version.get() is preferable than the three ensuing method invocations.

 // The modelLocationUri string could, for example, have been obtained via Copy Location
 // in the Model Manager workspace.
 ModelItemVersion version = DatabaseApiUtil.api().modelVersionByLocationUri(modelLocationUri);

 GetModelItemVersionResult result = version.get();

 String title = version.title();
 String description = version.description();
 String savedIn = version.savedIn();
 

A query operation in the Model Manager database API involves invoking a method that matches database objects via some filter criteria and then returning data values for these database objects as a so-called result stream. All result streams implement the interface DatabaseApiResultStream. Since the number of matching database objects could potentially be very large, the individual elements are read on demand from the database by invoking methods on the result stream instance. This includes, for example, looping over the elements in a "for-each loop" statement using the result stream as a source, by manually iterating using the Iterator returned by Iterable.iterator(), or by consuming the stream returned by DatabaseApiResultStream.stream(). In the following example, data values for versions matching a search are iterated in three different ways.

 SearchItemVersionResultStream resultStream = database.searchItemVersions("my model");

 // Iterating using a "for-each loop" statement.
 for (SearchItemVersionResult result : resultStream) {

 }

 // Manually iterating.
 java.util.Iterator<SearchItemVersionResult> iterator = resultStream.iterator();
 while (iterator.hasNext()) {
   SearchItemVersionResult result = iterator.next();
 }

 // Consuming a stream using the Java 8 Stream API.
 resultStream.stream().forEachOrdered(result -> {

 });
 

The result stream also has methods for reading the first element in the stream, as well as reading a fixed number of elements into an array or a list.

 SearchItemVersionResultStream resultStream = database.searchItemVersions("my model");

 SearchItemVersionResult firstResult = resultStream.firstOrNull();

 SearchItemVersionResult[] resultArray = resultStream.toArray(10);

 List<SearchItemVersionResult> resultList = resultStream.toList(5);
 

See the chapter Model Manager API in the Model Manager Reference Manual for further details on the Model Manager database API. This includes, for example, code snippets showcasing common operations when working with Model Manager databases.