/** * Gets a pooled object of a given type from this pool. * * @param type The ID of the type of object to find. * @return A pooled object, or <code>null</code> if no objects are available. */ public Object get(int type) { assert refCount > 0; int count = objectStackCounts[type]; if (count > 0) { Object[] os = objectStacks[type]; Object o = os[--count]; os[count] = null; objectStackCounts[type] = count; return o; } if (nextPool != null) { synchronized (nextPool) { return nextPool.get(type); } } return null; }
/** * Creates a new ObjectStack. * * @return A fresh {@link ObjectStack}. */ private ObjectStack newObjectStackInstance() { ObjectStack os = (ObjectStack) get(TYPE_OBJECTSTACK); if (os == null) { os = new ObjectStack(OBJECT_STACK_SIZE_LIMIT); } return os; }
/** * Create a new key, based on a given type and size. * * @param type The ID of the type of object to store. * @param key The key for this object type. This is the size of the data it holds. * @return A new {@link Key} instance. */ private Key newKeyInstance(int type, int key) { Key k = (Key) get(TYPE_KEY); if (k != null) { k.init(type, key); } else { k = new Key(type, key); } return k; }
/** * Factory method to create a new block, allocating a new data buffer. Uses the objectPool * if possible, or else creates a new block that will be returned to the object pool. * * @param objectPool The pool of blocks to get the block from. * @param blockFile The file to get the block from. * @param blockSize The size of the data in the block. * @param blockId The ID of the block from the file. * @param byteOrder Represents little endian or big endian byte ordering. * @return The block from the pool, or a new block which will be returned to the pool. */ public static Block newInstance( ObjectPool objectPool, BlockFile blockFile, int blockSize, long blockId, ByteOrder byteOrder ) { Block block = (Block) objectPool.get(ObjectPool.TYPE_S_BLOCK, blockSize); if (block != null) { block.init(objectPool, blockFile, blockId); } else { block = Block.newInstance( objectPool, blockFile, blockSize, blockId, 0, ByteBuffer.allocateDirect(blockSize).order(byteOrder), null, null, null ); block.ownsBuffer = true; } return block; }
/** * Factory method for an AVLNode. * * @param phase The phase form the AVLFile. * @param objectPool The object pool to return this node to when it's finished with. * @param parentNode The parent to this node. * @param childIndex The index of this node within its parent, either left or right. * @param nodeId The ID to find the data for this node in the file. * @return The new node, read from file, or with new file info in it. * @throws IOException If there was an I/O exception. */ static AVLNode newInstance( AVLFile.Phase phase, ObjectPool objectPool, AVLNode parentNode, int childIndex, long nodeId ) { AVLNode avlNode = (AVLNode) objectPool.get(ObjectPool.TYPE_AVLNODE); if (avlNode != null) { avlNode.init(phase, objectPool, parentNode, childIndex, nodeId); } else { avlNode = new AVLNode(phase, objectPool, parentNode, childIndex, nodeId); } return avlNode; }
/** * Gets a pooled object of a given type and size from this pool. * * @param type The ID of the type of object to find. * @param key The size of the object to find. * @return A pooled object, or <code>null</code> if no objects are available. */ public Object get(int type, int key) { assert refCount > 0; if ( (type == TYPE_S_BLOCK) && (key == 8192)) { return get(TYPE_BLOCK_8192); } Key k = newKeyInstance(type, key); ObjectStack os = (ObjectStack) sizedObjectStacks.get(k); put(TYPE_KEY, k); if ( (os != null) && !os.isEmpty()) { return os.pop(); } if (nextPool != null) { synchronized (nextPool) { return nextPool.get(type, key); } } return null; }
/** * Factory method to build a new local object pool. * * @param nextPool The next pool in the chain. Normally the shared pool * @return The new object pool. */ public static ObjectPool newInstance(ObjectPool nextPool) { ObjectPool op; if (nextPool != null) { synchronized (nextPool) { op = (ObjectPool) nextPool.get(TYPE_OBJECTPOOL); } } else { op = null; } if (op != null) { op.init(nextPool); } else { op = new ObjectPool(nextPool); } return op; }
/** * Factory method for an AVLNode. * * @param phase The phase form the AVLFile. * @param objectPool The object pool to return this node to when it's finished with. * @return The new node, backed by a new block in the file. * @throws IOException If there was an I/O exception. */ public static AVLNode newInstance( AVLFile.Phase phase, ObjectPool objectPool ) throws IOException { if (!phase.isCurrent()) { throw new IllegalStateException( "Attempt to allocate a new AVL node on a read-only phase." ); } AVLNode avlNode = (AVLNode) objectPool.get(ObjectPool.TYPE_AVLNODE); if (avlNode != null) { avlNode.init(phase, objectPool); } else { avlNode = new AVLNode(phase, objectPool); } return avlNode; }
ByteBuffer sbb, IntBuffer ib, LongBuffer lb ) { Block block = (Block) objectPool.get(ObjectPool.TYPE_BLOCK);