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 insert(String table, String key, Map<String, ByteIterator> values) { Document record = new Document(); record.set("id", key); for (Entry<String, ByteIterator> val : values.entrySet()) { record.set(val.getKey(), val.getValue().toString()); } try { documentClient.createDocument(collection.getSelfLink(), record, null, false); } catch (DocumentClientException e) { e.printStackTrace(System.err); return Status.ERROR; } return Status.OK; }
public <T> Document convertToDocument(T entity) { final String json = this.gson.toJson(entity); return new Document(json); }
public <T> Document convertToDocument(T entity) { final String json = this.gson.toJson(entity); return new Document(json); }
static Document FromObject(Object document, ObjectMapper objectMapper) { Document typedDocument; if (document instanceof Document) { typedDocument = (Document) document; } else { try { return new Document(objectMapper.writeValueAsString(document)); } catch (IOException e) { throw new IllegalArgumentException("Can't serialize the object into the json string", e); } } return typedDocument; }
public <T> T insert(String collectionName, T objectToSave) { final Document document = new Document(); mappingDocumentDbConverter.write(objectToSave, document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDocument in database {} collection {}", this.databaseName, collectionName); } try { if (!this.collectionCache.contains(collectionName)) { createCollectionIfNotExists(this.databaseName, collectionName); this.collectionCache.add(collectionName); } documentDbFactory.getDocumentClient() .createDocument(getCollectionLink(this.databaseName, collectionName), document, null, false); return objectToSave; } catch (DocumentClientException e) { throw new RuntimeException("insert exception", 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; }
public <T> T insert(String collectionName, T objectToSave, PartitionKey partitionKey) { final Document document = new Document(); mappingDocumentDbConverter.write(objectToSave, document); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDocument in database {} collection {}", this.databaseName, collectionName); } try { documentDbFactory.getDocumentClient() .createDocument(getCollectionLink(this.databaseName, collectionName), document, getRequestOptions(partitionKey, null), false); return objectToSave; } catch (DocumentClientException e) { throw new DocumentDBAccessException("insert exception", e); } }
public <T> void upsert(String collectionName, T object, Object id, PartitionKey partitionKey) { try { Document originalDoc = new Document(); if (object instanceof Document) { originalDoc = (Document) object; } else { mappingDocumentDbConverter.write(object, originalDoc); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute upsert document in database {} collection {} with id {}", this.databaseName, collectionName, id); } documentDbFactory.getDocumentClient().upsertDocument( getCollectionLink(this.databaseName, collectionName), originalDoc, getRequestOptions(partitionKey, null), false); } catch (DocumentClientException ex) { throw new DocumentDBAccessException("Failed to upsert document to database.", ex); } }