Package org.lilyproject.repository.api

Examples of org.lilyproject.repository.api.Record


        assertTrue(nodeWithPrefixes.get("recordFilter").get("field").getTextValue().endsWith("$stringField"));
    }

    @Test
    public void testRecordAttributes() throws Exception {
        Record record = repository.getRecordFactory().newRecord();
        record.getAttributes().put("one", "onevalue");

        ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, null, repository);
        ObjectNode attributes = (ObjectNode) recordNode.get("attributes");
        for (String key : record.getAttributes().keySet()) {
            Assert.assertEquals(record.getAttributes().get(key), attributes.get(key).asText());
        }

        attributes.put("write", "something new");

        record = RecordReader.INSTANCE.fromJson(recordNode, repository);
        Iterator<Entry<String, JsonNode>> it = attributes.getFields();
        while (it.hasNext()) {
            Entry<String,JsonNode> attr = it.next();
            Assert.assertEquals(attr.getValue().asText(), record.getAttributes().get(attr.getKey()));
        }
    }
View Full Code Here


        }
    }

    @Test
    public void testMetadata() throws Exception {
        Record record = repository.getRecordFactory().newRecord();
        record.setMetadata(new QName("ns", "field1"),
                new MetadataBuilder()
                        .value("stringfield", "string")
                        .value("intfield", 55)
                        .value("longfield", 999999999999L)
                        .value("booleanfield", Boolean.TRUE)
                        .value("floatfield", 33.33f)
                        .value("doublefield", 66.66d)
                        .value("binaryfield", new ByteArray("foo".getBytes()))
                        .build());

        record.setMetadata(new QName("ns", "field2"),
                new MetadataBuilder()
                        .value("stringfield", "another_string")
                        .build());

        ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, null, repository);

        // go through ser/deser
        String recordJson = JsonFormat.serializeAsString(recordNode);
        recordNode = (ObjectNode)JsonFormat.deserializeNonStd(recordJson);

        // Do some structural validations of the json
        ObjectNode metadataNode = (ObjectNode)recordNode.get("metadata");
        assertNotNull(metadataNode);
        assertNull(recordNode.get("metadataToDelete"));

        String prefix = recordNode.get("namespaces").get("ns").getTextValue();

        assertEquals(2, metadataNode.size());

        assertTrue(metadataNode.get(prefix + "$field1").get("stringfield").isTextual());
        assertEquals("string", metadataNode.get(prefix + "$field1").get("stringfield").getTextValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("intfield").isInt());
        assertEquals(55, metadataNode.get(prefix + "$field1").get("intfield").getIntValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("longfield").isLong());
        assertEquals(999999999999L, metadataNode.get(prefix + "$field1").get("longfield").getLongValue());

        assertTrue(metadataNode.get(prefix + "$field1").get("booleanfield").isBoolean());
        assertEquals(Boolean.TRUE, metadataNode.get(prefix + "$field1").get("booleanfield").getBooleanValue());

        // in JSON, no distinction between floats & doubles
        assertTrue(metadataNode.get(prefix + "$field1").get("floatfield").isFloatingPointNumber());
        assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

        assertTrue(metadataNode.get(prefix + "$field1").get("doublefield").isFloatingPointNumber());
        assertEquals(33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

        assertTrue(metadataNode.get(prefix + "$field1").get("binaryfield").isObject());
        ObjectNode binaryNode = (ObjectNode)metadataNode.get(prefix + "$field1").get("binaryfield");
        assertEquals("binary", binaryNode.get("type").getTextValue());
        assertArrayEquals("foo".getBytes(), binaryNode.get("value").getBinaryValue());

        assertTrue(metadataNode.get(prefix + "$field2").get("stringfield").isTextual());
        assertEquals("another_string", metadataNode.get(prefix + "$field2").get("stringfield").getTextValue());

        // Now parse json again to API objects
        record = RecordReader.INSTANCE.fromJson(recordNode, repository);
        assertEquals(2, record.getMetadataMap().size());

        Metadata metadata = record.getMetadata(new QName("ns", "field1"));
        assertEquals("string", metadata.get("stringfield"));
        assertEquals(55, metadata.getInt("intfield", null).intValue());
        assertEquals(999999999999L, metadata.getLong("longfield", null).longValue());
        assertEquals(Boolean.TRUE, metadata.getBoolean("booleanfield", null));
        assertEquals(33.33f, metadata.getFloat("floatfield", null), 0.001);
        assertEquals(66.66d, metadata.getDouble("doublefield", null), 0.001);
        assertEquals(new ByteArray("foo".getBytes()), metadata.getBytes("binaryfield"));

        metadata = record.getMetadata(new QName("ns", "field2"));
        assertEquals(1, metadata.getMap().size());
        assertEquals(0, metadata.getFieldsToDelete().size());
    }
