⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 valuestore.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if (isOwnValue) {			((NativeValue)value).setInternalId(id);			// Add value to MRU cache			synchronized (_mruCache) {				if (_mruCache.size() >= MRU_CACHE_SIZE) {					// Cache full, remove least-recently used value					_mruCache.removeLast();				}				_mruCache.addFirst(value);			}		}		return id;	}	/**	 * Changes the reference count for the value with the specified ID with the	 * specified amount. Values with a reference count equal to <tt>0</tt> is	 * subject to being garbage collected. This method can only be called as	 * part of a transaction. This method can only be called as part of a	 * transaction.	 *	 * @param id The ID of the value that should be removed.	 * @param amount The (positive or negative) amount to change the reference count.	 * @exception IOException If an I/O error occurred.	 * @see #startTransaction	 * @see #commitTransaction	 **/	public void changeReferenceCount(int id, int amount)		throws IOException	{		_dataStore.changeReferenceCount(id, amount);	}	/**	 * Removes all values from the ValueStore. This method can only be called as	 * part of a transaction. The ValueStore will not be cleared until the	 * transaction is committed.	 *	 * @exception IOException If an I/O error occurred.	 * @see #startTransaction	 * @see #commitTransaction	 **/	public void clear()		throws IOException	{		_dataStore.clear();		_namespaceStore.clear();		synchronized (_mruCache) {			_mruCache.clear();		}		_nextBNodeID = 1;	}	/**	 * Closes the ValueStore, releasing any file references, etc. In case a	 * transaction is currently open, it will be rolled back. Once closed, the	 * ValueStore can no longer be used.	 *	 * @exception IOException If an I/O error occurred.	 **/	public void close()		throws IOException	{		_dataStore.close();		_namespaceStore.close();		_mruCache = null;		_repository = null;	}	private byte[] _value2data(Value value, boolean dirtyReads, boolean create)		throws IOException	{		if (value instanceof URI) {			return _uri2data((URI)value, dirtyReads, create);		}		else if (value instanceof BNode) {			return _bnode2data((BNode)value, dirtyReads, create);		}		else if (value instanceof Literal) {			return _literal2data((Literal)value, dirtyReads, create);		}		else {			throw new IllegalArgumentException("value parameter should be a URI, BNode or Literal");		}	}	private byte[] _uri2data(URI uri, boolean dirtyReads, boolean create)		throws IOException	{		// Get namespace ID		int nsID = _namespaceStore.getID(uri.getNamespace(), dirtyReads);		if (nsID == 0) {			// Unknown namespace			if (create) {				// Add namespace				nsID = _namespaceStore.storeNamespace(uri.getNamespace());			}			else {				// Unknown namespace means unknown URI				return null;			}		}		// Get local name in UTF-8		byte[] localNameData = uri.getLocalName().getBytes("UTF-8");		// Combine parts in a single byte array		byte[] uriData = new byte[5 + localNameData.length];		uriData[0] = URI_VALUE;		ByteArrayUtil.putInt(nsID, uriData, 1);		ByteArrayUtil.put(localNameData, uriData, 5);		return uriData;	}	private byte[] _bnode2data(BNode bNode, boolean dirtyReads, boolean create)		throws IOException	{		byte[] idData = bNode.getID().getBytes("UTF-8");		byte[] bNodeData = new byte[1 + idData.length];		bNodeData[0] = BNODE_VALUE;		ByteArrayUtil.put(idData, bNodeData, 1);		return bNodeData;	}	private byte[] _literal2data(Literal literal, boolean dirtyReads, boolean create)		throws IOException	{		// Get datatype ID		int datatypeID = 0;		if (literal.getDatatype() != null) {			if (create) {				datatypeID = storeValue(literal.getDatatype());			}			else {				datatypeID = getID(literal.getDatatype(), dirtyReads);				if (datatypeID == 0) {					// Unknown datatype means unknown literal					return null;				}			}		}		// Get language tag in UTF-8		byte[] langData = null;		int langDataLength = 0;		if (literal.getLanguage() != null) {			langData = literal.getLanguage().getBytes("UTF-8");			langDataLength = langData.length;		}		// Get label in UTF-8		byte[] labelData = literal.getLabel().getBytes("UTF-8");		// Combine parts in a single byte array		byte[] literalData = new byte[6 + langDataLength + labelData.length];		literalData[0] = LITERAL_VALUE;		ByteArrayUtil.putInt(datatypeID, literalData, 1);		literalData[5] = (byte)langDataLength;		if (langData != null) {			ByteArrayUtil.put(langData, literalData, 6);		}		ByteArrayUtil.put(labelData, literalData, 6 + langDataLength);			return literalData;	}	private NativeValue _data2value(int id, byte[] data)		throws IOException	{		switch ( (data[0] & VALUE_TYPE_MASK) ) {			case URI_VALUE: return _data2uri(id, data);			case BNODE_VALUE: return _data2bnode(id, data);			case LITERAL_VALUE: return _data2literal(id, data);			default: throw new IllegalArgumentException("data does not specify a known value type");		}	}	private NativeURI _data2uri(int id, byte[] data)		throws IOException	{		int nsID = ByteArrayUtil.getInt(data, 1);		String namespace = _namespaceStore.getNamespaceName(nsID);		String localName = new String(data, 5, data.length-5, "UTF-8");		return new NativeURI(_repository, namespace, localName, id);	}	private NativeBNode _data2bnode(int id, byte[] data)		throws IOException	{		String nodeID = new String(data, 1, data.length-1, "UTF-8");		return new NativeBNode(_repository, nodeID, id);	}	private NativeLiteral _data2literal(int id, byte[] data)		throws IOException	{		// Get datatype		int datatypeID = ByteArrayUtil.getInt(data, 1);		URI datatype = null;		if (datatypeID != 0) {			datatype = (URI)getValue(datatypeID);		}		// Get language tag		String lang = null;		int langLength = data[5];		if (langLength > 0) {			lang = new String(data, 6, langLength, "UTF-8");		}		// Get label		String label = new String(data, 6 + langLength, data.length - 6 - langLength, "UTF-8");		if (datatype != null) {			return new NativeLiteral(_repository, label, datatype, id);		}		else if (lang != null) {			return new NativeLiteral(_repository, label, lang, id);		}		else {			return new NativeLiteral(_repository, label, id);		}	}	private boolean _isOwnValue(Value value) {		return value instanceof NativeValue &&			((NativeValue)value).getRepository() == _repository;	}/*------------------------------------+| Methods from interface ValueFactory |+------------------------------------*/	// Implements ValueFactory.createURI(String)	public URI createURI(String uri) {		return new NativeURI(_repository, uri);	}		public URI createURI(String namespace, String localName) {		return new NativeURI(_repository, namespace, localName);	}	// Implements ValueFactory.createBNode()	public BNode createBNode() {		if (_nextBNodeID == Integer.MAX_VALUE) {			// Start with a new bnode prefix			_updateBNodePrefix();		}		return createBNode(_bnodePrefix + _nextBNodeID++);	}		// Implements ValueFactory.createBNode(String)	public BNode createBNode(String nodeId) {		return new NativeBNode(_repository, nodeId);	}	// Implements ValueFactory.createLiteral(String)	public Literal createLiteral(String value) {		return new NativeLiteral(_repository, value);	}	// Implements ValueFactory.createLiteral(String, String)	public Literal createLiteral(String value, String language) {		return new NativeLiteral(_repository, value, language);	}	// Implements ValueFactory.createLiteral(String, URI)	public Literal createLiteral(String value, URI datatype) {		return new NativeLiteral(_repository, value, datatype);	}	// Implements ValueFactory.createStatement(Resource, URI, Value)	public Statement createStatement(Resource subject, URI predicate, Value object) {		return new StatementImpl(subject, predicate, object);	}/*-------------------+| Test/debug methods |+-------------------*/	public static void main(String[] args)		throws Exception	{		File dataDir = new File(args[0]);		NamespaceStore namespaceStore = new NamespaceStore(dataDir);		ValueStore valueStore = new ValueStore(dataDir, namespaceStore, null);		int maxID = valueStore._dataStore.getMaxID();		for (int id = 1; id <= maxID; id++) {			Value value = valueStore.getValue(id);			System.out.println("ID " + id + " : " + value.toString());		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -