public static BagMeta fromEntity(Entity entity) throws JasDBStorageException { String instance = entity.getValue(Constants.INSTANCE).toString(); String name = entity.getValue(Constants.NAME).toString(); List<IndexDefinition> indexDefinitionList = new ArrayList<>(); if(entity.hasProperty(Constants.INDEXES)) { for(Object indexDefinition : entity.getValues(Constants.INDEXES)) { indexDefinitionList.add(IndexDefinition.fromHeader(indexDefinition.toString())); } } return new BagMeta(instance, name, indexDefinitionList); }
private void loadRecords() throws JasDBStorageException { for(RecordIteratorImpl recordIterator = writer.readAllRecords(); recordIterator.hasNext(); ) { RecordResultImpl recordResult = recordIterator.next(); Entity entity = SimpleEntity.fromStream(recordResult.getStream()); String type = entity.getValue(Constants.META_TYPE).toString(); if(type.equals(Constants.INSTANCE_TYPE)) { InstanceMeta instance = InstanceMeta.fromEntity(entity); instanceMetaMap.put(instance.getInstanceId(), new MetaWrapper<>(instance, recordResult.getRecordPointer())); } else if(type.equals(Constants.BAG_TYPE)) { BagMeta bagMeta = BagMeta.fromEntity(entity); String bagId = getBagKey(bagMeta.getInstanceId(), bagMeta.getName()); bagMetaMap.put(bagId, new MetaWrapper<>(bagMeta, recordResult.getRecordPointer())); } else if(metadataProviders.containsKey(type)) { metadataProviders.get(type).registerMetadataEntity(entity, recordResult.getRecordPointer()); } else { throw new JasDBStorageException("Unable to load metadata record: " + entity.toString() + " unknown type: " + type); } } }
/** * Small helper method to update the bag definition * @param instanceId The instanceId * @param bagName The name of the bag * @param newBagData The new updated bag data * @throws JasDBStorageException If unable to update the bag */ private void updateBag(String instanceId, String bagName, Bag newBagData) throws JasDBStorageException { String bagId = getBagKey(instanceId, bagName); MetaWrapper<Bag> bagMetaWrapper = bagMetaMap.get(bagId); long recordPointer = writer.updateRecord(SimpleEntity.toJson(BagMeta.toEntity(newBagData)), () -> of(bagMetaWrapper.getRecordPointer()), null); bagMetaWrapper.setRecordPointer(recordPointer); bagMetaWrapper.setMetadataObject(newBagData); }
@Override public void addBag(Bag bag) throws JasDBStorageException { if(instanceMetaMap.containsKey(bag.getInstanceId())) { String bagId = getBagKey(bag.getInstanceId(), bag.getName()); if(!bagMetaMap.containsKey(bagId)) { SimpleEntity entity = BagMeta.toEntity(bag); String bagData = SimpleEntity.toJson(entity); long recordPointer = writer.writeRecord(bagData, null); bagMetaMap.put(bagId, new MetaWrapper<>(bag, recordPointer)); } else { throw new JasDBStorageException("Unable to add bag: " + bag.getName() + ", already exists"); } } else { throw new JasDBStorageException("Unable to create bag, instance: " + bag.getInstanceId() + " does not exist"); } }
@Override public void addBagIndex(String instanceId, String bagName, IndexDefinition indexDefinition) throws JasDBStorageException { Bag bag = getBag(instanceId, bagName); if(bag != null) { List<IndexDefinition> indexDefinitions = new ArrayList<>(bag.getIndexDefinitions()); if(!indexDefinitions.contains(indexDefinition)) { indexDefinitions.add(indexDefinition); updateBag(instanceId, bagName, new BagMeta(instanceId, bagName, indexDefinitions)); } } else { throw new JasDBStorageException("Unable to add index to bag: " + bagName + ",could not be found"); } }
@Override public void removeBagIndex(String instanceId, String bagName, IndexDefinition indexDefinition) throws JasDBStorageException { Bag bag = getBag(instanceId, bagName); if(bag != null) { List<IndexDefinition> indexDefinitions = new ArrayList<>(bag.getIndexDefinitions()); if(indexDefinitions.contains(indexDefinition)) { indexDefinitions.remove(indexDefinition); updateBag(instanceId, bagName, new BagMeta(instanceId, bagName, indexDefinitions)); } } else { throw new JasDBStorageException("Unable to remove index for bag: " + bagName + ",could not be found"); } }
private synchronized StorageService createStorageService(String instanceId, String bagName, String key) throws JasDBStorageException { //now we have a lock let's check again if(storageServices.containsKey(key)) { return storageServices.get(key); } else { if(metadataStore.containsInstance(instanceId)) { if(!metadataStore.containsBag(instanceId, bagName)) { metadataStore.addBag(new BagMeta(instanceId, bagName, new ArrayList<IndexDefinition>())); } Instance instance = metadataStore.getInstance(instanceId); StorageService serviceInstance = createStorageServiceInstance(instance, bagName); serviceInstance.openService(configurationLoader.getConfiguration()); storageServices.put(key, serviceInstance); //wrappedInstance); return serviceInstance; } else { throw new JasDBStorageException("Unable to create bag storage service, instance: " + instanceId + " does not exist"); } } }
@Override public EntityBag createOrGetBag(String instanceId, String bagName) throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); Bag meta = connector.getBag(getContext(), instanceId, bagName); if(meta == null) { meta = connector.createBag(getContext(), instanceId, new BagMeta(instanceId, bagName, new ArrayList<IndexDefinition>())); } return new RemoteEntityBag(instanceId, getContext(), nodeInformation, meta); }