View Full Code Here

        }
        if (value instanceof Record) {
            if (parentRecords.contains(value)) {
                return true;
            }
            Record record = (Record) value;
            parentRecords.add(record);
            Map<QName, Object> fields = record.getFields();
            for (Entry<QName, Object> entry : fields.entrySet()) {
                if (detectRecordRecursion(entry.getValue(), parentRecords)) {
                    return true;
                }
            }
View Full Code Here

        // Basic create-update-delete
        //
        {
            log.debug("Begin test V1");
            // Create a record
            Record record = defaultTable.newRecord();
            record.setRecordType(vRecordType1.getName());
            record.setField(vfield1.getName(), "apple");
            record.setField(liveTag.getName(), new Long(1));
            expectEvent(CREATE, Table.RECORD.name, record.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record = defaultTable.create(record);

            commitIndex();
            verifyResultCount("v_field1:apple", 1);
            verifyResultCount("+v_field1:apple +lily.version:1", 1);
            verifyResultCount("+v_field1:apple +lily.version:2", 0);

            // Update the record, this will create a new version, but we leave the live version tag pointing to version 1
            log.debug("Begin test V2");
            record.setField(vfield1.getName(), "pear");
            expectEvent(UPDATE, Table.RECORD.name, record.getId(), 2L, null, vfield1.getId());
            defaultTable.update(record);

            commitIndex();
            verifyResultCount("v_field1:pear", 0);
            verifyResultCount("v_field1:apple", 1);

            // Now move the live version tag to point to version 2
            log.debug("Begin test V3");
            record.setField(liveTag.getName(), new Long(2));
            expectEvent(UPDATE, Table.RECORD.name, record.getId(), liveTag.getId());
            record = defaultTable.update(record);

            commitIndex();
            verifyResultCount("v_field1:pear", 1);
            verifyResultCount("v_field1:apple", 0);

            // Now remove the live version tag
            log.debug("Begin test V4");
            record.delete(liveTag.getName(), true);
            expectEvent(UPDATE, Table.RECORD.name, record.getId(), liveTag.getId());
            record = defaultTable.update(record);

            commitIndex();
            verifyResultCount("v_field1:pear", 0);

            // Now test with multiple version tags
            log.debug("Begin test V5");
            record.setField(liveTag.getName(), new Long(1));
            record.setField(previewTag.getName(), new Long(2));
            record.setField(latestTag.getName(), new Long(2));
            expectEvent(UPDATE, Table.RECORD.name, record.getId(), liveTag.getId(), previewTag.getId(), latestTag.getId());
            record = defaultTable.update(record);

            commitIndex();
            verifyResultCount("v_field1:apple", 1);
            verifyResultCount("v_field1:pear", 2);

            verifyResultCount("+v_field1:pear +lily.vtagId:" + qesc(previewTag.getId().toString()), 1);
            verifyResultCount("+v_field1:pear +lily.vtagId:" + qesc(latestTag.getId().toString()), 1);
            verifyResultCount("+v_field1:pear +lily.vtagId:" + qesc(liveTag.getId().toString()), 0);
            verifyResultCount("+v_field1:apple +lily.vtagId:" + qesc(liveTag.getId().toString()), 1);
        }

        //
        // Deref
        //
        {
            // Create 4 records for the 4 kinds of dereferenced fields
            log.debug("Begin test V6");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vfield1.getName(), "fig");
            record1.setField(liveTag.getName(), Long.valueOf(1));
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(vLinkField1.getName(), new Link(record1.getId()));
            record2.setField(liveTag.getName(), Long.valueOf(1));
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vLinkField1.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("v_deref1:fig", 1);

            log.debug("Begin test V6.1");
            RecordId record3Id = idGenerator.newRecordId(record1.getId(), Collections.singletonMap("lang", "en"));
            Record record3 = defaultTable.newRecord(record3Id);
            record3.setRecordType(vRecordType1.getName());
            record3.setField(vfield1.getName(), "banana");
            record3.setField(liveTag.getName(), Long.valueOf(1));
            expectEvent(CREATE, Table.RECORD.name, record3.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record3 = defaultTable.create(record3);

            commitIndex();
            verifyResultCount("v_deref3:fig", 1);

            log.debug("Begin test V6.2");
            Map<String, String> varprops = new HashMap<String, String>();
            varprops.put("lang", "en");
            varprops.put("branch", "dev");
            RecordId record4Id = idGenerator.newRecordId(record1.getId(), varprops);
            Record record4 = defaultTable.newRecord(record4Id);
            record4.setRecordType(vRecordType1.getName());
            record4.setField(vfield1.getName(), "coconut");
            record4.setField(liveTag.getName(), Long.valueOf(1));
            expectEvent(CREATE, Table.RECORD.name, record4.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record4 = defaultTable.create(record4);

            commitIndex();
            verifyResultCount("v_deref3:fig", 2);       //master=>v_field1 (record3 and record4)
            verifyResultCount("v_deref2:fig", 1);       //-branch,-lang=>v_field1 (record4)
            verifyResultCount("v_deref4:banana", 1);    //-branch=>v_field1 (record4)
            verifyResultCount("v_deref5:coconut", 1);   //+branch=>v_field1 (record3)

            // remove the live tag from record1
            log.debug("Begin test V7");
            record1.delete(liveTag.getName(), true);
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), liveTag.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("v_deref1:fig", 0);

            // and add the live tag again record1
            log.debug("Begin test V8");
            record1.setField(liveTag.getName(), Long.valueOf(1));
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), liveTag.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("v_deref1:fig", 1);

            // Make second version of record1, assign both versions different tags, and assign these tags also
            // to version1 of record2.
            log.debug("Begin test V9");
            record1.setField(vfield1.getName(), "strawberries");
            record1.setField(previewTag.getName(), Long.valueOf(2));
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 2L, null, vfield1.getId(), previewTag.getId());
            record1 = defaultTable.update(record1);

            record2.setField(previewTag.getName(), Long.valueOf(1));
            expectEvent(UPDATE, Table.RECORD.name, record2.getId(), previewTag.getId());
            record2 = defaultTable.update(record2);

            commitIndex();
            verifyResultCount("+v_deref1:strawberries +lily.vtagId:" + qesc(previewTag.getId().toString()), 1);
            verifyResultCount("+v_deref1:strawberries +lily.vtagId:" + qesc(liveTag.getId().toString()), 0);
            verifyResultCount("+v_deref1:strawberries", 1);
            verifyResultCount("+v_deref1:fig +lily.vtagId:" + qesc(liveTag.getId().toString()), 1);
            verifyResultCount("+v_deref1:fig +lily.vtagId:" + qesc(previewTag.getId().toString()), 0);
            verifyResultCount("+v_deref1:fig", 1);

            // Now do something similar with a 3th version, but first update record2 and then record1
            log.debug("Begin test V10");
            record2.setField(latestTag.getName(), Long.valueOf(1));
            expectEvent(UPDATE, Table.RECORD.name, record2.getId(), latestTag.getId());
            record2 = defaultTable.update(record2);

            record1.setField(vfield1.getName(), "kiwi");
            record1.setField(latestTag.getName(), Long.valueOf(3));
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 3L, null, vfield1.getId(), latestTag.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("+v_deref1:kiwi +lily.vtag:latest", 1);
            verifyResultCount("+v_deref1:strawberries +lily.vtag:preview", 1);
            verifyResultCount("+v_deref1:fig +lily.vtag:live", 1);
            verifyResultCount("+v_deref1:kiwi +lily.vtag:live", 0);
            verifyResultCount("+v_field1:kiwi +lily.vtag:latest", 1);
            verifyResultCount("+v_field1:fig +lily.vtag:live", 1);

            // Perform updates to record3 and check if denorm'ed data in index of record4 follows
            log.debug("Begin test V11");
            record3.delete(vfield1.getName(), true);
            expectEvent(UPDATE, Table.RECORD.name, record3.getId(), 2L, null, vfield1.getId());
            record3 = defaultTable.update(record3);

            commitIndex();
            verifyResultCount("v_deref4:banana", 1); // live tag still points to version 1!

            log.debug("Begin test V11.1");
            defaultTable.read(record3Id, Long.valueOf(2)); // check version 2 really exists
            record3.setField(liveTag.getName(), Long.valueOf(2));
            expectEvent(UPDATE, Table.RECORD.name, record3.getId(), liveTag.getId());
            defaultTable.update(record3);

            commitIndex();
            verifyResultCount("v_deref4:banana", 0);
            verifyResultCount("v_field1:coconut", 1);

            // Perform updates to record4 and check if denorm'ed data in index of record3 follows
            log.debug("Begin test V12");
            record4.delete(vfield1.getName(), true);
            expectEvent(UPDATE, Table.RECORD.name, record4.getId(), 2L, null, vfield1.getId());
            record4 = defaultTable.update(record4);

            commitIndex();
            verifyResultCount("v_deref5:coconut", 1); // live tag still points to version 1!

            log.debug("Begin test V12.1");
            defaultTable.read(record4Id, Long.valueOf(2)); // check version 2 really exists
            record4.setField(liveTag.getName(), Long.valueOf(2));
            expectEvent(UPDATE, Table.RECORD.name, record4.getId(), liveTag.getId());
            defaultTable.update(record4);

            commitIndex();
            verifyResultCount("v_deref5:coconut", 0); // now it's gone

            // Delete master
            log.debug("Begin test V13");
            expectEvent(DELETE, Table.RECORD.name, record1.getId());
            defaultTable.delete(record1.getId());

            commitIndex();
            verifyResultCount("v_deref1:fig", 0);
            verifyResultCount("v_deref2:fig", 0);
            verifyResultCount("v_deref3:fig", 0);
        }

        //
        // Test that when using vtag pointing to version '0', versioned content is not accessible
        //
        {
            // Plain (without deref)
            log.debug("Begin test V14");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(nvfield1.getName(), "rollerblades");
            record1.setField(vfield1.getName(), "bicycle");
            record1.setField(liveTag.getName(), 1L);
            record1.setField(nvTag.getName(), 0L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, nvfield1.getId(), vfield1.getId(), liveTag.getId(),
                    nvTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(nvTag.getId().toString()) + " +nv_field1:rollerblades", 1);
            verifyResultCount("+lily.vtagId:" + qesc(nvTag.getId().toString()) + " +v_field1:bicycle", 0);
            verifyResultCount("+lily.vtagId:" + qesc(liveTag.getId().toString()) + " +nv_field1:rollerblades", 1);
            verifyResultCount("+lily.vtagId:" + qesc(liveTag.getId().toString()) + " +v_field1:bicycle", 1);

            // With deref
            log.debug("Begin test V15");
            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(nvLinkField2.getName(), new Link(record1.getId()));
            record2.setField(nvTag.getName(), 0L);
            record2.setField(liveTag.getName(), 0L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), nvLinkField2.getId(), nvTag.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(nvTag.getId().toString()) + " +nv_v_deref:bicycle", 0);
            verifyResultCount("+lily.vtagId:" + qesc(liveTag.getId().toString()) + " +nv_v_deref:bicycle", 1);
        }

        //
        // Test deref from a versionless record via a versioned field to a non-versioned field.
        // From the moment a versioned field is in the deref chain, when the vtag points to version 0,
        // the deref should evaluate to null.
        //
        {
            log.debug("Begin test V18");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(nvfield1.getName(), "Brussels");
            record1.setField(nvTag.getName(), 0L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), (Long) null, null, nvfield1.getId(), nvTag.getId());
            record1 = defaultTable.create(record1);

            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(vLinkField1.getName(), new Link(record1.getId()));
            record2.setField(nvTag.getName(), 0L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vLinkField1.getId(), nvTag.getId());
            record2 = defaultTable.create(record2);

            Record record3 = defaultTable.newRecord();
            record3.setRecordType(vRecordType1.getName());
            record3.setField(nvLinkField2.getName(), new Link(record2.getId()));
            record3.setField(nvTag.getName(), 0L);
            expectEvent(CREATE, Table.RECORD.name, record3.getId(), (Long) null, null, nvLinkField2.getId(), nvTag.getId());
            record3 = defaultTable.create(record3);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(nvTag.getId().toString()) + " +nv_v_nv_deref:Brussels", 0);

            // Give the records a live tag
            log.debug("Begin test V19");
            record1.setField(liveTag.getName(), 0L);
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), liveTag.getId());
            record1 = defaultTable.update(record1);

            record2.setField(liveTag.getName(), 1L);
            expectEvent(UPDATE, Table.RECORD.name, record2.getId(), liveTag.getId());
            record2 = defaultTable.update(record2);

            record3.setField(liveTag.getName(), 0L);
            expectEvent(UPDATE, Table.RECORD.name, record3.getId(), liveTag.getId());
            record3 = defaultTable.update(record3);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(liveTag.getId().toString()) + " +nv_v_nv_deref:Brussels", 1);
        }

        //
        // Test many-to-one dereferencing (= deref where there's actually more than one record pointing to another
        // record)
        // (Besides correctness, this test was also added to check/evaluate the processing time)
        //
        {
            log.debug("Begin test V19.1");

            final int COUNT = 5;

            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vfield1.getName(), "hyponiem");
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            // Create multiple records
            for (int i = 0; i < COUNT; i++) {
                Record record2 = defaultTable.newRecord();
                record2.setRecordType(vRecordType1.getName());
                record2.setField(vLinkField1.getName(), new Link(record1.getId()));
                record2.setField(liveTag.getName(), 1L);
                expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vLinkField1.getId(), liveTag.getId());
                record2 = defaultTable.create(record2);
            }

            commitIndex();
            verifyResultCount("v_deref1:hyponiem", COUNT);

            record1.setField(vfield1.getName(), "hyperoniem");
            record1.setField(liveTag.getName(), 2L);
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 2L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.update(record1);
            commitIndex();
            verifyResultCount("v_deref1:hyperoniem", COUNT);
        }

        //
        // Multi-value field tests
        //
        {
            // Test multi-value field
            log.debug("Begin test V30");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vStringMvField.getName(), Arrays.asList("Dog", "Cat"));
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vStringMvField.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("v_string_mv:Dog", 1);
            verifyResultCount("v_string_mv:Cat", 1);
            verifyResultCount("v_string_mv:(Dog Cat)", 1);
            verifyResultCount("v_string_mv:(\"Dog Cat\")", 0);

            // Test multiple single-valued fields indexed into one MV field
            // TODO

            // Test single-value field turned into multivalue by formatter
            // TODO

            // Test multi-valued deref to single-valued field
            // TODO
        }

        //
        // Long type tests
        //
        {
            log.debug("Begin test V40");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vLongField.getName(), 123L);
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vLongField.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("v_long:123", 1);
            verifyResultCount("v_long:[100 TO 150]", 1);
        }

        //
        // Datetime type test
        //
        {
            log.debug("Begin test V50");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vDateTimeField.getName(), new DateTime(2010, 10, 14, 15, 30, 12, 756, DateTimeZone.UTC));
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vDateTimeField.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("v_datetime:\"2010-10-14T15:30:12.756Z\"", 1);
            verifyResultCount("v_datetime:\"2010-10-14T15:30:12Z\"", 0);

            // Test without milliseconds
            log.debug("Begin test V51");
            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(vDateTimeField.getName(), new DateTime(2010, 10, 14, 15, 30, 12, 000, DateTimeZone.UTC));
            record2.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vDateTimeField.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("v_datetime:\"2010-10-14T15:30:12Z\"", 1);
            verifyResultCount("v_datetime:\"2010-10-14T15:30:12.000Z\"", 1);
            verifyResultCount("v_datetime:\"2010-10-14T15:30:12.000Z/SECOND\"", 1);
        }

        //
        // Date type test
        //
        {
            log.debug("Begin test V60");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vDateField.getName(), new LocalDate(2020, 1, 30));
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vDateField.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("v_date:\"2020-01-30T00:00:00Z/DAY\"", 1);
            verifyResultCount("v_date:\"2020-01-30T00:00:00.000Z\"", 1);
            verifyResultCount("v_date:\"2020-01-30T00:00:00Z\"", 1);
            verifyResultCount("v_date:\"2020-01-30T00:00:01Z\"", 0);

            verifyResultCount("v_date:[2020-01-29T00:00:00Z/DAY TO 2020-01-31T00:00:00Z/DAY]", 1);

            log.debug("Begin test V61");
            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(vDateField.getName(), new LocalDate(2020, 1, 30));
            record2.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vDateField.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("v_date:\"2020-01-30T00:00:00Z/DAY\"", 2);
        }

        //
        // Blob tests
        //
        {
            log.debug("Begin test V70");
            Blob blob1 = createBlob("blob1_msword.doc", "application/msword", "blob1_msword.doc");
            Blob blob1dup = createBlob("blob1_msword.doc", "application/msword", "blob1_msword.doc");
            Blob blob2 = createBlob("blob2.pdf", "application/pdf", "blob2.pdf");
            Blob blob3 =
                    createBlob("blob3_oowriter.odt", "application/vnd.oasis.opendocument.text", "blob3_oowriter.odt");
            Blob blob4 = createBlob("blob4_excel.xls", "application/excel", "blob4_excel.xls");

            // Single-valued blob field
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vBlobField.getName(), blob1);
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vBlobField.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("v_blob:sollicitudin", 1);
            verifyResultCount("v_blob:\"Sed pretium pretium lorem\"", 1);
            verifyResultCount("v_blob:lily", 0);

            // Multi-value and hierarchical blob field
            log.debug("Begin test V71");
            HierarchyPath path1 = new HierarchyPath(blob1dup, blob2);
            HierarchyPath path2 = new HierarchyPath(blob3, blob4);
            List<HierarchyPath> blobs = Arrays.asList(path1, path2);

            Record record2 = defaultTable.newRecord();
            record2.setRecordType(vRecordType1.getName());
            record2.setField(vBlobMvHierField.getName(), blobs);
            record2.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vBlobMvHierField.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("v_blob:blob1", 2);
            verifyResultCount("v_blob:blob2", 1);
            verifyResultCount("v_blob:blob3", 1);
            verifyResultCount("+v_blob:blob4 +v_blob:\"Netherfield Park\"", 1);

            // Nested blob field
            log.debug("Begin test V72");
            List<List<List<Blob>>> nestedBlobs = Arrays.asList(
                    Arrays.<List<Blob>>asList(
                            Arrays.<Blob>asList(
                                    createBlob("niobium".getBytes(), "text/plain", "foo.txt"),
                                    createBlob("tantalum".getBytes(), "text/plain", "foo.txt")
                            ),
                            Arrays.<Blob>asList(
                                    createBlob("fermium".getBytes(), "text/plain", "foo.txt"),
                                    createBlob("seaborgium".getBytes(), "text/plain", "foo.txt")
                            )
                    ),
                    Arrays.<List<Blob>>asList(
                            Arrays.<Blob>asList(
                                    createBlob("einsteinium".getBytes(), "text/plain", "foo.txt")
                            )
                    )
            );

            Record record3 = defaultTable.newRecord();
            record3.setRecordType(vRecordType1.getName());
            record3.setField(vBlobNestedField.getName(), nestedBlobs);
            record3.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record3.getId(), 1L, null, vBlobNestedField.getId(), liveTag.getId());
            record3 = defaultTable.create(record3);

            commitIndex();
            verifyResultCount("v_blob:niobium", 1);
            verifyResultCount("v_blob:tantalum", 1);
            verifyResultCount("v_blob:fermium", 1);
            verifyResultCount("v_blob:seaborgium", 1);
            verifyResultCount("v_blob:einsteinium", 1);
        }

        //
        // Test field with explicitly configured formatter
        //
        {
            log.debug("Begin test V80");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vDateTimeField.getName(), new DateTime(2058, 10, 14, 15, 30, 12, 756, DateTimeZone.UTC));
            record1.setField(vStringMvField.getName(), Arrays.asList("wood", "plastic"));
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vDateTimeField.getId(), vStringMvField.getId(),
                    liveTag.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("year:2058", 1);
            verifyResultCount("firstValue:wood", 1);
            verifyResultCount("firstValue:plastic", 0);
        }

        //
        // Test inheritance of variant properties for link fields
        //
        {
            log.debug("Begin test V100");
            Map<String, String> varProps = new HashMap<String, String>();
            varProps.put("lang", "nl");
            varProps.put("user", "ali");

            RecordId record1Id = repository.getIdGenerator().newRecordId(varProps);
            Record record1 = defaultTable.newRecord(record1Id);
            record1.setRecordType(vRecordType1.getName());
            record1.setField(vfield1.getName(), "venus");
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            RecordId record2Id = repository.getIdGenerator().newRecordId(varProps);
            Record record2 = defaultTable.newRecord(record2Id);
            record2.setRecordType(vRecordType1.getName());
            // Notice we make the link to the record without variant properties
            record2.setField(vLinkField1.getName(), new Link(record1.getId().getMaster()));
            record2.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record2.getId(), 1L, null, vLinkField1.getId(), liveTag.getId());
            record2 = defaultTable.create(record2);

            commitIndex();
            verifyResultCount("v_deref1:venus", 1);

            log.debug("Begin test V101");
            record1.setField(vfield1.getName(), "mars");
            record1.setField(liveTag.getName(), 2L);
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 2L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("v_deref1:mars", 1);
        }

        // Test that the index is updated when a version is created, in absence of changes to the vtag fields.
        // This would fail if the 'versionCreated' is not in the record event.
        {
            log.debug("Begin test V120");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(liveTag.getName(), 1L);
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            record1.setField(vfield1.getName(), "stool");
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("v_field1:stool", 1);
        }

        // Test that the index is updated when a version is updated, in absence of changes to the vtag fields.
        // This would fail if the 'versionCreated' is not in the record event.
        {
            log.debug("Begin test V130");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(vRecordType1.getName());
            record1.setField(liveTag.getName(), 2L);
            record1.setField(vfield1.getName(), "wall");
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId(), liveTag.getId());
            record1 = defaultTable.create(record1);

            record1.setField(vfield1.getName(), "floor");
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 2L, null, vfield1.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("v_field1:floor", 1);
        }

        //
        // Test the automatic vtag 'last', which is a virtual vtag which always points to the last version
        // of any record, without having to add it to the record or record type.
        //
        {
            log.debug("Begin test V140");
            Record record1 = defaultTable.newRecord();
            record1.setRecordType(lastRecordType.getName());
            record1.setField(nvfield1.getName(), "north");
            expectEvent(CREATE, Table.RECORD.name, record1.getId(), nvfield1.getId());
            record1 = defaultTable.create(record1);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(lastTag.getId().toString()) + " +nv_field1:north", 1);

            record1.setField(vfield1.getName(), "south");
            expectEvent(UPDATE, Table.RECORD.name, record1.getId(), 1L, null, vfield1.getId());
            record1 = defaultTable.update(record1);

            commitIndex();
            verifyResultCount("+lily.vtagId:" + qesc(lastTag.getId().toString()) + " +nv_field1:north", 1);
            verifyResultCount("+lily.vtag:last +v_field1:south", 1);
