Add support for .index.db

This commit is contained in:
Grahame Grieve 2023-09-15 22:01:12 -07:00
parent a68ba66d1a
commit e7010d7af0
5 changed files with 98 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package org.hl7.fhir.convertors.misc;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -10,6 +11,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;
import javax.annotation.Nonnull;
@ -35,6 +37,7 @@ import org.hl7.fhir.r5.model.Enumerations.FHIRVersionEnumFactory;
import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.model.JsonArray;
import org.hl7.fhir.utilities.json.model.JsonObject;
@ -118,11 +121,11 @@ public class NpmPackageVersionConverter {
NpmPackageIndexBuilder indexer = indexers.get(n);
if (indexer == null) {
indexer = new NpmPackageIndexBuilder();
indexer.start();
indexer.start(Utilities.path("[tmp]", "tmp-"+UUID.randomUUID().toString()+".db"));
indexers.put(n, indexer);
}
indexer.seeFile(s, b);
if (!s.equals(".index.json") && !s.equals("package.json")) {
if (!s.equals(".index.json") && !s.equals("package.json") && !s.equals(".index.db")) {
TarArchiveEntry entry = new TarArchiveEntry(e.getKey());
entry.setSize(b.length);
tar.putArchiveEntry(entry);
@ -137,6 +140,13 @@ public class NpmPackageVersionConverter {
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
cnt = TextFile.fileToBytes(e.getValue().getDbFilename());
new File(e.getValue().getDbFilename()).delete();
entry = new TarArchiveEntry(e.getKey() + "/.index.db");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
}
byte[] cnt = output.get("package/package.json");

View File

@ -57,6 +57,7 @@ import org.hl7.fhir.r4b.model.Enumeration;
import org.hl7.fhir.r4b.model.Enumerations.FHIRVersion;
import org.hl7.fhir.r4b.model.ImplementationGuide;
import org.hl7.fhir.r4b.model.ImplementationGuide.ImplementationGuideDependsOnComponent;
import org.hl7.fhir.r4b.utils.NPMPackageGenerator.Category;
import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
@ -110,6 +111,7 @@ public class NPMPackageGenerator {
private JsonObject packageManifest;
private NpmPackageIndexBuilder indexer;
private String igVersion;
private String indexdb;
public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig,
Date date, boolean notForPublication) throws FHIRException, IOException {
@ -324,8 +326,9 @@ public class NPMPackageGenerator {
bufferedOutputStream = new BufferedOutputStream(OutputStream);
gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
tar = new TarArchiveOutputStream(gzipOutputStream);
indexdb = Utilities.path("[tmp]", "tmp-"+UUID.randomUUID().toString()+".db");
indexer = new NpmPackageIndexBuilder();
indexer.start();
indexer.start(indexdb);
}
public void addFile(Category cat, String name, byte[] content) throws IOException {
@ -367,6 +370,9 @@ public class NPMPackageGenerator {
private void buildIndexJson() throws IOException {
byte[] content = TextFile.stringToBytes(indexer.build(), false);
addFile(Category.RESOURCE, ".index.json", content);
content = TextFile.fileToBytes(indexdb);
new File(indexdb).delete();
addFile(Category.RESOURCE, ".index.db", content);
}
public String filename() {

View File

@ -101,6 +101,7 @@ public class NPMPackageGenerator {
private JsonObject packageManifest;
private NpmPackageIndexBuilder indexer;
private String igVersion;
private String indexdb;
public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date, boolean notForPublication) throws FHIRException, IOException {
@ -315,8 +316,9 @@ public class NPMPackageGenerator {
bufferedOutputStream = new BufferedOutputStream(OutputStream);
gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
tar = new TarArchiveOutputStream(gzipOutputStream);
indexdb = Utilities.path("[tmp]", "tmp-"+UUID.randomUUID().toString()+".db");
indexer = new NpmPackageIndexBuilder();
indexer.start();
indexer.start(indexdb);
}
@ -383,6 +385,9 @@ public class NPMPackageGenerator {
private void buildIndexJson() throws IOException {
byte[] content = TextFile.stringToBytes(indexer.build(), false);
addFile(Category.RESOURCE, ".index.json", content);
content = TextFile.fileToBytes(indexdb);
new File(indexdb).delete();
addFile(Category.RESOURCE, ".index.db", content);
}
public String filename() {

View File

@ -52,6 +52,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -598,7 +599,7 @@ public class NpmPackage {
public void indexFolder(String desc, NpmPackageFolder folder) throws FileNotFoundException, IOException {
List<String> remove = new ArrayList<>();
NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
indexer.start();
indexer.start(folder.folder != null ? Utilities.path(folder.folder.getAbsolutePath(), ".index.db") : null);
for (String n : folder.listFiles()) {
if (!indexer.seeFile(n, folder.fetchFile(n))) {
remove.add(n);
@ -1076,7 +1077,7 @@ public class NpmPackage {
Utilities.createDirectory(pd.getAbsolutePath());
}
NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
indexer.start();
indexer.start(Utilities.path(dir.getAbsolutePath(), n, ".index.db"));
for (String s : folder.content.keySet()) {
byte[] b = folder.content.get(s);
indexer.seeFile(s, b);
@ -1112,7 +1113,8 @@ public class NpmPackage {
n = "package/"+n;
}
NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
indexer.start();
String filename = Utilities.path("[tmp]", "tmp-"+UUID.randomUUID().toString()+".db");
indexer.start(filename);
for (String s : folder.content.keySet()) {
byte[] b = folder.content.get(s);
String name = n+"/"+s;
@ -1135,6 +1137,13 @@ public class NpmPackage {
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
cnt = TextFile.fileToBytes(filename);
new File(filename).delete();
entry = new TarArchiveEntry(n+"/.index.db");
entry.setSize(cnt.length);
tar.putArchiveEntry(entry);
tar.write(cnt);
tar.closeArchiveEntry();
}
byte[] cnt = TextFile.stringToBytes(JsonParser.compose(npm, true), false);
TarArchiveEntry entry = new TarArchiveEntry("package/package.json");

View File

@ -2,6 +2,11 @@ package org.hl7.fhir.utilities.npm;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.utilities.TextFile;
@ -13,6 +18,8 @@ import org.hl7.fhir.utilities.json.parser.JsonParser;
/**
* This class builds the .index.json for a package
*
* it also builds a .index.db since that may provide faster access
*
* @author grahame
*
*/
@ -21,14 +28,42 @@ public class NpmPackageIndexBuilder {
public static final Integer CURRENT_INDEX_VERSION = 2;
private JsonObject index;
private JsonArray files;
private Connection conn;
private PreparedStatement psql;
private String dbFilename;
public void start() {
public void start(String filename) {
index = new JsonObject();
index.add("index-version", CURRENT_INDEX_VERSION);
files = new JsonArray();
index.add("files", files);
dbFilename = filename;
if (filename != null) {
try {
new File(filename).delete();
conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE index (\r\n"+
"FileName nvarchar NOT NULL,\r\n"+
"ResourceType nvarchar NOT NULL,\r\n"+
"Id nvarchar NULL,\r\n"+
"Url nvarchar NULL,\r\n"+
"Version nvarchar NULL,\r\n"+
"Kind nvarchar NULL,\r\n"+
"Type nvarchar NULL,\r\n"+
"Supplements nvarchar NULL,\r\n"+
"Content nvarchar NULL,\r\n"+
"PRIMARY KEY (FileName))\r\n");
psql = conn.prepareStatement("Insert into index (FileName, ResourceType, Id, Url, Version, Kind, Type, Supplements, Content) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
} catch (Exception e) {
conn = null;
}
}
}
public boolean seeFile(String name, byte[] content) {
if (name.endsWith(".json")) {
try {
@ -60,6 +95,18 @@ public class NpmPackageIndexBuilder {
if (json.hasPrimitive("content")) {
fi.add("content", json.asString("content"));
}
if (psql != null) {
psql.setString(1, name); // FileName);
psql.setString(2, json.asString("resourceType")); // ResourceType");
psql.setString(3, json.asString("id")); // Id");
psql.setString(4, json.asString("url")); // Url");
psql.setString(5, json.asString("version")); // Version");
psql.setString(6, json.asString("kind")); // Kind");
psql.setString(7, json.asString("type")); // Type");
psql.setString(8, json.asString("supplements")); // Supplements");
psql.setString(9, json.asString("content")); // Content");
psql.execute();
}
}
} catch (Exception e) {
System.out.println("Error parsing "+name+": "+e.getMessage());
@ -72,6 +119,13 @@ public class NpmPackageIndexBuilder {
}
public String build() {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// nothing
}
String res = JsonParser.compose(index, true);
index = null;
files = null;
@ -95,7 +149,7 @@ public class NpmPackageIndexBuilder {
if (!existsFile(folder, "package.json")) {
throw new FHIRException("Not a proper package? (can't find package.json)");
}
start();
start(Utilities.path(folder, ".index.db"));
File dir = new File(folder);
for (File f : dir.listFiles()) {
seeFile(f.getName(), TextFile.fileToBytes(f));
@ -103,7 +157,6 @@ public class NpmPackageIndexBuilder {
TextFile.stringToFile(build(), Utilities.path(folder, ".index.json"));
}
private boolean existsFolder(String... args) throws IOException {
File f = new File(Utilities.path(args));
return f.exists() && f.isDirectory();
@ -114,7 +167,6 @@ public class NpmPackageIndexBuilder {
return f.exists() && !f.isDirectory();
}
public static void main(String[] args) throws IOException {
new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.core");
new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.examples");
@ -143,4 +195,8 @@ public class NpmPackageIndexBuilder {
new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#4.0.1");
}
public String getDbFilename() {
return dbFilename;
}
}