> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# The API

> Declare Copper measures and hierarchies in the Atoti cube builder, and check which Copper features a query cube supports and which must be defined in the data cubes of a distributed cluster

The Copper API defines the measures and hierarchies of an Atoti cube in Java code.
It assumes familiarity with the [Copper introduction](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_intro.md).

## Atoti cube builder

Copper calculations are declared in the `withCalculations(Consumer<ICopperContext>)` method of the cube builder.

The entry point of the Copper API is the Copper context, or simply the context.
The context knows the measures and hierarchies that are already defined.
Copper measures and hierarchies are created with the API, then published to the context.
The following snippet declares Copper calculations in the cube builder:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
.withCalculations(
    context -> {
      // Here you can describe your calculations with the context
    });
```

> [!NOTE]
>
> The snippet defines all the Copper measures in a single lambda.
> For brevity, the other examples in the Copper documentation show only the body of this lambda.

The measures and hierarchies declared in the lambda are added to the cube description before the cube starts.

The [Copper](https://assets.activeviam.com/products/atoti/server/latest/javadoc/com/activeviam/activepivot/copper/api/Copper.html) class provides the static methods that create measures and hierarchies.
Every Copper measure implements the `CopperMeasure` interface.
These measures are the building blocks of Copper calculations: their methods build more complex calculations.

A measure published in the lambda can be reused by the Copper calculations that follow it.
The following pseudo-code publishes a measure, then builds a second measure on top of it:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
CopperMeasure m1 = Copper.method(); // Create a measure with a static method from the Copper class
m1.publish(context);
// m1 is now available in following calculations

CopperMeasure m2 = m1.function(); // Apply function() on m1 to create a new measure
m2.publish(context);
// m2 is now available in following calculations
```

All the measures of a cube can be declared in this lambda, in the same way.
This includes Copper measures, native measures, aggregated measures, and legacy post-processors.

## How does Copper work in distributed environments?

Copper works in both the data cubes and the query cubes of a distributed Atoti cluster.
A [cluster](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_intro.md#clusters) is a group of data cubes and query cubes that communicate together.
Within a cluster, an [application](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_intro.md#applications) is a group of data cubes that share the same topology: the same hierarchies, levels, and measures.
For more information about the distributed architecture of Atoti and its concepts, see [Distributed architecture](https://docs.activeviam.com/engine/java-sdk/6.2/distributed/distributed_intro.md).

### Copper in data cubes

Copper works in distributed data cubes without extra properties or calls.
Measures behave as intended in a distributed environment, and their computations are distributed when possible.

### Copper in query cubes

Copper in a query cube declares measures that build on top of the measures defined in the data cubes.
A query cube can combine measures from several applications, even when they **do not share the same topology**.
For example, a query cube connected to a PnL application and a VaR application can define a measure from `VaR_95` and `PnL.SUM`, which neither application defines.

The Copper API in a query cube is similar to [the API that defines measures in a data cube](https://docs.activeviam.com/engine/java-sdk/6.2/copper/copper_measures.md).
The main difference is how a calculation references an existing measure:

| Measure to reference | Use |
| - | - |
| Measure defined in a data cube | `Copper.measureFromDataCube(measureName, measureType)` |
| Measure defined in the same query cube | `Copper.measure(measureName)` |

The following snippet defines a query cube measure from a data cube measure:

```java theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
final CopperMeasure m1 =
    Copper.measureFromDataCube("measureFromDataCube1", ILiteralType.DOUBLE);
final CopperMeasure m2 = Copper.measureFromDataCube("measureFromDataCube2", ILiteralType.LONG);
final CopperMeasure m1_plus_m2 = m1.plus(m2).as("m1+m2");
Copper.measure("m1+m2").multiply(Copper.constant(2)).as("multiply");
```

## Which Copper features are restricted in query cubes?

Any Copper calculation that a query cube supports also works in a data cube, as long as it uses measures from a single application.
The reverse is not true: a query cube has no database and no hierarchies of its own.
The Copper features that read the database or need information from the cube description, such as the type of a level, cannot be used directly in a query cube.
Define them in the data cubes, then reference them in the query cube with `Copper.measureFromDataCube(measureName, measureType)`.

Calculations on existing measures, such as arithmetic, `per(...).sum()`, `shift(...)`, `parentValueOn(...)`, `grandTotalExcluding(...)`, and window functions, work directly in a query cube.

For example, the following features must be defined in the data cubes:

* Field aggregations, such as `Copper.sum(fieldName)`
* `Copper.storeLookup(...)`
* Joins with `Copper.store(...).joinToCube(...)`
* `filter(...)`

A query cube also cannot publish new hierarchies.
It rejects them with the error "Adding new hierarchies is not permitted in Query cubes."

When a measure is published in a query cube, Atoti checks every measure it depends on.
If any of these measures needs a data cube, publication fails with the following error: "measure cannot be published because it relies on features that only work in Data cubes."
A supported operation applied to an unsupported measure is therefore also rejected.
To fix the error, define the measure that needs a data cube in the data cubes, then reference it in the query cube with `Copper.measureFromDataCube(measureName, measureType)`.

> [!NOTE]
>
> A measure that uses `Copper.storeLookup` can be defined in a data cube and used from the query cube.
> Continuous queries on the query cube are not notified of the database updates that affect this measure.
> The query cube has no access to the database.
