/** * {@inheritDoc} */ @Override public void preWriteFinalization() { for (IStorageTreeComponent<E> branch : branches) { branch.preWriteFinalization(); } }
/** * Constructor that uses the {@link IStorageDescriptor} to initiate the initial values. * * @param storageDescriptor * Storage descriptor that will be used to denote the range. */ public RangeDescriptor(IStorageDescriptor storageDescriptor) { start = storageDescriptor.getPosition(); end = (storageDescriptor.getPosition() + storageDescriptor.getSize()) - 1; }
/** * {@inheritDoc} */ @Override protected ITreeComponent<IStorageDescriptor, E> getNextTreeComponent(E element) { return storageBranchIndexer.getNextTreeComponent(element); }
/** * {@inheritDoc} */ @Override public int compareTo(IStorageDescriptor other) { if ((this.getChannelId() - other.getChannelId()) != 0) { return this.getChannelId() - other.getChannelId(); } if ((this.getPosition() - other.getPosition()) != 0) { return (int) (this.getPosition() - other.getPosition()); } if ((this.getSize() - other.getSize()) != 0) { return (int) (this.getSize() - other.getSize()); } return 0; }
/** * Joins the information from the other storage descriptor if possible. This method will return * true if the join was successfully done, and false if no join was done. The join is possible * only if the two descriptors are actually describing the data in the same channel that are * next to each other. There is no difference if the other descriptor is pointing to the data * after or before this descriptor. After successful join the joined data descriptor will be * represented by this descriptor. * * @param other * Descriptor information to join. * @return This method will return true if the join was successfully done, and false if no join * was done. */ public boolean join(IStorageDescriptor other) { if (this.getChannelId() != other.getChannelId()) { return false; } else { return join(other.getPosition(), other.getSize()); } }
@Test public void successfulWrite() throws IndexingException { IStorageDescriptor storageDescriptor = mock(IStorageDescriptor.class); when(storageDescriptor.getChannelId()).thenReturn(1); DefaultData defaultData = mock(DefaultData.class); when(indexingTree.put(defaultData)).thenReturn(storageDescriptor); WriteTask writeTask = mock(WriteTask.class); when(writeTask.getData()).thenReturn(defaultData); int channel = indexingTreeHandler.startWrite(writeTask); assertThat(channel, is(1)); assertThat(indexingTreeHandler.getWriteTaskInProgressCount(), is(1)); long position = 20L; long size = 30L; indexingTreeHandler.writeSuccessful(writeTask, position, size); assertThat(indexingTreeHandler.getWriteTaskInProgressCount(), is(0)); verify(storageDescriptor, times(1)).setPositionAndSize(position, size); }
IStorageDescriptor storageDescriptor = indexingTree.put(data); if (null == storageDescriptor) { throw new IndexingException("Indexing failed. Storage descriptor was null."); treeDescriptorPair.setStorageDescriptor(storageDescriptor); return storageDescriptor.getChannelId();
/** * Returns the URL location of the file on the server where the descriptor is pointing to, * without ip and port information. * <p> * Example locations is: /storageId/descriptorId.itdata * * @param storageData * {@link StorageData} * @param descriptor * {@link StorageDescriptor} * @return URL location without ip and port. */ public String getHttpFileLocation(IStorageData storageData, IStorageDescriptor descriptor) { return this.getHttpFileLocation(storageData, Integer.valueOf(descriptor.getChannelId())); }
/** * {@inheritDoc} * <p> * Method returns combined results from all branches that are combined. */ public List<IStorageDescriptor> query(StorageIndexQuery query) { List<IStorageDescriptor> combinedResult = new ArrayList<>(); for (IStorageTreeComponent<E> branch : branches) { List<IStorageDescriptor> branchResult = branch.query(query); if ((branchResult != null) && !branchResult.isEmpty()) { combinedResult.addAll(branchResult); } } return combinedResult; }
/** * Prepares the stream for read. Must be called before any read operation is executed. * * @throws IOException * if preparation fails due to inability to obtain defined number of byte buffers */ @Override public void prepare() throws IOException { super.prepare(); // set total size long totalSize = 0; for (IStorageDescriptor descriptor : descriptors) { totalSize += descriptor.getSize(); } setTotalSize(totalSize); executorService.execute(new ReadTask()); }
/** * Adds wanted amount of elements to the leaf. * * @param treeComponent * {@link IStorageTreeComponent}. * @param amount * Amount of elements to add. * @throws IndexingException */ private void addElements(IStorageTreeComponent<DefaultData> treeComponent, int amount) throws IndexingException { DefaultData element = mock(DefaultData.class); for (int i = 1; i <= amount; i++) { when(element.getId()).thenReturn((long) i); treeComponent.put(element); } } }
/** * {@inheritDoc} */ @Override public IStorageDescriptor get(E element) { for (IStorageTreeComponent<E> branch : branches) { IStorageDescriptor descriptor = branch.get(element); if (descriptor != null) { return descriptor; } } return null; }
/** * {@inheritDoc} */ @Override public void preWriteFinalization() { for (ITreeComponent<IStorageDescriptor, E> storageTreeComponent : getComponentMap().values()) { if (storageTreeComponent instanceof IStorageTreeComponent) { ((IStorageTreeComponent<E>) storageTreeComponent).preWriteFinalization(); } } }
/** * Signals to the {@link StorageIndexingTreeHandler} that the write has been successful with * correct information about write position and size. * <p> * Internally this method will update the {@link IStorageDescriptor} for the given * {@link DefaultData} object in the write task, and remove the task from the set of tasks being * currently processed. * * @param writeTask * Write task that succeeded. * @param position * Write position. * @param size * Write size. */ public void writeSuccessful(WriteTask writeTask, long position, long size) { // get the data from the map TreeDescriptorPair treeDescriptorPair = writeTasksInProcess.get(writeTask); if (null != treeDescriptorPair) { IStorageDescriptor storageDescriptor = treeDescriptorPair.getStorageDescriptor(); // update the descriptor with the information provided if (null != storageDescriptor) { storageDescriptor.setPositionAndSize(position, size); } } // remove the entry in map after the data has been updated in indexing tree writeTasksInProcess.remove(writeTask); }
/** * Signals to the {@link StorageIndexingTreeHandler} that the write has failed. * <p> * Internally this method will update the {@link IStorageTreeComponent} by removing the given * {@link DefaultData} object in the write task, and remove the task from the set of tasks being * currently processed. * * @param writeTask * Write task that failed. */ public void writeFailed(WriteTask writeTask) { // get the data from the map TreeDescriptorPair treeDescriptorPair = writeTasksInProcess.get(writeTask); if (null != treeDescriptorPair) { IStorageTreeComponent<DefaultData> indexingTree = treeDescriptorPair.getIndexingTree(); // if write fails, remove the descriptor for the data from indexing tree if (null != indexingTree) { indexingTree.getAndRemove(writeTask.getData()); } } // remove the entry in map after the indexing tree was informed writeTasksInProcess.remove(writeTask); }
/** * {@inheritDoc} */ @Override public long getComponentSize(IObjectSizes objectSizes) { long size = objectSizes.getSizeOfObjectHeader() + objectSizes.getPrimitiveTypesSize(1, 0, 0, 0, 0, 0); size += objectSizes.getSizeOf(branches); for (IStorageTreeComponent<E> branch : branches) { size += branch.getComponentSize(objectSizes); } return objectSizes.alignTo8Bytes(size); }
@Test public void failedWrite() throws IndexingException { IStorageDescriptor storageDescriptor = mock(IStorageDescriptor.class); when(storageDescriptor.getChannelId()).thenReturn(1); DefaultData defaultData = mock(DefaultData.class); when(indexingTree.put(defaultData)).thenReturn(storageDescriptor); WriteTask writeTask = mock(WriteTask.class); when(writeTask.getData()).thenReturn(defaultData); int channel = indexingTreeHandler.startWrite(writeTask); assertThat(channel, is(1)); assertThat(indexingTreeHandler.getWriteTaskInProgressCount(), is(1)); indexingTreeHandler.writeFailed(writeTask); assertThat(indexingTreeHandler.getWriteTaskInProgressCount(), is(0)); verify(storageDescriptor, times(0)).setPositionAndSize(anyLong(), anyLong()); }
/** * Returns the {@link Path} of the channel for given {@link StorageData} and * {@link StorageDescriptor}. * * @param storageData * {@link IStorageData} object. * @param descriptor * {@link StorageDescriptor} object. * @return {@link Path} that can be used in IO operations. */ public Path getChannelPath(IStorageData storageData, IStorageDescriptor descriptor) { return getStoragePath(storageData).resolve(descriptor.getChannelId() + StorageFileType.DATA_FILE.getExtension()); }
/** * {@inheritDoc} * <p> * Method returns combined results from all branches that are combined. */ @Override public List<IStorageDescriptor> query(IIndexQuery query) { List<IStorageDescriptor> combinedResult = new ArrayList<>(); for (IStorageTreeComponent<E> branch : branches) { List<IStorageDescriptor> branchResult = branch.query(query); if ((branchResult != null) && !branchResult.isEmpty()) { combinedResult.addAll(branchResult); } } return combinedResult; }
@Test(expectedExceptions = { IndexingException.class }) public void noDescriptorFromTree() throws IndexingException { DefaultData defaultData = mock(DefaultData.class); when(indexingTree.put(defaultData)).thenReturn(null); indexingTreeHandler.startWrite(storageWriter.new WriteTask(defaultData, null)); }