smiklosovic commented on code in PR #4277:
URL: https://github.com/apache/cassandra/pull/4277#discussion_r2237106701
##########
src/java/org/apache/cassandra/io/sstable/format/TOCComponent.java:
##########
@@ -78,16 +83,41 @@ public static Set<Component> loadTOC(Descriptor descriptor,
boolean skipMissing)
}
/**
- * Appends new component names to the TOC component.
+ * Updates the TOC file by reading existing component entries, merging
them with the given components,
+ * sorting the combined list in lexicographic order for deterministic
output.
+ *
+ * @param descriptor the SSTable descriptor for which to update the TOC
+ * @param components new components to merge into the TOC (existing TOC
entries are preserved)
+ * @throws FSReadError if an I/O error occurs when reading the existing
TOC file
+ * @throws FSWriteError if an I/O error occurs when creating or
overwriting the TOC file
*/
- public static void appendTOC(Descriptor descriptor, Collection<Component>
components)
+ public static void updateTOC(Descriptor descriptor, Collection<Component>
components)
{
File tocFile = descriptor.fileFor(Components.TOC);
- try (FileOutputStreamPlus out = tocFile.newOutputStream(APPEND);
- PrintWriter w = new PrintWriter(out))
+
+ // read existing entries if any
+ Path tocPath = tocFile.toPath();
+ Set<String> componentNames = new HashSet<>();
+ try
+ {
+ if (Files.exists(tocPath))
+ componentNames.addAll(Files.readAllLines(tocPath));
+ }
+ catch (IOException e)
{
- for (Component component : components)
- w.println(component.name);
+ throw new FSReadError(e, tocFile);
+ }
+
+ componentNames.addAll(Collections2.transform(components,
Component::name));
+ List<String> sortedNames = new ArrayList<>(componentNames);
+ sortedNames.sort(String.CASE_INSENSITIVE_ORDER);
+
+ try (FileOutputStreamPlus out = tocFile.newOutputStream(OVERWRITE);
PrintWriter w = new PrintWriter(out))
Review Comment:
This is not necessary, just use `FileUtils.write`. with CREATE and
TRUNCATE_EXISTING
##########
src/java/org/apache/cassandra/io/sstable/format/TOCComponent.java:
##########
@@ -78,16 +83,41 @@ public static Set<Component> loadTOC(Descriptor descriptor,
boolean skipMissing)
}
/**
- * Appends new component names to the TOC component.
+ * Updates the TOC file by reading existing component entries, merging
them with the given components,
+ * sorting the combined list in lexicographic order for deterministic
output.
+ *
+ * @param descriptor the SSTable descriptor for which to update the TOC
+ * @param components new components to merge into the TOC (existing TOC
entries are preserved)
+ * @throws FSReadError if an I/O error occurs when reading the existing
TOC file
+ * @throws FSWriteError if an I/O error occurs when creating or
overwriting the TOC file
*/
- public static void appendTOC(Descriptor descriptor, Collection<Component>
components)
+ public static void updateTOC(Descriptor descriptor, Collection<Component>
components)
{
File tocFile = descriptor.fileFor(Components.TOC);
- try (FileOutputStreamPlus out = tocFile.newOutputStream(APPEND);
- PrintWriter w = new PrintWriter(out))
+
+ // read existing entries if any
+ Path tocPath = tocFile.toPath();
+ Set<String> componentNames = new HashSet<>();
+ try
+ {
+ if (Files.exists(tocPath))
+ componentNames.addAll(Files.readAllLines(tocPath));
+ }
+ catch (IOException e)
{
- for (Component component : components)
- w.println(component.name);
+ throw new FSReadError(e, tocFile);
+ }
+
+ componentNames.addAll(Collections2.transform(components,
Component::name));
+ List<String> sortedNames = new ArrayList<>(componentNames);
+ sortedNames.sort(String.CASE_INSENSITIVE_ORDER);
+
+ try (FileOutputStreamPlus out = tocFile.newOutputStream(OVERWRITE);
PrintWriter w = new PrintWriter(out))
Review Comment:
This is not necessary, just use `FileUtils.write`. with `CREATE` and
`TRUNCATE_EXISTING`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]