Skip to content

Commit

Permalink
#217 added most query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Jun 28, 2024
1 parent 1120858 commit e7e81f5
Show file tree
Hide file tree
Showing 16 changed files with 561 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.thmarx.cms.api.feature.features.SitePropertiesFeature;
import com.github.thmarx.cms.api.utils.MapUtil;
import com.github.thmarx.cms.api.utils.SectionUtil;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
Expand All @@ -40,7 +41,7 @@
* @author t.marx
*/
public record ContentNode(String uri, String name, Map<String, Object> data,
boolean directory, Map<String, ContentNode> children, LocalDate lastmodified) {
boolean directory, Map<String, ContentNode> children, LocalDate lastmodified) implements Serializable {

public ContentNode(String uri, String name, Map<String, Object> data, boolean directory, Map<String, ContentNode> children) {
this(uri, name, data, directory, children, LocalDate.now());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package com.github.thmarx.cms.api.utils;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -18,4 +40,4 @@ public static void deleteFolder(Path pathToBeDeleted) throws IOException {
.map(Path::toFile)
.forEach(File::delete);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.thmarx.cms.api.ModuleFileSystem;
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.api.db.DBFileSystem;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
Expand All @@ -33,6 +34,7 @@
import com.github.thmarx.cms.api.eventbus.events.ReIndexContentMetaDataEvent;
import com.github.thmarx.cms.api.eventbus.events.TemplateChangedEvent;
import com.github.thmarx.cms.api.utils.PathUtil;
import com.github.thmarx.cms.filesystem.metadata.AbstractMetaData;
import com.github.thmarx.cms.filesystem.metadata.persistent.PersistentMetaData;
import com.github.thmarx.cms.filesystem.query.Query;
import java.io.IOException;
Expand Down Expand Up @@ -79,22 +81,12 @@ public Path base () {
return hostBaseDirectory;
}

public <T> Query<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper) {
return new Query(new ArrayList<>(metaData.nodes().values()), metaData, nodeMapper);
public <T> ContentQuery<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper) {
return metaData.query(nodeMapper);
}

public <T> Query<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper) {

final String uri;
if (startURI.startsWith("/")) {
uri = startURI.substring(1);
} else {
uri = startURI;
}

var nodes = metaData.nodes().values().stream().filter(node -> node.uri().startsWith(uri)).toList();

return new Query(nodes, metaData, nodeMapper);
public <T> ContentQuery<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper) {
return metaData.query(startURI, nodeMapper);
}

public boolean isVisible(final String uri) {
Expand All @@ -103,7 +95,7 @@ public boolean isVisible(final String uri) {
return false;
}
var n = node.get();
return MemoryMetaData.isVisible(n);
return AbstractMetaData.isVisible(n);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package com.github.thmarx.cms.filesystem;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.ContentQuery;
import com.github.thmarx.cms.filesystem.index.IndexProviding;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

/**
*
Expand Down Expand Up @@ -36,4 +60,7 @@ public enum Type {
Map<String, ContentNode> nodes();

Map<String, ContentNode> tree();

<T> ContentQuery<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper);
<T> ContentQuery<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.thmarx.cms.filesystem.metadata;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.db.ContentNode;

/**
*
* @author t.marx
*/
public class AbstractMetaData {
public static boolean isVisible (ContentNode node) {
return node != null
// check if some parent is hidden
&& !node.uri().startsWith(".") && !node.uri().contains("/.")
&& node.isPublished()
&& !node.isHidden()
&& !node.isSection();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@

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.index.IndexProviding;
import com.github.thmarx.cms.filesystem.index.SecondaryIndex;
import com.github.thmarx.cms.filesystem.metadata.AbstractMetaData;
import com.github.thmarx.cms.filesystem.query.Query;
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;
Expand All @@ -38,6 +42,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -46,7 +51,7 @@
*
* @author t.marx
*/
public class MemoryMetaData implements IndexProviding, MetaData {
public class MemoryMetaData extends AbstractMetaData implements IndexProviding, MetaData {

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

Expand Down Expand Up @@ -75,10 +80,12 @@ public void clear() {
secondaryIndexes.clear();
}

@Override
public ConcurrentMap<String, ContentNode> nodes() {
return new ConcurrentHashMap<>(nodes);
}

@Override
public ConcurrentMap<String, ContentNode> tree() {
return new ConcurrentHashMap<>(tree);
}
Expand Down Expand Up @@ -146,15 +153,6 @@ protected ContentNode mapToIndex(ContentNode node) {
}
}

public static boolean isVisible (ContentNode node) {
return node != null
// check if some parent is hidden
&& !node.uri().startsWith(".") && !node.uri().contains("/.")
&& node.isPublished()
&& !node.isHidden()
&& !node.isSection();
}

@Override
public Optional<ContentNode> findFolder(String uri) {
return getFolder(uri);
Expand Down Expand Up @@ -225,6 +223,26 @@ public void open() throws IOException {
@Override
public void close() throws IOException {
}

@Override
public <T> ContentQuery<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper) {
return new Query(new ArrayList<>(nodes.values()), this, nodeMapper);
}

@Override
public <T> ContentQuery<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper) {

final String uri;
if (startURI.startsWith("/")) {
uri = startURI.substring(1);
} else {
uri = startURI;
}

var filtered = nodes().values().stream().filter(node -> node.uri().startsWith(uri)).toList();

return new Query(filtered, this, nodeMapper);
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package com.github.thmarx.cms.filesystem.metadata.persistent;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 - 2024 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.utils.FileUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
Expand All @@ -22,8 +47,9 @@
*
* @author t.marx
*/
@Slf4j
public class LuceneIndex implements AutoCloseable {

private Directory directory;
private IndexWriter writer = null;

Expand All @@ -34,45 +60,54 @@ public class LuceneIndex implements AutoCloseable {
public void close() throws Exception {
if (nrt_manager != null) {
nrt_manager.close();

writer.commit();
writer.close();
directory.close();
}
}

public void commit() throws IOException {
writer.flush();
writer.commit();
nrt_manager.maybeRefresh();
}
void add (Document document) throws IOException {

void add(Document document) throws IOException {
writer.addDocument(document);
commit();
}
void update (Term term, Document document) throws IOException {

void update(Term term, Document document) throws IOException {
writer.updateDocument(term, document);
commit();
}
void delete (Query query) throws IOException {

void delete(Query query) throws IOException {
writer.deleteDocuments(query);
commit();
}
Optional<Document> query (Query query) throws IOException {

List<Document> query(Query query) throws IOException {
IndexSearcher searcher = nrt_manager.acquire();
try {

var topDocs = searcher.search(query, Integer.MAX_VALUE);

List<Document> result = new ArrayList<>();
for (var scoreDoc : topDocs.scoreDocs) {
result.add(searcher.storedFields().document(scoreDoc.doc));
}

return result;
} catch (IOException e) {
log.error("", e);
} finally {
nrt_manager.release(searcher);
}
return Optional.empty();
return Collections.emptyList();
}
public void open (Path path) throws IOException {

public void open(Path path) throws IOException {
if (Files.exists(path)) {
FileUtils.deleteFolder(path);
}
Expand Down
Loading

0 comments on commit e7e81f5

Please sign in to comment.