Skip to content

Commit

Permalink
GH-4665 Clean up HDT code (#4666)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad authored Jul 23, 2023
2 parents d9d5b19 + 307e5fd commit 1cf9678
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public long getValue() {

@Override
public void reset() {
value = 0xFFFFFFFF;
value = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Bart Hanssens
*/
abstract class HDTArray extends HDTPart {
protected enum Type {
enum Type {
LOG64(1),
UINT32(2),
UINT64(3);
Expand All @@ -47,7 +47,7 @@ protected enum Type {
*
* @return value 1,2 or 3
*/
public int getValue() {
int getValue() {
return value;
}

Expand All @@ -56,22 +56,22 @@ public int getValue() {
}
}

protected int nrbits;
protected int entries;
int nrbits;
int entries;

/**
* Get the type of the array
*
* @return byte
*/
protected abstract int getType();
abstract int getType();

/**
* Get number of bits used to encode an entry
*
* @return positive integer value
*/
protected int getNrBits() {
int getNrBits() {
return nrbits;
}

Expand All @@ -80,7 +80,7 @@ protected int getNrBits() {
*
* @return positive integer value
*/
protected int size() {
int size() {
return entries;
}

Expand All @@ -90,10 +90,10 @@ protected int size() {
* @param i zero-based index
* @return entry
*/
protected abstract int get(int i);
abstract int get(int i);

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
CRC8 crc8 = new CRC8();
crc8.update(getType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class HDTArrayFactory {

protected static HDTArray parse(InputStream is) throws IOException {
static HDTArray parse(InputStream is) throws IOException {
int dtype = is.read();
if (dtype != HDTArray.Type.LOG64.getValue()) {
throw new UnsupportedOperationException("Array section: encoding " + Long.toHexString(dtype) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
* @author Bart Hanssens
*/
class HDTArrayLog64 extends HDTArray {
private byte buffer[];
private byte[] buffer;

@Override
protected int getType() {
int getType() {
return HDTArray.Type.LOG64.getValue();
}

@Override
protected int get(int i) {
int get(int i) {
// start byte of the value, and start bit in that start byte
int bytePos = (i * nrbits) / 8;
int bitPos = (i * nrbits) % 8;
Expand All @@ -63,14 +63,14 @@ protected int get(int i) {
}

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
super.parse(is);

// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC32())) {
// read bytes, minimum 1
long bytes = (nrbits * entries + 7) / 8;
long bytes = ((long) nrbits * entries + 7) / 8;
if (bytes > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Maximum number of bytes in array exceeded: " + bytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected void parse(InputStream is) throws IOException {
", but only bitmap v1 is supported");
}

long b = (int) VByte.decode(cis);
long b = VByte.decode(cis);
if (b > Integer.MAX_VALUE) {
throw new UnsupportedOperationException("Maximum number of entries in bitmap exceeded: " + b);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
* @author Bart Hanssens
*/
class HDTDictionary extends HDTPart {
protected final static byte[] DICT_FORMAT = "<http://purl.org/HDT/hdt#dictionaryFour>"
final static byte[] DICT_FORMAT = "<http://purl.org/HDT/hdt#dictionaryFour>"
.getBytes(StandardCharsets.US_ASCII);
protected final static String DICT_MAPPING = "mapping";
protected final static String DICT_ELEMENTS = "elements";
final static String DICT_MAPPING = "mapping";
final static String DICT_ELEMENTS = "elements";

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Bart Hanssens
*/
abstract class HDTDictionarySection extends HDTPart {
protected enum Type {
enum Type {
PLAIN(1),
FRONT(2),
HTFC(3),
Expand All @@ -28,7 +28,7 @@ protected enum Type {

private final int value;

protected int getValue() {
int getValue() {
return value;
}

Expand All @@ -42,23 +42,23 @@ protected int getValue() {
*
* @return
*/
protected abstract int size();
abstract int size();

/**
* Get the entry
*
* @param i zero-based index
* @return
*/
protected abstract byte[] get(int i) throws IOException;
abstract byte[] get(int i) throws IOException;

/**
* Constructor
*
* @param pos position
* @param name name
*/
protected HDTDictionarySection(String name, long pos) {
HDTDictionarySection(String name, long pos) {
super(name, pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HDTDictionarySectionFactory {
* @return dictionary section
* @throws IOException
*/
protected static HDTDictionarySection parse(InputStream is, String name, long pos) throws IOException {
static HDTDictionarySection parse(InputStream is, String name, long pos) throws IOException {
int dtype = is.read();
if (dtype != HDTDictionarySection.Type.FRONT.getValue()) {
throw new UnsupportedOperationException("Dictionary " + name + ": encoding "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
* @param name
* @param pos
*/
protected HDTDictionarySectionPFC(String name, long pos) {
HDTDictionarySectionPFC(String name, long pos) {
super(name, pos);
}

@Override
protected int size() {
int size() {
return totalStrings;
}

@Override
protected byte[] get(int i) throws IOException {
byte[] get(int i) throws IOException {
// HDT index start counting from 1
int idx = i - 1;

Expand All @@ -101,7 +101,7 @@ protected byte[] get(int i) throws IOException {
}

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
CRC8 crc8 = new CRC8();
crc8.update((byte) HDTDictionarySection.Type.FRONT.getValue());

Expand All @@ -112,7 +112,7 @@ protected void parse(InputStream is) throws IOException {
CheckedInputStream cis = new CheckedInputStream(uis, crc8)) {

long val = VByte.decode(cis);
if (totalStrings > Integer.MAX_VALUE) {
if (val > Integer.MAX_VALUE) {
throw new UnsupportedOperationException(getDebugPartStr() + " max number of strings exceeded: " + val);
}
totalStrings = (int) val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
* @author Bart Hanssens
*/
class HDTGlobal extends HDTPart {
protected final static byte[] GLOBAL_FORMAT = "<http://purl.org/HDT/hdt#HDTv1>".getBytes(StandardCharsets.US_ASCII);
protected final static String GLOBAL_BASEURI = "BaseUri";
protected final static String GLOBAL_SOFTWARE = "Software";
final static byte[] GLOBAL_FORMAT = "<http://purl.org/HDT/hdt#HDTv1>".getBytes(StandardCharsets.US_ASCII);
final static String GLOBAL_BASEURI = "BaseUri";
final static String GLOBAL_SOFTWARE = "Software";

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
*/
class HDTHeader extends HDTPart {

protected final static byte[] HEADER_FORMAT = "ntriples".getBytes(StandardCharsets.US_ASCII);
protected final static String HEADER_LENGTH = "length";
final static byte[] HEADER_FORMAT = "ntriples".getBytes(StandardCharsets.US_ASCII);
final static String HEADER_LENGTH = "length";

private byte[] headerData;

@Override
protected void parse(InputStream is) throws IOException {
void parse(InputStream is) throws IOException {
// don't close CheckedInputStream, as it will close the underlying inputstream
try (UncloseableInputStream uis = new UncloseableInputStream(is);
CheckedInputStream cis = new CheckedInputStream(uis, new CRC16())) {
Expand All @@ -63,7 +63,7 @@ protected void parse(InputStream is) throws IOException {
*
* @return byte array
*/
protected byte[] getHeaderData() {
byte[] getHeaderData() {
return headerData;
}

Expand All @@ -75,7 +75,7 @@ protected byte[] getHeaderData() {
* @throws IOException
*/
private byte[] parseHeaderData(InputStream is, int len) throws IOException {
byte b[] = new byte[len];
byte[] b = new byte[len];
is.read(b);
return b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.rdf4j.rio.hdt;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -97,9 +96,9 @@ public synchronized void parse(InputStream in, String baseURI)
throw new IllegalArgumentException("Input stream must not be 'null'");
}

if (in instanceof FileInputStream) {
// "TODO: use more optimized way to parse the file, eg. filechannel / membuffer"
}
// if (in instanceof FileInputStream) {
// TODO: use more optimized way to parse the file, eg. filechannel / membuffer
// }

HDTDictionarySection shared = null;
HDTDictionarySection subjects = null;
Expand Down Expand Up @@ -163,9 +162,10 @@ public synchronized void parse(InputStream in, String baseURI)
rdfHandler.startRDF();
}

int cnt = 0;
assert shared != null;
int size = shared.size();

assert section != null;
while (section.hasNext()) {
int[] t = section.next();
byte[] s = getSO(t[0], size, shared, subjects);
Expand All @@ -188,7 +188,7 @@ public synchronized void parse(InputStream in, String baseURI)
*/
@Override
public synchronized void parse(Reader reader, String baseURI)
throws IOException, RDFParseException, RDFHandlerException {
throws RDFParseException, RDFHandlerException {
throw new UnsupportedOperationException("HDT is binary, text readers not supported.");
}

Expand Down
Loading

0 comments on commit 1cf9678

Please sign in to comment.