Package org.lilyproject.repository.api

Examples of org.lilyproject.repository.api.Record


        assertEquals(recordType1.getName(), readRecord.getRecordTypeName(Scope.VERSIONED_MUTABLE));
    }

    @Test
    public void testUpdateMutableFieldCopiesValueToNext() throws Exception {
        Record record = createDefaultRecord();
        Record updateRecord = record.cloneRecord();
        updateRecord.setField(fieldType1.getName(), "value2");
        updateRecord.setField(fieldType2.getName(), 789);
        updateRecord = repository.update(updateRecord); // Leave mutable field
        // same on first update

        updateRecord.setField(fieldType3.getName(), false);
        updateRecord = repository.update(updateRecord);

        Record readRecord = repository.read(record.getId(), Long.valueOf(2));
        assertEquals(true, readRecord.getField(fieldType3.getName()));

        updateRecord = repository.newRecord(record.getId());
        updateRecord.setRecordType(recordType1.getName(), recordType1.getVersion());
        updateRecord.setField(fieldType3.getName(), false);
        updateRecord.setVersion(1L);

        repository.update(updateRecord, true, false);

        readRecord = repository.read(record.getId(), Long.valueOf(1));
        assertFalse((Boolean) readRecord.getField(fieldType3.getName()));
        readRecord = repository.read(record.getId(), Long.valueOf(2));
        assertTrue((Boolean) readRecord.getField(fieldType3.getName()));
        readRecord = repository.read(record.getId(), Long.valueOf(3));
        assertFalse((Boolean) readRecord.getField(fieldType3.getName()));
    }
View Full Code Here


        assertFalse((Boolean) readRecord.getField(fieldType3.getName()));
    }

    @Test
    public void testDeleteMutableField() throws Exception {
        Record record = createDefaultRecord();
        Record updateRecord = record.cloneRecord();
        updateRecord.setField(fieldType1.getName(), "value2");
        updateRecord.setField(fieldType2.getName(), 789);
        updateRecord.setField(fieldType3.getName(), false);
        repository.update(updateRecord);

        Record deleteRecord = repository.newRecord(record.getId());
        deleteRecord.setVersion(Long.valueOf(1));
        deleteRecord.setRecordType(record.getRecordTypeName(), record.getRecordTypeVersion());
        deleteRecord.addFieldsToDelete(Arrays.asList(fieldType1.getName(), fieldType2.getName(), fieldType3.getName()));

        repository.update(deleteRecord, true, false);

        Record readRecord = repository.read(record.getId(), Long.valueOf(1));
        // The non-mutable fields were ignored
        assertEquals("value2", readRecord.getField(fieldType1.getName()));
        assertEquals(123, readRecord.getField(fieldType2.getName()));
        try {
            // The mutable field got deleted
            if (avro) {
                System.out.println("Expecting FieldNotFoundException");
            }
            readRecord.getField(fieldType3.getName());
            fail();
        } catch (FieldNotFoundException expected) {
        }

        readRecord = repository.read(record.getId());
        assertEquals(false, readRecord.getField(fieldType3.getName()));
    }
