Package org.lilyproject.repository.api

Examples of org.lilyproject.repository.api.Record


        assertEquals(123, readRecord.getField(fieldType2.getName()));
    }

    @Test
    public void testDeleteNonVersionableFieldAndUpdateVersionableField() throws Exception {
        Record record = createDefaultRecord();
        Record updateRecord = repository.newRecord(record.getId());
        updateRecord.setRecordType(record.getRecordTypeName(), record.getRecordTypeVersion());
        updateRecord.setField(fieldType2.getName(), 999);
        updateRecord.addFieldsToDelete(Arrays.asList(fieldType1.getName()));
        repository.update(updateRecord);

        Record readRecord = repository.read(record.getId());
        assertEquals(999, readRecord.getField(fieldType2.getName()));
        try {
            if (avro) {
                System.out.println("Expecting FieldNotFoundException");
            }
            readRecord.getField(fieldType1.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }

        readRecord = repository.read(record.getId(), Long.valueOf(1));
        try {
            if (avro) {
                System.out.println("Expecting FieldNotFoundException");
            }
            readRecord.getField(fieldType1.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }

    }
View Full Code Here


    public static final IgnoreEmptyFieldsRecordReader INSTANCE = new IgnoreEmptyFieldsRecordReader();

    @Override
    protected Record readNestedRecord(ValueHandle handle, ReadContext context)
            throws InterruptedException, RepositoryException, JsonFormatException {
        Record record = super.readNestedRecord(handle, context);
        return record.getFields().isEmpty() ? null : record;
    }
View Full Code Here

    }

    @Test
    public void testUpdateAndDeleteSameField() throws Exception {
        Record record = createDefaultRecord();
        Record updateRecord = repository.newRecord(record.getId());
        updateRecord.setRecordType(record.getRecordTypeName(), record.getRecordTypeVersion());
        updateRecord.setField(fieldType2.getName(), 789);
        updateRecord.addFieldsToDelete(Arrays.asList(fieldType2.getName()));
        repository.update(updateRecord);

        try {
            if (avro) {
                System.out.println("Expecting FieldNotFoundException");
View Full Code Here

    /**
     * Gets the requested version of the record (fields and recordTypes) from the Result object.
     */
    public Record decodeRecord(RecordId recordId, Long requestedVersion, ReadContext readContext,
                               Result result, FieldTypes fieldTypes) throws InterruptedException, RepositoryException {
        Record record = recordFactory.newRecord(recordId);
        record.setVersion(requestedVersion);

        // If the version is null, this means the record has no version an thus only contains non-versioned fields (if any)
        // All non-versioned fields are stored at version 1, so we extract the fields at version 1
        Long versionToRead = (requestedVersion == null) ? 1L : requestedVersion;

        // Get a map of all fields with their values for each (cell-)version
        NavigableMap<byte[], NavigableMap<Long, byte[]>> mapWithVersions = result.getMap().get(RecordCf.DATA.bytes);
        if (mapWithVersions != null) {
            // Iterate over all columns
            for (Map.Entry<byte[], NavigableMap<Long, byte[]>> columnWithAllVersions : mapWithVersions.entrySet()) {
                // Check if the retrieved column is from a data field, and not a system field
                byte[] key = columnWithAllVersions.getKey();
                if (key[0] == LilyHBaseSchema.RecordColumn.DATA_PREFIX) {
                    NavigableMap<Long, byte[]> allValueVersions = columnWithAllVersions.getValue();
                    // Get the entry for the version (can be a cell with a lower version number if the field was not changed)
                    Map.Entry<Long, byte[]> ceilingEntry = allValueVersions.ceilingEntry(versionToRead);
                    if (ceilingEntry != null) {
                        // Extract and decode the value of the field
                        ExtractedField field =
                                extractField(key, ceilingEntry.getValue(), readContext, fieldTypes);
                        if (field != null) {
                            record.setField(field.type.getName(), field.value);
                            if (field.metadata != null) {
                                record.setMetadata(field.type.getName(), field.metadata);
                            }
                        }
                    }
                }
            }
        }

        for (Scope scope : Scope.values()) {
            Pair<SchemaId, Long> recordTypePair =  requestedVersion == null ? extractLatestRecordType(scope, result) :
                extractVersionRecordType(scope, result, requestedVersion);
            if (recordTypePair != null) {
                // We read the last version of the record type, though it might seem more logical to read
                // the exact version stored in the record. However, this doesn't make any difference, since
                // we just need the name of the record type, which is the same for all versions. The reason
                // why reading the last version is preferred is because at the time of this writing, only
                // the last version of record types is cached, and this makes an enormous difference in
                // record read or scan speed.
                QName recordTypeName =
                        typeManager.getRecordTypeById(recordTypePair.getV1(), null).getName();
                record.setRecordType(scope, recordTypeName, recordTypePair.getV2());
                if (readContext != null) {
                    readContext.setRecordTypeId(scope, recordTypePair.getV1());
                }
            }
        }
View Full Code Here

        }
    }

    @Test
    public void testDeleteRecordById() throws Exception {
        Record record = createDefaultRecord();
        repository.delete(record.getId());
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.read(record.getId());
            fail();
        } catch (RecordNotFoundException expected) {
        }
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.update(record);
            fail();
        } catch (RecordNotFoundException expected) {
        }
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.delete(record.getId());
            fail();
        } catch (RecordNotFoundException expected) {
        }
    }
