MetadataChange change = mds.apply(new Drop(entity)); Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, Metadata.EMPTY), change); verifyMetadata(mds, entity, Metadata.EMPTY); change = mds.apply(new Drop(entity)); Assert.assertEquals(new MetadataChange(entity, metadata, Metadata.EMPTY), change); verifyMetadata(mds, entity, Metadata.EMPTY); change = mds.apply(new Drop(entity)); Assert.assertEquals(new MetadataChange(entity, Metadata.EMPTY, Metadata.EMPTY), change); verifyMetadata(mds, entity, Metadata.EMPTY);
private MetadataChange apply(MetadataDatasetContext context, MetadataMutation mutation) { switch (mutation.getType()) { case CREATE: MetadataMutation.Create create = (MetadataMutation.Create) mutation; return create(context, create.getEntity(), create.getMetadata(), create.getDirectives()); case DROP: MetadataMutation.Drop drop = (MetadataMutation.Drop) mutation; return drop(context, drop.getEntity()); case UPDATE: MetadataMutation.Update update = (MetadataMutation.Update) mutation; return update(context, update.getEntity(), update.getUpdates()); case REMOVE: MetadataMutation.Remove remove = (MetadataMutation.Remove) mutation; return remove(context, remove); default: throw new IllegalStateException( String.format("Unknown MetadataMutation type %s for %s", mutation.getType(), mutation.getEntity())); } }
mds.apply(new Drop(entity));
@Test public void testSearchOnTagsUpdate() throws IOException { MetadataStorage mds = getMetadataStorage(); MetadataEntity entity = NamespaceId.DEFAULT.app("appX").workflow("wtf").toMetadataEntity(); Metadata meta = new Metadata(SYSTEM, tags("tag1", "tag2")); mds.apply(new Update(entity, meta)); Assert.assertEquals(meta.getTags(SYSTEM), mds.read(new Read(entity, SYSTEM)).getTags(SYSTEM)); assertResults(mds, SearchRequest.of("tag1").build(), new MetadataRecord(entity, meta)); // add an more tags mds.apply(new Update(entity, new Metadata(SYSTEM, tags("tag3", "tag4")))); Set<String> newTags = tags("tag1", "tag2", "tag3", "tag4"); Metadata newMeta = new Metadata(SYSTEM, newTags); Assert.assertEquals(newTags, mds.read(new Read(entity, SYSTEM)).getTags(SYSTEM)); for (String expectedTag : newTags) { assertResults(mds, SearchRequest.of(expectedTag).build(), new MetadataRecord(entity, newMeta)); } // add an empty set of tags. This should have no effect on retrieval or search of tags mds.apply(new Update(entity, new Metadata(SYSTEM, tags()))); Assert.assertEquals(newTags, mds.read(new Read(entity, SYSTEM)).getTags(SYSTEM)); for (String expectedTag : newTags) { assertResults(mds, SearchRequest.of(expectedTag).build(), new MetadataRecord(entity, newMeta)); } // clean up mds.apply(new Drop(entity)); }
@Test public void testSearchDifferentNamespaces() throws IOException { MetadataStorage mds = getMetadataStorage(); final String ns1 = "ns1"; final NamespaceId ns1Id = new NamespaceId(ns1); final MetadataEntity artifact = ns1Id.artifact("artifact", "1.0").toMetadataEntity(); final MetadataEntity sysArtifact = NamespaceId.SYSTEM.artifact("artifact", "1.0").toMetadataEntity(); final String multiWordKey = "multiword"; final String multiWordValue = "aV1 av2 , - , av3 - av4_av5 av6"; Metadata meta = new Metadata(SYSTEM, props(multiWordKey, multiWordValue)); MetadataRecord artifactRecord = new MetadataRecord(artifact, meta); MetadataRecord sysArtifactRecord = new MetadataRecord(sysArtifact, meta); mds.apply(new Update(artifact, meta)); mds.apply(new Update(sysArtifact, meta)); // searching only user namespace should not return system entity assertResults(mds, SearchRequest.of("aV5").addNamespace(ns1).build(), artifactRecord); // searching only user namespace and system should return only the system entity assertResults(mds, SearchRequest.of("aV5").addSystemNamespace().build(), sysArtifactRecord); // searching only user namespace and system should return both entities assertResults(mds, SearchRequest.of("aV5").addNamespace(ns1).addSystemNamespace().build(), artifactRecord, sysArtifactRecord); // clean up mds.batch(ImmutableList.of(new Drop(artifact), new Drop(sysArtifact))); }
@Test public void testUpdateSearch() throws IOException { MetadataStorage mds = getMetadataStorage(); final String ns = "ns"; final MetadataEntity program = new NamespaceId(ns).app("app1").worker("wk1").toMetadataEntity(); Metadata meta = new Metadata(USER, tags("tag1", "tag2"), props("key1", "value1", "key2", "value2")); MetadataRecord programRecord = new MetadataRecord(program, meta); mds.apply(new Update(program, meta)); assertResults(mds, SearchRequest.of("value1").addNamespace(ns).build(), programRecord); assertResults(mds, SearchRequest.of("value2").addNamespace(ns).build(), programRecord); assertResults(mds, SearchRequest.of("tag2").addNamespace(ns).build(), programRecord); mds.apply(new Update(program, new Metadata(USER, props("key1", "value3")))); mds.apply(new Remove(program, ImmutableSet.of(new ScopedNameOfKind(PROPERTY, USER, "key2"), new ScopedNameOfKind(TAG, USER, "tag2")))); programRecord = new MetadataRecord(program, new Metadata(USER, tags("tag1"), props("key1", "value3"))); // Searching for value1 should be empty assertEmpty(mds, SearchRequest.of("value1").addNamespace(ns).build()); // Instead key1 has value value3 now assertResults(mds, SearchRequest.of("value3").addNamespace(ns).build(), programRecord); // key2 and tag2 were deleted assertEmpty(mds, SearchRequest.of("value2").addNamespace(ns).build()); assertEmpty(mds, SearchRequest.of("tag2").addNamespace(ns).build()); // tag1 is still here assertResults(mds, SearchRequest.of("tag1").addNamespace(ns).build(), programRecord); // clean up mds.apply(new Drop(program)); }
@Test public void testSearchOnTypes() throws Exception { MetadataStorage mds = getMetadataStorage(); MetadataEntity myDs = NamespaceId.DEFAULT.dataset("myDs").toMetadataEntity(); MetadataEntity myField1 = MetadataEntity.builder(myDs).appendAsType("field", "myField1").build(); MetadataEntity myField2 = MetadataEntity.builder(myDs).appendAsType("field", "myField2").build(); MetadataRecord record1 = new MetadataRecord(myField1, new Metadata(USER, props("testKey1", "testValue1"))); MetadataRecord record2 = new MetadataRecord(myField2, new Metadata(USER, props("testKey2", "testValue2"))); mds.apply(new Update(myField1, record1.getMetadata())); mds.apply(new Update(myField2, record2.getMetadata())); // Search for it based on value assertResults(mds, SearchRequest.of("field:myField1").build(), record1); // should return both fields assertResults(mds, SearchRequest.of("field:myFie*").build(), record1, record2); assertResults(mds, SearchRequest.of("field*").build(), record1, record2); // clean up mds.batch(ImmutableList.of(new Drop(myField1), new Drop(myField2))); }
@Test public void testCrossNamespaceDefaultSearch() throws IOException { MetadataStorage mds = getMetadataStorage(); NamespaceId ns1 = new NamespaceId("ns1"); NamespaceId ns2 = new NamespaceId("ns2"); MetadataEntity ns1app = ns1.app("a").toMetadataEntity(); MetadataEntity ns2app = ns2.app("a").toMetadataEntity(); MetadataRecord app1Record = new MetadataRecord(ns1app, new Metadata(USER, props("k1", "v1", "k2", "v2"))); MetadataRecord app2Record = new MetadataRecord(ns2app, new Metadata(USER, props("k1", "v1"))); mds.apply(new Update(ns1app, app1Record.getMetadata())); mds.apply(new Update(ns2app, app2Record.getMetadata())); assertResults(mds, SearchRequest.of("v1").build(), app1Record, app2Record); assertResults(mds, SearchRequest.of("v2").build(), app1Record); assertResults(mds, SearchRequest.of("*").build(), app1Record, app2Record); // clean up mds.batch(ImmutableList.of(new Drop(ns1app), new Drop(ns2app))); }
@Test public void testCrossNamespaceCustomSearch() throws Exception { MetadataStorage mds = getMetadataStorage(); String appName = "app"; MetadataEntity ns1App = new NamespaceId("ns1").app(appName).toMetadataEntity(); MetadataEntity ns2App = new NamespaceId("ns2").app(appName).toMetadataEntity(); Metadata meta = new Metadata(SYSTEM, props(ENTITY_NAME_KEY, appName)); MetadataRecord app1Record = new MetadataRecord(ns1App, meta); MetadataRecord app2Record = new MetadataRecord(ns2App, meta); mds.apply(new Update(ns1App, meta)); mds.apply(new Update(ns2App, meta)); assertInOrder(mds, SearchRequest.of("*").setSorting(new Sorting(ENTITY_NAME_KEY, Sorting.Order.ASC)).build(), app1Record, app2Record); // clean up mds.batch(ImmutableList.of(new Drop(ns1App), new Drop(ns2App))); }
@Override public void removeMetadata(MetadataEntity entity) { applyMutation(new MetadataMutation.Drop(entity)); }
@Override public String toString() { return "Drop{" + "entity=" + getEntity() + '}'; } }