/** * Create a new feedbackstore with the specified storeId and fileNumber. * * @param storeId * the storeId * @param fileNumber * the new sequence number for this storeID * @return a feedbackstore */ private FeedbackStore createStore(long storeId, int fileNumber) throws IOException { File storeFile = new File(m_baseDir, getStoreName(storeId, fileNumber)); m_storeFileIdx.get(storeId).add(Integer.valueOf(fileNumber)); return new FeedbackStore(storeFile, storeId); }
/** * Create a new empty feedbackstore with a new storeId. * * @return A new feedbackstore with a new storeID */ private FeedbackStore newFeedbackStore() throws IOException { long storeId = System.currentTimeMillis(); String storeFilename; File storeFile; do { storeFilename = getStoreName(storeId, 1); storeFile = new File(m_baseDir, storeFilename); if (storeFile.createNewFile()) { break; } storeId++; } while (true); m_storeFileIdx.put(storeId, new TreeSet<>(Arrays.asList(1))); return new FeedbackStore(storeFile, storeId); }
@Test public void testTimedWrite() throws Exception { File storeFile = File.createTempFile("feedback", ".store"); storeFile.deleteOnExit(); final int recordCount = 1000000; final FeedbackStore store = new FeedbackStore(storeFile, 1); long start = System.nanoTime(); for (int i = 0; i < recordCount; i++) { store.append(i, "Hello World!".getBytes()); } long end = System.nanoTime(); System.out.printf("Writing %d records took %.3f ms.%n", recordCount, (end - start) / 1.0e6); }
/** * Tests that concurrent use of a {@link FeedbackStore} with a single reader and writer works as expected. */ @Test public void testConcurrentUseSingleReaderAndSingleWriter() throws Exception { File storeFile = File.createTempFile("feedback", ".store"); storeFile.deleteOnExit(); final int recordCount = 10000; final FeedbackStore store = new FeedbackStore(storeFile, 1); final CountDownLatch start = new CountDownLatch(1); final CountDownLatch stop = new CountDownLatch(2); Writer writer = new Writer(store, start, stop, recordCount); Reader reader = new Reader(store, start, stop, recordCount); // gents, start your engines... writer.start(); reader.start(); // 3, 2, 1... GO... start.countDown(); // waiting both threads to finish... assertTrue(stop.await(15, TimeUnit.SECONDS)); writer.join(); reader.join(); int writeCount = writer.m_written.size(); int readCount = reader.m_seen.size(); assertEquals(recordCount, writeCount, "Not all records were written?"); assertEquals(readCount, writeCount, "Not all records were seen?"); verifyStoreContents(store, recordCount, writer); }
final int writerCount = Runtime.getRuntime().availableProcessors() + 1; final FeedbackStore store = new FeedbackStore(storeFile, 1);
final int writerCount = Runtime.getRuntime().availableProcessors() + 1; final FeedbackStore store = new FeedbackStore(storeFile, 1);