View Full Code Here

     * Version of #decodeRecord() which returns an {@link IdRecord} instance rather than a {@link Record} instance.
     */
    public IdRecord decodeRecordWithIds(RecordId recordId, Long requestedVersion, Result result, FieldTypes fieldTypes)
            throws InterruptedException, RepositoryException {
        final ReadContext readContext = new ReadContext();
        final Record record = decodeRecord(recordId, requestedVersion, readContext, result, fieldTypes);

        Map<SchemaId, QName> idToQNameMapping = new HashMap<SchemaId, QName>();
        for (FieldType fieldType : readContext.getFieldTypes().values()) {
            idToQNameMapping.put(fieldType.getId(), fieldType.getName());
        }
View Full Code Here

    public List<Record> decodeRecords(RecordId recordId, List<Long> requestedVersions, Result result,
                                      FieldTypes fieldTypes) throws InterruptedException, RepositoryException {
        Map<Long, Record> records = new HashMap<Long, Record>(requestedVersions.size());
        Map<Long, Set<Scope>> scopes = new HashMap<Long, Set<Scope>>(requestedVersions.size());
        for (Long requestedVersion : requestedVersions) {
            Record record = recordFactory.newRecord(recordId);
            record.setVersion(requestedVersion);
            records.put(requestedVersion, record);
            scopes.put(requestedVersion, EnumSet.noneOf(Scope.class));
        }

        // Get a map of all fields with their values for each (cell-)version
        NavigableMap<byte[], NavigableMap<Long, byte[]>> mapWithVersions = result.getMap().get(RecordCf.DATA.bytes);
        if (mapWithVersions != null) {

            // Iterate over all columns
            for (Map.Entry<byte[], NavigableMap<Long, byte[]>> columnWithAllVersions : mapWithVersions.entrySet()) {

                // Check if the retrieved column is from a data field, and not a system field
                byte[] key = columnWithAllVersions.getKey();
                if (key[0] == RecordColumn.DATA_PREFIX) {
                    NavigableMap<Long, byte[]> allValueVersions = columnWithAllVersions.getValue();

                    // Keep the last decoded field value, to avoid decoding the same value again and again if unchanged
                    // between versions (sparse storage). Note that lastDecodedField can be null, in case of a field
                    // deletion marker
                    Long lastDecodedFieldVersion = null;
                    ExtractedField lastDecodedField = null;
                    for (Long versionToRead : requestedVersions) {
                        Record record = records.get(versionToRead);
                        // Get the entry for the version (can be a cell with a lower version number if the field was
                        // not changed)
                        Map.Entry<Long, byte[]> ceilingEntry = allValueVersions.ceilingEntry(versionToRead);
                        if (ceilingEntry != null) {
                            if (lastDecodedFieldVersion == null ||
                                    !lastDecodedFieldVersion.equals(ceilingEntry.getKey())) {
                                // Not yet decoded, do it now
                                lastDecodedFieldVersion = ceilingEntry.getKey();
                                lastDecodedField = extractField(key, ceilingEntry.getValue(), null, fieldTypes);
                            }
                            if (lastDecodedField != null) {
                                record.setField(lastDecodedField.type.getName(), lastDecodedField.value);
                                scopes.get(versionToRead).add(lastDecodedField.type.getScope());
                                if (lastDecodedField.metadata != null) {
                                    record.setMetadata(lastDecodedField.type.getName(), lastDecodedField.metadata);
                                }
                            }
                        }
                    }
                }
View Full Code Here

    }


    @Test
    public void testDeleteRecord() throws Exception {
        Record record = createDefaultRecord();
        repository.delete(record);
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.read(record.getId());
            fail();
        } catch (RecordNotFoundException expected) {
        }
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.update(record);
            fail();
        } catch (RecordNotFoundException expected) {
        }
        try {
            if (avro) {
                System.out.println("Expecting RecordNotFoundException");
            }
            repository.delete(record.getId());
            fail();
        } catch (RecordNotFoundException expected) {
        }
    }
View Full Code Here

    }

    public Response post(String id, PostAction<Record> postAction, UriInfo uriInfo, LRepository repository, LTable table) {
        if (postAction.getAction().equals("update")) {
            RecordId recordId = repository.getIdGenerator().fromString(id);
            Record record = postAction.getEntity();

            if (record.getId() != null && !record.getId().equals(recordId)) {
                throw new ResourceException("Record id in submitted record does not match record id in URI.",
                        BAD_REQUEST.getStatusCode());
            }

            record.setId(recordId);

            ImportResult<Record> result;
            try {
                result = RecordImport.importRecord(record, ImportMode.UPDATE, postAction.getConditions(), table);
            } catch (Exception e) {
                throw new ResourceException(e, INTERNAL_SERVER_ERROR.getStatusCode());
            }

            // TODO record we respond with should be full record or be limited to user-specified field list
            record = result.getEntity();
            Response response;

            ImportResultType resultType = result.getResultType();
            switch (resultType) {
                case CANNOT_UPDATE_DOES_NOT_EXIST:
                    throw new ResourceException("Record not found: " + recordId, NOT_FOUND.getStatusCode());
                case UPDATED:
                case UP_TO_DATE:
                    response = Response.ok(Entity.create(record, uriInfo)).build();
                    break;
                case CONDITION_CONFLICT:
                    response = Response.status(CONFLICT.getStatusCode()).entity(Entity.create(record, uriInfo)).build();
                    break;
                default:
                    throw new RuntimeException("Unexpected import result type: " + resultType);
            }

            return response;

        } else if (postAction.getAction().equals("delete")) {
            RecordId recordId = repository.getIdGenerator().fromString(id);
            try {
                Record record = table.delete(recordId, postAction.getConditions());
                if (record != null && record.getResponseStatus() == ResponseStatus.CONFLICT) {
                    return Response.status(CONFLICT.getStatusCode()).entity(Entity.create(record, uriInfo)).build();
                }
            } catch (RecordNotFoundException e) {
                throw new ResourceException(e, NOT_FOUND.getStatusCode());
            } catch (Exception e) {
View Full Code Here

        }
    }

    @Test
    public void testDeleteRecordCleansUpData() throws Exception {
        Record record = createDefaultRecord();
        RecordId recordId = record.getId();
        repository.delete(recordId);

        record = repository.newRecord(recordId);
        record.setRecordType(recordType2.getName(), recordType2.getVersion());
        record.setField(fieldType4.getName(), 555);
        record.setField(fieldType5.getName(), false);
        record.setField(fieldType6.getName(), "zzz");
        repository.create(record);
        Record readRecord = repository.read(recordId);
        assertEquals(Long.valueOf(2), readRecord.getVersion());
        try {
            readRecord.getField(fieldType1.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }
        try {
            readRecord.getField(fieldType2.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }
        try {
            readRecord.getField(fieldType3.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }

        assertEquals(555, readRecord.getField(fieldType4.getName()));
        assertFalse((Boolean) readRecord.getField(fieldType5.getName()));
        assertEquals("zzz", readRecord.getField(fieldType6.getName()));
    }
View Full Code Here

TOP

Related Classes of org.lilyproject.repository.api.Record

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.