/** * Create a document with the given document type and identifier. * @param docType DocumentType to use for creation * @param id The id for this document */ public Document(DocumentType docType, String id) { this(docType, new DocumentId(id)); }
/** * Creates a DocumentUpdate. * * @param docId the ID of the update * @param docType the document type that this update is valid for */ public DocumentUpdate(DocumentType docType, String docId) { this(docType, new DocumentId(docId)); }
@Override protected DocumentMessage doDecode(DocumentDeserializer buf) { return new GetDocumentMessage(new DocumentId(buf), decodeString(buf)); }
/** * Creates a document id based on a serialized document id URI string. * * The document id string is not allowed to contain 0x0 byte characters. * Otherwise all characters are allowed to ensure that document ids * already stored can be de-serialized. */ public static DocumentId createFromSerialized(String id) { return new DocumentId(IdString.createFromSerialized(id)); }
public FeedErrorMessage(String operationId) { try { DocumentId id = new DocumentId(operationId); sequenceId = Arrays.hashCode(id.getGlobalId()); } catch (Exception e) { sequenceId = 0; } }
private static FieldValue readReferenceFieldValue(final String refText, DataType expectedType) { final FieldValue value = expectedType.createFieldValue(); if (!refText.isEmpty()) { value.assign(new DocumentId(refText)); } return value; } }
/** * Only used by PacketCacheTestCase, should not be used otherwise */ public PacketWrapper(CacheKey key, BasicPacket[] packets) { // Should support key == null as this is for testing if (key == null) { keySize = 0; } else { this.keySize = key.byteSize(); } resultPackets.add(packets[0]); this.packets = new LinkedHashMap<>(); this.distributionKey = Optional.empty(); for (int i = 0; i < packets.length - 1; i++) { this.packets.put(new DocsumPacketKey(new GlobalId(new DocumentId("doc:test:" + i).getGlobalId()), i, null), packets[i + 1]); } }
private void handleIdentLevelOne(DocumentParseInfo documentParseInfo, boolean docIdAndOperationIsSetExternally) throws IOException { JsonToken currentToken = parser.getCurrentToken(); if (currentToken == JsonToken.VALUE_TRUE || currentToken == JsonToken.VALUE_FALSE) { try { if (CREATE_IF_NON_EXISTENT.equals(parser.getCurrentName())) { documentParseInfo.create = Optional.ofNullable(parser.getBooleanValue()); return; } } catch (IOException e) { throw new RuntimeException("Got IO exception while parsing document", e); } } if ((currentToken == JsonToken.VALUE_TRUE || currentToken == JsonToken.VALUE_FALSE) && CREATE_IF_NON_EXISTENT.equals(parser.getCurrentName())) { documentParseInfo.create = Optional.of(currentToken == JsonToken.VALUE_TRUE); } else if (currentToken == JsonToken.VALUE_STRING && CONDITION.equals(parser.getCurrentName())) { documentParseInfo.condition = Optional.of(parser.getText()); } else if (currentToken == JsonToken.VALUE_STRING) { // Value is expected to be set in the header not in the document. Ignore any unknown field // as well. if (! docIdAndOperationIsSetExternally) { documentParseInfo.operationType = operationNameToOperationType(parser.getCurrentName()); documentParseInfo.documentId = new DocumentId(parser.getText()); } } }
protected void decodeInto(RemoveDocumentMessage msg, DocumentDeserializer buf) { msg.setDocumentId(new DocumentId(buf)); decodeTasCondition(msg, buf); }
public Document createRandomDocumentAtLocation(long location, long timestamp, int minSize, int maxSize) { Document document = new Document(getDocumentType("testdoctype1"), new DocumentId("userdoc:footype:" + location + ":" + timestamp)); document.setFieldValue("headerval", new IntegerFieldValue((int)timestamp)); StringBuffer value = new StringBuffer(); int length = (int)(Math.random() * (maxSize - minSize)) + minSize; for (int i = 0; i < length; ++i) { value.append("A"); } document.setFieldValue("content", new StringFieldValue(value.toString())); return document; } }
@Override public Optional<String> get(RestUri restUri, Optional<String> fieldSet) throws RestApiException { SyncSession syncSession = syncSessions.alloc(); setRoute(syncSession, Optional.empty()); try { DocumentId id = new DocumentId(restUri.generateFullId()); final Document document = syncSession.get(id, fieldSet.orElse(restUri.getDocumentType() + ":[document]"), DocumentProtocol.Priority.NORMAL_1); if (document == null) { return Optional.empty(); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); JsonWriter jsonWriter = new JsonWriter(outputStream); jsonWriter.write(document); return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name())); } catch (Exception e) { throw new RestApiException(Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED)); } finally { syncSessions.free(syncSession); } }
public DocumentState(Deserializer buf) { byte hasDocId = buf.getByte(null); if (hasDocId == (byte) 1) { docId = new DocumentId(buf); } gid = new GlobalId(buf); timestamp = buf.getLong(null); removeEntry = buf.getByte(null)>0; }
/** * Reads a single operation. The operation is not expected to be part of an array. * @param operationType the type of operation (update or put) * @param docIdString document ID. * @return the document */ public DocumentOperation readSingleDocument(DocumentParser.SupportedOperation operationType, String docIdString) { DocumentId docId = new DocumentId(docIdString); final DocumentParseInfo documentParseInfo; try { DocumentParser documentParser = new DocumentParser(parser); documentParseInfo = documentParser.parse(Optional.of(docId)).get(); } catch (IOException e) { state = END_OF_FEED; throw new RuntimeException(e); } documentParseInfo.operationType = operationType; VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(); DocumentOperation operation = vespaJsonDocumentReader.createDocumentOperation( getDocumentTypeFromString(documentParseInfo.documentId.getDocType(), typeManager), documentParseInfo); operation.setCondition(TestAndSetCondition.fromConditionString(documentParseInfo.condition)); return operation; }
public void RPC_removeById(Request req) { addRemoveResult( provider.remove( getBucket(req, 0), req.parameters().get(2).asInt64(), new DocumentId(req.parameters().get(3).asString())), req); }
public void RPC_removeIfFound(Request req) { addRemoveResult( provider.removeIfFound( getBucket(req, 0), req.parameters().get(2).asInt64(), new DocumentId(req.parameters().get(3).asString())), req); }
void testIterationRequiringDocumentIdOnlyMatching(PersistenceProvider spi) throws Exception { Bucket b = new Bucket((short)0, new BucketId(8, 0x1)); spi.createBucket(b); feedDocs(spi, b, 100, 100, 100); DocumentId removedId = new DocumentId("userdoc:blarg:1:unknowndoc"); // Document does not already exist, remove should create a // remove entry for it regardless. assertFalse(spi.remove(b, 2000, removedId).wasFound()); spi.flush(b); CreateIteratorResult iter = spi.createIterator(b, new AllFields(), new Selection("id == '" + removedId.toString() + "'", 0, Long.MAX_VALUE), PersistenceProvider.IncludedVersions.NEWEST_DOCUMENT_OR_REMOVE); List<IterateResult> chunks = doIterate(spi, iter.getIteratorId(), 4096, 0); List<String> removes = new ArrayList<String>(); List<DocEntry> docs = new ArrayList<DocEntry>(); removes.add(removedId.toString()); verifyDocs(docs, chunks, removes); spi.destroyIterator(iter.getIteratorId()); }
public void read(DocumentUpdate update) { short serializationVersion = getShort(null); update.setId(new DocumentId(this)); byte contents = getByte(null); if ((contents & 0x1) == 0) { throw new DeserializationException("Cannot deserialize DocumentUpdate without doctype"); } update.setDocumentType(readDocumentType()); int size = getInt(null); for (int i = 0; i < size; i++) { update.addFieldUpdate(new FieldUpdate(this, update.getDocumentType(), serializationVersion)); } }
@Override public void delete(RestUri restUri, String condition, Optional<String> route) throws RestApiException { SyncSession syncSession = syncSessions.alloc(); Response response; try { Instant startTime = Instant.now(); DocumentId id = new DocumentId(restUri.generateFullId()); DocumentRemove documentRemove = new DocumentRemove(id); setRoute(syncSession, route); if (condition != null && ! condition.isEmpty()) { documentRemove.setCondition(new TestAndSetCondition(condition)); } syncSession.remove(documentRemove); metricsHelper.reportSuccessful(DocumentOperationType.REMOVE, startTime); return; } catch (DocumentAccessException documentException) { if (documentException.hasConditionNotMetError()) { response = Response.createErrorResponse(412, "Condition not met: " + documentException.getMessage(), restUri, RestUri.apiErrorCodes.DOCUMENT_CONDITION_NOT_MET); } else { response = Response.createErrorResponse(400, documentException.getMessage(), restUri, RestUri.apiErrorCodes.DOCUMENT_EXCEPTION); } } catch (Exception e) { response = Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED); } finally { syncSessions.free(syncSession); } metricsHelper.reportFailure(DocumentOperationType.REMOVE, DocumentOperationStatus.fromHttpStatusCode(response.getStatus())); throw new RestApiException(response); }
public void RPC_get(Request req) { GetResult result = provider.get(getBucket(req, 0), getFieldSet(req, 2), new DocumentId(req.parameters().get(3).asString())); addResult(result, req); req.returnValues().add(new Int64Value(result.getLastModifiedTimestamp())); req.returnValues().add(new DataValue(serializeDocument(result.getDocument()))); }
public void read(DocumentUpdate update) { update.setId(new DocumentId(this)); update.setDocumentType(readDocumentType()); int size = getInt(null); for (int i = 0; i < size; i++) { update.addFieldUpdate(new FieldUpdate(this, update.getDocumentType(), 8)); } int sizeAndFlags = getInt(null); update.setCreateIfNonExistent(DocumentUpdateFlags.extractFlags(sizeAndFlags).getCreateIfNonExistent()); size = DocumentUpdateFlags.extractValue(sizeAndFlags); for (int i = 0; i < size; i++) { int type = getByte(null); update.addFieldPathUpdate(FieldPathUpdate.create(FieldPathUpdate.Type.valueOf(type), update.getDocumentType(), this)); } }