Skip to content

Commit

Permalink
#217 refactor sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jul 1, 2024
1 parent 3c98ecd commit 1a4e9eb
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@
* #L%
*/

import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.filesystem.metadata.memory.MemoryMetaData;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
*
* @author t.marx
*/
public class AbstractMetaData {
public abstract class AbstractMetaData implements MetaData {
public static boolean isVisible (ContentNode node) {
return node != null
// check if some parent is hidden
Expand All @@ -37,4 +49,97 @@ public static boolean isVisible (ContentNode node) {
&& !node.isHidden()
&& !node.isSection();
}

@Override
public Optional<ContentNode> byUri(String uri) {
if (!nodes().containsKey(uri)) {
return Optional.empty();
}
return Optional.of(nodes().get(uri));
}

@Override
public Optional<ContentNode> findFolder(String uri) {
return getFolder(uri);
}

protected Optional<ContentNode> getFolder(String uri) {
var parts = uri.split(Constants.SPLIT_PATH_PATTERN);

final AtomicReference<ContentNode> folder = new AtomicReference<>(null);
Stream.of(parts).forEach(part -> {
if (part.endsWith(".md")) {
return;
}
if (folder.get() == null) {
folder.set(tree().get(part));
} else {
folder.set(folder.get().children().get(part));
}
});
return Optional.ofNullable(folder.get());
}

@Override
public void createDirectory(String uri) {
if (Strings.isNullOrEmpty(uri)) {
return;
}
var parts = uri.split(Constants.SPLIT_PATH_PATTERN);
ContentNode n = new ContentNode(uri, parts[parts.length - 1], Map.of(), true);

Optional<ContentNode> parentFolder;
if (parts.length == 1) {
parentFolder = getFolder(uri);
} else {
var parentPath = Arrays.copyOfRange(parts, 0, parts.length - 1);
var parentUri = String.join("/", parentPath);
parentFolder = getFolder(parentUri);
}

if (parentFolder.isPresent()) {
parentFolder.get().children().put(n.name(), n);
} else {
tree().put(n.name(), n);
}
}

@Override
public List<ContentNode> listChildren(String uri) {
if ("".equals(uri)) {
return tree().values().stream()
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());

} else {
Optional<ContentNode> findFolder = findFolder(uri);
if (findFolder.isPresent()) {
return findFolder.get().children().values()
.stream()
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());
}
}
return Collections.emptyList();
}

protected ContentNode mapToIndex(ContentNode node) {
if (node.isDirectory()) {
var tempNode = node.children().entrySet().stream().filter((entry)
-> entry.getKey().equals("index.md")
).findFirst();
if (tempNode.isPresent()) {
return tempNode.get().getValue();
}
return null;
} else {
return node;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,20 @@
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.ContentQuery;
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.filesystem.metadata.AbstractMetaData;
import com.google.common.base.Strings;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
*
* @author t.marx
*/
public class MemoryMetaData extends AbstractMetaData implements MetaData {
public class MemoryMetaData extends AbstractMetaData {

private final ConcurrentMap<String, ContentNode> nodes = new ConcurrentHashMap<>();

Expand All @@ -69,91 +60,6 @@ public ConcurrentMap<String, ContentNode> tree() {
return new ConcurrentHashMap<>(tree);
}

@Override
public void createDirectory(final String uri) {
if (Strings.isNullOrEmpty(uri)) {
return;
}
var parts = uri.split(Constants.SPLIT_PATH_PATTERN);
ContentNode n = new ContentNode(uri, parts[parts.length - 1], Map.of(), true);

Optional<ContentNode> parentFolder;
if (parts.length == 1) {
parentFolder = getFolder(uri);
} else {
var parentPath = Arrays.copyOfRange(parts, 0, parts.length - 1);
var parentUri = String.join("/", parentPath);
parentFolder = getFolder(parentUri);
}

if (parentFolder.isPresent()) {
parentFolder.get().children().put(n.name(), n);
} else {
tree.put(n.name(), n);
}
}

@Override
public List<ContentNode> listChildren(String uri) {
if ("".equals(uri)) {
return tree.values().stream()
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());

} else {
Optional<ContentNode> findFolder = findFolder(uri);
if (findFolder.isPresent()) {
return findFolder.get().children().values()
.stream()
.filter(node -> !node.isHidden())
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MemoryMetaData::isVisible)
.collect(Collectors.toList());
}
}
return Collections.emptyList();
}

protected ContentNode mapToIndex(ContentNode node) {
if (node.isDirectory()) {
var tempNode = node.children().entrySet().stream().filter((entry)
-> entry.getKey().equals("index.md")
).findFirst();
if (tempNode.isPresent()) {
return tempNode.get().getValue();
}
return null;
} else {
return node;
}
}

@Override
public Optional<ContentNode> findFolder(String uri) {
return getFolder(uri);
}

private Optional<ContentNode> getFolder(String uri) {
var parts = uri.split(Constants.SPLIT_PATH_PATTERN);

final AtomicReference<ContentNode> folder = new AtomicReference<>(null);
Stream.of(parts).forEach(part -> {
if (part.endsWith(".md")) {
return;
}
if (folder.get() == null) {
folder.set(tree.get(part));
} else {
folder.set(folder.get().children().get(part));
}
});
return Optional.ofNullable(folder.get());
}

@Override
public void addFile(final String uri, final Map<String, Object> data, final LocalDate lastModified) {

Expand All @@ -170,14 +76,6 @@ public void addFile(final String uri, final Map<String, Object> data, final Loca
}
}

@Override
public Optional<ContentNode> byUri(final String uri) {
if (!nodes.containsKey(uri)) {
return Optional.empty();
}
return Optional.of(nodes.get(uri));
}

void remove(String uri) {
var node = nodes.remove(uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class QueryUtil {



protected static Map<Object, List<ContentNode>> groupby(final Stream<ContentNode> nodes, final String field) {
public static Map<Object, List<ContentNode>> groupby(final Stream<ContentNode> nodes, final String field) {
return nodes.collect(Collectors.groupingBy((node) -> MapUtil.getValue(node.data(), field)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github.thmarx.cms.api.db.Page;
import com.github.thmarx.cms.filesystem.MetaData;
import com.github.thmarx.cms.filesystem.metadata.AbstractMetaData;
import com.github.thmarx.cms.filesystem.metadata.memory.QueryUtil;
import com.github.thmarx.cms.filesystem.metadata.query.ExcerptMapperFunction;
import com.github.thmarx.cms.filesystem.metadata.query.Queries;
import com.github.thmarx.cms.filesystem.metadata.query.ExtendableQuery;
Expand Down Expand Up @@ -126,7 +127,7 @@ public List<T> get() {
return queryNodes().nodes;
}

private NodeResult<T> queryNodes() {
private List<ContentNode> queryContentNodes() {
queryBuilder.add(new TermQuery(new Term("content.type", contentType)), BooleanClause.Occur.MUST);
if (startUri.isPresent()) {
queryBuilder.add(new PrefixQuery(new Term("_uri", startUri.get())), BooleanClause.Occur.FILTER);
Expand All @@ -135,7 +136,7 @@ private NodeResult<T> queryNodes() {
try {
List<Document> result = index.query(queryBuilder.build());

var nodes = result.stream()
var contentNodes = result.stream()
.map(document -> document.get("_uri"))
.map(metaData::byUri)
.filter(Optional::isPresent)
Expand All @@ -145,7 +146,7 @@ private NodeResult<T> queryNodes() {
.toList();

if (!extensionOperations.isEmpty()) {
nodes = nodes.stream()
contentNodes = contentNodes.stream()
.filter((node) -> {
return extensionOperations.stream()
.map(predicate -> predicate.test(node))
Expand All @@ -154,24 +155,29 @@ private NodeResult<T> queryNodes() {
})
.toList();
}
return contentNodes;
} catch (IOException ex) {
log.error("", ex);
}
return Collections.emptyList();
}

var contentNodes = nodes.stream()
.map(nodeMapper)
.toList();
private NodeResult<T> queryNodes() {
var contentNodes = queryContentNodes();

var total = nodes.size();
var mappedContentNodes = contentNodes.stream()
.map(nodeMapper)
.toList();

return new NodeResult<>(total, contentNodes);
var total = contentNodes.size();

} catch (IOException ex) {
log.error("", ex);
}
return new NodeResult<>(0, Collections.emptyList());
return new NodeResult<>(total, mappedContentNodes);
}

@Override
public Map<Object, List<ContentNode>> groupby(String field) {
return Collections.emptyMap();
var nodes = queryContentNodes();
return QueryUtil.groupby(nodes.stream(), field);
}

@Override
Expand Down
Loading

0 comments on commit 1a4e9eb

Please sign in to comment.