Class CellsetService

java.lang.Object
io.skipcheck.tm1.client.service.data.CellsetService

public final class CellsetService extends Object
Cellsets: the server-side results of an MDX query or a view, with their axes and cells.

A cellset lives on the server until it's deleted, or the session ends; one that isn't deleted holds the server's memory. The 1:1 methods (execute(String, ODataQuery), cells(String, ODataQuery), partition(String, long, long, ODataQuery), delete(String)) leave that to the caller. stream(String, ODataQuery, int, BiFunction) does it all: runs the query, pages the cells in order, and deletes the cellset when the result completes, fails or is cancelled.

Read the cells of an MDX query, placing each by its ordinal on the axes:

String mdx = """
		SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb], [Month].[Month].[Mar]} ON 0,
		  {TM1FILTERBYLEVEL({TM1SUBSETALL([Region].[Region])}, 0)} ON 1
		FROM [Sales]
		WHERE ([Version].[Version].[Actual], [Measure].[Measure].[Revenue])""";
return tm1.cellsets()
	.stream(mdx, ODataQuery.none().select(CellsetCell.ORDINAL, CellsetCell.VALUE), 10_000, (cellset, cells) -> {
		// Axis 0 is the columns, axis 1 the rows; ordinals run along a row
		List<CellsetAxisTuple> columns = cellset.axes().get(0).tuples();
		List<CellsetAxisTuple> rows = cellset.axes().get(1).tuples();
		return cells.map(cell -> {
			CellsetAxisTuple row = rows.get((int) (cell.ordinal() / columns.size()));
			CellsetAxisTuple column = columns.get((int) (cell.ordinal() % columns.size()));
			return row.members().get(0).name() + ", " + column.members().get(0).name() + ": " + cell.value();
		});
	});

Write into the cells of a query by ordinal, deleting its cellset after:

// Write by ordinal into the cells of a query, then delete its cellset; the cells
// must be leaves, as TM1 doesn't spread to a consolidation
CellsetService cellsets = tm1.cellsets();
String mdx = "SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb]} ON 0, {[Region].[Region].[North]} ON 1"
		+ " FROM [Sales] WHERE ([Version].[Version].[Budget], [Measure].[Measure].[Revenue])";
return Mono.usingWhen(cellsets.execute(mdx, ODataQuery.none()),
		cellset -> cellsets.updateCells(cellset.id(),
				List.of(CellsetUpdatePatch.builder().ordinal(0L).value(1200).build(),
						CellsetUpdatePatch.builder().ordinal(1L).value(1350).build())),
		cellset -> cellsets.delete(cellset.id()));

Read the values of a saved view:

