Skip to content

Commit

Permalink
Fixes document generation
Browse files Browse the repository at this point in the history
  • Loading branch information
chedim committed Aug 5, 2024
1 parent 2d48cca commit 09d14b0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,12 @@ private static void loadKVDocuments(DefaultMutableTreeNode parentNode, Tree tree
}

public static boolean stubsAvailable(String bucket, String scope, String collection) {
return ActiveCluster.getInstance().getChild(bucket).flatMap(b -> b.getChild(scope)).flatMap(s -> s.getChild(collection)).map(col -> ((CouchbaseCollection) col).generateDocument()).filter(Objects::nonNull).findAny().isPresent();
return ActiveCluster.getInstance()
.getChild(bucket)
.flatMap(b -> b.getChild(scope))
.flatMap(s -> s.getChild(collection))
.map(col -> ((CouchbaseCollection) col).generateDocument())
.anyMatch(Objects::nonNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ default Stream<? extends CouchbaseClusterEntity> getChild(String name) {
}
Stream<? extends CouchbaseClusterEntity> childrenStream = getChildren().stream();
if (this.getName() != null) {
childrenStream = Streams.concat(Stream.of(this), childrenStream);
childrenStream = Streams.concat(childrenStream, Stream.of(this));
}
return childrenStream
.flatMap(c -> c.getName() == null ? c.getChildren().stream() : Stream.of(c))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.manager.collection.CollectionSpec;
import com.couchbase.intellij.database.InferHelper;
import com.couchbase.intellij.utils.Subscribable;
import com.couchbase.intellij.workbench.Log;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import org.jetbrains.annotations.NotNull;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class CouchbaseCollection implements CouchbaseClusterEntity {
private CollectionSpec spec;
private CouchbaseScope parent;
private Set<CouchbaseDocumentFlavor> children;
private Subscribable<Set<CouchbaseDocumentFlavor>> children = new Subscribable<>();

private AtomicBoolean updating = new AtomicBoolean(false);

Expand Down Expand Up @@ -79,11 +79,11 @@ private void loadSchema(JsonObject schema) {
final String path = path();
if (schema != null) {
JsonArray content = schema.getArray("content");
children = IntStream.range(0, content.size()).boxed()
children.set(IntStream.range(0, content.size()).boxed()
.map(content::getObject)
.map(flavor -> new CouchbaseDocumentFlavor(this, flavor))
.peek(CouchbaseDocumentFlavor::updateSchema)
.collect(Collectors.toSet());
.collect(Collectors.toSet()));
} else {
Log.debug("nada data for schemata " + path);
}
Expand All @@ -96,10 +96,14 @@ public Cluster getCluster() {

@Override
public Set<CouchbaseDocumentFlavor> getChildren() {
if (children == null) {
if (!children.isPresent()) {
updateSchema();
}
return children;
try {
return children.get(1000);
} catch (InterruptedException e) {
return null;
}
}

public JsonObject generateDocument() {
Expand All @@ -119,7 +123,8 @@ public JsonObject generateDocument() {
public JsonArray toJson() {
JsonArray result = JsonArray.create();
if (children != null) {
children.stream().map(CouchbaseDocumentFlavor::toJson)
children.get().stream().flatMap(Collection::stream)
.map(CouchbaseDocumentFlavor::toJson)
.forEach(result::add);
}
return result;
Expand All @@ -130,11 +135,8 @@ public Set<String> getAllAttributeNames() {
Set<String> attributeNames = new HashSet<>();

// Check if children are not null
if (this.children != null) {
for (CouchbaseDocumentFlavor flavor : this.children) {
collectAttributeNames(flavor, attributeNames);
}
}
this.children.get().stream().flatMap(Collection::stream)
.forEach(flavor -> collectAttributeNames(flavor, attributeNames));

return attributeNames;
}
Expand Down
51 changes: 37 additions & 14 deletions src/main/java/com/couchbase/intellij/utils/Subscribable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import cn.hutool.core.stream.StreamUtil;

import java.lang.ref.WeakReference;
import java.time.Duration;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -21,19 +23,16 @@ public Subscribable(T value) {
}

public void set(T value) {
this.value = Optional.ofNullable(value);
subscribers = subscribers.entrySet().stream()
.filter(subscriber -> subscriber.getKey() != null
&& subscriber.getKey().get() != null
&& subscriber.getValue() != null)
.filter(subscriber -> {
try {
return subscriber.getValue().apply(this.value);
} catch (Throwable e) {
return false;
}
})
.collect(Collectors.toMap(s -> s.getKey(), s -> s.getValue()));
synchronized (this) {
this.value = Optional.ofNullable(value);
subscribers = subscribers.entrySet().stream().filter(subscriber -> subscriber.getKey() != null && subscriber.getKey().get() != null && subscriber.getValue() != null).filter(subscriber -> {
try {
return subscriber.getValue().apply(this.value);
} catch (Throwable e) {
return false;
}
}).collect(Collectors.toMap(s -> s.getKey(), s -> s.getValue()));
}
}

/**
Expand All @@ -58,7 +57,31 @@ public void unsubscribe(Object key) {
}

public Optional<T> get() {
return value;
try {
return Optional.of(get(0));
} catch (InterruptedException e) {
return Optional.empty();
}
}

public T get(long millis) throws InterruptedException {
synchronized (this) {
if (!value.isPresent()) {
wait(millis);
}
return value.get();
}
}

public void get(Consumer<Optional<T>> subscriber) {
if (isPresent()) {
subscriber.accept(value);
} else {
subscribe(subscriber, t -> {
subscriber.accept(t);
return false;
});
}
}

public boolean isPresent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ protected void setUp() throws Exception {

@Override
protected void tearDown() throws Exception {
server.close();
try {
server.close();
} catch (Exception e) {
// noop
}
}

protected void enqueueResponse(String text) {
Expand Down

0 comments on commit 09d14b0

Please sign in to comment.