/** * Insert a record in the database. Any field/value pairs in the specified * values HashMap will be written into the record with the specified record * key. * * @param table * The name of the table * @param key * The record key of the record to insert. * @param values * A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See the * {@link DB} class's description for a discussion of error codes. */ @Override public Status insert(String table, String key, Map<String, ByteIterator> values) { try { BaseDocument toInsert = new BaseDocument(key); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { toInsert.addAttribute(entry.getKey(), byteIteratorToString(entry.getValue())); } DocumentCreateOptions options = new DocumentCreateOptions().waitForSync(waitForSync); arangoDB.db(databaseName).collection(table).insertDocument(toInsert, options); return Status.OK; } catch (ArangoDBException e) { logger.error("Exception while trying insert {} {} with ex {}", table, key, e.toString()); } return Status.ERROR; }
@Override public void serialize(final BaseDocument value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException { final Map<String, Object> doc = new HashMap<>(); doc.putAll(value.getProperties()); doc.put(DocumentField.Type.ID.getSerializeName(), value.getId()); doc.put(DocumentField.Type.KEY.getSerializeName(), value.getKey()); doc.put(DocumentField.Type.REV.getSerializeName(), value.getRevision()); gen.writeObject(doc); } };
@Override public <K, V> void put(K key, V value) throws NullPointerException { Objects.requireNonNull(key, "Key is required"); Objects.requireNonNull(value, "value is required"); BaseDocument baseDocument = new BaseDocument(); baseDocument.setKey(key.toString()); baseDocument.addAttribute(VALUE, JSONB.toJson(value)); if (arangoDB.db(bucketName).collection(namespace).documentExists(key.toString())) { arangoDB.db(bucketName).collection(namespace).deleteDocument(key.toString()); } arangoDB.db(bucketName).collection(namespace) .insertDocument(baseDocument); }
@SuppressWarnings("unchecked") @Override public BaseDocument deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException { return new BaseDocument(p.readValueAs(Map.class)); } };
@Override public void update(DocumentEntity entity, Consumer<DocumentEntity> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException { requireNonNull(entity, "entity is required"); requireNonNull(callBack, "callBack is required"); String collectionName = entity.getName(); checkCollection(collectionName); BaseDocument baseDocument = getBaseDocument(entity); CompletableFuture<DocumentUpdateEntity<BaseDocument>> future = arangoDBAsync.db(database).collection(collectionName) .updateDocument(baseDocument.getKey(), baseDocument); future.thenAccept(d -> createConsumer(entity, callBack, d.getKey(), d.getId(), d.getRev())); }
static DocumentEntity toEntity(BaseDocument document) { Map<String, Object> properties = document.getProperties(); List<Document> documents = properties.keySet().stream() .map(k -> toDocument(k, properties)) .collect(Collectors.toList()); documents.add(Document.of(KEY, document.getKey())); documents.add(Document.of(ID, document.getId())); documents.add(Document.of(REV, document.getRevision())); String collection = document.getId().split("/")[0]; return DocumentEntity.of(collection, documents); }
@SuppressWarnings("rawtypes") @Override public BaseDocument deserialize( final VPackSlice parent, final VPackSlice vpack, final VPackDeserializationContext context) throws VPackException { return new BaseDocument((Map) context.deserialize(vpack, Map.class)); } };
@Override public DocumentEntity update(DocumentEntity entity) { String collectionName = entity.getName(); checkCollection(collectionName); BaseDocument baseDocument = getBaseDocument(entity); DocumentUpdateEntity<BaseDocument> arandoDocument = arangoDB.db(database) .collection(collectionName).updateDocument(baseDocument.getKey(), baseDocument); updateEntity(entity, arandoDocument.getKey(), arandoDocument.getId(), arandoDocument.getRev()); return entity; }
@Override public void serialize( final VPackBuilder builder, final String attribute, final BaseDocument value, final VPackSerializationContext context) throws VPackException { final Map<String, Object> doc = new HashMap<String, Object>(); doc.putAll(value.getProperties()); doc.put(DocumentField.Type.ID.getSerializeName(), value.getId()); doc.put(DocumentField.Type.KEY.getSerializeName(), value.getKey()); doc.put(DocumentField.Type.REV.getSerializeName(), value.getRevision()); context.serialize(builder, attribute, doc); } };
private BaseDocument readBaseDocument(final Class<?> type, final VPackSlice source) { @SuppressWarnings("unchecked") final Map<String, Object> properties = (Map<String, Object>) readMap(ClassTypeInformation.MAP, source); if (BaseDocument.class.equals(type)) { return new BaseDocument(properties); } // else if (BaseEdgeDocument.class.equals(type)) { return new BaseEdgeDocument(properties); } // else { throw new MappingException(String.format("Can't read type %s as %s!", type, BaseDocument.class)); } }
try { if (!transactionUpdate) { BaseDocument updateDoc = new BaseDocument(); for (Entry<String, ByteIterator> field : values.entrySet()) { updateDoc.addAttribute(field.getKey(), byteIteratorToString(field.getValue()));
private void writeBaseDocument( final String attribute, final BaseDocument source, final VPackBuilder sink, final TypeInformation<?> definedType) { final VPackBuilder builder = new VPackBuilder(); writeMap(attribute, source.getProperties(), builder, definedType); builder.add(_ID, source.getId()); builder.add(_KEY, source.getKey()); builder.add(_REV, source.getRevision()); sink.add(attribute, builder.slice()); }
private BaseDocument readBaseDocument(final Class<?> type, final VPackSlice source) { @SuppressWarnings("unchecked") final Map<String, Object> properties = (Map<String, Object>) readMap(ClassTypeInformation.MAP, source); if (BaseDocument.class.equals(type)) { return new BaseDocument(properties); } // else if (BaseEdgeDocument.class.equals(type)) { return new BaseEdgeDocument(properties); } // else { throw new MappingException(String.format("Can't read type %s as %s!", type, BaseDocument.class)); } }
private void writeBaseDocument( final String attribute, final BaseDocument source, final VPackBuilder sink, final TypeInformation<?> definedType) { final VPackBuilder builder = new VPackBuilder(); writeMap(attribute, source.getProperties(), builder, definedType); builder.add(_ID, source.getId()); builder.add(_KEY, source.getKey()); builder.add(_REV, source.getRevision()); sink.add(attribute, builder.slice()); }
static BaseDocument getBaseDocument(DocumentEntity entity) { Map<String, Object> map = new HashMap<>(); for (Document document : entity.getDocuments()) { map.put(document.getName(), convert(document.getValue())); } return new BaseDocument(map); }