View Full Code Here

        }
        if (value instanceof Blob) {
            return ((Blob)value).clone();
        }
        if (value instanceof Record) {
            Record record = (Record) value;
            return (record).cloneRecord(parentRecords);
        }
        return value; // All other values are immutable
    }
View Full Code Here

        assertEquals(0, metadata.getFieldsToDelete().size());
    }

    @Test
    public void testMetadataToDelete() throws Exception {
        Record record = repository.getRecordFactory().newRecord();
        record.setMetadata(new QName("ns", "field1"),
                new MetadataBuilder()
                        .value("mfield1", "value1")
                        .delete("mfield2")
                        .delete("mfield3")
                        .build());

        record.setMetadata(new QName("ns", "field2"),
                new MetadataBuilder()
                        .delete("mfield4")
                        .build());

        WriteOptions options = new WriteOptions();
        options.setUseNamespacePrefixes(false);
        ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, options, repository);

        // Go through ser/deser
        String recordJson = JsonFormat.serializeAsString(recordNode);
        recordNode = (ObjectNode)JsonFormat.deserializeNonStd(recordJson);

        assertNotNull(recordNode.get("metadataToDelete"));
        assertEquals(2, recordNode.get("metadataToDelete").size());

        assertEquals(2, recordNode.get("metadataToDelete").get("{ns}field1").size());
        assertEquals(1, recordNode.get("metadataToDelete").get("{ns}field2").size());

        // Now parse json again to API objects
        record = RecordReader.INSTANCE.fromJson(recordNode, repository);
        assertEquals(2, record.getMetadataMap().size());

        Metadata metadata = record.getMetadata(new QName("ns", "field1"));
        assertTrue(metadata.getFieldsToDelete().contains("mfield2"));
        assertTrue(metadata.getFieldsToDelete().contains("mfield3"));
        assertEquals("value1", metadata.get("mfield1"));

        metadata = record.getMetadata(new QName("ns", "field2"));
        assertTrue(metadata.getFieldsToDelete().contains("mfield4"));
        assertEquals(1, metadata.getFieldsToDelete().size());
    }
