@Override public ValidationResult validate(String subject, String uri, ValidationContext context) { String message = "not set"; boolean isValid = true; if (uri.trim().isEmpty()) { isValid = false; } else { final boolean elPresent = context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(uri); if (!elPresent) { try { new URIBuilder(URI.create(uri)).build(); } catch (RuntimeException e) { message = e.getMessage(); isValid = false; } } } return new ValidationResult.Builder() .subject(subject) .input(uri) .explanation("Dataset URI is invalid: " + message) .valid(isValid) .build(); } };
@Override public URI getUri() { URIBuilder builder = new URIBuilder(dataset.getUri()); for (Map.Entry<String, String> entry : constraints.toQueryMap().entrySet()) { builder.with(entry.getKey(), entry.getValue()); } return builder.build(); }
@Test public void testEmptyConstraints() { Assert.assertEquals("Empty constraints should produce dataset URI", URI.create("dataset:file:/datasets/test"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty) .build()); } }
/** * Builds a dataset URI from the given repository URI string, namespace, and * dataset name. * * @param repoUri a repository URI string * @param namespace a String namespace * @param dataset a String dataset name * @return a dataset URI for the namespace and dataset name in the repository * * @since 0.17.0 */ public static URI build(String repoUri, String namespace, String dataset) { return build(URI.create(repoUri), namespace, dataset); }
@Override public void run() { new URIBuilder("repo:file:/datasets", "ns", null).build();
@Override public void run() { Datasets.list(new URIBuilder(repoUri, "ns", "test") .with("field", 34) .build()); } });
@Test public void testAddExistsConstraints() { Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?id="), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.with("id")) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?id=×tamp="), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.with("id").with("timestamp")) .build()); }
@Test public void signalReadyOnUnboundedDataset() { final FileSystemDataset<Record> ds = new FileSystemDataset.Builder<Record>() .namespace("ns") .name("users") .configuration(getConfiguration()) .descriptor( new DatasetDescriptor.Builder().schema(USER_SCHEMA).format(format) .location(testDirectory).build()) .type(Record.class) .uri(URIBuilder.build(URI.create("repo:" + testDirectory.toUri()), "ns", "name")) .build(); Assert.assertFalse("Unbounded dataset has not been signaled", ds.isReady()); ds.signalReady(); Assert.assertTrue("Unbounded dataset has been signaled and should be ready", ds.isReady()); }
DEFAULT_NAMESPACE); this.datasetUri = new URIBuilder(repositoryURI, namespace, datasetName) .build();
@Test public void testBuildViewUri() { URI uri = new URIBuilder("repo:file:/tmp/data", "ns", "test-ds") .with("username", "bob").with("lastName", "Smith").build(); Assert.assertEquals(URI.create("view:file:/tmp/data/ns/test-ds?username=bob&lastName=Smith"), uri); }
@Test public void testAddRangeConstraints() { Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?color=[green,)"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.from("color", "green")) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?color=(,green]"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.to("color", "green")) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?timestamp=[0,1405720705333)&color=(green,red]"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty .from("timestamp", 0l).toBefore("timestamp", 1405720705333L) .fromAfter("color", "green").to("color", "red")) .build()); }
@Test public void testReadySignalUpdatesModifiedTime() { final FileSystemDataset<Record> ds = new FileSystemDataset.Builder<Record>() .namespace("ns") .name("users") .configuration(getConfiguration()) .descriptor( new DatasetDescriptor.Builder().schema(USER_SCHEMA).format(format) .location(testDirectory).build()) .type(Record.class) .uri(URIBuilder.build(URI.create("repo:" + testDirectory.toUri()), "ns", "name")) .build(); Assert.assertFalse("Dataset should not be ready before being signaled", ds.isReady()); // the modified time depends on the filesystem, and may only be granular to the second // signal and check until the modified time is after the current time, or until // enough time has past that the signal should have been distinguishable long signaledTime = 0; long currentTime = System.currentTimeMillis(); while(currentTime >= signaledTime && (System.currentTimeMillis() - currentTime) <= 2000) { ds.signalReady(); signaledTime = ds.getLastModified(); } Assert.assertTrue("Dataset should have been signaled as ready", ds.isReady()); Assert.assertTrue("Signal should update the modified time", signaledTime > currentTime); Assert.assertFalse("Only the dataset should have been signaled", ((Signalable)ds.with("username", "bob")).isReady()); }
/** * Builds a dataset URI from the given repository URI, namespace, and dataset * name. * * @param repoUri a repository URI * @param namespace a String namespace * @param dataset a String dataset name * @return a dataset URI for the namespace and dataset name in the repository * * @since 0.17.0 */ public static URI build(URI repoUri, String namespace, String dataset) { return new URIBuilder(repoUri, namespace, dataset).build(); }
@Test public void testRepoUriHasQueryString() { URI uri = new URIBuilder("repo:file:/tmp/data?repoParam=x", "ns", "test-ds") .with("username", "bob") .build(); Assert.assertEquals(URI.create("view:file:/tmp/data/ns/test-ds?repoParam=x&username=bob"), uri); } }
@Test public void testAddEqualityConstraints() { Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?id=" + ID), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.with("id", new Utf8(ID))) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?id=a,b"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.with("id", new Utf8("a"), new Utf8("b"))) .build()); Assert.assertEquals("Should add equality constraints", URI.create("view:file:/datasets/test?id=" + ID + "×tamp=1405720705333"), new URIBuilder("dataset:file:/datasets/test") .constraints( empty.with("id", new Utf8(ID)).with("timestamp", 1405720705333L)) .build()); Assert.assertEquals("Should add encoded equality constraints", URI.create("view:file:/datasets/test?id=a%2Fb"), new URIBuilder("dataset:file:/datasets/test") .constraints(empty.with("id", new Utf8("a/b"))) .build()); }
@Override public void run() { new URIBuilder("dataset:file:/datasets/test-name", "ns", "test-name-2") .build(); } });
@Test public void testDatasetUriAddEquals() { Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value"), new URIBuilder("dataset:file:/datasets/test-name") .with("prop", "value") .build()); Assert.assertEquals("Should produce an equivalent dataset URI", URI.create("view:file:/datasets/test-name?prop=value&num=34"), new URIBuilder("dataset:file:/datasets/test-name") .with("prop", "value") .with("num", 34) .build()); }
@Override public void run() { new URIBuilder((URI) null).build(); } });
@Test public void testRepoUriAndNameAddEquals() { Assert.assertEquals("Should construct the correct dataset URI", URI.create("view:file:/datasets/ns/test-name?prop=value"), new URIBuilder("repo:file:/datasets", "ns", "test-name") .with("prop", "value") .build()); // order should be preserved Assert.assertEquals("Should construct the correct dataset URI", URI.create("view:file:/datasets/ns/test-name?prop=value&num=34"), new URIBuilder("repo:file:/datasets", "ns", "test-name") .with("prop", "value") .with("num", 34) .build()); }
@Override public void run() { new URIBuilder("view:file:/datasets/test-name?n=34", "ns", "test-name-2") .build(); } });