public void invalidateSchemaBranch(SchemaBranchCache.Key key) { LOG.info("Invalidating cache entry for key [{}]", key); // If the cache doesn't have entry for the key, then no need to invalidate the cache if(loadingCache.getIfPresent(key) == null) return; loadingCache.invalidate(key); Key otherKey = key.id == null ? Key.of(schemaBranchNameToIdMap.get(key.getSchemaBranchKey())) : Key.of(schemaBranchNameToIdMap.inverse().get(key.id)); loadingCache.invalidate(otherKey); }
public void invalidateSchemaBranch(SchemaBranchCache.Key key) { LOG.info("Invalidating cache entry for key [{}]", key); // If the cache doesn't have entry for the key, then no need to invalidate the cache if(loadingCache.getIfPresent(key) == null) return; loadingCache.invalidate(key); Key otherKey = key.id == null ? Key.of(schemaBranchNameToIdMap.get(key.getSchemaBranchKey())) : Key.of(schemaBranchNameToIdMap.inverse().get(key.id)); loadingCache.invalidate(otherKey); }
private void deleteSchemaVersionBranchMapping(Long schemaVersionId) throws SchemaNotFoundException, SchemaLifecycleException { List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList(); schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId .toString())); List<OrderByField> orderByFields = new ArrayList<>(); orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false)); Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields); if (storables == null || storables.isEmpty()) { LOG.debug("No need to delete schema version mapping as the database did a cascade delete"); return; } if (storables.size() > 1) { List<String> branchNamesTiedToSchema = storables.stream().map(storable -> schemaBranchCache.get(SchemaBranchCache.Key.of(storable.getSchemaBranchId())).getName()).collect(Collectors.toList()); throw new SchemaLifecycleException(String.format("Schema version with id : '%s' is tied with more than one branch : '%s' ", schemaVersionId.toString(), Arrays.toString(branchNamesTiedToSchema.toArray()))); } storageManager.remove(new StorableKey(SchemaBranchVersionMapping.NAMESPACE, storables.iterator() .next() .getPrimaryKey())); }
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(new SchemaBranchKey(schemaBranchName, schemaMetadata .getName()))); } catch (SchemaBranchNotFoundException e) {
SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId)); SchemaBranchCache.Key keyOfSchemaBranchToDelete = SchemaBranchCache.Key.of(schemaBranchId); schemaBranchCache.invalidateSchemaBranch(keyOfSchemaBranchToDelete); List<String> forkedBranchName = mappingsForSchemaTiedToMutlipleBranch.stream(). filter(mapping -> !mapping.getSchemaBranchId().equals(schemaBranchId)). map(mappping -> schemaBranchCache.get(SchemaBranchCache.Key.of(mappping.getSchemaBranchId())).getName()). collect(Collectors.toList()); schemaVersionTiedToOtherBranch.put(schemaVersionInfo.getVersion(), forkedBranchName);
return null; } else { SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(new SchemaBranchKey(schemaBranchName, schemaMetadataName)));
@Override public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException { Preconditions.checkNotNull(schemaBranch.getName(), "Schema branch name can't be null"); SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId)); SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranch.getName(), schemaVersionInfo.getName()); SchemaBranch existingSchemaBranch = null; try { existingSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { // Ignore this error } if (existingSchemaBranch != null) throw new SchemaBranchAlreadyExistsException(String.format("A schema branch with name : '%s' already exists", schemaBranch.getName())); SchemaBranchStorable schemaBranchStorable = SchemaBranchStorable.from(schemaBranch); schemaBranchStorable.setSchemaMetadataName(schemaVersionInfo.getName()); schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE)); storageManager.add(schemaBranchStorable); SchemaBranch persistedSchemaBranch; try { persistedSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(String.format("Failed to fetch persisted schema branch : '%s' from the database", schemaBranch.getName())); } SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(persistedSchemaBranch.getId(), schemaVersionInfo.getId()); storageManager.add(schemaBranchVersionMapping); return persistedSchemaBranch; }
private List<SchemaVersionInfo> getSortedSchemaVersions(Long schemaBranchId) throws SchemaNotFoundException, SchemaBranchNotFoundException { List<QueryParam> schemaVersionMappingStorableQueryParams = Lists.newArrayList(); schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranchId .toString())); List<OrderByField> orderByFields = new ArrayList<>(); orderByFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false)); List<SchemaVersionInfo> schemaVersionInfos = new ArrayList<>(); Collection<SchemaBranchVersionMapping> storables = storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams, orderByFields); if (storables == null || storables.size() == 0) { if (schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId)) .getName() .equals(SchemaBranch.MASTER_BRANCH)) return Collections.emptyList(); else throw new InvalidSchemaBranchVersionMapping(String.format("No schema versions are attached to the schema branch id : '%s'", schemaBranchId)); } for (SchemaBranchVersionMapping storable : storables) { SchemaIdVersion schemaIdVersion = new SchemaIdVersion(storable.getSchemaVersionInfoId()); schemaVersionInfos.add(schemaVersionInfoCache.getSchema(SchemaVersionInfoCache.Key.of(schemaIdVersion))); } return schemaVersionInfos; }
@Override public SchemaBranch load(Key key) throws Exception { SchemaBranch schemaBranch; Key otherKey; if (key.getSchemaBranchKey() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getSchemaBranchKey()); otherKey = Key.of(schemaBranch.getId()); schemaBranchNameToIdMap.put(key.getSchemaBranchKey(), schemaBranch.getId()); } else if (key.getId() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getId()); otherKey = Key.of(new SchemaBranchKey(schemaBranch.getName(), schemaBranch.getSchemaMetadataName())); schemaBranchNameToIdMap.put(otherKey.schemaBranchKey, schemaBranch.getId()); } else { throw new IllegalArgumentException("Given argument is not valid: " + key); } loadingCache.put(otherKey, schemaBranch); return schemaBranch; } });
@Override public SchemaBranch load(Key key) throws Exception { SchemaBranch schemaBranch; Key otherKey; if (key.getSchemaBranchKey() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getSchemaBranchKey()); otherKey = Key.of(schemaBranch.getId()); schemaBranchNameToIdMap.put(key.getSchemaBranchKey(), schemaBranch.getId()); } else if (key.getId() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getId()); otherKey = Key.of(new SchemaBranchKey(schemaBranch.getName(), schemaBranch.getSchemaMetadataName())); schemaBranchNameToIdMap.put(otherKey.schemaBranchKey, schemaBranch.getId()); } else { throw new IllegalArgumentException("Given argument is not valid: " + key); } loadingCache.put(otherKey, schemaBranch); return schemaBranch; } });
public Set<SchemaBranch> getSchemaBranches(Long schemaVersionId) throws SchemaBranchNotFoundException { List<QueryParam> schemaVersionMappingStorableQueryParams = new ArrayList<>(); Set<SchemaBranch> schemaBranches = new HashSet<>(); schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, schemaVersionId .toString())); for (Storable storable : storageManager.find(SchemaBranchVersionMapping.NAMESPACE, schemaVersionMappingStorableQueryParams)) { schemaBranches.add(schemaBranchCache.get(SchemaBranchCache.Key.of(((SchemaBranchVersionMapping) storable).getSchemaBranchId()))); } return schemaBranches; }
public Collection<SchemaVersionInfo> getAllVersions(final String schemaBranchName, final String schemaName) throws SchemaNotFoundException, SchemaBranchNotFoundException { Preconditions.checkNotNull(schemaBranchName, "Schema branch name can't be null"); Collection<SchemaVersionInfo> schemaVersionInfos; SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranchName, schemaName); schemaVersionInfos = Lists.reverse(getSortedSchemaVersions(schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)))); if (schemaVersionInfos == null || schemaVersionInfos.isEmpty()) schemaVersionInfos = Collections.emptyList(); return schemaVersionInfos; }