/** * {@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; }
/** * {@inheritDoc} */ @Override public IStorageDescriptor get(E element) { for (IStorageTreeComponent<E> branch : branches) { IStorageDescriptor descriptor = branch.get(element); if (descriptor != null) { return descriptor; } } return null; }
/** * 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); }
/** * Test tree with empty query. All elements should be returned. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test public void emptyQueryTest() throws IndexingException { IStorageTreeComponent<DefaultData> rootBranch = new StorageBranch<>(new StorageBranchIndexer<>(new ObjectTypeIndexer<>(), false)); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); IStorageDescriptor storageDescriptor1 = rootBranch.put(defaultData); storageDescriptor1.setPositionAndSize(0, 1); SqlStatementData defaultData2 = mock(SqlStatementData.class); when(defaultData2.getId()).thenReturn(2L); IStorageDescriptor storageDescriptor2 = rootBranch.put(defaultData2); storageDescriptor2.setPositionAndSize(2, 1); List<IStorageDescriptor> results = rootBranch.query(storageIndexQuery); assertThat(results.size(), is(equalTo(2))); // Test for forkJoinPool List<IStorageDescriptor> resultsForkJoin = rootBranch.query(storageIndexQuery, forkJoinPool); assertThat(resultsForkJoin.size(), is(equalTo(2))); }
/** * Test that get will work even when branch can not generate key for the element if ID is * correctly set. * * @throws IndexingException */ @Test public void getWithNoKey() throws IndexingException { IStorageTreeComponent<DefaultData> rootBranch = new StorageBranch<>(new StorageBranchIndexer<>(new TimestampIndexer<>(), false)); InvocationSequenceData invocation = mock(InvocationSequenceData.class); when(invocation.getId()).thenReturn(1L); when(invocation.getTimeStamp()).thenReturn(new Timestamp(new Date().getTime())); IStorageDescriptor storageDescriptor = rootBranch.put(invocation); when(invocation.getTimeStamp()).thenReturn(null); // test get assertThat(rootBranch.get(invocation), is(equalTo(storageDescriptor))); // then get and remove assertThat(rootBranch.getAndRemove(invocation), is(equalTo(storageDescriptor))); // confirm it is removed assertThat(rootBranch.get(invocation), is(nullValue())); }
/** * Test that the get on the {@link LeafWithNoDescriptors} will throw an exception if invoked. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test(expectedExceptions = { UnsupportedOperationException.class }) public void putAndGetElement() throws IndexingException { IStorageTreeComponent<DefaultData> rootBranch = new LeafWithNoDescriptors<>(); DefaultData defaultData = mock(DefaultData.class); when(defaultData.getId()).thenReturn(1L); rootBranch.put(defaultData); rootBranch.get(defaultData); }
/** * 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); } } }
final IStorageTreeComponent<DefaultData> currentIndexingTree = storageIndexingTreeReference.get(); if (null != currentIndexingTree) { long treeSize = currentIndexingTree.getComponentSize(objectSizes);
/** * {@inheritDoc} */ @Override public void preWriteFinalization() { for (IStorageTreeComponent<E> branch : branches) { branch.preWriteFinalization(); } }
IStorageDescriptor storageDescriptor1 = rootBranch.put(defaultData1); storageDescriptor1.setPositionAndSize(0L, 100L); IStorageDescriptor storageDescriptor2 = rootBranch.put(defaultData2); storageDescriptor2.setPositionAndSize(0L, 200L); storageIndexQuery.setObjectClasses(searchedClasses); List<IStorageDescriptor> results = rootBranch.query(storageIndexQuery); assertThat(results.size(), is(equalTo(1))); for (IStorageDescriptor result : results) { List<IStorageDescriptor> resultsForkJoin = rootBranch.query(storageIndexQuery, forkJoinPool); assertThat(resultsForkJoin.size(), is(equalTo(1))); for (IStorageDescriptor result : resultsForkJoin) {
/** * Test that putting one element will return the same {@link IStorageDescriptor} as when get is * executed.. * * @throws IndexingException * If {@link IndexingException} occurs. */ @Test public void putAndGetInvocation() throws IndexingException { IStorageTreeComponent<DefaultData> rootBranch = new StorageBranch<>(new StorageBranchIndexer<>(new ObjectTypeIndexer<>(), false)); InvocationSequenceData invocationSequenceData = new InvocationSequenceData(); invocationSequenceData.setId(1L); IStorageDescriptor storageDescriptor = rootBranch.put(invocationSequenceData); assertThat(storageDescriptor, comparesEqualTo(rootBranch.get(invocationSequenceData))); }
@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)); }
/** * {@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); }
/** * {@inheritDoc} */ @Override public void preWriteFinalization() { for (ITreeComponent<IStorageDescriptor, E> storageTreeComponent : getComponentMap().values()) { if (storageTreeComponent instanceof IStorageTreeComponent) { ((IStorageTreeComponent<E>) storageTreeComponent).preWriteFinalization(); } } }
when(defaultData1.getId()).thenReturn(1L); when(defaultData1.getTimeStamp()).thenReturn(new Timestamp(new Date().getTime())); IStorageDescriptor storageDescriptor1 = rootBranch.put(defaultData1); storageDescriptor1.setPositionAndSize(0L, 100L); when(defaultData2.getId()).thenReturn(2L); when(defaultData2.getTimeStamp()).thenReturn(plusHour); IStorageDescriptor storageDescriptor2 = rootBranch.put(defaultData2); storageDescriptor2.setPositionAndSize(0L, 200L); storageIndexQuery.setToDate(plusHour); List<IStorageDescriptor> results = rootBranch.query(storageIndexQuery); assertThat(results.size(), is(equalTo(1))); for (IStorageDescriptor result : results) { List<IStorageDescriptor> resultsForkJoin = rootBranch.query(storageIndexQuery, forkJoinPool); assertThat(resultsForkJoin.size(), is(equalTo(1))); for (IStorageDescriptor result : resultsForkJoin) {
/** * {@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 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); }
@Test public void indexingTreeSavingTask() { reset(executorService); indexingTreeHandler.maximumIndexingTreeSize = 10L; when(indexingTree.getComponentSize(objectSizes)).thenReturn(5L); indexingTreeHandler.new IndexingTreeSavingTask().run(); verifyZeroInteractions(executorService); when(indexingTree.getComponentSize(objectSizes)).thenReturn(15L); indexingTreeHandler.new IndexingTreeSavingTask().run(); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(executorService, times(1)).submit(captor.capture()); when(storageWriter.writeNonDefaultDataObject(eq(indexingTree), anyString())).thenReturn(true); captor.getValue().run(); verify(storageWriter, times(1)).writeNonDefaultDataObject(eq(indexingTree), anyString()); } }
@Override public void run() { boolean safeToSave = Collections.disjoint(writeTasksToWait, writeTasksInProcess.keySet()); if (safeToSave) { currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } else { executorService.schedule(this, WAITING_FOR_TREE_TO_BE_READY, TimeUnit.MILLISECONDS); } } };
when(defaultData1.getId()).thenReturn(1L); when(defaultData1.getPlatformIdent()).thenReturn(10L); IStorageDescriptor storageDescriptor1 = rootBranch.put(defaultData1); storageDescriptor1.setPositionAndSize(0L, 100L); when(defaultData2.getId()).thenReturn(2L); when(defaultData2.getPlatformIdent()).thenReturn(20L); IStorageDescriptor storageDescriptor2 = rootBranch.put(defaultData2); storageDescriptor2.setPositionAndSize(0L, 200L); List<IStorageDescriptor> results = rootBranch.query(storageIndexQuery); assertThat(results.size(), is(equalTo(1))); for (IStorageDescriptor result : results) { List<IStorageDescriptor> resultsForkJoin = rootBranch.query(storageIndexQuery, forkJoinPool); assertThat(resultsForkJoin.size(), is(equalTo(1))); for (IStorageDescriptor result : resultsForkJoin) {