Skip to content

Commit

Permalink
gh-4454 Remove DocRefInfoService from DictionaryStoreImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
at055612 committed Sep 24, 2024
1 parent 61da0aa commit f36355f
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 48 deletions.
1 change: 1 addition & 0 deletions stroom-dictionary/stroom-dictionary-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ext.moduleName = 'stroom.dictionary.api'
dependencies {
implementation project(':stroom-core-shared')
implementation project(':stroom-docref')
implementation project(':stroom-explorer:stroom-docrefinfo-api')

implementation libs.jackson_annotations
implementation libs.jaxb_api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import stroom.dictionary.shared.WordList;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;

import java.util.List;
import java.util.Optional;
Expand All @@ -39,7 +40,16 @@ public interface WordListProvider {
*/
String[] getWords(DocRef dictionaryRef);

WordList getCombinedWordList(DocRef dictionaryRef);
/**
* Gets all the words from the passed dictionaryRef and all of its imports (recursing into
* all of their imports). Words are returned in depth first order, i.e. imports first.
*
* @param dictionaryRef The Dictionary to get a combined word list for.
* @param docRefDecorator Optionally supply a {@link DocRefDecorator} to ensure any returned
* {@link DocRef}s have the correct name/type information.
* @return
*/
WordList getCombinedWordList(DocRef dictionaryRef, DocRefDecorator docRefDecorator);

List<DocRef> findByName(String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import stroom.dictionary.shared.WordList.Builder;
import stroom.docref.DocRef;
import stroom.docref.DocRefInfo;
import stroom.docrefinfo.api.DocRefInfoService;
import stroom.docrefinfo.api.DocRefDecorator;
import stroom.docstore.api.AuditFieldFilter;
import stroom.docstore.api.DependencyRemapper;
import stroom.docstore.api.Store;
Expand All @@ -41,7 +41,6 @@
import stroom.util.string.StringUtil;

import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;

import java.util.Collection;
Expand All @@ -65,14 +64,11 @@ class DictionaryStoreImpl implements DictionaryStore, WordListProvider {
DictionaryDoc.DOCUMENT_TYPE,
DictionaryDoc.ICON);
private final Store<DictionaryDoc> store;
private final Provider<DocRefInfoService> docRefInfoServiceProvider;

@Inject
DictionaryStoreImpl(final StoreFactory storeFactory,
final DictionarySerialiser serialiser,
final Provider<DocRefInfoService> docRefInfoServiceProvider) {
final DictionarySerialiser serialiser) {

this.docRefInfoServiceProvider = docRefInfoServiceProvider;
this.store = storeFactory.createStore(
serialiser,
DictionaryDoc.DOCUMENT_TYPE,
Expand Down Expand Up @@ -170,37 +166,37 @@ private BiConsumer<DictionaryDoc, DependencyRemapper> createMapper() {
@Override
public DictionaryDoc readDocument(final DocRef docRef) {
final DictionaryDoc dictionaryDoc = store.readDocument(docRef);
decorateImports(dictionaryDoc);
return dictionaryDoc;
}

@Override
public DictionaryDoc writeDocument(final DictionaryDoc document) {
decorateImports(document);
return store.writeDocument(document);
}

/**
* Ensure all the imports have the correct names. Modifies the import list.
*/
private void decorateImports(final DictionaryDoc dictionaryDoc) {
if (dictionaryDoc != null && NullSafe.hasItems(dictionaryDoc.getImports())) {
final DocRefInfoService docRefInfoService = docRefInfoServiceProvider.get();
final List<DocRef> decoratedImports = dictionaryDoc.getImports()
.stream()
.map(docRef -> decorateDocRef(docRefInfoService, docRef))
.toList();
dictionaryDoc.setImports(decoratedImports);
}
}

private DocRef decorateDocRef(final DocRefInfoService docRefInfoService,
// /**
// * Ensure all the imports have the correct names. Modifies the import list.
// */
// private void decorateImports(final DictionaryDoc dictionaryDoc) {
// if (dictionaryDoc != null && NullSafe.hasItems(dictionaryDoc.getImports())) {
// final DocRefInfoService docRefInfoService = docRefInfoServiceProvider.get();
// final List<DocRef> decoratedImports = dictionaryDoc.getImports()
// .stream()
// .map(docRef -> decorateDocRef(docRefInfoService, docRef))
// .toList();
// dictionaryDoc.setImports(decoratedImports);
// }
// }

private DocRef decorateDocRef(final DocRefDecorator docRefDecorator,
final DocRef docRef) {
if (docRef == null) {
return null;
} else if (docRefDecorator == null) {
return docRef;
} else {
try {
return docRefInfoService.decorate(docRef, true);
return docRefDecorator.decorate(docRef, true);
} catch (Exception e) {
// Likely docRef doesn't exist, so we will just leave it as is, i.e.
// a broken dep
Expand Down Expand Up @@ -291,15 +287,16 @@ public List<DocRef> list() {

@Override
public String getCombinedData(final DocRef docRef) {
return getCombinedWordList(docRef, true).asString();
return getCombinedWordList(docRef, null, true).asString();
}

public WordList getCombinedWordList(final DocRef docRef,
final DocRefDecorator docRefDecorator,
final boolean deDup) {
final Builder builder = WordList.builder(deDup);
final Set<DocRef> visited = new HashSet<>();
final Stack<DocRef> visitPath = new Stack<>();
doGetCombinedWordList(docRefInfoServiceProvider.get(), builder, docRef, visited, visitPath);
doGetCombinedWordList(docRefDecorator, builder, docRef, visited, visitPath);


final WordList wordList = builder.build();
Expand All @@ -312,23 +309,24 @@ public WordList getCombinedWordList(final DocRef docRef,

@Override
public String[] getWords(final DocRef dictionaryRef) {
return getCombinedWordList(dictionaryRef, true).asWordArray();
return getCombinedWordList(dictionaryRef, null, true).asWordArray();
}

@Override
public WordList getCombinedWordList(final DocRef dictionaryRef) {
return getCombinedWordList(dictionaryRef, true);
public WordList getCombinedWordList(final DocRef dictionaryRef,
final DocRefDecorator docRefDecorator) {
return getCombinedWordList(dictionaryRef, docRefDecorator, true);
}

private void doGetCombinedWordList(final DocRefInfoService docRefInfoService,
private void doGetCombinedWordList(final DocRefDecorator docRefDecorator,
final WordList.Builder wordListBuilder,
final DocRef docRef,
final Set<DocRef> visited,
final Stack<DocRef> visitPath) {

// As we are adding the docRef to the WordList, we want to ensure it
// has a name and the correct name
final DocRef decorateDocRef = decorateDocRef(docRefInfoService, docRef);
final DocRef decorateDocRef = decorateDocRef(docRefDecorator, docRef);
LOGGER.debug(() -> LogUtil.message("decorateDocRef: {}, visitPath: {}",
decorateDocRef.toShortString(), docRefsToStr(visitPath)));
visitPath.push(decorateDocRef);
Expand All @@ -355,7 +353,7 @@ private void doGetCombinedWordList(final DocRefInfoService docRefInfoService,
for (final DocRef importDocRef : imports) {
// Recurse
doGetCombinedWordList(
docRefInfoService,
docRefDecorator,
wordListBuilder,
importDocRef,
visited,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stroom.dictionary.shared.WordList;
import stroom.dictionary.shared.WordListResource;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;
import stroom.event.logging.rs.api.AutoLogged;

import jakarta.inject.Inject;
Expand All @@ -34,10 +35,13 @@ class WordListResourceImpl implements WordListResource {
private static final Logger LOGGER = LoggerFactory.getLogger(WordListResourceImpl.class);

private final Provider<WordListProvider> wordListProviderProvider;
private final Provider<DocRefDecorator> docRefDecoratorProvider;

@Inject
WordListResourceImpl(final Provider<WordListProvider> wordListProviderProvider) {
WordListResourceImpl(final Provider<WordListProvider> wordListProviderProvider,
final Provider<DocRefDecorator> docRefDecoratorProvider) {
this.wordListProviderProvider = wordListProviderProvider;
this.docRefDecoratorProvider = docRefDecoratorProvider;
}

@Override
Expand All @@ -48,7 +52,7 @@ public WordList getWords(final String uuid) {
.build();

@SuppressWarnings("UnnecessaryLocalVariable") final WordList wordList = wordListProviderProvider.get()
.getCombinedWordList(dictDocRef);
.getCombinedWordList(dictDocRef, docRefDecoratorProvider.get());
return wordList;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import stroom.dictionary.shared.DictionaryDoc;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefInfoService;
import stroom.docstore.api.Store;
import stroom.docstore.api.StoreFactory;

Expand All @@ -42,8 +41,6 @@ class TestDictionaryStoreImpl {
private Store<DictionaryDoc> mockStore;
@Mock
private StoreFactory mockStoreFactory;
@Mock
private DocRefInfoService mockDocRefInfoService;

@Test
void getWords_unix() {
Expand Down Expand Up @@ -244,13 +241,8 @@ private DictionaryStoreImpl getDictionaryStore() {
Mockito.eq(DictionaryDoc.class)))
.thenReturn(mockStore);

Mockito.when(mockDocRefInfoService.decorate(Mockito.any(DocRef.class), Mockito.anyBoolean()))
.thenAnswer(invocation ->
invocation.getArgument(0));

return new DictionaryStoreImpl(
mockStoreFactory,
mockDictionarySerialiser,
() -> mockDocRefInfoService);
mockDictionarySerialiser);
}
}
1 change: 1 addition & 0 deletions stroom-dictionary/stroom-dictionary-mock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ext.moduleName = 'stroom.dictionary.mock'
dependencies {
implementation project(':stroom-core-shared')
implementation project(':stroom-dictionary:stroom-dictionary-api')
implementation project(':stroom-explorer:stroom-docrefinfo-api')
implementation project(':stroom-docref')

implementation libs.guice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import stroom.dictionary.api.WordListProvider;
import stroom.dictionary.shared.WordList;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
Expand Down Expand Up @@ -53,7 +54,8 @@ public String[] getWords(final DocRef dictionaryRef) {
}

@Override
public WordList getCombinedWordList(final DocRef dictionaryRef) {
public WordList getCombinedWordList(final DocRef dictionaryRef,
final DocRefDecorator docRefDecorator) {
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import stroom.dictionary.api.WordListProvider;
import stroom.dictionary.shared.WordList;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;
import stroom.docrefinfo.mock.MockDocRefInfoModule;
import stroom.index.impl.IndexStore;
import stroom.index.impl.IndexVolumeGroupService;
Expand Down Expand Up @@ -98,7 +99,8 @@ public String[] getWords(final DocRef dictionaryRef) {
}

@Override
public WordList getCombinedWordList(final DocRef dictionaryRef) {
public WordList getCombinedWordList(final DocRef dictionaryRef,
final DocRefDecorator docRefDecorator) {
return null;
}
};
Expand Down
1 change: 1 addition & 0 deletions stroom-index/stroom-index-lucene553/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation libs.ws_rs_api

testImplementation project(':stroom-index:stroom-index-mock')
testImplementation project(':stroom-explorer:stroom-docrefinfo-api')
testImplementation project(':stroom-test-common')

testImplementation libs.assertj_core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stroom.dictionary.shared.DictionaryDoc;
import stroom.dictionary.shared.WordList;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;
import stroom.expression.api.DateTimeSettings;
import stroom.index.shared.LuceneIndexField;
import stroom.query.api.v2.ExpressionOperator;
Expand Down Expand Up @@ -76,7 +77,8 @@ public String[] getWords(final DocRef dictionaryRef) {
}

@Override
public WordList getCombinedWordList(final DocRef dictionaryRef) {
public WordList getCombinedWordList(final DocRef dictionaryRef,
final DocRefDecorator docRefDecorator) {
return WordList.builder(true)
.addWord("1", dictionaryRef)
.addWord("2", dictionaryRef)
Expand Down
1 change: 1 addition & 0 deletions stroom-index/stroom-index-lucene980/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation libs.ws_rs_api

testImplementation project(':stroom-index:stroom-index-mock')
testImplementation project(':stroom-explorer:stroom-docrefinfo-api')
testImplementation project(':stroom-test-common')

testImplementation libs.assertj_core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import stroom.dictionary.shared.DictionaryDoc;
import stroom.dictionary.shared.WordList;
import stroom.docref.DocRef;
import stroom.docrefinfo.api.DocRefDecorator;
import stroom.expression.api.DateTimeSettings;
import stroom.index.shared.LuceneIndexField;
import stroom.query.api.v2.ExpressionOperator;
Expand Down Expand Up @@ -76,7 +77,8 @@ public String[] getWords(final DocRef dictionaryRef) {
}

@Override
public WordList getCombinedWordList(final DocRef dictionaryRef) {
public WordList getCombinedWordList(final DocRef dictionaryRef,
final DocRefDecorator docRefDecorator) {
return WordList.builder(true)
.addWord("1", dictionaryRef)
.addWord("2", dictionaryRef)
Expand Down

0 comments on commit f36355f

Please sign in to comment.