private Document getDocumentDefinition(String key, Map<String, ByteIterator> values) { Document document = new Document(); document.set("id", key); if (!this.useHashQueryForScan) { // This field is only needed for range scans. // Even if this field is present in the document you // should still partition on id for simplicity of config. document.set("myid", key); } for (Entry<String, ByteIterator> entry : values.entrySet()) { document.set(entry.getKey(), entry.getValue().toString()); } return document; } }
@Override public Status delete(String table, String key) { Document record = getDocumentById(table, key); try { // Delete the document by self link. documentClient.deleteDocument(record.getSelfLink(), null); } catch (DocumentClientException e) { e.printStackTrace(); return Status.ERROR; } return Status.OK; }
@Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { Document record = getDocumentById(table, key); if (record != null) { Set<String> fieldsToReturn = (fields == null ? record.getHashMap().keySet() : fields); for (String field : fieldsToReturn) { if (field.startsWith("_")) { continue; } result.put(field, new StringByteIterator(record.getString(field))); } return Status.OK; } // Unable to find the specidifed document. return Status.ERROR; }
public <T> Document convertToDocument(T entity) { final String json = this.gson.toJson(entity); return new Document(json); }
public void writeInternal(final Object entity, final Document targetDocument, final DocumentDbPersistentEntity<?> entityInformation) { if (entity == null) { return; } if (entityInformation == null) { throw new MappingException("no mapping metadata for entity type: " + entity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(entity); final DocumentDbPersistentProperty idProperty = entityInformation.getIdProperty(); if (idProperty != null) { targetDocument.setId((String) accessor.getProperty(idProperty)); } for (final Field field : entity.getClass().getDeclaredFields()) { if (null != idProperty && field.getName().equals(idProperty.getName())) { continue; } targetDocument.set(field.getName(), accessor.getProperty(entityInformation.getPersistentProperty(field.getName()))); } }
private DocumentServiceRequest getCreateDocumentRequest(String documentCollectionLink, Object document, RequestOptions options, boolean disableAutomaticIdGeneration, OperationType operationType) { if (StringUtils.isEmpty(documentCollectionLink)) { throw new IllegalArgumentException("documentCollectionLink"); } if (document == null) { throw new IllegalArgumentException("document"); } Document typedDocument = Document.FromObject(document, objectMapper); DocumentClient.validateResource(typedDocument); if (typedDocument.getId() == null && !disableAutomaticIdGeneration) { // We are supposed to use GUID. Basically UUID is the same as GUID // when represented as a string. typedDocument.setId(Utils.getSecureRandomUUID().toString()); } String path = Utils.joinPath(documentCollectionLink, Paths.DOCUMENTS_PATH_SEGMENT); Map<String, String> requestHeaders = getRequestHeaders(options); DocumentServiceRequest request = DocumentServiceRequest.create(operationType, ResourceType.Document, path, typedDocument, requestHeaders); this.addPartitionKeyInformation(request, typedDocument, options); return request; }
protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); final JSONObject jsonObject = new JSONObject(sourceDocument.toJson()); if (idProperty != null) { // Replace the key id to the actual id field name in domain jsonObject.remove(Constants.ID_PROPERTY_NAME); jsonObject.put(idProperty.getName(), idValue); } return objectMapper.readValue(jsonObject.toString(), type); } catch (IOException e) { throw new IllegalStateException("Failed to read the source document " + sourceDocument.toJson() + " to target type " + type, e); } }
public Document writeDoc(Object sourceEntity) { if (sourceEntity == null) { return null; } final DocumentDbPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(sourceEntity.getClass()); if (persistentEntity == null) { throw new MappingException("no mapping metadata for entity type: " + sourceEntity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(sourceEntity); final DocumentDbPersistentProperty idProperty = persistentEntity.getIdProperty(); final Document document; try { document = new Document(objectMapper.writeValueAsString(sourceEntity)); } catch (JsonProcessingException e) { throw new DocumentDBAccessException("Failed to map document value.", e); } if (idProperty != null) { final Object value = accessor.getProperty(idProperty); final String id = value == null ? null : value.toString(); document.setId(id); } return document; }
private RxDocumentServiceRequest getCreateDocumentRequest(String documentCollectionLink, Object document, RequestOptions options, boolean disableAutomaticIdGeneration, OperationType operationType) { if (StringUtils.isEmpty(documentCollectionLink)) { throw new IllegalArgumentException("documentCollectionLink"); } if (document == null) { throw new IllegalArgumentException("document"); } Document typedDocument = documentFromObject(document); RxDocumentClientImpl.validateResource(typedDocument); if (typedDocument.getId() == null && !disableAutomaticIdGeneration) { // We are supposed to use GUID. Basically UUID is the same as GUID // when represented as a string. typedDocument.setId(UUID.randomUUID().toString()); } String path = Utils.joinPath(documentCollectionLink, Paths.DOCUMENTS_PATH_SEGMENT); Map<String, String> requestHeaders = this.getRequestHeaders(options); RxDocumentServiceRequest request = RxDocumentServiceRequest.create(operationType, ResourceType.Document, path, typedDocument, requestHeaders); // NOTE: if the collection is not currently cached this will be a // blocking call DocumentCollection collection = this.collectionCache.resolveCollection(request); this.addPartitionKeyInformation(request, typedDocument, options, collection); return request; }
private void deleteDocument(@NonNull Document document, @NonNull List<String> partitionKeyNames) { try { final RequestOptions options = new RequestOptions(); Assert.isTrue(partitionKeyNames.size() <= 1, "Only one Partition is supported."); if (!partitionKeyNames.isEmpty() && StringUtils.hasText(partitionKeyNames.get(0))) { options.setPartitionKey(new PartitionKey(document.get(partitionKeyNames.get(0)))); } getDocumentClient().deleteDocument(document.getSelfLink(), options); } catch (DocumentClientException e) { throw new DocumentDBAccessException("Failed to delete document: " + document.getSelfLink(), e); } }
protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { final R result = instantiate(type); final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(result); final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); if (idProperty != null) { accessor.setProperty(idProperty, idValue); } entity.doWithProperties((PropertyHandler<DocumentDbPersistentProperty>) prop -> { if (idProperty != null && idProperty.equals(prop)) { return; } accessor.setProperty(prop, sourceDocument.get(prop.getName())); } ); return result; }
@Override public Status update(String table, String key, Map<String, ByteIterator> values) { Document record = getDocumentById(table, key); if (record == null) { return Status.ERROR; } // Update each field. for (Entry<String, ByteIterator> val : values.entrySet()) { record.set(val.getKey(), val.getValue().toString()); } // Replace the document. try { documentClient.replaceDocument(record, null); } catch (DocumentClientException e) { e.printStackTrace(System.err); return Status.ERROR; } return Status.OK; }
private HashMap<String, ByteIterator> extractResult(Document item) { if (null == item) { return null; } HashMap<String, ByteIterator> rItems = new HashMap<>(item.getHashMap().size()); for (Entry<String, Object> attr : item.getHashMap().entrySet()) { LOGGER.trace("Result- key: {}, value: {}", attr.getKey(), attr.getValue().toString()); rItems.put(attr.getKey(), new StringByteIterator(attr.getValue().toString())); } return rItems; }
public <T> T convertFromDocument(Document document, Class<T> entityClass) { return this.gson.fromJson(document.toJson(), entityClass); }
public static Document documentFromObject(Object document) { return Document.FromObject(document); }
public <T> Document convertToDocument(T entity) { final String json = this.gson.toJson(entity); return new Document(json); }
public void writeInternal(final Object entity, final Document targetDocument, final DocumentDbPersistentEntity<?> entityInformation) { if (entity == null) { return; } if (entityInformation == null) { throw new MappingException("no mapping metadata for entity type: " + entity.getClass().getName()); } final ConvertingPropertyAccessor accessor = getPropertyAccessor(entity); final DocumentDbPersistentProperty idProperty = entityInformation.getIdProperty(); if (idProperty != null) { targetDocument.setId((String) accessor.getProperty(idProperty)); } for (final Field field : entity.getClass().getDeclaredFields()) { if (null != idProperty && field.getName().equals(idProperty.getName())) { continue; } final PersistentProperty property = entityInformation.getPersistentProperty(field.getName()); Assert.notNull(property, "Property is null."); targetDocument.set(field.getName(), mapToDocumentDBValue(accessor.getProperty(property))); } }
protected <R extends Object> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> type, final Document sourceDocument) { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.registerModule(provideAdvancedSerializersModule()); try { final DocumentDbPersistentProperty idProperty = entity.getIdProperty(); final Object idValue = sourceDocument.getId(); final JSONObject jsonObject = new JSONObject(sourceDocument.toJson()); if (idProperty != null) { // Replace the key id to the actual id field name in domain jsonObject.remove(Constants.ID_PROPERTY_NAME); jsonObject.put(idProperty.getName(), idValue); } return objectMapper.readValue(jsonObject.toString(), type); } catch (IOException e) { throw new IllegalStateException("Failed to read the source document " + sourceDocument.toJson() + " to target type " + type, e); } }
private long getCountValue(SqlQuerySpec querySpec, boolean isCrossPartitionQuery, String collectionName) { final FeedResponse<Document> feedResponse = executeQuery(querySpec, isCrossPartitionQuery, collectionName); final Object value = feedResponse.getQueryIterable().toList().get(0).getHashMap().get(COUNT_VALUE_KEY); if (value instanceof Integer) { return Long.valueOf((Integer) value); } else if (value instanceof Long) { return (Long) value; } else { throw new IllegalStateException("Unexpected value type " + value.getClass() + " of value: " + value); } }
public <T> T convertFromDocument(Document document, Class<T> entityClass) { return this.gson.fromJson(document.toJson(), entityClass); }