View Full Code Here

        assertEquals(false, readRecord.getField(fieldType3.getName()));
    }

    @Test
    public void testDeleteMutableFieldCopiesValueToNext() throws Exception {
        Record record = createDefaultRecord();
        Record updateRecord = record.cloneRecord();
        updateRecord.setField(fieldType1.getName(), "value2");
        updateRecord.setField(fieldType2.getName(), 789);
        updateRecord = repository.update(updateRecord); // Leave mutable field
        // same on first update

        updateRecord.setField(fieldType3.getName(), false);
        updateRecord = repository.update(updateRecord);

        Record readRecord = repository.read(record.getId(), Long.valueOf(2));
        assertEquals(true, readRecord.getField(fieldType3.getName()));

        Record deleteMutableFieldRecord = repository.newRecord(record.getId());
        deleteMutableFieldRecord.setVersion(Long.valueOf(1));
        deleteMutableFieldRecord.setRecordType(record.getRecordTypeName(), record.getRecordTypeVersion());
        deleteMutableFieldRecord.addFieldsToDelete(Arrays.asList(fieldType3.getName()));

        repository.update(deleteMutableFieldRecord, true, false);

        readRecord = repository.read(record.getId(), Long.valueOf(1));
        try {
View Full Code Here

        RecordType recordType4 = typeManager.newRecordType(new QName(namespace, "RT4"));
        recordType4.addFieldTypeEntry(typeManager.newFieldTypeEntry(fieldType6.getId(), false));
        recordType4.addSupertype(recordType1.getId()); // In fact recordType1B should be taken as supertype
        recordType4 = typeManager.createRecordType(recordType4);

        Record record = repository.newRecord(idGenerator.newRecordId());
        record.setRecordType(recordType4.getName(), recordType4.getVersion());
        record.setField(fieldType1.getName(), "foo");
        record.setField(fieldType2.getName(), 555);
        record.setField(fieldType3.getName(), true);
        record.setField(fieldType1B.getName(), "fromLatestSupertypeRecordTypeVersion");
        record.setField(fieldType6.getName(), "bar");
        record = repository.create(record);

        Record readRecord = repository.read(record.getId());
        assertEquals(Long.valueOf(1), readRecord.getVersion());
        assertEquals("foo", readRecord.getField(fieldType1.getName()));
        assertEquals(555, readRecord.getField(fieldType2.getName()));
        assertEquals(true, readRecord.getField(fieldType3.getName()));
        assertEquals("fromLatestSupertypeRecordTypeVersion", readRecord.getField(fieldType1B.getName()));
        assertEquals("bar", readRecord.getField(fieldType6.getName()));
    }
View Full Code Here

    }

    @Test
    public void testNonVersionedToVersioned() throws Exception {
        // Create a record with only a versioned and non-versioned field
        Record record = repository.newRecord();
        record.setRecordType(recordType1.getName(), recordType1.getVersion());
        record.setField(fieldType1.getName(), "hello");
        record.setField(fieldType2.getName(), new Integer(4));
        record = repository.create(record);

        // Try to read the created version
        record = repository.read(record.getId(), 1L);
    }
View Full Code Here

        // period to be expected, and one might need to rebuild indexes.
        //

        RecordEvent.IndexRecordFilterData idxSel = recordEvent.getIndexRecordFilterData();
        if (idxSel != null) {
            Record newRecord = idxSel.getNewRecordExists() ? repository.getRecordFactory().newRecord(recordId) : null;
            Record oldRecord = idxSel.getOldRecordExists() ? repository.getRecordFactory().newRecord(recordId) : null;

            TypeManager typeManager = repository.getTypeManager();

            if (idxSel.getFieldChanges() != null) {
                for (RecordEvent.FieldChange fieldChange : idxSel.getFieldChanges()) {
                    FieldType fieldType = typeManager.getFieldTypeById(fieldChange.getId());
                    QName name = fieldType.getName();

                    if (fieldChange.getNewValue() != null) {
                        Object value = fieldType.getValueType().read(fieldChange.getNewValue());
                        newRecord.setField(name, value);
                    }

                    if (fieldChange.getOldValue() != null) {
                        Object value = fieldType.getValueType().read(fieldChange.getOldValue());
                        oldRecord.setField(name, value);
                    }
                }
            }

            if (idxSel.getNewRecordType() != null) {
                newRecord.setRecordType(typeManager.getRecordTypeById(idxSel.getNewRecordType(), null).getName());
            }

            if (idxSel.getOldRecordType() != null) {
                oldRecord.setRecordType(typeManager.getRecordTypeById(idxSel.getOldRecordType(), null).getName());
            }

            return Pair.create(oldRecord, newRecord);
        }
