📄 valuestore.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.sailimpl.nativerdf;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.LinkedList;import org.openrdf.util.ByteArrayUtil;import org.openrdf.model.BNode;import org.openrdf.model.Literal;import org.openrdf.model.Resource;import org.openrdf.model.Statement;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.ValueFactory;import org.openrdf.model.impl.StatementImpl;import org.openrdf.sesame.sailimpl.nativerdf.datastore.DataStore;import org.openrdf.sesame.sailimpl.nativerdf.model.NativeBNode;import org.openrdf.sesame.sailimpl.nativerdf.model.NativeLiteral;import org.openrdf.sesame.sailimpl.nativerdf.model.NativeURI;import org.openrdf.sesame.sailimpl.nativerdf.model.NativeValue;/** * Class that provides indexed storage and retrieval of RDF values. **/public class ValueStore implements ValueFactory {/*-------------+| Constants |+-------------*/ private static final String FILENAME_PREFIX = "values"; private static final int MRU_CACHE_SIZE = 16; private static final byte VALUE_TYPE_MASK = 0x3; // 0000 0011 private static final byte URI_VALUE = 0x1; // 0000 0001 private static final byte BNODE_VALUE = 0x2; // 0000 0010 private static final byte LITERAL_VALUE = 0x3; // 0000 0011/*-------------+| Variables |+-------------*/ private DataStore _dataStore; private NamespaceStore _namespaceStore; private NativeRdfRepository _repository; /** A simple cache containing the [MRU_CACHE_SIZE] most-recently used values for getValue(int id). **/ private LinkedList _mruCache; /** The prefix for any new bnode IDs. **/ private String _bnodePrefix; /** The ID for the next bnode that is created. **/ private int _nextBNodeID;/*-------------+| Constructors |+-------------*/ public ValueStore(File dataDir, NamespaceStore namespaceStore, NativeRdfRepository repository) throws IOException { _dataStore = new DataStore(dataDir, FILENAME_PREFIX); _namespaceStore = namespaceStore; _repository = repository; _mruCache = new LinkedList(); _updateBNodePrefix(); } /** * Generates a new bnode prefix based on <tt>currentTimeMillis()</tt> and * resets <tt>_nextBNodeID</tt> to <tt>1</tt>. **/ private void _updateBNodePrefix() { // BNode prefix is based on currentTimeMillis(). Combined with a // sequential number per session, this gives a unique identifier. _bnodePrefix = "node" + Long.toString(System.currentTimeMillis(), 32) + "x"; _nextBNodeID = 1; }/*----------+| Methods |+----------*/ /** * Gets the value for the specified ID. * * @param id A value ID. * @return The value for the ID, or <tt>null</tt> no such value could be * found. * @exception IOException If an I/O error occurred. **/ public Value getValue(int id) throws IOException { NativeValue result = null; // Check MRU cache synchronized (_mruCache) { Iterator iter = _mruCache.iterator(); while (iter.hasNext()) { NativeValue cacheValue = (NativeValue)iter.next(); if (cacheValue.getInternalId() == id) { // Cache hit, move hit to most-recent position iter.remove(); _mruCache.addFirst(cacheValue); result = cacheValue; break; } } } if (result == null) { // Value not in cache, fetch it from file byte[] data = _dataStore.getData(id); if (data != null) { result = _data2value(id, data); // Add value to MRU cache synchronized (_mruCache) { if (_mruCache.size() >= MRU_CACHE_SIZE) { // Cache full, remove least-recently used value _mruCache.removeLast(); } _mruCache.addFirst(result); } } } return result; } /** * Gets the ID for the specified value. * * @param value A value. * @return The ID for the specified value, or <tt>0</tt> if no such ID could * be found. * @exception IOException If an I/O error occurred. **/ public int getID(Value value) throws IOException { return getID(value, false); } public int getID(Value value, boolean dirtyReads) throws IOException { boolean isOwnValue = _isOwnValue(value); if (isOwnValue) { int internalId = ((NativeValue)value).getInternalId(); if (internalId != 0) { // internal ID has been set return internalId; } } int id = 0; byte[] data = _value2data(value, dirtyReads, false); if (data != null) { id = _dataStore.getID(data); if (isOwnValue) { // Store id in value for fast access in any consecutive calls ((NativeValue)value).setInternalId(id); } } return id; } /** * Starts a transaction. This prepares the ValueStore for upcoming updates * to the stored data. * * @exception IOException If an I/O error occurred. **/ public void startTransaction() throws IOException { _namespaceStore.startTransaction(); _dataStore.startTransaction(); } /** * Commits a transaction, applying all updates that have been performed * during the transaction. * * @exception IOException If an I/O error occurred. **/ public void commitTransaction() throws IOException { _dataStore.commitTransaction(); _namespaceStore.commitTransaction(); } /** * Rolls back all updates that have been performed in the current * transaction and closes it. * * @exception IOException If an I/O error occurred. **/ public void rollbackTransaction() throws IOException { _dataStore.rollbackTransaction(); _namespaceStore.rollbackTransaction(); } /** * Stores the supplied value and returns the ID that has been assigned to * it. In case the value was already present, the value will not be stored * again and the ID of the existing value is returned. This method can only * be called as part of a transaction. The supplied value will not be stored * until the transaction has been committed. * * @param value The Value to store. * @return The ID that has been assigned to the value. * @exception IOException If an I/O error occurred. * @see #startTransaction * @see #commitTransaction **/ public int storeValue(Value value) throws IOException { int id = 0; // Try to get the internal ID from the value itself boolean isOwnValue = _isOwnValue(value); if (isOwnValue) { id = ((NativeValue)value).getInternalId(); } if (id == 0) { // Try to get the internal ID from a value in the MRU Cache synchronized (_mruCache) { Iterator iter = _mruCache.iterator(); while (iter.hasNext()) { NativeValue cacheValue = (NativeValue)iter.next(); if (cacheValue.equals(value)) { // Cache hit, move hit to most-recent position iter.remove(); _mruCache.addFirst(cacheValue); id = cacheValue.getInternalId(); break; } } } } if (id != 0) { // value has already been stored, increase reference count only _dataStore.changeReferenceCount(id, 1); return id; } // Unable to get internal ID in a cheap way, just store it in the data // store which will handle duplicates byte[] valueData = _value2data(value, true, true); id = _dataStore.storeData(valueData);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -