📄 driverrdb.java
字号:
// belongs in URI table
DBIDInt URIid = getURIID(qname,addIfLong);
if ( URIid == null ) return res;
dbid = URIid.getIntID();
res = new String(RDBCodeURI + RDBCodeRef + pfx + dbid);
} else {
res = RDBCodeURI + RDBCodeValue + pfx + qname + EOS;
}
} else if ( node.isLiteral() ){
// TODO: may need to encode literal value when datatype is not a string.
Node_Literal litNode = (Node_Literal) node;
String lval = litNode.getLiteralLexicalForm();
String lang = litNode.getLiteralLanguage();
String dtype = litNode.getLiteralDatatypeURI();
String ld = litLangTypeToRDBString(lang,dtype);
int encodeLen = RDBCodeLiteral.length() + 2 + ld.length() + EOS_LEN;
boolean litIsLong = objectIsLong(encodeLen,lval);
if ( litIsLong ) {
int dbid;
//System.err.println("Long literal("+lval.length()+" => "+encodeLen+")") ;
// belongs in literal table
DBIDInt lid = getLiteralID(litNode,addIfLong);
if ( lid == null ) return res;
dbid = lid.getIntID();
res = new String(RDBCodeLiteral + RDBCodeRef + RDBCodeDelim + dbid);
} else {
res = new String(RDBCodeLiteral + RDBCodeValue + RDBCodeDelim + ld + lval + EOS);
}
} else if ( node.isBlank() ) {
String bnid = node.getBlankNodeId().toString();
String delims = "::";
int encodeLen = RDBCodeBlank.length() + 1 + delims.length() + EOS_LEN;
boolean BisLong = objectIsLong(encodeLen,bnid);
if ( BisLong ) {
int dbid;
// belongs in URI table
DBIDInt URIid = getBlankID(bnid,addIfLong);
if ( URIid == null ) return res;
dbid = URIid.getIntID();
res = new String(RDBCodeBlank + RDBCodeRef + delims + dbid);
} else {
res = new String(RDBCodeBlank + RDBCodeValue + delims + bnid + EOS);
}
} else if ( node.isVariable() ){
String name = ((Node_Variable)node).getName();
int len = name.length();
if ( (len + 3 + EOS_LEN) > LONG_OBJECT_LENGTH )
throw new JenaException ("Variable name too long: " + name );
res = RDBCodeVariable + RDBCodeValue + RDBCodeDelim + name + EOS;
} else if ( node.equals(Node.ANY) ) {
res = RDBCodeANY + RDBCodeValue + RDBCodeDelim;
} else {
throw new RDFRDBException ("Expected Concrete Node, got " + node.toString() );
}
return res;
}
/**
* Convert an RDB string to the node that it encodes. Return null if failure.
* @param RDBstring The string to convert to a node.
* @return The node or null if failure.
*/
public Node RDBStringToNode ( String RDBString ) throws RDFRDBException {
Node res = null;
int len = RDBString.length();
if ( len < 3 )
throw new RDFRDBException("Bad RDBString Header: " + RDBString);
String nodeType = RDBString.substring(0,1);
String valType = RDBString.substring(1,2);
if ( (!(valType.equals(RDBCodeRef) || valType.equals(RDBCodeValue))) ||
(RDBString.charAt(2) != RDBCodeDelimChar) )
throw new RDFRDBException("Bad RDBString Header: " + RDBString);
int pos = 3;
int npos;
if ( nodeType.equals(RDBCodeURI) ) {
ParseInt pi = new ParseInt(pos);
String prefix = "";
RDBStringParseInt(RDBString, pi, false);
if ( pi.val != null ) {
if ( URI_COMPRESS == false )
throw new RDFRDBException("Bad URI: Prefix Compression Disabled: " + RDBString);
prefix = IDtoPrefix(pi.val.intValue());
if ( prefix == null )
throw new RDFRDBException("Bad URI Prefix: " + RDBString);
}
pos = pi.pos + 1;
String qname;
if ( valType.equals(RDBCodeRef) ) {
qname = IDtoURI(RDBString.substring(pos));
if ( qname == null )
throw new RDFRDBException("Bad URI: " + RDBString);
} else
qname = RDBString.substring(pos,len - EOS_LEN);
res = Node.createURI(prefix + qname);
} else if ( nodeType.equals(RDBCodeLiteral) ) {
res = RDBLiteralStringToLiteralNode( RDBString, len, valType, pos );
} else if ( nodeType.equals(RDBCodeBlank) ) {
String bstr = null;
if ( valType.equals(RDBCodeValue) ) {
bstr = RDBString.substring(4,len-EOS_LEN);
} else {
bstr = IDtoBlank(RDBString.substring(4));
if ( bstr == null )
throw new RDFRDBException("Bad URI: " + RDBString);
}
res = Node.createAnon( new AnonId (bstr) );
} else if ( nodeType.equals(RDBCodeVariable) ) {
String vname = RDBString.substring(3,len-EOS_LEN);
res = Node.createVariable(vname);
} else if ( nodeType.equals(RDBCodeANY) ) {
res = Node.ANY;
} else
throw new RDFRDBException ("Invalid RDBString Prefix, " + RDBString );
return res;
}
/**
Answer a literal Node constructed according to the RDB String.
@param RDBString
@param len
@param valType
@param pos
@return
*/
protected Node RDBLiteralStringToLiteralNode( String RDBString, int len, String valType, int pos )
{
ParseInt pi = new ParseInt( pos );
String litString = null;
if ( valType.equals(RDBCodeRef) ) {
RDBStringParseInt(RDBString,pi,true);
if ( pi.val != null )
litString = IDtoLiteral(pi.val.intValue());
if ( litString == null )
throw new RDFRDBException("Bad Literal Reference: " + RDBString);
} else
litString = RDBString.substring(pos,len-EOS_LEN);
len = litString.length();
pi.pos = 0;
RDBStringParseInt(litString, pi, false);
int langLen = pi.val == null ? 0 : pi.val.intValue();
pi.pos = pi.pos + 1;
RDBStringParseInt(litString, pi, false);
int dtypeLen = pi.val == null ? 0 : pi.val.intValue();
pos = pi.pos + 1;
if ( (pos + langLen + dtypeLen) > len )
throw new RDFRDBException("Malformed Literal: " + litString);
String lang = litString.substring( pos, pos + langLen );
pos = pos + langLen;
String dtype = litString.substring( pos, pos + dtypeLen );
String val = litString.substring( pos + dtypeLen );
return createLiteral( val, lang, dtype );
}
/**
Answer a Node literal with the indicated lexical form, language,
and datatype. If the datatype is the empty string, there is no
datatype. If the language is the empty string, there is no language.
@param val
@param lang
@param dtype
@return
*/
protected Node createLiteral( String val, String lang, String dtype ) {
if (dtype.equals( "" )) {
return Node.createLiteral( val, lang, null );
} else {
RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtype);
return Node.createLiteral( val, lang, dt );
}
}
/** This is cuurently a copy of Util.splitNamespace. It was
* copied rather than used directly for two reasons. 1) in the
* future it may be desirable to use a different split algorithm
* for persistence. 2) the util version could change at any time,
* which would render existing databases inaccessible. having a
* copy allows the db version to evolve in a controlled way.
*
* Given an absolute URI, determine the split point between the namespace part
* and the localname part.
* If there is no valid localname part then the length of the
* string is returned.
* The algorithm tries to find the longest NCName at the end
* of the uri, not immediately preceeded by the first colon
* in the string.
* @param uri
* @return the index of the first character of the localname
*/
public static int dbSplitNamespace(String uri) {
char ch;
int lg = uri.length();
if (lg == 0)
return 0;
int j;
int i;
for (i = lg - 1; i >= 1; i--) {
ch = uri.charAt(i);
if (!XMLChar.isNCName(ch))
break;
}
for (j = i + 1; j < lg; j++) {
ch = uri.charAt(j);
if (XMLChar.isNCNameStart(ch)) {
if (uri.charAt(j - 1) == ':'
&& uri.lastIndexOf(':', j - 2) == -1)
continue; // split "mailto:me" as "mailto:m" and "e" !
else
break;
}
}
return j;
}
class ParseInt {
int pos;
Integer val;
ParseInt(int p) {pos = p;}
}
protected void RDBStringParseInt ( String RDBString, ParseInt pi, boolean toEnd ) {
int npos = toEnd ? RDBString.length() : RDBString.indexOf(RDBCodeDelimChar,pi.pos);
if ( npos < 0 ) {
throw new RDFRDBException("Bad RDB String: " + RDBString);
}
String intStr = RDBString.substring(pi.pos,npos);
pi.pos = npos;
if ( intStr.equals("") )
pi.val = null;
else try {
pi.val = new Integer(intStr);
} catch (NumberFormatException e1) {
throw new RDFRDBException("Bad RDB String: " + RDBString);
}
return;
}
DBIDInt URItoPrefix ( String uri, int pos, boolean add ) {
DBIDInt res;
Object key = prefixCache.getByValue(uri.substring(0,pos));
if ( key == null ) {
RDBLongObject lobj = PrefixToLongObject(uri,pos);
res = getLongObjectID(lobj, PREFIX_TABLE, add);
if ( res != null )
prefixCache.put(res,uri.substring(0,pos));
} else
res = (DBIDInt) key;
return res;
}
protected RDBLongObject PrefixToLongObject ( String prefix, int split ) {
RDBLongObject res = new RDBLongObject();
int headLen;
int avail;
res.head = RDBCodePrefix + RDBCodeValue + RDBCodeDelim;
headLen = res.head.length();
avail = INDEX_KEY_LENGTH - (headLen + EOS_LEN);
if ( split > avail ) {
res.head = res.head + prefix.substring(0,avail);
res.tail = prefix.substring(avail,split);
res.hash = stringToHash(res.tail);
} else {
res.head = res.head + prefix.substring(0,split);
res.tail = "";
}
res.head = res.head + EOS;
return res;
}
/**
* Encode a literal node's lang and datatype as a string of the
* form ":[langLen]:[datatypeLen]:[langString][dataTypeString]"
* @return the string.
*/
public String litLangTypeToRDBString ( String lang, String dtype ) throws RDFRDBException {
String res = RDBCodeDelim;
res = ((lang == null) ? "" : Integer.toString(lang.length())) + RDBCodeDelim;
res = res + ((dtype == null) ? "" : Integer.toString(dtype.length())) + RDBCodeDelim;
res = res + (lang == null ? "" : lang) + (dtype == null ? "" : dtype);
return res;
}
/**
* Check if an object is long, i.e., it exceeds the length
* limit for storing in a statement table.
* @return true if literal is long, else false.
*/
protected boolean objectIsLong ( int encodingLen, String objAsString ) {
return ( (encodingLen + objAsString.length()) > LONG_OBJECT_LENGTH);
}
class RDBLongObject {
String head; /* prefix of long object that can be indexed */
long hash; /* hash encoding of tail */
String tail; /* remainder of long object */
}
protected RDBLongObject literalToLongObject ( Node_Literal node ) {
RDBLongObject res = new RDBLongObject();
int headLen;
int avail;
String lang = node.getLiteralLanguage();
String dtype = node.getLiteralDatatypeURI();
String val = node.getLiteralLexicalForm();
String langType = litLangTypeToRDBString(lang,dtype);
res.head = RDBCodeLiteral + RDBCodeValue + RDBCodeDelim + langType;
headLen = res.head.length();
avail = INDEX_KEY_LENGTH - (headLen + EOS_LEN);
if ( val.length() > avail ) {
res.head = res.head + val.substring(0,avail);
res.tail = val.substring(avail);
res.hash = stringToHash(res.tail);
} else {
res.head = res.head + val;
res.tail = "";
}
res.head = res.head + EOS;
return res;
}
protected long stringToHash ( String str ) {
CRC32 checksum = new CRC32();
checksum.update(str.getBytes());
return checksum.getValue();
}
/**
* Return the database ID for the URI, if it exists
*/
public DBIDInt getBlankID(String bstr, boolean add) throws RDFRDBException {
RDBLongObject lobj = URIToLongObject (bstr,RDBCodeBlank);
return getLongObjectID(lobj, LONG_URI_TABLE, add);
}
/**
* Return the database ID for the URI, if it exists
*/
public DBIDInt getURIID(String qname, boolean add) throws RDFRDBException {
RDBLongObject lobj = URIToLongObject (qname,RDBCodeURI);
return getLongObjectID(lobj, LONG_URI_TABLE, add);
}
protected RDBLongObject URIToLongObject ( String qname, String code ) {
RDBLongObject res = new RDBLongObject();
int headLen;
int avail;
res.head = code + RDBCodeValue + RDBCodeDelim;
headLen = res.head.length();
avail = INDEX_KEY_LENGTH - (headLen + EOS_LEN);
if ( qname.length() > avail ) {
res.head = res.head + qname.substring(0,avail);
res.tail = qname.substring(avail);
res.hash = stringToHash(res.tail);
} else {
res.head = res.head + qname;
res.tail = "";
}
res.head = res.head + EOS;
return res;
}
/**
* Return the database ID for the literal, if it exists
*/
public DBIDInt getLiteralID(Node_Literal lnode, boolean add) throws RDFRDBException {
RDBLongObject lobj = literalToLongObject (lnode);
return getLongObjectID(lobj, LONG_LIT_TABLE, add);
}
public DBIDInt getLongObjectID(RDBLongObject lobj, String table, boolean add) throws RDFRDBException {
ResultSet rs = null;
PreparedStatement ps = null;
try {
String opName = "getLongObjectID";
if ( lobj.tail.length() > 0 )
opName += "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -