summaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/datatypes/StringTestCase.java
blob: cca39ee5bc4e8072913e2e31da6b6b3a5c675cec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.datatypes;

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.DocumentTypeManagerConfigurer;
import com.yahoo.document.Field;
import com.yahoo.document.annotation.AbstractTypesTest;
import com.yahoo.document.annotation.Annotation;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.document.annotation.AnnotationTypeRegistry;
import com.yahoo.document.annotation.Span;
import com.yahoo.document.annotation.SpanList;
import com.yahoo.document.annotation.SpanNode;
import com.yahoo.document.annotation.SpanTree;
import com.yahoo.document.serialization.*;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.vespa.objects.BufferSerializer;
import org.junit.Test;

import java.util.Collection;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class StringTestCase extends AbstractTypesTest {

    @Test
    public void testDeserialize() throws Exception {
        byte[] buf = new byte[1500];
        GrowableByteBuffer data = GrowableByteBuffer.wrap(buf);
        //short string
        java.lang.String foo = "foo";

        data.put((byte)0);
        data.put((byte)(foo.length() + 1));

        data.put(foo.getBytes());
        data.put((byte)0);

        int positionAfterPut = data.position();

        data.position(0);

        StringFieldValue tmp = new StringFieldValue();
        DocumentDeserializer deser = DocumentDeserializerFactory.create42(null, data);
        tmp.deserialize(deser);
        java.lang.String foo2 = tmp.getString();

        assertTrue(foo.equals(foo2));
        assertEquals(data.position(), positionAfterPut);

        //=====================

        buf = new byte[1500];
        data = GrowableByteBuffer.wrap(buf);

        //long string
        java.lang.String blah = "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah" +
                                "blahblahblahblahblahblahblahblahblahblahblah";

        int length = blah.length() + 1;

        data.put((byte)0);
        data.putInt(length | 0x80000000);

        data.put(blah.getBytes());
        data.put((byte)0);

        positionAfterPut = data.position();

        data.position(0);

        tmp = new StringFieldValue();

        deser = DocumentDeserializerFactory.create42(null, data);
        tmp.deserialize(deser);
        java.lang.String blah2 = tmp.getString();

        assertEquals(data.position(), positionAfterPut);
        assertTrue(blah.equals(blah2));
    }

/*    public void testSpanTree() {
        StringFieldValue annotatedText = new StringFieldValue("banana airlines");
        SpanList lingTree = new SpanList();
        annotatedText.setSpanTree("linguistics", lingTree);
        for (Annotation anAnnotation : annotatedText.getSpanTree("linguistics")) {
            System.err.println(anAnnotation);
        }
    }*/

    @Test
    public void testSerializeDeserialize() throws Exception {
        java.lang.String test = "Hello hello";
        BufferSerializer data = new BufferSerializer(new GrowableByteBuffer(100, 2.0f));
        StringFieldValue value = new StringFieldValue(test);
        value.serialize(data);

        data.getBuf().position(0);

        StringFieldValue tmp = new StringFieldValue();
        DocumentDeserializer deser = DocumentDeserializerFactory.create42(null, data.getBuf());
        tmp.deserialize(deser);
        java.lang.String test2 = tmp.getString();
        assertEquals(test, test2);
    }

    @Test
    public void testSerializationWithTree() {
        StringFieldValue text = getAnnotatedString();

        serializeAndAssert(text);
    }

    private void serializeAndAssert(StringFieldValue stringFieldValue) {
        Field f = new Field("text", DataType.STRING);

        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
        serializer.write(f, stringFieldValue);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(man, buffer);
        StringFieldValue stringFieldValue2 = new StringFieldValue();
        deserializer.read(f, stringFieldValue2);

        assertEquals(stringFieldValue, stringFieldValue2);
        assertNotSame(stringFieldValue, stringFieldValue2);
    }

    @Test
    public void testNestedSpanTreeBug4187377() {
        AnnotationType type = new AnnotationType("ann", DataType.STRING);

        StringFieldValue outerString = new StringFieldValue("Ballooo");
        SpanTree outerTree = new SpanTree("outer");
        outerString.setSpanTree(outerTree);

        SpanList outerRoot = (SpanList)outerTree.getRoot();
        Span outerSpan = new Span(0, 1);
        outerRoot.add(outerSpan);

        StringFieldValue innerString = new StringFieldValue("innerBalloooo");

        outerTree.annotate(outerSpan, new Annotation(type, innerString));

        SpanTree innerTree = new SpanTree("inner");
        innerString.setSpanTree(innerTree);

        SpanList innerRoot = (SpanList)innerTree.getRoot();
        Span innerSpan = new Span(0, 1);
        innerRoot.add(innerSpan);
        innerTree.annotate(innerSpan, new Annotation(type));

        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);

        try {
            serializer.write(null, outerString);
            fail("Should have failed, nested span trees are not supported.");
        } catch (SerializationException se) {
            //OK!
        }
    }

    /**
     * Test for bug 4066566. No assertions, but works if it runs without exceptions.
     */
    @Test
    public void testAnnotatorConsumer() {
        DocumentTypeManager manager = new DocumentTypeManager();
        DocumentTypeManagerConfigurer
                .configure(manager, "file:src/test/java/com/yahoo/document/datatypes/documentmanager.blog.sd");

        DocumentType blogType = manager.getDocumentType("blog");
        Document doc = new Document(blogType, "doc:blog:http://blogs.sun.com/praveenm");
        doc.setFieldValue("url", new StringFieldValue("http://blogs.sun.com/praveenm"));
        doc.setFieldValue("title", new StringFieldValue("Beginning JavaFX"));
        doc.setFieldValue("author", new StringFieldValue("Praveen Mohan"));
        doc.setFieldValue("body", new StringFieldValue(
                "JavaFX can expand its wings across different domains such as manufacturing, logistics, retail, etc. Many companies have adopted it - IBM, Oracle, Yahoo, Honeywell. Even the non-IT industries such as GE, WIPRO, Ford etc. So it is a success for Christopher Oliver and Richard Bair. Scott Mcnealy is happy"));

        doc = annotate(doc, manager);
        doc = serializeAndDeserialize(doc, manager);
        doc = consume(doc, manager);
        System.err.println(doc);
    }

    private Document serializeAndDeserialize(Document doc, DocumentTypeManager manager) {
        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
        serializer.write(doc);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(manager, buffer);
        return new Document(deserializer);
    }

    public Document annotate(Document document, DocumentTypeManager manager) {
        AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();

        AnnotationType company = registry.getType("company");
        AnnotationType industry = registry.getType("industry");
        AnnotationType person = registry.getType("person");
        AnnotationType location = registry.getType("location");

        Map<String, AnnotationType> m = registry.getTypes();
        for (String key : m.keySet()) {
            System.out.println("Key: " + key);
            AnnotationType val = m.get(key);
            parseAnnotationType(val);
        }

        SpanTree tree = new SpanTree("testannotations");
        SpanList root = (SpanList)tree.getRoot();

        SpanNode companySpan = new Span(0, 5);
        SpanNode industrySpan = new Span(5, 10);
        SpanNode personSpan = new Span(10, 15);
        SpanNode locationSpan = new Span(15, 20);

        root.add(companySpan);
        root.add(industrySpan);
        root.add(personSpan);
        root.add(locationSpan);

        Struct companyValue = (Struct)company.getDataType().createFieldValue();
        companyValue.setFieldValue("name", new StringFieldValue("Sun"));
        companyValue.setFieldValue("ceo", new StringFieldValue("Scott Mcnealy"));
        companyValue.setFieldValue("lat", new DoubleFieldValue(37.7));
        companyValue.setFieldValue("lon", new DoubleFieldValue(-122.44));
        companyValue.setFieldValue("vertical", new StringFieldValue("software"));
        Annotation compAn = new Annotation(company, companyValue);
        tree.annotate(companySpan, compAn);

        Struct personValue = new Struct(manager.getDataType("annotation.person"));
        personValue.setFieldValue("name", new StringFieldValue("Richard Bair"));
        Annotation personAn = new Annotation(person, personValue);
        tree.annotate(personSpan, personAn);

        Struct locValue = new Struct(manager.getDataType("annotation.location"));
        locValue.setFieldValue("name", new StringFieldValue("Prinsens Gate"));
        Annotation loc = new Annotation(location, locValue);
        tree.annotate(locationSpan, loc);

        Struct locValue2 = new Struct(manager.getDataType("annotation.location"));
        locValue2.setFieldValue("name", new StringFieldValue("Kongens Gate"));
        Annotation locAn = new Annotation(location, locValue2);
        tree.annotate(locationSpan, locAn);

        SpanList branch = new SpanList();

        SpanNode span1 = new Span(0, 3);
        SpanNode span2 = new Span(1, 9);
        SpanNode span3 = new Span(12, 10);

        branch.add(span1);
        branch.add(span3);
        branch.add(span2);

        Struct industryValue = new Struct(manager.getDataType("annotation.industry"));
        industryValue.setFieldValue("vertical", new StringFieldValue("Manufacturing"));
        Annotation ind = new Annotation(industry, industryValue);
        tree.annotate(span1, ind);

        Struct pValue = new Struct(manager.getDataType("annotation.person"));
        pValue.setFieldValue("name", new StringFieldValue("Praveen Mohan"));
        Annotation pAn = new Annotation(person, pValue);
        tree.annotate(span2, pAn);

        Struct lValue = new Struct(manager.getDataType("annotation.location"));
        lValue.setFieldValue("name", new StringFieldValue("Embassy Golf Links"));
        Annotation locn = new Annotation(location, lValue);
        tree.annotate(span3, locn);

        Struct cValue = (Struct)company.getDataType().createFieldValue();
        cValue.setFieldValue("name", new StringFieldValue("Yahoo"));
        cValue.setFieldValue("ceo", new StringFieldValue("Carol Bartz"));
        cValue.setFieldValue("lat", new DoubleFieldValue(127.7));
        cValue.setFieldValue("lon", new DoubleFieldValue(-42.44));
        cValue.setFieldValue("vertical", new StringFieldValue("search"));
        Annotation cAn = new Annotation(company, cValue);
        tree.annotate(branch, cAn);

        Struct pVal = new Struct(manager.getDataType("annotation.person"));
        pVal.setFieldValue("name", new StringFieldValue("Kim Omar"));
        Annotation an = new Annotation(person, pVal);
        tree.annotate(root, an);
        root.add(branch);

        StringFieldValue body = (StringFieldValue)document.getFieldValue(document.getDataType().getField("body"));

        root.remove(branch);
        tree.cleanup();

        System.out.println("No. Of Annotations: " + tree.numAnnotations());
        body.setSpanTree(tree);

        document.setFieldValue(document.getField("body"), body);

        return document;
    }

    public Document consume(Document document, DocumentTypeManager docTypeMgr) {
        DocumentType type = docTypeMgr.getDocumentType("blog");
        Collection<Field> fc = type.getFields();
        for (Field f : fc) {
            System.out.println("\n\nField Name: " + f.getName());
            System.out.println("DataType: " + f.getDataType());
            System.out.println("isHeader? " + f.isHeader());

            FieldValue val = document.getFieldValue(f);
            if (val instanceof StringFieldValue) {
                StringFieldValue sfv = (StringFieldValue)val;
                System.out.println(f.getName() + " is a StringField. Field Value: " + sfv.getString());
                Collection<SpanTree> c = sfv.getSpanTrees();
                for (SpanTree tree : c) {
                    System.out.println(f.getName() + " has annotations");
                    consumeAnnotations(tree, (SpanList)tree.getRoot());
                }
            }
        }

        return document;
    }

    public void consumeAnnotations(SpanTree tree, SpanList root) {
        System.out.println("\n\nSpanList: " + root + " num Children: " + root.numChildren());
        System.out.println("-------------------");
        Iterator<SpanNode> childIterator = root.childIterator();
        while (childIterator.hasNext()) {
            SpanNode node = childIterator.next();
            System.out.println("Span Node: " + node); // + " Span Text: " + node.getText(fieldValStr));
            if (node instanceof SpanList) {
                System.out.println("Encountered another span list");
                SpanList spl = (SpanList)node;
                ListIterator<SpanNode> lli = spl.childIterator();
                while (lli.hasNext()) {
                    System.out.print(" " + lli.next() + " ");
                }
                consumeAnnotations(tree, (SpanList)node);
            } else {
                System.out.println("\nGetting annotations for this span node: " + node);
                getAnnotationsForNode(tree, node);
            }
        }
        System.out.println("\nGetting annotations for the SpanList itself : " + root);
        getAnnotationsForNode(tree, root);
    }

    public void getAnnotationsForNode(SpanTree tree, SpanNode node) {
        Iterator<Annotation> iter = tree.iterator(node);
        boolean annotationPresent = false;
        while (iter.hasNext()) {
            annotationPresent = true;
            Annotation xx = iter.next();
            Struct fValue = (Struct)xx.getFieldValue();
            System.out.println("Annotation: " + xx);
            if (fValue == null) {
                System.out.println("Field Value is null");
                return;
            }
            Iterator fieldIter = fValue.iterator();
            while (fieldIter.hasNext()) {
                Map.Entry m = (Map.Entry)fieldIter.next();
                Field f = (Field)m.getKey();
                FieldValue val = (FieldValue)m.getValue();
                System.out.println("Field : " + f + " Value: " + val);
            }
        }
        if (!annotationPresent) {
            System.out.println("****No annotations found for the span node: " + node);
        }
    }

    public void parseAnnotationType(AnnotationType t) {
        System.out.println("Type Name: " + t.getName());
        System.out.println("Type ID: " + t.getId());
        DataType dt = t.getDataType();
        String dataTypeStr;
        if (dt == DataType.STRING) {
            dataTypeStr = "String";
        } else if (dt == DataType.INT) {
            dataTypeStr = "Integer";
        } else if (dt == DataType.URI) {
            dataTypeStr = "URL";
        } else {
            dataTypeStr = "UNKNOWN";
        }
        System.out.println("Type DataType: " + dataTypeStr);
    }
}