Search in sources :

Example 1 with SchemaVersionInfoCache

use of com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache in project registry by hortonworks.

the class SchemaVersionInfoCacheTest method testSchemaVersionCache.

@Test
public void testSchemaVersionCache() throws Exception {
    final Map<SchemaIdVersion, SchemaVersionInfo> schemaIdWithVersionInfo = new HashMap<>();
    final Map<SchemaVersionKey, SchemaVersionInfo> schemaKeyWithVersionInfo = new HashMap<>();
    long schemaMetadataId = 1L;
    String schemaName = "schema-1";
    Integer version = 2;
    long schemaVersionId = 3L;
    SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(schemaVersionId, schemaName, version, schemaMetadataId, "schema-text", System.currentTimeMillis(), "schema-description", SchemaVersionLifecycleStates.ENABLED.getId());
    SchemaIdVersion withVersionId = new SchemaIdVersion(schemaVersionId);
    SchemaIdVersion withMetaIdAndVersion = new SchemaIdVersion(schemaMetadataId, version);
    SchemaIdVersion withBoth = new SchemaIdVersion(schemaMetadataId, version, schemaVersionId);
    SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName, version);
    schemaKeyWithVersionInfo.put(schemaVersionKey, schemaVersionInfo);
    HashSet<SchemaIdVersion> allIdVersions = Sets.newHashSet(withVersionId, withMetaIdAndVersion, withBoth);
    allIdVersions.stream().forEach(x -> schemaIdWithVersionInfo.put(x, schemaVersionInfo));
    SchemaVersionInfo otherSchemaVersionInfo = new SchemaVersionInfo(schemaVersionId + 1, "other-" + schemaName, version, schemaMetadataId + 1L, "other-schema-text", System.currentTimeMillis(), "schema-description", SchemaVersionLifecycleStates.ENABLED.getId());
    SchemaIdVersion otherIdVersion = new SchemaIdVersion(otherSchemaVersionInfo.getId());
    SchemaVersionKey otherSchemaVersionKey = new SchemaVersionKey(otherSchemaVersionInfo.getName(), otherSchemaVersionInfo.getVersion());
    schemaIdWithVersionInfo.put(otherIdVersion, otherSchemaVersionInfo);
    schemaKeyWithVersionInfo.put(otherSchemaVersionKey, otherSchemaVersionInfo);
    SchemaVersionRetriever schemaRetriever = new SchemaVersionRetriever() {

        @Override
        public SchemaVersionInfo retrieveSchemaVersion(SchemaVersionKey key) throws SchemaNotFoundException {
            return schemaKeyWithVersionInfo.get(key);
        }

        @Override
        public SchemaVersionInfo retrieveSchemaVersion(SchemaIdVersion key) throws SchemaNotFoundException {
            return schemaIdWithVersionInfo.get(key);
        }
    };
    SchemaVersionInfoCache schemaVersionInfoCache = new SchemaVersionInfoCache(schemaRetriever, 32, 60 * 1000L);
    // invalidate key without accessing earlier.
    for (SchemaVersionKey versionKey : schemaKeyWithVersionInfo.keySet()) {
        schemaVersionInfoCache.invalidateSchema(SchemaVersionInfoCache.Key.of(versionKey));
    }
    for (SchemaIdVersion schemaIdVersion : schemaIdWithVersionInfo.keySet()) {
        schemaVersionInfoCache.invalidateSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion));
    }
    // access with all version id keys
    for (SchemaIdVersion key : allIdVersions) {
        SchemaVersionInfo recvdSchemaVersionInfo = schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(key));
        Assert.assertEquals(schemaVersionInfo, recvdSchemaVersionInfo);
    }
    // access with version key
    SchemaVersionInfo recvdSchemaVersionInfo = schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaVersionKey));
    Assert.assertEquals(schemaVersionInfo, recvdSchemaVersionInfo);
    // invalidate one kind of schemaIdVersion, and all other combinations should return null
    schemaVersionInfoCache.invalidateSchema(SchemaVersionInfoCache.Key.of(withVersionId));
    for (SchemaIdVersion idVersion : allIdVersions) {
        recvdSchemaVersionInfo = schemaVersionInfoCache.getSchemaIfPresent(SchemaVersionInfoCache.Key.of(idVersion));
        Assert.assertNull(recvdSchemaVersionInfo);
    }
    SchemaVersionInfo recvdOtherSchemaVersionInfo = schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(otherSchemaVersionKey));
    Assert.assertEquals(otherSchemaVersionInfo, recvdOtherSchemaVersionInfo);
    recvdOtherSchemaVersionInfo = schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(otherIdVersion));
    Assert.assertEquals(otherSchemaVersionInfo, recvdOtherSchemaVersionInfo);
    // all values for these keys should be non existent in cache
    for (SchemaIdVersion idVersion : allIdVersions) {
        Assert.assertNull(schemaVersionInfoCache.getSchemaIfPresent(SchemaVersionInfoCache.Key.of(idVersion)));
    }
    // all values for these keys should be loaded
    for (SchemaIdVersion idVersion : allIdVersions) {
        Assert.assertEquals(schemaVersionInfo, schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(idVersion)));
    }
    // all values for these keys should exist locally without loading from target
    for (SchemaIdVersion idVersion : allIdVersions) {
        Assert.assertEquals(schemaVersionInfo, schemaVersionInfoCache.getSchemaIfPresent(SchemaVersionInfoCache.Key.of(idVersion)));
    }
}
Also used : HashMap(java.util.HashMap) SchemaVersionInfoCache(com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache) Test(org.junit.Test)

Aggregations

SchemaVersionInfoCache (com.hortonworks.registries.schemaregistry.cache.SchemaVersionInfoCache)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1