View Full Code Here

        record = repository.read(record.getId(), 1L);
    }

    @Test
    public void testIdRecord() throws Exception {
        Record record = repository.newRecord();
        record.setRecordType(recordType1.getName(), recordType1.getVersion());
        record.setField(fieldType1.getName(), "hello");
        record.setField(fieldType2.getName(), new Integer(4));
        record = repository.create(record);

        IdRecord idRecord = repository.readWithIds(record.getId(), null, null);
        assertEquals("hello", idRecord.getField(fieldType1.getId()));
        assertTrue(idRecord.hasField(fieldType1.getId()));
        assertEquals(new Integer(4), idRecord.getField(fieldType2.getId()));
        assertTrue(idRecord.hasField(fieldType2.getId()));
View Full Code Here

    }

    @Test
    public void testVersionNumbers() throws Exception {
        // Create a record without versioned fields, the record will be without versions
        Record record = repository.newRecord();
        record.setRecordType(recordType1.getName(), recordType1.getVersion());
        record.setField(fieldType1.getName(), "hello");
        record = repository.create(record);

        // Check the version is null
        assertEquals(null, record.getVersion());

        // Check version number stays null after additional update
        record.setField(fieldType1.getName(), "hello2");
        repository.update(record);
        record = repository.read(record.getId());
        assertEquals(null, record.getVersion());

        // add a versioned field to the record
        record.setField(fieldType2.getName(), new Integer(4));
        record = repository.update(record);
        assertEquals(new Long(1), record.getVersion());

        // Verify the last version number after a fresh record read
        record = repository.read(record.getId());
        assertEquals(new Long(1), record.getVersion());

        // Read specific version
        record = repository.read(record.getId(), 1L);
        assertEquals(new Long(1), record.getVersion());
        assertTrue(record.hasField(fieldType2.getName()));
        assertEquals(2, record.getFields().size());

        try {
            if (avro) {
                System.out.println("Expecting VersionNotFoundException");
            }
            record = repository.read(record.getId(), 2L);
            fail("expected exception");
        } catch (VersionNotFoundException e) {
            // expected
        }
    }
View Full Code Here

    public void testConditionUpdateConcurrency() throws Exception {
        // This test uses MutationCondition's to let multiple threads concurrently increment a counter field
        // in the same record.

        // Create record with counter field initially set to 0
        Record record = repository.newRecord();
        record.setId(repository.getIdGenerator().newRecordId());
        record.setRecordType(recordType2.getName());
        record.setField(fieldType4.getName(), new Integer(0));
        repository.createOrUpdate(record);
        final RecordId recordId = record.getId();

        // Run concurrent threads to increment the counter field
        int threads = 5;
        int count = 200;
        ThreadPoolExecutor executor = new ThreadPoolExecutor(threads, threads, 10, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(count));

        List<Future> futures = new ArrayList<Future>();
        for (int i = 0; i < count; i++) {
            futures.add(executor.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    int iteration = 0;
                    while (true) {
                        try {
                            iteration++;
                            Record record = repository.read(recordId);
                            int oldValue = (Integer) record.getField(fieldType4.getName());
                            record.setField(fieldType4.getName(), new Integer(oldValue + 1));
                            MutationCondition cond = new MutationCondition(fieldType4.getName(), oldValue, false);
                            record = repository.update(record, Lists.newArrayList(cond));

                            if (record.getResponseStatus() == ResponseStatus.CONFLICT) {
                                if (iteration > 20) {
                                    System.out.println("cas failed, will retry, iteration " + iteration);
                                }
                                Thread.sleep((int) (Math.random() * 50));
                            } else if (record.getResponseStatus() == ResponseStatus.UPDATED) {
                                // success
                                return null;
                            } else {
                                fail("unexpected response status = " + record.getResponseStatus());
                            }
                        } catch (ConcurrentRecordUpdateException e) {
                            if (iteration > 20) {
                                System.out.println("concurrent update, will retry, iteration " + iteration);
                            }
                            Thread.sleep((int) (Math.random() * 50));
                        }
                    }
                }
            }));
        }

        for (Future future : futures) {
            future.get();
        }

        executor.shutdown();

        // verify the value of the counter field is as expected
        record = repository.read(record.getId());
        assertEquals(count, record.getField(fieldType4.getName()));
    }
View Full Code Here

        assertEquals(count, record.getField(fieldType4.getName()));
    }

    @Test
    public void testConditionalDelete() throws Exception {
        Record record = createDefaultRecord();

        // perform delete with not-satisfied conditions
        record = repository.delete(record.getId(),
                Collections.singletonList(new MutationCondition(fieldType1.getName(), "xyz")));

        assertNotNull(record);
        assertEquals(ResponseStatus.CONFLICT, record.getResponseStatus());
        assertEquals("value1", record.getField(fieldType1.getName()));
        assertEquals(1, record.getFields().size());

        // check record was surely not deleted
        record = repository.read(record.getId());

        // perform delete with satisfied conditions
        record = repository.delete(record.getId(),
                Collections.singletonList(new MutationCondition(fieldType1.getName(), "value1")));
        assertNull(record);
    }
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.