// The values of a saved view, read in the same round trip as its cellset
CellsetService cellsets = tm1.cellsets();
return Flux.usingWhen(
		cellsets.executeView("Sales", ViewScope.PUBLIC, "Revenue by Region",
				ODataQuery.none().expand(Cellset.CELLS, c -> c.select(CellsetCell.VALUE))),
		cellset -> Flux.fromIterable(cellset.cells()).map(CellsetCell::value),
		cellset -> cellsets.delete(cellset.id()));
  • Constructor Details

    • CellsetService

      public CellsetService(Tm1Session session)
      A service sending its requests through session.
      Parameters:
      session - the session
  • Method Details

    • execute

      @Tm1Endpoint("ActionImport ExecuteMDX") public reactor.core.publisher.Mono<Cellset> execute(String mdx, ODataQuery query)
      ExecuteMDX: runs an MDX query, leaving its cellset on the server until delete(String). Expand Axes and Cells (with $top and $select) to get them in the same round trip.
      Parameters:
      mdx - the query
      query - what to expand of the cellset
      Returns:
      the cellset, with its ID
    • executeStatement

      @Tm1Endpoint("ActionImport ExecuteMDXStatement") public reactor.core.publisher.Mono<Void> executeStatement(String mdx)
      ExecuteMDXStatement: runs an MDX statement for its effect, keeping no cellset. Needs schema 11.8.20. TM1 (11.8) runs a SELECT and returns nothing, and rejects CREATE SET as a syntax error (code 501).
      Parameters:
      mdx - the statement
      Returns:
      completes when it's run
    • executeView

      @Tm1Endpoint("Action NativeView/Execute") @Tm1Endpoint("Action MDXView/Execute") public reactor.core.publisher.Mono<Cellset> executeView(String cube, ViewScope scope, String view, ODataQuery query)
      tm1.Execute on a native or MDX view: its cellset, left on the server until delete(String).
      Parameters:
      cube - the cube's name
      scope - where the view is
      view - the view's name
      query - what to expand of the cellset
      Returns:
      the cellset, with its ID
    • list

      @Tm1Endpoint("EntitySet Cellsets") public reactor.core.publisher.Flux<Cellset> list(ODataQuery query)
      GET Cellsets, streamed. TM1 (11.8) lists none, though each cellset reads by its ID with get(String, ODataQuery): keep the IDs of the cellsets you create.
      Parameters:
      query - what to select and expand
      Returns:
      the cellsets
    • get

      @Tm1Endpoint("EntitySet Cellsets") public reactor.core.publisher.Mono<Cellset> get(String id, ODataQuery query)
      GET Cellsets('id').
      Parameters:
      id - the cellset's ID
      query - what to expand
      Returns:
      the cellset, or a Tm1NotFoundException if it's gone
    • delete

      @Tm1Endpoint("EntitySet Cellsets") public reactor.core.publisher.Mono<Void> delete(String id)
      DELETE Cellsets('id'): frees the cellset's memory on the server.
      Parameters:
      id - the cellset's ID
      Returns:
      completes when it's deleted
    • axes

      @Tm1Endpoint("Navigation Cellset/Axes") public reactor.core.publisher.Flux<CellsetAxis> axes(String id, ODataQuery query)
      GET Cellsets('id')/Axes, streamed: columns (0), rows (1), and so on, then the slicer. TM1 (11.8) adds the slicer even without a WHERE: it holds the cube's other dimensions, such as its virtual Sandboxes, with cardinality 1.
      Parameters:
      id - the cellset's ID
      query - what to expand, such as Tuples($expand=Members)
      Returns:
      the axes
    • hierarchies

      @Tm1Endpoint("Navigation CellsetAxis/Hierarchies") public reactor.core.publisher.Flux<Hierarchy> hierarchies(String id, int axis, ODataQuery query)
      GET Cellsets('id')/Axes(n)/Hierarchies, streamed.
      Parameters:
      id - the cellset's ID
      axis - the axis ordinal
      query - what to select
      Returns:
      the hierarchies on the axis
    • tuples

      @Tm1Endpoint("Navigation CellsetAxis/Tuples") public reactor.core.publisher.Flux<CellsetAxisTuple> tuples(String id, int axis, ODataQuery query)
      GET Cellsets('id')/Axes(n)/Tuples, streamed, in order: an axis can be large.
      Parameters:
      id - the cellset's ID
      axis - the axis ordinal
      query - what to expand, such as Members($select=Name)
      Returns:
      the axis's tuples
    • tupleMembers

      @Tm1Endpoint("Navigation CellsetAxisTuple/Members") public reactor.core.publisher.Flux<Member> tupleMembers(String id, int axis, int tuple, ODataQuery query)
      GET Cellsets('id')/Axes(n)/Tuples(t)/Members, streamed: one member per hierarchy on the axis.
      Parameters:
      id - the cellset's ID
      axis - the axis ordinal
      tuple - the tuple ordinal
      query - what to select
      Returns:
      the tuple's members
    • cells

      @Tm1Endpoint("Navigation Cellset/Cells") public reactor.core.publisher.Flux<CellsetCell> cells(String id, ODataQuery query)
      GET Cellsets('id')/Cells, streamed, in ordinal order. Use $top and $skip, or partition(String, long, long, ODataQuery), for pages.
      Parameters:
      id - the cellset's ID
      query - what to select of each cell (Ordinal, Value, …)
      Returns:
      the cells
    • cellMembers

      @Tm1Endpoint("Navigation CellsetCell/Members") public reactor.core.publisher.Flux<Member> cellMembers(String id, long ordinal, ODataQuery query)
      GET Cellsets('id')/Cells(n)/Members, streamed: the members that place a cell, one per hierarchy of the cube. TM1 (11.8) includes the virtual Sandboxes dimension's, Base, first.
      Parameters:
      id - the cellset's ID
      ordinal - the cell's ordinal
      query - what to select
      Returns:
      the cell's members
    • cube

      @Tm1Endpoint("Navigation Cellset/Cube") public reactor.core.publisher.Mono<Cube> cube(String id, ODataQuery query)
      GET Cellsets('id')/Cube.
      Parameters:
      id - the cellset's ID
      query - what to select and expand
      Returns:
      the cube the cellset is from
    • partition

      @Tm1Endpoint("Function Cellset/GetPartition") public reactor.core.publisher.Flux<CellsetCell> partition(String id, long begin, long end, ODataQuery query)
      tm1.GetPartition(Begin=b,End=e): a rectangle of cells, streamed. Needs schema 10.2.2.3.

      TM1 (11.8) reads Begin and End as the top-left and bottom-right cells of a rectangle on the first two axes, not as a range of ordinals: with two columns, partition(id, 2, 4, …) is column 0 of rows 1 and 2, cells 2 and 4, not 3. A run of whole rows, from a row's first cell to a row's last, is a range.

      Parameters:
      id - the cellset's ID
      begin - the ordinal of the rectangle's top-left cell
      end - the ordinal of its bottom-right cell
      query - what to select of each cell; across pages, keep it the same
      Returns:
      the cells in the rectangle
    • update

      @Tm1Endpoint("Action Cellset/Update") public reactor.core.publisher.Mono<Void> update(String id, long ordinal, @Nullable Object value)
      tm1.Update: writes one value to a cell of the cellset, by ordinal. The action takes a range (BeginOrdinal, EndOrdinal), but TM1 (11.8) writes only the first cell of it, so this takes one ordinal; updateCells(String, List) writes many.
      Parameters:
      id - the cellset's ID
      ordinal - the cell's ordinal
      value - the value, or null to clear the cell
      Returns:
      completes when it's written
    • updateCells

      @Tm1Endpoint("Action Cellset/UpdateCells") @Tm1Endpoint("Navigation CellsetUpdate/ReferenceCube") @Tm1Endpoint("Navigation CellsetUpdate/ReferenceCell") public reactor.core.publisher.Mono<Void> updateCells(String id, List<CellsetUpdatePatch> updates)
      tm1.UpdateCells: writes many of the cellset's cells, each its own value, by ordinal. Needs schema 11.0.0.
      Parameters:
      id - the cellset's ID
      updates - the writes: CellsetUpdatePatch.builder().ordinal(n).value(v), or with a reference cube and cell to copy from
      Returns:
      completes when they're written
    • saveViewAs

      @Tm1Endpoint("Action Cellset/SaveViewAs") public reactor.core.publisher.Mono<View> saveViewAs(String id, String name)
      tm1.SaveViewAs: saves the cellset's query as a public view of its cube, failing if one has the name. Needs schema 11.0.0.
      Parameters:
      id - the cellset's ID
      name - the view's name
      Returns:
      the view
    • saveViewAs

      @Tm1Endpoint("Action Cellset/SaveViewAs") public reactor.core.publisher.Mono<View> saveViewAs(String id, String name, boolean overwrite, boolean makePrivate)
      tm1.SaveViewAs: saves the cellset's query as a view of its cube. Needs schema 11.0.0.
      Parameters:
      id - the cellset's ID
      name - the view's name
      overwrite - whether to replace a view that has the name
      makePrivate - whether the view is private
      Returns:
      the view
    • stream

      @Tm1Endpoint("ActionImport ExecuteMDX") @Tm1Endpoint("Function Cellset/GetPartition") @Tm1Endpoint("EntitySet Cellsets") public <R> reactor.core.publisher.Flux<R> stream(String mdx, ODataQuery cellQuery, int pageSize, BiFunction<Cellset, reactor.core.publisher.Flux<CellsetCell>, org.reactivestreams.Publisher<R>> handler)
      Runs an MDX query and streams its cells to handler, then deletes the cellset: when the result completes, fails, or is cancelled.

      One round trip brings the axes (hierarchies by name, tuples with their members' names and unique names) and the first pageSize cells; the rest come in pages of GetPartition, a few at a time and in ordinal order, so memory stays bounded on both sides however large the cellset.

      String mdx = """
      		SELECT {[Month].[Month].[Jan], [Month].[Month].[Feb], [Month].[Month].[Mar]} ON 0,
      		  {TM1FILTERBYLEVEL({TM1SUBSETALL([Region].[Region])}, 0)} ON 1
      		FROM [Sales]
      		WHERE ([Version].[Version].[Actual], [Measure].[Measure].[Revenue])""";
      return tm1.cellsets()
      	.stream(mdx, ODataQuery.none().select(CellsetCell.ORDINAL, CellsetCell.VALUE), 10_000, (cellset, cells) -> {
      		// Axis 0 is the columns, axis 1 the rows; ordinals run along a row
      		List<CellsetAxisTuple> columns = cellset.axes().get(0).tuples();
      		List<CellsetAxisTuple> rows = cellset.axes().get(1).tuples();
      		return cells.map(cell -> {
      			CellsetAxisTuple row = rows.get((int) (cell.ordinal() / columns.size()));
      			CellsetAxisTuple column = columns.get((int) (cell.ordinal() % columns.size()));
      			return row.members().get(0).name() + ", " + column.members().get(0).name() + ": " + cell.value();
      		});
      	});
      
      Type Parameters:
      R - what the handler gives
      Parameters:
      mdx - the query
      cellQuery - what to select of each cell; include Ordinal to place them
      pageSize - how many cells to fetch per request
      handler - given the cellset (its ID and expanded Axes) and its cells, in ordinal order; its result is this method's
      Returns:
      what the handler gives, with the cellset deleted at the end