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"); } }
public static IndexDefinition map(IndexEntry entry) { return new IndexDefinition(entry.getName(), entry.getKeyHeader(), entry.getValueHeader(), entry.getIndexType()); } }
@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 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); }
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); }