View Full Code Here

    @Test
    public void testLineBasedJsonImport() throws Exception {
        JsonImport.loadJsonLines(table, repository,
                getClass().getResourceAsStream("json_line_input.txt"), new ImportSettings());

        Record record = table.read(repository.getIdGenerator().fromString("USER.jsonline1"));
        assertEquals("hello1", record.getField(new QName("ns", "stringField")));

        record = table.read(repository.getIdGenerator().fromString("USER.jsonline2"));
        assertEquals("hello2", record.getField(new QName("ns", "stringField")));
    }
View Full Code Here

        QName integerField = new QName("ns", "integer");
        QName stringlist = new QName("ns", "stringlist");
        QName nested1 = new QName("ns", "nested1");
        QName nested2 = new QName("ns", "nested2");

        Record record = table.read(repository.getIdGenerator().fromString("USER.record1"));
        assertTrue(record.hasField(stringField));
        assertTrue(record.hasField(integerField));
        assertTrue(record.hasField(stringlist));
        assertTrue(record.hasField(nested1));
        assertTrue(record.hasField(nested2));

        JsonImport.load(table, repository, getClass().getResourceAsStream("emptyfieldsignore_2.json"), settings);

        record = table.read(repository.getIdGenerator().fromString("USER.record1"));
        assertFalse(record.hasField(stringField));
        assertFalse(record.hasField(integerField));
        assertFalse(record.hasField(stringlist));
        assertFalse(record.hasField(nested1));
        assertTrue(record.hasField(nested2));
        Record nestedRecord = record.getField(nested2);
        assertFalse(nestedRecord.hasField(new QName("ns", "field1")));
        assertTrue(nestedRecord.hasField(new QName("ns", "field2")));
    }
View Full Code Here

    @Test
    public void testRecordCreateWithoutRecordType() throws Exception {
        IMocksControl control = createControl();
        control.replay();
        Record record = repository.newRecord(idGenerator.newRecordId());
        try {
            if (avro) {
                System.out.println("Expecting InvalidRecordException");
            }
            record = repository.create(record);
View Full Code Here

        control.verify();
    }

    @Test
    public void testEmptyRecordCreate() throws Exception {
        Record record = repository.newRecord();
        record.setRecordType(recordType1.getName());
        try {
            if (avro) {
                System.out.println("Expecting InvalidRecordException");
            }
            record = repository.create(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.