Skip to content

Commit

Permalink
#34 add query and experimental secondary index to filessystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 16, 2023
1 parent b11a847 commit 58d5cac
Show file tree
Hide file tree
Showing 85 changed files with 1,692 additions and 877 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.thmarx.cms.api.annotations;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* @author t.marx
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.TYPE, ElementType.METHOD })
public @interface Experimental {
}

32 changes: 32 additions & 0 deletions cms-filesystem/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-parent</artifactId>
<version>2.9.0-SNAPSHOT</version>
</parent>
<artifactId>cms-filesystem</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-api</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
*/
import com.github.thmarx.cms.api.ModuleFileSystem;
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.api.annotations.Experimental;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
import com.github.thmarx.cms.api.eventbus.events.TemplateChangedEvent;
import com.github.thmarx.cms.api.utils.PathUtil;
import com.github.thmarx.cms.filesystem.datafilter.dimension.Dimension;
import com.github.thmarx.cms.filesystem.query.Query;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -35,9 +37,11 @@
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -53,22 +57,43 @@ public class FileSystem implements ModuleFileSystem {

private final Path hostBaseDirectory;
private final EventBus eventBus;
final Function<Path, Map<String, Object>> contentParser;

private MultiRootRecursiveWatcher fileWatcher;

final ContentParser contentParser = new ContentParser(this);
private Path contentBase;

@Getter
private final MetaData metaData = new MetaData();

public Query query () {
return new Query(new ArrayList<>(metaData.nodes().values()));
}

public Query query (final String startURI) {
Optional<MetaData.MetaNode> startNode = metaData.findFolder(startURI);
if (startNode.isEmpty()) {
return new Query(Collections.emptyList());
}
return new Query(new ArrayList<>(startNode.get().children().values()));

}

@Experimental
protected <T> Dimension<T, MetaData.MetaNode> createDimension (final String name, Function<MetaData.MetaNode, T> dimFunc, Class<T> type) {
return metaData.getDataFilter().dimension(name, dimFunc, type);
}
@Experimental
protected Dimension<?, MetaData.MetaNode> getDimension (final String name) {
return metaData.getDataFilter().dimension(name);
}

public boolean isVisible(final String uri) {
var node = metaData.byUri(uri);
if (node.isEmpty()) {
return false;
}
var n = node.get();
return n.isPublished() && !n.isHidden() && !n.isSection();
return MetaData.isVisible(n);
}

public void shutdown() {
Expand Down Expand Up @@ -187,7 +212,7 @@ private void addOrUpdateMetaData(Path file) throws IOException {
return;
}
log.debug("update meta data for {}", file.toString());
Map<String, Object> fileMeta = contentParser.parseMeta(file);
Map<String, Object> fileMeta = contentParser.apply(file);

var uri = PathUtil.toRelativeFile(file, contentBase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.utils.SectionUtil;
import com.github.thmarx.cms.filesystem.datafilter.DataFilter;
import com.google.common.base.Strings;
import java.time.Instant;
import java.util.Arrays;
Expand All @@ -36,6 +37,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Getter;

/**
*
Expand All @@ -46,10 +48,14 @@ public class MetaData {
private ConcurrentMap<String, MetaNode> nodes = new ConcurrentHashMap<>();

private ConcurrentMap<String, MetaNode> tree = new ConcurrentHashMap<>();

@Getter
private final DataFilter<MetaNode> dataFilter = DataFilter.builder(MetaNode.class).build();

void clear() {
nodes.clear();
tree.clear();
dataFilter.clear();
}

ConcurrentMap<String, MetaNode> nodes() {
Expand Down Expand Up @@ -101,9 +107,7 @@ public List<MetaNode> listChildren(String uri) {
}
})
.filter(node -> node != null)
.filter(node -> !node.isHidden())
.filter(node -> node.isPublished())
.filter(node -> !node.isSection())
.filter(MetaData::isVisible)
.collect(Collectors.toList());

} else {
Expand All @@ -126,15 +130,20 @@ public List<MetaNode> listChildren(String uri) {
}
})
.filter(node -> node != null)
.filter(node -> !node.isHidden())
.filter(node -> node.isPublished())
.filter(node -> !node.isSection())
.filter(MetaData::isVisible)
.collect(Collectors.toList());
}
}
return Collections.emptyList();
}

public static boolean isVisible (MetaNode node) {
return node != null
&& node.isPublished()
&& !node.isHidden()
&& !node.isSection();
}

public Optional<MetaNode> findFolder(String uri) {
return getFolder(uri);
}
Expand Down Expand Up @@ -162,6 +171,7 @@ public void addFile(final String uri, final Map<String, Object> data) {
final MetaNode node = new MetaNode(uri, parts[parts.length - 1], data);

nodes.put(uri, node);
dataFilter.add(node);

var folder = getFolder(uri);
if (folder.isPresent()) {
Expand Down Expand Up @@ -218,5 +228,25 @@ public boolean isPublished() {
public boolean isSection() {
return SectionUtil.isSection(name);
}

@Override
public boolean equals (Object other) {

if (this == other) {
return true;
}

if (other == null) {
return false;
}

if (!(other instanceof MetaData.MetaNode)) {
return false;
}

var otherNode = (MetaData.MetaNode)other;

return uri.equals(otherNode.uri);
}
}
}
Loading

0 comments on commit 58d5cac

Please sign in to comment.