Skip to content

Commit

Permalink
Merge pull request #196 from QuentinJanuel/dev
Browse files Browse the repository at this point in the history
Adding support for named graphs
  • Loading branch information
D063520 authored Sep 14, 2023
2 parents c325269 + 3f4ba41 commit 8a73fcd
Show file tree
Hide file tree
Showing 89 changed files with 3,722 additions and 302 deletions.
13 changes: 13 additions & 0 deletions hdt-api/src/main/java/org/rdfhdt/hdt/dictionary/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public interface Dictionary extends Closeable {
*/
long getNobjects();

/**
* Returns the number of objects in the dictionary. Note: Includes shared
*/
long getNgraphs();

/**
* Returns the number of subjects/objects in the dictionary.
*/
Expand All @@ -111,6 +116,8 @@ public interface Dictionary extends Closeable {

DictionarySection getObjects();

DictionarySection getGraphs();

Map<? extends CharSequence, DictionarySection> getAllObjects();

DictionarySection getShared();
Expand All @@ -125,4 +132,10 @@ public interface Dictionary extends Closeable {
* @return type
*/
String getType();

/**
* Returns whether the dictionary supports graphs
* @return true if it supports graphs, false otherwise
*/
boolean supportGraphs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum DictionarySectionRole {
SUBJECT,
PREDICATE,
OBJECT,
SHARED
SHARED,
GRAPH,
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public enum TripleComponentRole {
PREDICATE,
/** The triple is an object */
OBJECT,
/** The triple is a graph */
GRAPH,
}
3 changes: 3 additions & 0 deletions hdt-api/src/main/java/org/rdfhdt/hdt/hdt/HDTVocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public class HDTVocabulary {
public static final String DICTIONARY_TYPE_PLAIN = HDT_DICTIONARY_BASE+"Plain>";
public static final String DICTIONARY_TYPE_FOUR_SECTION = HDT_DICTIONARY_BASE+"Four>";
public static final String DICTIONARY_TYPE_MULT_SECTION = HDT_DICTIONARY_BASE+"Mult>";
public static final String DICTIONARY_TYPE_FOUR_QUAD_SECTION = HDT_DICTIONARY_BASE+"FourQuad>";

public static final String DICTIONARY_TYPE_FOUR_PSFC_SECTION = HDT_DICTIONARY_BASE+"FourPsfc>";

Expand All @@ -106,6 +107,7 @@ public class HDTVocabulary {
public static final String TRIPLES_TYPE_PLAIN = HDT_TRIPLES_BASE+"Plain>";
public static final String TRIPLES_TYPE_COMPACT = HDT_TRIPLES_BASE+"Compact>";
public static final String TRIPLES_TYPE_BITMAP = HDT_TRIPLES_BASE+"Bitmap>";
public static final String TRIPLES_TYPE_BITMAP_QUAD = HDT_TRIPLES_BASE+"BitmapQuad>";

// Index type
public static final String INDEX_TYPE_FOQ = HDT_BASE+"indexFoQ>";
Expand All @@ -119,6 +121,7 @@ public class HDTVocabulary {

// Bitmaps
public static final String BITMAP_TYPE_PLAIN = HDT_BITMAP_BASE+"Plain>";
public static final String BITMAP_TYPE_ROARING = HDT_BITMAP_BASE+"Roaring>";

// Misc
public static final String ORIGINAL_SIZE = HDT_BASE+"originalSize>";
Expand Down
11 changes: 10 additions & 1 deletion hdt-api/src/main/java/org/rdfhdt/hdt/options/HDTOptionsKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ public class HDTOptionsKeys {
*/
@Value(key = TEMP_DICTIONARY_IMPL_KEY, desc = "hash dictionary")
public static final String TEMP_DICTIONARY_IMPL_VALUE_HASH = "hash";
/**
* use Hash quad to create the HDTQ
*/
public static final String TEMP_DICTIONARY_IMPL_VALUE_HASH_QUAD = "hashQuad";
/**
* use Hash map to create the HDT and store the multisection dictionary, mandatory to create MSC
*/
Expand All @@ -282,8 +286,13 @@ public class HDTOptionsKeys {
/**
* 4 Section dictionary
*/
@Value(key = DICTIONARY_TYPE_KEY, desc = "Four sectiob dictionary")
@Value(key = DICTIONARY_TYPE_KEY, desc = "Four section dictionary")
public static final String DICTIONARY_TYPE_VALUE_FOUR_SECTION = HDTVocabulary.DICTIONARY_TYPE_FOUR_SECTION;
/*
* 4 Quad Section dictionary
*/
@Value(key = DICTIONARY_TYPE_KEY, desc = "Four quad section dictionary")
public static final String DICTIONARY_TYPE_VALUE_FOUR_QUAD_SECTION = HDTVocabulary.DICTIONARY_TYPE_FOUR_QUAD_SECTION;
/**
* Prefix AND Suffix front-coded (PSFC) 4 Section dictionary
*/
Expand Down
10 changes: 6 additions & 4 deletions hdt-api/src/main/java/org/rdfhdt/hdt/quad/QuadString.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public QuadString(CharSequence subject, CharSequence predicate, CharSequence obj

public QuadString(TripleString other) {
super(other);
this.context = other.getObject();
this.context = other.getGraph();
}

@Override
Expand All @@ -29,9 +29,6 @@ public void clear() {

@Override
public boolean equals(Object other) {
if (context.length() == 0) {
return super.equals(other);
}
if (!(other instanceof QuadString)) {
return false;
}
Expand Down Expand Up @@ -116,4 +113,9 @@ public int hashCode() {
public QuadString tripleToString() {
return new QuadString(subject.toString(), predicate.toString(), object.toString(), context.toString());
}

@Override
public String toString() {
return super.toString() + " " + context;
}
}
24 changes: 24 additions & 0 deletions hdt-api/src/main/java/org/rdfhdt/hdt/rdf/RDFAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,28 @@ public interface RDFAccess {
* @throws NotFoundException when the triple cannot be found
*/
IteratorTripleString search(CharSequence subject, CharSequence predicate, CharSequence object) throws NotFoundException;

/**
* Iterate over the triples of an RDF Set that match the specified pattern.
* null and empty strings act as a wildcard.
* (e.g. search(null, null, null, null) iterates over all elements)
*
* @param subject
* The subject to search
* @param predicate
* The predicate to search
* @param object
* The object to search
* @param graph
* The graph to search
*
* @return Iterator of TripleStrings
* @throws NotFoundException when the triple cannot be found
*/
IteratorTripleString search(
CharSequence subject,
CharSequence predicate,
CharSequence object,
CharSequence graph
) throws NotFoundException;
}
89 changes: 80 additions & 9 deletions hdt-api/src/main/java/org/rdfhdt/hdt/triples/TripleID.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public final class TripleID implements Comparable<TripleID>, Serializable, Clone
private long subject;
private long predicate;
private long object;
private long graph;
private boolean isQuad = false;

/**
* Basic constructor
Expand All @@ -68,6 +70,27 @@ public TripleID(long subject, long predicate, long object) {
this.object = object;
}

/**
* Constructor
*
* @param subject
* The subject
* @param predicate
* The predicate
* @param object
* The object
* @param graph
* The graph
*/
public TripleID(long subject, long predicate, long object, long graph) {
super();
this.subject = subject;
this.predicate = predicate;
this.object = object;
this.graph = graph;
this.isQuad = true;
}

/**
* Build a TripleID as a copy of another one.
* @param other the triple ID to copy
Expand All @@ -77,6 +100,12 @@ public TripleID(TripleID other) {
this.subject = other.subject;
this.predicate = other.predicate;
this.object = other.object;
this.graph = other.graph;
this.isQuad = other.isQuad;
}

public boolean isQuad() {
return isQuad;
}

/**
Expand Down Expand Up @@ -124,6 +153,22 @@ public void setPredicate(long predicate) {
this.predicate = predicate;
}

/**
* @return long the graph
*/
public long getGraph() {
return graph;
}

/**
* @param graph
* the graph to set
*/
public void setGraph(long graph) {
this.graph = graph;
this.isQuad = true;
}

/**
* Replace all components of a TripleID at once. Useful to reuse existing objects.
* @param subject subject ID
Expand All @@ -136,17 +181,35 @@ public void setAll(long subject, long predicate, long object) {
this.object = object;
}

/**
* Replace all components of a TripleID at once. Useful to reuse existing objects.
* @param subject subject ID
* @param predicate predicate ID
* @param object object ID
* @param graph graph ID
*/
public void setAll(long subject, long predicate, long object, long graph) {
this.subject = subject;
this.predicate = predicate;
this.object = object;
this.graph = graph;
this.isQuad = true;
}

public void assign(TripleID replacement) {
subject = replacement.getSubject();
object = replacement.getObject();
predicate = replacement.getPredicate();
graph = replacement.getGraph();
isQuad = replacement.isQuad();
}

/**
* Set all components to zero.
*/
public void clear() {
subject = predicate = object = 0;
subject = predicate = object = graph = 0;
isQuad = false;
}

/*
Expand All @@ -156,6 +219,8 @@ public void clear() {
*/
@Override
public String toString() {
if (isQuad)
return subject + " " + predicate + " " + object + " " + graph;
return subject + " " + predicate + " " + object;
}

Expand Down Expand Up @@ -192,11 +257,13 @@ public boolean match(TripleID pattern) {
long subjectPattern = pattern.getSubject();
long predicatePattern = pattern.getPredicate();
long objectPattern = pattern.getObject();
long graphPattern = pattern.getGraph();

/* Remember that 0 acts as a wildcard */
if (subjectPattern == 0 || this.subject == subjectPattern) {
if (predicatePattern == 0 || this.predicate == predicatePattern) {
return objectPattern == 0 || this.object == objectPattern;
if (objectPattern == 0 || this.object == objectPattern) {
return graphPattern == 0 || this.graph == graphPattern;
}
}
}
return false;
Expand All @@ -207,23 +274,26 @@ public boolean match(TripleID pattern) {
* @return boolean
*/
public boolean isEmpty() {
return !(subject != 0 || predicate != 0 || object != 0);
return !(subject != 0 || predicate != 0 || object != 0 || graph != 0);
}

/**
* Check whether none of the components of the triple are empty.
* @return boolean
*/
public boolean isValid() {
return subject>0 && predicate>0 && object>0;
return subject>0 && predicate>0 && object>0 && (isQuad ? graph>0 : true);
}

/**
* Checks whether any of the components of the triple are "no match" (-1).
* @return boolean
*/
public boolean isNoMatch() {
return subject == -1 || predicate == -1 || object == -1;
return subject == -1
|| predicate == -1
|| object == -1
|| isQuad && graph == -1;
}

/**
Expand All @@ -234,7 +304,8 @@ public String getPatternString() {
return "" +
(subject==0 ? '?' : 'S') +
(predicate==0 ? '?' : 'P') +
(object==0 ? '?' : 'O');
(object==0 ? '?' : 'O') +
(isQuad ? (graph==0 ? '?' : 'G') : "");
}

/**
Expand All @@ -252,7 +323,7 @@ public boolean equals(Object o) {
return false;
}
TripleID other = (TripleID) o;
return !( subject!=other.subject || predicate!=other.predicate || object!=other.object );
return !( subject!=other.subject || predicate!=other.predicate || object!=other.object || graph!=other.graph );
}

@Override
Expand All @@ -266,6 +337,6 @@ public TripleID clone() {

@Override
public int hashCode() {
return (int) (subject * 13 + predicate * 17 + object * 31);
return (int) (subject * 13 + predicate * 17 + object * 31 + graph * 37);
}
}
Loading

0 comments on commit 8a73fcd

Please sign in to comment.