Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit cd55163

Browse files
committed
Merge pull request #32 from launchdarkly/jko/store-javadoc
Store javadocs
2 parents 0602c8e + fd9b96e commit cd55163

File tree

5 files changed

+145
-9
lines changed

5 files changed

+145
-9
lines changed

src/main/java/com/launchdarkly/client/FeatureStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public interface FeatureStore {
6666
void upsert(String key, FeatureRep<?> feature);
6767

6868
/**
69-
* Returns true if this store has been initialized (i.e. if {@link #init(java.util.Map)} has been called)
69+
* Returns true if this store has been initialized
7070
*
7171
* @return true if this store has been initialized
7272
*/

src/main/java/com/launchdarkly/client/InMemoryFeatureStore.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44
import java.util.Map;
55
import java.util.concurrent.locks.ReentrantReadWriteLock;
66

7+
/**
8+
* A thread-safe, versioned store for {@link com.launchdarkly.client.FeatureRep} objects based on a
9+
* {@link HashMap}
10+
*
11+
*/
712
public class InMemoryFeatureStore implements FeatureStore {
813

914
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1015
final Map<String, FeatureRep<?>> features = new HashMap<String, FeatureRep<?>>();
1116
volatile boolean initialized = false;
1217

18+
19+
/**
20+
*
21+
* Returns the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
22+
* null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
23+
* been deleted.
24+
*
25+
* @param key the key whose associated {@link com.launchdarkly.client.FeatureRep} is to be returned
26+
* @return the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
27+
* null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
28+
* been deleted.
29+
*/
1330
@Override
1431
public FeatureRep<?> get(String key) {
1532
try {
@@ -24,6 +41,12 @@ public FeatureRep<?> get(String key) {
2441
}
2542
}
2643

44+
/**
45+
* Returns a {@link java.util.Map} of all associated features.
46+
*
47+
*
48+
* @return a map of all associated features.
49+
*/
2750
@Override
2851
public Map<String, FeatureRep<?>> all() {
2952
try {
@@ -41,18 +64,33 @@ public Map<String, FeatureRep<?>> all() {
4164
}
4265
}
4366

67+
68+
/**
69+
* Initializes (or re-initializes) the store with the specified set of features. Any existing entries
70+
* will be removed.
71+
*
72+
* @param features the features to set the store
73+
*/
4474
@Override
45-
public void init(Map<String, FeatureRep<?>> fs) {
75+
public void init(Map<String, FeatureRep<?>> features) {
4676
try {
4777
lock.writeLock().lock();
4878
this.features.clear();
49-
this.features.putAll(fs);
79+
this.features.putAll(features);
5080
initialized = true;
5181
} finally {
5282
lock.writeLock().unlock();
5383
}
5484
}
5585

86+
/**
87+
*
88+
* Deletes the feature associated with the specified key, if it exists and its version
89+
* is less than or equal to the specified version.
90+
*
91+
* @param key the key of the feature to be deleted
92+
* @param version the version for the delete operation
93+
*/
5694
@Override
5795
public void delete(String key, int version) {
5896
try {
@@ -72,6 +110,13 @@ else if (f == null) {
72110
}
73111
}
74112

113+
/**
114+
* Update or insert the feature associated with the specified key, if its version
115+
* is less than or equal to the version specified in the argument feature.
116+
*
117+
* @param key
118+
* @param feature
119+
*/
75120
@Override
76121
public void upsert(String key, FeatureRep<?> feature) {
77122
try {
@@ -87,6 +132,11 @@ public void upsert(String key, FeatureRep<?> feature) {
87132
}
88133
}
89134

135+
/**
136+
* Returns true if this store has been initialized
137+
*
138+
* @return true if this store has been initialized
139+
*/
90140
@Override
91141
public boolean initialized() {
92142
return initialized;

src/main/java/com/launchdarkly/client/LDConfig.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class LDConfig {
3434
final boolean stream;
3535
final boolean debugStreaming;
3636
final FeatureStore featureStore;
37+
final boolean useLdd;
3738

3839
protected LDConfig(Builder builder) {
3940
this.baseURI = builder.baseURI;
@@ -46,6 +47,7 @@ protected LDConfig(Builder builder) {
4647
this.stream = builder.stream;
4748
this.debugStreaming = builder.debugStreaming;
4849
this.featureStore = builder.featureStore;
50+
this.useLdd = builder.useLdd;
4951
}
5052

5153
/**
@@ -72,6 +74,7 @@ public static class Builder{
7274
private String proxyScheme;
7375
private boolean stream = true;
7476
private boolean debugStreaming = false;
77+
private boolean useLdd = false;
7578
private FeatureStore featureStore = new InMemoryFeatureStore();
7679

7780
/**
@@ -110,7 +113,7 @@ public Builder featureStore(FeatureStore store) {
110113
* retrieved feature with the value in the feature store. There is a performance cost to this, so it is not
111114
* recommended for production use.
112115
* @param debugStreaming whether streaming mode should be debugged
113-
* @return
116+
* @return the builder
114117
*/
115118
public Builder debugStreaming(boolean debugStreaming) {
116119
this.debugStreaming = debugStreaming;
@@ -215,7 +218,7 @@ public Builder capacity(int capacity) {
215218
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
216219
* </p>
217220
* @param host
218-
* @return
221+
* @return the builder
219222
*/
220223
public Builder proxyHost(String host) {
221224
this.proxyHost = host;
@@ -231,7 +234,7 @@ public Builder proxyHost(String host) {
231234
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
232235
* </p>
233236
* @param port
234-
* @return
237+
* @return the builder
235238
*/
236239
public Builder proxyPort(int port) {
237240
this.proxyPort = port;
@@ -247,13 +250,24 @@ public Builder proxyPort(int port) {
247250
* a proxy will not be used, and {@link LDClient} will connect to LaunchDarkly directly.
248251
* </p>
249252
* @param scheme
250-
* @return
253+
* @return the builder
251254
*/
252255
public Builder proxyScheme(String scheme) {
253256
this.proxyScheme = scheme;
254257
return this;
255258
}
256259

260+
/**
261+
* Set whether this client should subscribe to the streaming API, or whether the LaunchDarkly daemon is in use
262+
* instead
263+
* @param useLdd
264+
* @return the builder
265+
*/
266+
public Builder useLdd(boolean useLdd) {
267+
this.useLdd = useLdd;
268+
return this;
269+
}
270+
257271
HttpHost proxyHost() {
258272
if (this.proxyHost == null && this.proxyPort == -1 && this.proxyScheme == null) {
259273
return null;

src/main/java/com/launchdarkly/client/RedisFeatureStore.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,47 @@
1515
import java.util.Map;
1616
import java.util.concurrent.TimeUnit;
1717

18+
/**
19+
* A thread-safe, versioned store for {@link com.launchdarkly.client.FeatureRep} objects backed by Redis. Also
20+
* supports an optional in-memory cache configuration that can be used to improve performance.
21+
*
22+
*/
1823
public class RedisFeatureStore implements FeatureStore {
1924
private static final String DEFAULT_PREFIX = "launchdarkly";
2025
private final Jedis jedis;
2126
private LoadingCache<String, FeatureRep<?>> cache;
2227
private String prefix;
2328

29+
/**
30+
*
31+
* @param host the host for the Redis connection
32+
* @param port the port for the Redis connection
33+
* @param prefix a namespace prefix for all keys stored in Redis
34+
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
35+
*/
2436
public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSecs) {
2537
jedis = new Jedis(host, port);
2638
setPrefix(prefix);
2739
createCache(cacheTimeSecs);
2840
}
2941

42+
/**
43+
* Creates a new store instance that connects to Redis with the provided URI, prefix, and cache timeout.
44+
*
45+
* @param uri the URI for the Redis connection
46+
* @param prefix a namespace prefix for all keys stored in Redis
47+
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
48+
*/
3049
public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) {
3150
jedis = new Jedis(uri);
3251
setPrefix(prefix);
3352
createCache(cacheTimeSecs);
3453
}
3554

55+
/**
56+
* Creates a new store instance that connects to Redis with a default connection (localhost port 6379) and no in-memory cache.
57+
*
58+
*/
3659
public RedisFeatureStore() {
3760
jedis = new Jedis("localhost");
3861
this.prefix = DEFAULT_PREFIX;
@@ -59,7 +82,17 @@ public FeatureRep<?> load(String key) throws Exception {
5982
}
6083
}
6184

62-
85+
/**
86+
*
87+
* Returns the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
88+
* null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
89+
* been deleted.
90+
*
91+
* @param key the key whose associated {@link com.launchdarkly.client.FeatureRep} is to be returned
92+
* @return the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
93+
* null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
94+
* been deleted.
95+
*/
6396
@Override
6497
public FeatureRep<?> get(String key) {
6598
if (cache != null) {
@@ -69,6 +102,14 @@ public FeatureRep<?> get(String key) {
69102
}
70103
}
71104

105+
106+
/**
107+
* Returns a {@link java.util.Map} of all associated features. This implementation does not take advantage
108+
* of the in-memory cache, so fetching all features will involve a fetch from Redis.
109+
*
110+
*
111+
* @return a map of all associated features.
112+
*/
72113
@Override
73114
public Map<String, FeatureRep<?>> all() {
74115
Map<String,String> featuresJson = jedis.hgetAll(featuresKey());
@@ -84,7 +125,12 @@ public Map<String, FeatureRep<?>> all() {
84125

85126
return result;
86127
}
87-
128+
/**
129+
* Initializes (or re-initializes) the store with the specified set of features. Any existing entries
130+
* will be removed.
131+
*
132+
* @param features the features to set the store
133+
*/
88134
@Override
89135
public void init(Map<String, FeatureRep<?>> features) {
90136
Gson gson = new Gson();
@@ -99,6 +145,15 @@ public void init(Map<String, FeatureRep<?>> features) {
99145
t.exec();
100146
}
101147

148+
149+
/**
150+
*
151+
* Deletes the feature associated with the specified key, if it exists and its version
152+
* is less than or equal to the specified version.
153+
*
154+
* @param key the key of the feature to be deleted
155+
* @param version the version for the delete operation
156+
*/
102157
@Override
103158
public void delete(String key, int version) {
104159
try {
@@ -126,6 +181,13 @@ public void delete(String key, int version) {
126181

127182
}
128183

184+
/**
185+
* Update or insert the feature associated with the specified key, if its version
186+
* is less than or equal to the version specified in the argument feature.
187+
*
188+
* @param key
189+
* @param feature
190+
*/
129191
@Override
130192
public void upsert(String key, FeatureRep<?> feature) {
131193
try {
@@ -149,6 +211,11 @@ public void upsert(String key, FeatureRep<?> feature) {
149211
}
150212
}
151213

214+
/**
215+
* Returns true if this store has been initialized
216+
*
217+
* @return true if this store has been initialized
218+
*/
152219
@Override
153220
public boolean initialized() {
154221
return jedis.exists(featuresKey());

src/main/java/com/launchdarkly/client/StreamProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class StreamProcessor implements Closeable {
4242
}
4343

4444
void subscribe() {
45+
// If the LaunchDarkly daemon is to be used, then do not subscribe to the stream
46+
if (config.useLdd) {
47+
return;
48+
}
49+
4550
MultivaluedMap<String, Object> headers = new StringKeyIgnoreCaseMultivaluedMap<Object>();
4651
headers.putSingle("Authorization", "api_key " + this.apiKey);
4752
headers.putSingle("User-Agent", "JavaClient/" + LDClient.CLIENT_VERSION);

0 commit comments

Comments
 (0)