-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Create file open hints on IOContext to replace ReadAdvice #14482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thecoop
wants to merge
7
commits into
apache:main
Choose a base branch
from
thecoop:iocontext-split
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−111
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
739f816
Split IOContext into separate implementations
thecoop 0843b20
Anonymous classes
thecoop a29183a
Add a hints field
thecoop b70ce5e
Add hint classes and validation method
thecoop edfc21e
Add calls to validate and a test
thecoop 5a6fbec
Map from IOContext to ReadAdvice
thecoop 808e7fa
PR comments
thecoop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lucene/core/src/java/org/apache/lucene/store/DataAccessHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.store; | ||
|
||
/** Hint on the data access pattern likely to be used */ | ||
public enum DataAccessHint implements IOContext.FileOpenHint { | ||
RANDOM, | ||
SEQUENTIAL | ||
} |
71 changes: 71 additions & 0 deletions
71
lucene/core/src/java/org/apache/lucene/store/DefaultIOContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.store; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
record DefaultIOContext(Optional<ReadAdvice> readAdvice, Set<FileOpenHint> hints) | ||
implements IOContext { | ||
|
||
public DefaultIOContext { | ||
Objects.requireNonNull(readAdvice); | ||
Objects.requireNonNull(hints); | ||
if (readAdvice.isPresent() && !hints.isEmpty()) | ||
throw new IllegalArgumentException("Either ReadAdvice or hints can be specified, not both"); | ||
} | ||
|
||
public DefaultIOContext(Optional<ReadAdvice> readAdvice, FileOpenHint... hints) { | ||
this(readAdvice, Set.of(hints)); | ||
} | ||
|
||
@Override | ||
public Context context() { | ||
return Context.DEFAULT; | ||
} | ||
|
||
@Override | ||
public MergeInfo mergeInfo() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public FlushInfo flushInfo() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public IOContext withHints(FileOpenHint... hints) { | ||
if (readAdvice().isPresent()) | ||
throw new IllegalArgumentException("ReadAdvice has been specified directly"); | ||
// TODO: see if this is needed or not | ||
if (!hints().isEmpty()) throw new IllegalArgumentException("Hints have already been specified"); | ||
return new DefaultIOContext(Optional.empty(), hints); | ||
} | ||
|
||
private static final DefaultIOContext[] READADVICE_TO_IOCONTEXT = | ||
Arrays.stream(ReadAdvice.values()) | ||
.map(r -> new DefaultIOContext(Optional.of(r))) | ||
.toArray(DefaultIOContext[]::new); | ||
|
||
@Override | ||
public DefaultIOContext withReadAdvice(ReadAdvice advice) { | ||
return READADVICE_TO_IOCONTEXT[advice.ordinal()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,12 @@ | |
import java.io.IOException; | ||
import java.nio.file.NoSuchFileException; | ||
import java.util.Collection; // for javadocs | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import org.apache.lucene.index.IndexFileNames; | ||
import org.apache.lucene.util.Constants; | ||
import org.apache.lucene.util.IOUtils; | ||
|
||
/** | ||
|
@@ -79,6 +83,31 @@ public abstract class Directory implements Closeable { | |
*/ | ||
public abstract long fileLength(String name) throws IOException; | ||
|
||
protected void validateIOContext(IOContext context) { | ||
Map<Class<? extends IOContext.FileOpenHint>, List<IOContext.FileOpenHint>> hintClasses = | ||
context.hints().stream().collect(Collectors.groupingBy(IOContext.FileOpenHint::getClass)); | ||
|
||
// there should only be one of FileType, FileData, DataAccess | ||
List<IOContext.FileOpenHint> fileTypes = | ||
hintClasses.getOrDefault(FileTypeHint.class, List.of()); | ||
if (fileTypes.size() > 1) { | ||
throw new IllegalArgumentException("Multiple file type hints specified: " + fileTypes); | ||
} | ||
List<IOContext.FileOpenHint> fileData = hintClasses.getOrDefault(FileDataHint.class, List.of()); | ||
if (fileData.size() > 1) { | ||
throw new IllegalArgumentException("Multiple file data hints specified: " + fileData); | ||
} | ||
List<IOContext.FileOpenHint> dataAccess = | ||
hintClasses.getOrDefault(DataAccessHint.class, List.of()); | ||
if (dataAccess.size() > 1) { | ||
throw new IllegalArgumentException("Multiple data access hints specified: " + dataAccess); | ||
} | ||
} | ||
|
||
protected ReadAdvice toReadAdvice(IOContext context) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add javadocs, I think protected method on a class as central as Directory really needs it? |
||
return context.readAdvice().orElse(Constants.DEFAULT_READADVICE); | ||
} | ||
|
||
/** | ||
* Creates a new, empty file in the directory and returns an {@link IndexOutput} instance for | ||
* appending data to this file. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
lucene/core/src/java/org/apache/lucene/store/FileDataHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.store; | ||
|
||
/** Hints on the type of data stored in the file */ | ||
public enum FileDataHint implements IOContext.FileOpenHint { | ||
POSTINGS, | ||
STORED_FIELDS, | ||
VECTORS | ||
} |
24 changes: 24 additions & 0 deletions
24
lucene/core/src/java/org/apache/lucene/store/FileTypeHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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. | ||
*/ | ||
package org.apache.lucene.store; | ||
|
||
/** Hints on the type of file being opened */ | ||
public enum FileTypeHint implements IOContext.FileOpenHint { | ||
METADATA, | ||
DATA, | ||
INDEX | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this method being used, but I don't understand what it is doing. Why is explicit validation needed, and when should subclasses be invoking this method?
Could the validation be moved to IOContext itself, e.g. into IOContext ctor to prevent that "invalid" IOContexts ever get created in the first place?