Skip to content

Commit 8615787

Browse files
committed
1 parent 001746b commit 8615787

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/Context.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@
1717

1818
package org.apache.ignite.internal.thread.context;
1919

20-
/** Represents mapping of {@link ContextAttribute} to their corresponding values with the ability to be attached to the thread. */
20+
/**
21+
* Represents a set of mappings from the {@link ContextAttribute} to its corresponding value. The Context can be attached
22+
* to a thread, making the {@link ContextAttribute} values accessible through the {@link ContextAttribute#get()}
23+
* method when invoked from the same thread.
24+
*
25+
* @see ContextAttribute
26+
* @see ContextSnapshot
27+
* @see AttributeValueHolder#attach()
28+
*/
2129
public final class Context {
2230
/**
23-
* Creates a new {@link AttributeValueHolder} with a single mapping of the specified {@link AttributeValueHolder}
24-
* to the specified value. The {@link AttributeValueHolder} can be used to accumulate mappings.
31+
* Creates a new Context containing a single mapping from the specified {@link AttributeValueHolder} to its value.
32+
* The returned {@link AttributeValueHolder} represents the added mapping and can be used to accumulate to Context
33+
* more mappings form {@link ContextAttribute} to its value.
2534
*
2635
* @see AttributeValueHolder#with(ContextAttribute, Object)
2736
*/
@@ -30,7 +39,7 @@ public static <T> AttributeValueHolder with(ContextAttribute<T> attr, T val) {
3039
}
3140

3241
/** */
33-
public static final class AttributeValueHolder extends ContextDataChain<AttributeValueHolder> {
42+
public static final class AttributeValueHolder extends ContextDataChainNode<AttributeValueHolder> {
3443
/** */
3544
private static final AttributeValueHolder ROOT = new AttributeValueHolder();
3645

@@ -54,7 +63,13 @@ private <T> AttributeValueHolder(ContextAttribute<T> attr, T val, AttributeValue
5463
this.val = val;
5564
}
5665

57-
/** Adds a new mapping of the specified attribute to its value to the current {@link ContextDataChain}. */
66+
/**
67+
* Expands Context by adding new mapping from the specified {@link ContextAttribute} to its value.
68+
* After this operation, the Context contains all previously added mappings plus the new one.
69+
*
70+
* @return {@link AttributeValueHolder} instance that represents the added mapping and can be used to accumulate
71+
* more mappings from {@link ContextAttribute} to its values.
72+
*/
5873
public <T> AttributeValueHolder with(ContextAttribute<T> attr, T val) {
5974
return attr.get() == val ? this : new AttributeValueHolder(attr, val, this);
6075
}
@@ -79,9 +94,9 @@ <T> T value() {
7994
}
8095

8196
/**
82-
* Attaches {@link ContextAttribute} values stored in current {@link ContextDataChain} to the thread
83-
* this method is called from. If {@link ContextAttribute} value was already attached for the current thread,
84-
* its value will be stashed and replaced by the new ones.
97+
* Attaches {@link ContextAttribute} values stored in current Context to the thread from which this method is
98+
* called. If {@link ContextAttribute} value was already attached for the current thread, its value will be
99+
* stashed and replaced by the new one.
85100
*
86101
* @return {@link Scope} instance that, when closed, resets the values for all {@link ContextAttribute}s added
87102
* to the current Context and restores them to the previously attached values, if any.

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ContextDataChain.java renamed to modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ContextDataChainNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
package org.apache.ignite.internal.thread.context;
1919

2020
/** */
21-
public abstract class ContextDataChain<T> {
21+
public abstract class ContextDataChainNode<T> {
2222
/** */
2323
private final int storedAttrIdBits;
2424

2525
/** */
2626
private final T prev;
2727

2828
/** */
29-
protected ContextDataChain() {
29+
protected ContextDataChainNode() {
3030
storedAttrIdBits = 0;
3131
prev = null;
3232
}
3333

3434
/** */
35-
protected ContextDataChain(int storedAttrIdBits, T prev) {
35+
protected ContextDataChainNode(int storedAttrIdBits, T prev) {
3636
this.storedAttrIdBits = storedAttrIdBits;
3737
this.prev = prev;
3838
}

modules/commons/src/main/java/org/apache/ignite/internal/thread/context/ContextSnapshot.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
package org.apache.ignite.internal.thread.context;
1919

20-
/** */
21-
public class ContextSnapshot extends ContextDataChain<ContextSnapshot> {
20+
/**
21+
* Represents Snapshot of all {@link ContextAttribute}s and their corresponding values. Note that Snapshot also stores
22+
* the states of {@link ContextAttribute}s for which value are note explicitly specified.
23+
*/
24+
public class ContextSnapshot extends ContextDataChainNode<ContextSnapshot> {
2225
/** */
2326
static final ContextSnapshot ROOT = new ContextSnapshot();
2427

@@ -37,7 +40,13 @@ private ContextSnapshot(Context.AttributeValueHolder data, ContextSnapshot prev)
3740
this.data = data;
3841
}
3942

40-
/** */
43+
/**
44+
* Stashes all {@link ContextAttribute} values attached to the thread from which this method is called and replaces
45+
* them with ones stored in the current Snapshot.
46+
*
47+
* @return {@link Scope} instance that, when closed, restores the values of all {@link ContextAttribute}s to the
48+
* state they were in before the current method was called.
49+
*/
4150
public Scope restore() {
4251
ThreadLocalContextStorage threadData = ThreadLocalContextStorage.get();
4352

@@ -71,7 +80,10 @@ Context.AttributeValueHolder data() {
7180
return this == ROOT;
7281
}
7382

74-
/** */
83+
/**
84+
* Captures Snapshot of all {@link ContextAttribute}s and their corresponding values attached to the thread from which
85+
* this method is called.
86+
*/
7587
public static ContextSnapshot capture() {
7688
return ThreadLocalContextStorage.get().snapshot();
7789
}

0 commit comments

Comments
 (0)