@Override public String getName() throws JasDBStorageException { return meta.getName(); }
public static SimpleEntity toEntity(Bag bag) throws JasDBStorageException { SimpleEntity entity = new SimpleEntity(); entity.addProperty(Constants.META_TYPE, Constants.BAG_TYPE); entity.addProperty(Constants.INSTANCE, bag.getInstanceId()); entity.addProperty(Constants.NAME, bag.getName()); for(IndexDefinition indexDefinition : bag.getIndexDefinitions()) { entity.addProperty(Constants.INDEXES, indexDefinition.toHeader()); } return entity; }
@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 boolean containsIndex(String instanceId, String bagName, IndexDefinition indexDefinition) throws JasDBStorageException { Bag bag = getBag(instanceId, bagName); return bag.getIndexDefinitions().contains(indexDefinition); }
private synchronized void loadIndexes(final String bagName) throws JasDBStorageException { LOG.debug("Loading indexes for bag: {}", bagName); if(!indexes.containsKey(bagName)) { Bag bag = metadataStore.getBag(instanceId, bagName); if(bag != null) { Set<IndexDefinition> indexDefinitions = new HashSet<>(bag.getIndexDefinitions()); LOG.info("Found {} potential indexes for bag: {}", indexDefinitions.size(), bagName); Map<String, Index> bagIndexes = new HashMap<>(); for(IndexDefinition indexDefinition : indexDefinitions) { Index index = loadIndex(bagName, indexDefinition); bagIndexes.put(index.getKeyInfo().getKeyName(), index); } this.indexes.put(bagName, bagIndexes); } } }
@Override public QueryExecutor find(QueryBuilder queryBuilder) throws JasDBStorageException { return new RemoteQueryExecutor(instance, context, meta.getName(), nodeInformation, queryBuilder); }
@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"); } }
@Override public List<String> getIndexNames() throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); List<IndexDefinition> indexDefinitions = connector.getIndexDefinitions(context, instance, meta.getName()); List<String> indexNames = new ArrayList<>(indexDefinitions.size()); for(IndexDefinition definition : indexDefinitions) { indexNames.add(definition.getIndexName()); } return indexNames; }
@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 removeEntity(String entityId) throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); connector.removeEntity(context, instance, meta.getName(), entityId); }
@Override public Entity getEntity(String entityId) throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); return connector.findById(context, instance, meta.getName(), entityId); } }
@Override public void flush() throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); connector.flushBag(context, instance, meta.getName()); }
@Override public Entity addEntity(Entity entity) throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); return connector.insertEntity(context, instance, meta.getName(), entity); }
@Override public Entity persist(Entity entity) throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); return connector.updateEntity(context, instance, meta.getName(), entity); }
private List<EntityBag> getBags(DBInstance bagInstance) throws JasDBStorageException { List<Bag> bagMetas = bagInstance.getBags(); List<EntityBag> bags = new ArrayList<>(); for(Bag bagMeta : bagMetas) { bags.add(new EntityBagImpl(bagInstance.getInstanceId(), bagMeta.getName(), userSession)); } return bags; }
@Override public void removeIndex(String indexKeyName) throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); connector.removeIndex(context, instance, meta.getName(), indexKeyName); }
@Override public QueryResult getEntities() throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); return connector.find(context, instance, meta.getName()); }
@Override public QueryResult getEntities(int max) throws JasDBStorageException { EntityConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, EntityConnector.class); return connector.find(context, instance, meta.getName(), max); }
@Override public long getDiskSize() throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); return connector.getBag(context, instance, meta.getName()).getDiskSize(); }
private EntityBag getBag(DBInstance bagInstance, String bagName) throws JasDBStorageException { Bag bagMeta = bagInstance.getBag(bagName); if(bagMeta != null) { return new EntityBagImpl(bagInstance.getInstanceId(), bagMeta.getName(), userSession); } else { return null; } }