private void offsetCheck(int offset) throws JasDBStorageException { if(offset >= (DataBlockHeader.HEADER_SIZE - BLOCK_MANDATORY_HEADER)) { throw new JasDBStorageException("Illegal offset for header, offset needs smaller than block header size: " + (DataBlockHeader.HEADER_SIZE - BLOCK_MANDATORY_HEADER)); } }
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Indexes({indexName})", method = DELETE, consumes = "application/json", produces = "application/json") public ResponseEntity<?> removeEntry(@PathVariable String instanceId, @PathVariable String bagName, @PathVariable String indexName) throws RestException { if(StringUtils.hasText(instanceId) && StringUtils.hasText(bagName)) { LOG.debug("Removing index from instance: {} bag: {} with name: {}", instanceId, bagName, indexName); try { StorageService storageService = storageServiceFactory.getStorageService(instanceId, bagName); storageService.removeIndex(indexName); return response(null, HttpStatus.NO_CONTENT); } catch(JasDBStorageException e) { throw new RestException("Unable to remove index data: " + e.getMessage()); } } else { throw new RestException("Cannot remove index without a specified bag or InstanceId"); } }
@RequestMapping(value = "/Instances", method = POST, consumes = "application/json", produces = "application/json") public RestEntity writeEntry(@RequestBody InstanceRest dbInstance) throws RestException { try { instanceFactory.addInstance(dbInstance.getInstanceId()); return getInstance(dbInstance.getInstanceId()); } catch(JasDBStorageException e) { throw new RestException("Unable to create new instance: " + e.getMessage()); } }
private void doInstanceCheck() throws JasDBStorageException { if(instance == null) { throw new JasDBStorageException("DB current session has no active instance bound"); } }
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Entities({entityId})", produces = "application/json", method = DELETE) public RestEntity removeEntry(@PathVariable String instanceId, @PathVariable String bagName, @PathVariable String entityId, HttpServletRequest request) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName) && stringNotEmpty(entityId)) { try { LOG.debug("Doing remove of entity with id: {}", entityId); StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); storageService.removeEntity(getRequestContext(request), entityId); return null; } catch(JasDBStorageException e) { throw new RestException("Unable to remove entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot remove entity, invalid parameters for delete operation"); } }
@Override public void open() throws JasDBStorageException { try { //make sure we always allow the header block first, which is one data block long initialPosition = fileChannel.size() > 0 ? fileChannel.size() : blockSize; this.nextPosition = new AtomicLong(initialPosition); } catch(IOException e) { throw new JasDBStorageException("Unable to open file channel for data block storage", e); } }
@RequestMapping(value = "/Instances({instanceId})/Bags({bagName})/Indexes", method = POST, consumes = "application/json", produces = "application/json") public RestEntity writeEntry(@PathVariable String instanceId, @PathVariable String bagName, @RequestBody IndexEntry indexEntry) throws RestException { if(StringUtils.hasText(instanceId) && StringUtils.hasText(bagName)) { try { StorageService storageService = storageServiceFactory.getStorageService(instanceId, bagName); KeyInfo keyInfo = new KeyInfoImpl(indexEntry.getKeyHeader(), indexEntry.getValueHeader()); List<IndexField> indexFields = keyInfo.getIndexKeyFields(); List<IndexField> indexFieldValues = keyInfo.getIndexValueFields(); IndexField[] indexFieldValueArray = indexFieldValues.toArray(new IndexField[indexFieldValues.size()]); if (indexFields.size() > SINGLE_KEY_FIELD) { CompositeIndexField compositeIndexField = new SimpleCompositeIndexField(indexFields.toArray(new IndexField[indexFields.size()])); storageService.ensureIndex(compositeIndexField, indexEntry.isUniqueConstraint(), indexFieldValueArray); } else if (indexFields.size() == SINGLE_KEY_FIELD) { storageService.ensureIndex(indexFields.get(0), indexEntry.isUniqueConstraint(), indexFieldValueArray); } else { throw new RestException("Unable to create index, no key fields specified"); } return indexEntry; } catch (JasDBStorageException e) { throw new RestException("Unable to write index data: " + e.getMessage()); } } else { throw new RestException("No InstanceId or Bag Name where specified when creating index"); } }
private String determinePath(String instanceId) throws JasDBStorageException { File instanceDirectory = new File(datastoreLocation, instanceId); if(instanceDirectory.mkdirs()) { return instanceDirectory.toString(); } else { throw new JasDBStorageException("Could not create instance storage directory: " + instanceDirectory.toString()); } }
private RestEntity doModificationOperation(String instanceId, String bagName, String rawData, RequestContext context, OPERATION_TYPE type) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName)) { LOG.debug("Raw entity data received: {}", rawData); StreamedEntity streamedEntity = ENTITY_HANDLER.deserialize(StreamedEntity.class, rawData); Entity storeEntity = streamedEntity.getEntity(); try { StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); if(type == OPERATION_TYPE.UPDATE) { LOG.debug("Updating entity with id: {}", storeEntity.getInternalId()); storageService.persistEntity(context, storeEntity); } else if(type == OPERATION_TYPE.INSERT) { LOG.debug("Inserting new entity into bag: {}", bagName); storageService.insertEntity(context, storeEntity); } return new StreamedEntity(storeEntity); } catch(JasDBStorageException e) { throw new RestException("Unable to store entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot store entity, no Bag specified"); } }
public static String toString(InputStream inputStream) throws JasDBStorageException { StringBuilder stringBuilder = new StringBuilder(); int read; byte[] buffer = new byte[4096]; try { while((read = inputStream.read(buffer)) != -1) { stringBuilder.append(new String(buffer, 0, read, Charset.forName(UTF_8))); } } catch(IOException e) { throw new JasDBStorageException("Unable to convert inputstream", e); } return stringBuilder.toString(); }
@Override public void writeKey(Key key, int curPosition, ByteBuffer byteBuffer) throws JasDBStorageException { if(key instanceof StringKey) { StringKey stringKey = (StringKey) key; byte[] keyBytes = stringKey.getUnicodeBytes(); if(keyBytes.length > maxSize) { throw new JasDBStorageException("Key is too big to be stored in index, byte size: " + keyBytes.length + " max allowed: " + maxSize); } else { byteBuffer.position(curPosition); byteBuffer.put(keyBytes); } } else { throw new JasDBStorageException("The key is of an unexpected type: " + key.getClass().toString()); } }
@Override public DBInstance getInstance(String instanceId) throws JasDBStorageException { try { return instanceFactory.getInstance(instanceId); } catch(ConfigurationException e) { throw new JasDBStorageException("Unable to retrieve instance: " + instanceId, e); } }
public ClonableDataStream getStream() throws JasDBStorageException { try { return stream.clone(); } catch(CloneNotSupportedException e) { throw new JasDBStorageException("Unable to clone datastream", e); } }
public static Object getValue(Object mappableObject, PropertyMetadata propertyMetadata) throws JasDBStorageException { Method readMethod = propertyMetadata.getReadMethod(); try { return readMethod.invoke(mappableObject); } catch(InvocationTargetException | IllegalAccessException e) { throw new JasDBStorageException("Unable to read property: " + propertyMetadata, e); } }
@Override public DataBlock writeKey(Key key, DataBlock dataBlock) throws JasDBStorageException { if(key instanceof CompositeKey) { return multiKeyloader.writeKeys(key, dataBlock); } else { throw new JasDBStorageException("Unable to write key not a composite key"); } }
public static <T> TypeMapper<T> getTypeMapper(Class<T> type) throws JasDBStorageException { Optional<TypeMapper> typeMapperOptional = typeMappers.stream().filter(t -> t.isSupportedType(type)).findAny(); if(typeMapperOptional.isPresent()) { return typeMapperOptional.get(); } else { throw new JasDBStorageException("Unable to find type mapper for return type: " + type); } } }
@Override public void writeKey(Key key, int curPosition, ByteBuffer byteBuffer) throws JasDBStorageException { if(key instanceof LongKey) { LongKey longKey = (LongKey) key; byteBuffer.putLong(curPosition, longKey.getKey()); } else { throw new JasDBStorageException("The key is of an unexpected type: " + key.getClass().toString()); } }
public static <T extends Annotation> T getAnnotation(AnnotatedElement type, Class<T> annotationType) throws JasDBStorageException { Optional<T> annotation = getOptionalAnnotation(type, annotationType); if(annotation.isPresent()) { return annotation.get(); } else { throw new JasDBStorageException("Unable to get annotation: " + annotationType); } }
@Override public IndexManager getIndexManager(String instanceId) throws JasDBStorageException { if(!indexManagers.containsKey(instanceId)) { if(metadataStore.containsInstance(instanceId)) { IndexManager indexManager = (IndexManager) applicationContext.getBean("IndexManager", instanceId); indexManagers.putIfAbsent(instanceId, indexManager); } else { throw new JasDBStorageException("Unable to get index manager, instance does not exist"); } } return indexManagers.get(instanceId); }