public static IndexEntry map(IndexDefinition definition, boolean isUnique) { return new IndexEntry(definition.getIndexName(), definition.getHeaderDescriptor(), definition.getValueDescriptor(), isUnique, definition.getIndexType()); }
@Override public String toString() { return toHeader(); } }
public static IndexDefinition fromHeader(String header) throws MetadataParseException { String[] definitionSections = header.split("/"); if(definitionSections.length == 4) { try { String indexName = definitionSections[0]; String headerDescriptor = definitionSections[1]; String valueDescriptor = definitionSections[2]; int indexType = Integer.parseInt(definitionSections[3]); return new IndexDefinition(indexName, headerDescriptor, valueDescriptor, indexType); } catch(NumberFormatException e) { throw new MetadataParseException("Unable to get index type for heade: " + header); } } else { throw new MetadataParseException("Unable to parse index metadata section"); } }
@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; }
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); }
public static IndexDefinition map(IndexEntry entry) { return new IndexDefinition(entry.getName(), entry.getKeyHeader(), entry.getValueHeader(), entry.getIndexType()); } }
@Override public boolean equals(Object o) { if(o instanceof IndexDefinition) { IndexDefinition other = (IndexDefinition) o; if(other.getHeaderDescriptor().equals(headerDescriptor) && other.getValueDescriptor().equals(valueDescriptor) && other.getIndexName().equals(indexName) && other.getIndexType() == indexType) { return true; } } return false; }
@Override public void removeIndex(String bagName, String keyName) throws JasDBStorageException { Index index = getIndex(bagName, keyName); if(index != null) { Map<String, Index> bagIndexes = indexes.get(bagName); bagIndexes.remove(keyName); KeyInfo keyInfo = index.getKeyInfo(); IndexDefinition definition = new IndexDefinition(keyInfo.getKeyName(), keyInfo.keyAsHeader(), keyInfo.valueAsHeader(), index.getIndexType()); metadataStore.removeBagIndex(instanceId, bagName, definition); index.removeIndex(); } else { throw new JasDBStorageException("Could not remove index, does not exist"); } }
@Override public int hashCode() { return toHeader().hashCode(); }
private Index loadIndex(String bagName, IndexDefinition indexDefinition) throws JasDBStorageException { try { KeyInfo keyInfo = new KeyInfoImpl(indexDefinition.getHeaderDescriptor(), indexDefinition.getValueDescriptor()); File indexFile = createIndexFile(bagName, indexDefinition.getIndexName(), false); switch(IndexTypes.getTypeFor(indexDefinition.getIndexType())) { case BTREE: LOG.debug("Loaded BTree Index for key: {}", indexDefinition.getIndexName()); Index btreeIndex = new BTreeIndex(indexFile, keyInfo); return configureIndex(IndexTypes.BTREE, btreeIndex); default: throw new JasDBStorageException("Reading from this index type: " + indexDefinition.getIndexName() + " is not supported"); } } catch(ConfigurationException e) { throw new JasDBStorageException("Unable to load index, invalid configuration", e); } }
@Override public void ensureIndex(CompositeIndexField queryFields, boolean isUnique, IndexField... valueFields) throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); KeyInfo keyInfo = new KeyInfoImpl(queryFields.getIndexFields(), Lists.newArrayList(valueFields)); connector.createIndex(context, instance, meta.getName(), new IndexDefinition(keyInfo.getKeyName(), keyInfo.keyAsHeader(), keyInfo.valueAsHeader(), -1), isUnique); }
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; }
private void initializeIndex(Index index) throws JasDBStorageException { KeyInfo keyInfo = index.getKeyInfo(); IndexDefinition definition = new IndexDefinition(keyInfo.getKeyName(), keyInfo.keyAsHeader(), keyInfo.valueAsHeader(), index.getIndexType()); if(metadataStore.containsIndex(instanceId, bagName, definition)) { try { indexRebuilder.submit(new IndexScanAndRecovery(index, getRecordWriter().readAllRecords(), true)).get(); } catch(ExecutionException | InterruptedException e) { throw new JasDBStorageException("Unable to initialize index, index rebuild failed", e); } } else { throw new JasDBStorageException("Cannot initialize index, does not exist in store"); } }
private Index createInStore(String bagName, KeyInfo keyInfo) throws JasDBStorageException { if(!indexes.containsKey(bagName)) { loadIndexes(bagName); } Map<String, Index> bagIndexes = this.indexes.get(bagName); if(bagIndexes != null && !bagIndexes.containsKey(keyInfo.getKeyName())) { File indexFile = createIndexFile(bagName, keyInfo.getKeyName(), false); try { Index index = new BTreeIndex(indexFile, keyInfo); configureIndex(IndexTypes.BTREE, index); IndexDefinition definition = new IndexDefinition(keyInfo.getKeyName(), keyInfo.keyAsHeader(), keyInfo.valueAsHeader(), index.getIndexType()); metadataStore.addBagIndex(instanceId, bagName, definition); bagIndexes.put(keyInfo.getKeyName(), index); return index; } catch(ConfigurationException e) { throw new JasDBStorageException("Unable to create index, configuration error", e); } } else if(bagIndexes != null){ return bagIndexes.get(keyInfo.getKeyName()); } else { return null; } }
@Override public void ensureIndex(IndexField indexField, boolean isUnique, IndexField... valueFields) throws JasDBStorageException { BagConnector connector = RemoteConnectorFactory.createConnector(nodeInformation, BagConnector.class); KeyInfo keyInfo = new KeyInfoImpl(indexField, valueFields); connector.createIndex(context, instance, meta.getName(), new IndexDefinition(keyInfo.getKeyName(), keyInfo.keyAsHeader(), keyInfo.valueAsHeader(), -1), isUnique); }