/** same as {@code newConfig(0, 0, 0, tuplePredicate)} * * @param <T> Tuple type * @param tuplePredicate cycle if {@code tuplePredicate} returns true. null to disable. * @return the cycle configuration */ public static <T> FileWriterCycleConfig<T> newPredicateBasedConfig(Predicate<T> tuplePredicate) { return newConfig(0, 0, 0, tuplePredicate); }
/** same as {@code newConfig(0, 0, periodMsec, null)} * * @param <T> Tuple type * @param periodMsec cycle after {@code periodMsec} has elapsed since the last time based cycle. 0 to disable. * @return the cycle configuration */ public static <T> FileWriterCycleConfig<T> newTimeBasedConfig(long periodMsec) { if (periodMsec < 1) throw new IllegalArgumentException("periodMsec"); return newConfig(0, 0, periodMsec, null); } /** same as {@code newConfig(0, 0, 0, tuplePredicate)}
/** same as {@code newConfig(fileSize, 0, 0, null)} * * @param <T> Tuple type * @param fileSize cycle after {@code fileSize} bytes have been written. 0 to disable. * @return the cycle configuration */ public static <T> FileWriterCycleConfig<T> newFileSizeBasedConfig(long fileSize) { if (fileSize < 1) throw new IllegalArgumentException("fileSize"); return newConfig(fileSize, 0, 0, null); } /** same as {@code newConfig(0, cntTuples, 0, null)}
/** same as {@code newConfig(0, cntTuples, 0, null)} * * @param <T> Tuple type * @param cntTuples cycle after every {@code cntTuple} tuples have been written. 0 to disable. * @return the cycle configuration */ public static <T> FileWriterCycleConfig<T> newCountBasedConfig(int cntTuples) { if (cntTuples < 1) throw new IllegalArgumentException("cntTuples"); return newConfig(0, cntTuples, 0, null); } /** same as {@code newConfig(0, 0, periodMsec, null)}
expectIAE(() -> FileWriterCycleConfig.newPredicateBasedConfig(null)); cfg = FileWriterCycleConfig.newConfig(1, 2, 3, p); checkFileWriterConfig(cfg, 1, 2, 3, p, trueTuple, falseTuple); expectIAE(() -> FileWriterCycleConfig.newConfig(0, 0, 0, null)); expectIAE(() -> FileWriterCycleConfig.newConfig(-1, 0, 0, null)); expectIAE(() -> FileWriterCycleConfig.newConfig(0, -1, 0, null)); expectIAE(() -> FileWriterCycleConfig.newConfig(0, 0, -1, null));
@Test public void testAllTimeBased() throws Exception { // exercise case with multiple timer based policies Topology t = newTopology("testAllTimeBased"); // establish a base path Path basePath = createTempFile("test1", "txt", new String[0]); String[] lines = getLines(); // build expected results // keep all given age and TMO_SEC int ageSec = 10; long periodMsec = TimeUnit.SECONDS.toMillis(1); // net one tuple per file List<List<String>> expResults = buildExpResults(lines, tuple -> true); TStream<String> s = t.strings(lines); IFileWriterPolicy<String> policy = new FileWriterPolicy<String>( FileWriterFlushConfig.newTimeBasedConfig(TimeUnit.MILLISECONDS.toMillis(250)), FileWriterCycleConfig.newConfig(1, 2000, TimeUnit.SECONDS.toMillis(10), null), FileWriterRetentionConfig.newAgeBasedConfig(ageSec, periodMsec) ); FileStreams.textFileWriter(s, () -> basePath.toString(), () -> policy); completeAndValidateWriter(t, TMO_SEC, basePath, expResults); }