Skip to content

Commit

Permalink
Use InheritableThreadLocal in datastores (getgauge#696)
Browse files Browse the repository at this point in the history
* Use InheritableThreadLocal in datastores

Signed-off-by: Glib Briia <[email protected]>

* Bump up the version to 0.9.2

Signed-off-by: Glib Briia <[email protected]>

---------

Signed-off-by: Glib Briia <[email protected]>
  • Loading branch information
Glib Briia authored May 29, 2023
1 parent d0f5f07 commit 2f185e9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ gauge run specs
#### Install specific version
* Installing specific version
```
gauge install java --version 0.9.1
gauge install java --version 0.9.2
```

#### Offline installation
* Download the plugin from [Releases](https://github.com/getgauge/gauge-java/releases)
```
gauge install java --file gauge-java-0.9.1-windows.x86_64.zip
gauge install java --file gauge-java-0.9.2-windows.x86_64.zip
```

#### Build from source
Expand Down
2 changes: 1 addition & 1 deletion java.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "java",
"version": "0.9.1",
"version": "0.9.2",
"description": "Java support for gauge",
"install": {
"windows": [],
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@

<properties>
<maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
<projectVersion>0.9.1</projectVersion>
<projectVersion>0.9.2</projectVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* @deprecated DataStore is no longer valid. The usage together with DataStoreFactory API will throw an Exception in multithreaded execution.
* <p>Use specific data stores instead.</p>
* @see com.thoughtworks.gauge.datastore.SuiteDataStore
* @see com.thoughtworks.gauge.datastore.SpecDataStore
* @see com.thoughtworks.gauge.datastore.ScenarioDataStore
*/
@Deprecated
public class DataStore {

private HashMap<Object, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import java.util.concurrent.ConcurrentHashMap;

public class ScenarioDataStore {
private static ThreadLocal<ConcurrentHashMap<Object, Object>> map = ThreadLocal.withInitial(ConcurrentHashMap::new);
private static final InheritableThreadLocal<ConcurrentHashMap<Object, Object>> MAP = new InheritableThreadLocal<ConcurrentHashMap<Object, Object>>() {
@Override
protected ConcurrentHashMap<Object, Object> initialValue() {
return new ConcurrentHashMap<>();
}
};

/**
* @param key - Key of the data entry
* @param value - value of the Data entry
*/
public static synchronized void put(Object key, Object value) {
if (key != null && value != null) {
map.get().put(key, value);
MAP.get().put(key, value);
}
}

Expand All @@ -29,7 +34,7 @@ public static synchronized void put(Object key, Object value) {
*/
public static synchronized Object remove(Object key) {
if (key != null) {
return map.get().remove(key);
return MAP.get().remove(key);
}
return null;
}
Expand All @@ -40,7 +45,7 @@ public static synchronized Object remove(Object key) {
*/
public static synchronized Object get(Object key) {
if (key != null) {
return map.get().get(key);
return MAP.get().get(key);
}
return null;
}
Expand All @@ -50,11 +55,11 @@ public static synchronized Object get(Object key) {
* @return A set of keys stored in datastore
*/
public static synchronized Set<Object> items() {
return Collections.unmodifiableSet(map.get().keySet());
return Collections.unmodifiableSet(MAP.get().keySet());
}

static synchronized void clear() {
map.get().clear();
MAP.get().clear();
}

}
18 changes: 11 additions & 7 deletions src/main/java/com/thoughtworks/gauge/datastore/SpecDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
import java.util.concurrent.ConcurrentHashMap;

public class SpecDataStore {
private static ThreadLocal<ConcurrentHashMap<Object, Object>> map = ThreadLocal.withInitial(ConcurrentHashMap::new);

private static final InheritableThreadLocal<ConcurrentHashMap<Object, Object>> MAP = new InheritableThreadLocal<ConcurrentHashMap<Object, Object>>() {
@Override
protected ConcurrentHashMap<Object, Object> initialValue() {
return new ConcurrentHashMap<>();
}
};
/**
* @param key - Key of the data entry
* @param value - value of the Data entry
*/
public static synchronized void put(Object key, Object value) {
if (key != null && value != null) {
map.get().put(key, value);
MAP.get().put(key, value);
}
}

Expand All @@ -29,7 +33,7 @@ public static synchronized void put(Object key, Object value) {
*/
public static synchronized Object remove(Object key) {
if (key != null) {
return map.get().remove(key);
return MAP.get().remove(key);
}
return null;
}
Expand All @@ -40,7 +44,7 @@ public static synchronized Object remove(Object key) {
*/
public static synchronized Object get(Object key) {
if (key != null) {
return map.get().get(key);
return MAP.get().get(key);
}
return null;
}
Expand All @@ -50,10 +54,10 @@ public static synchronized Object get(Object key) {
* @return A set of keys stored in datastore
*/
public static synchronized Set<Object> items() {
return Collections.unmodifiableSet(map.get().keySet());
return Collections.unmodifiableSet(MAP.get().keySet());
}

static synchronized void clear() {
map.get().clear();
MAP.get().clear();
}
}
17 changes: 11 additions & 6 deletions src/main/java/com/thoughtworks/gauge/datastore/SuiteDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import java.util.concurrent.ConcurrentHashMap;

public class SuiteDataStore {
private static ThreadLocal<ConcurrentHashMap<Object, Object>> map = ThreadLocal.withInitial(ConcurrentHashMap::new);
private static final InheritableThreadLocal<ConcurrentHashMap<Object, Object>> MAP = new InheritableThreadLocal<ConcurrentHashMap<Object, Object>>() {
@Override
protected ConcurrentHashMap<Object, Object> initialValue() {
return new ConcurrentHashMap<>();
}
};

/**
* @param key - Key of the data entry
* @param value - value of the Data entry
*/
public static synchronized void put(Object key, Object value) {
if (key != null && value != null) {
map.get().put(key, value);
MAP.get().put(key, value);
}
}

Expand All @@ -29,7 +34,7 @@ public static synchronized void put(Object key, Object value) {
*/
public static synchronized Object remove(Object key) {
if (key != null) {
return map.get().remove(key);
return MAP.get().remove(key);
}
return null;
}
Expand All @@ -40,7 +45,7 @@ public static synchronized Object remove(Object key) {
*/
public static synchronized Object get(Object key) {
if (key != null) {
return map.get().get(key);
return MAP.get().get(key);
}
return null;
}
Expand All @@ -50,10 +55,10 @@ public static synchronized Object get(Object key) {
* @return A set of keys stored in datastore
*/
public static synchronized Set<Object> items() {
return Collections.unmodifiableSet(map.get().keySet());
return Collections.unmodifiableSet(MAP.get().keySet());
}

static synchronized void clear() {
map.get().clear();
MAP.get().clear();
}
}

0 comments on commit 2f185e9

Please sign in to comment.