aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
blob: 7be50b58b61184b61adba61be70b739cab54aa31 (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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.datatypes.Array;
import com.yahoo.document.datatypes.FloatFieldValue;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.TensorFieldValue;
import com.yahoo.document.datatypes.WeightedSet;
import com.yahoo.document.fieldpathupdate.FieldPathUpdate;
import com.yahoo.document.serialization.DocumentDeserializer;
import com.yahoo.document.serialization.DocumentDeserializerFactory;
import com.yahoo.document.serialization.DocumentSerializer;
import com.yahoo.document.serialization.DocumentSerializerFactory;
import com.yahoo.document.serialization.DocumentUpdateFlags;
import com.yahoo.document.update.AssignValueUpdate;
import com.yahoo.document.update.FieldUpdate;
import com.yahoo.document.update.TensorAddUpdate;
import com.yahoo.document.update.TensorModifyUpdate;
import com.yahoo.document.update.TensorRemoveUpdate;
import com.yahoo.document.update.ValueUpdate;
import com.yahoo.io.GrowableByteBuffer;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Tests applying and serializing document updates.
 *
 * @author Einar M R Rosenvinge
 */
public class DocumentUpdateTestCase {

    private DocumentTypeManager docMan;

    private DocumentType docType = null;
    private DocumentType docType2 = null;
    private DocumentUpdate docUp = null;

    private FieldUpdate assignSingle = null;
    private FieldUpdate assignMultiList = null;
    private FieldUpdate assignMultiWset = null;

    private FieldUpdate clearSingleField = null;
    private FieldUpdate removeMultiList = null;
    private FieldUpdate removeMultiWset = null;

    private FieldUpdate addSingle = null;
    private FieldUpdate addMultiList = null;
    private FieldUpdate addMultiWset = null;

    private final String documentId = "id:ns:foobar::foooo";
    private final String tensorField = "tensorfield";
    private final TensorType tensorType = new TensorType.Builder().mapped("x").build();

    private Document createDocument() {
        return new Document(docMan.getDocumentType("foobar"), new DocumentId(documentId));
    }

    @Before
    public void setUp() {
        docMan = new DocumentTypeManager();

        docType = new DocumentType("foobar");
        docType.addField(new Field("strfoo", DataType.STRING));

        DataType stringarray = DataType.getArray(DataType.STRING);
        docType.addField(new Field("strarray", stringarray));

        DataType stringwset = DataType.getWeightedSet(DataType.STRING);
        docType.addField(new Field("strwset", stringwset));

        docType.addField(new Field(tensorField, new TensorDataType(tensorType)));
        docMan.register(docType);

        docType2 = new DocumentType("otherdoctype");
        docType2.addField(new Field("strinother", DataType.STRING));
        docMan.register(docType2);

        docUp = new DocumentUpdate(docType, new DocumentId("id:ns:foobar::bar"));

        assignSingle = FieldUpdate.createAssign(docType.getField("strfoo"), new StringFieldValue("something"));

        Array<StringFieldValue> assignList = new Array<>(docType.getField("strarray").getDataType());
        assignList.add(new StringFieldValue("assigned val 0"));
        assignList.add(new StringFieldValue("assigned val 1"));
        assignMultiList = FieldUpdate.createAssign(docType.getField("strarray"), assignList);

        WeightedSet<StringFieldValue> assignWset = new WeightedSet<>(docType.getField("strwset").getDataType());
        assignWset.put(new StringFieldValue("assigned val 0"), 5);
        assignWset.put(new StringFieldValue("assigned val 1"), 10);
        assignMultiWset = FieldUpdate.createAssign(docType.getField("strwset"), assignWset);

        clearSingleField = FieldUpdate.createClearField(docType.getField("strfoo"));

        removeMultiList = FieldUpdate.createRemove(docType.getField("strarray"), new StringFieldValue("remove val 1"));

        removeMultiWset = FieldUpdate.createRemove(docType.getField("strwset"), new StringFieldValue("remove val 1"));

        try {
            addSingle = FieldUpdate.createAdd(docType.getField("strfoo"), new StringFieldValue("add val"));
            fail("Shouldn't be able to add to a single-value field");
        } catch (UnsupportedOperationException uoe) {
            //success
        }

        List<StringFieldValue> addList = new ArrayList<>();
        addList.add(new StringFieldValue("bo"));
        addList.add(new StringFieldValue("ba"));
        addList.add(new StringFieldValue("by"));
        addMultiList = FieldUpdate.createAddAll(docType.getField("strarray"), addList);

        WeightedSet<StringFieldValue> addSet = new WeightedSet<StringFieldValue>(docType.getField("strwset").getDataType());
        addSet.put(new StringFieldValue("banana"), 137);
        addSet.put(new StringFieldValue("is"), 143);
        addSet.put(new StringFieldValue("a"), 157);
        addSet.put(new StringFieldValue("great"), 163);
        addSet.put(new StringFieldValue("fruit"), 189);
        addMultiWset = FieldUpdate.createAddAll(docType.getField("strwset"), addSet);
    }

    @Test
    public void testApplyRemoveSingle() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strfoo"));
        doc.setFieldValue("strfoo", new StringFieldValue("cocacola"));
        assertEquals(new StringFieldValue("cocacola"), doc.getFieldValue("strfoo"));
        docUp.addFieldUpdate(clearSingleField);
        docUp.applyTo(doc);
        assertNull(doc.getFieldValue("strfoo"));
    }

    @Test
    public void testApplyRemoveMultiList() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strarray"));
        Array<StringFieldValue> strArray = new Array<>(DataType.getArray(DataType.STRING));
        strArray.add(new StringFieldValue("hello hello"));
        strArray.add(new StringFieldValue("remove val 1"));
        doc.setFieldValue("strarray", strArray);
        assertNotNull(doc.getFieldValue("strarray"));
        docUp.addFieldUpdate(removeMultiList);
        docUp.applyTo(doc);
        assertEquals(1, ((List)doc.getFieldValue("strarray")).size());
        List docList = (List)doc.getFieldValue("strarray");
        assertEquals(new StringFieldValue("hello hello"), docList.get(0));
    }

    @Test
    public void testApplyRemoveMultiWset() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strwset"));
        WeightedSet<StringFieldValue> strwset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
        strwset.put(new StringFieldValue("hello hello"), 10);
        strwset.put(new StringFieldValue("remove val 1"), 20);
        doc.setFieldValue("strwset", strwset);
        assertNotNull(doc.getFieldValue("strwset"));
        docUp.addFieldUpdate(removeMultiWset);
        docUp.applyTo(doc);
        assertEquals(1, ((WeightedSet)doc.getFieldValue("strwset")).size());
        WeightedSet docWset = (WeightedSet)doc.getFieldValue("strwset");
        assertEquals(Integer.valueOf(10), docWset.get(new StringFieldValue("hello hello")));
    }

    @Test
    public void testApplyAssignSingle() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strfoo"));
        docUp.addFieldUpdate(assignSingle);
        docUp.applyTo(doc);
        assertEquals(new StringFieldValue("something"), doc.getFieldValue("strfoo"));
    }

    @Test
    public void testApplyAssignMultiList() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strarray"));
        Array<StringFieldValue> strArray = new Array<>(DataType.getArray(DataType.STRING));
        strArray.add(new StringFieldValue("hello hello"));
        strArray.add(new StringFieldValue("blah blah"));
        doc.setFieldValue("strarray", strArray);
        assertNotNull(doc.getFieldValue("strarray"));
        docUp.addFieldUpdate(assignMultiList);
        docUp.applyTo(doc);
        assertEquals(2, ((List)doc.getFieldValue("strarray")).size());
        List docList = (List)doc.getFieldValue("strarray");
        assertEquals(new StringFieldValue("assigned val 0"), docList.get(0));
        assertEquals(new StringFieldValue("assigned val 1"), docList.get(1));
    }

    @Test
    public void testApplyAssignMultiWlist() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strwset"));
        WeightedSet<StringFieldValue> strwset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
        strwset.put(new StringFieldValue("hello hello"), 164);
        strwset.put(new StringFieldValue("blahdi blahdi"), 243);
        doc.setFieldValue("strwset", strwset);
        assertNotNull(doc.getFieldValue("strwset"));
        docUp.addFieldUpdate(assignMultiWset);
        docUp.applyTo(doc);
        assertEquals(2, ((WeightedSet)doc.getFieldValue("strwset")).size());
        WeightedSet docWset = (WeightedSet)doc.getFieldValue("strwset");
        assertEquals(Integer.valueOf(5), docWset.get(new StringFieldValue("assigned val 0")));
        assertEquals(Integer.valueOf(10), docWset.get(new StringFieldValue("assigned val 1")));
    }

    @Test
    public void testApplyAddMultiList() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strarray"));

        docUp.addFieldUpdate(addMultiList);
        docUp.applyTo(doc);
        List<StringFieldValue> values = new ArrayList<>();
        values.add(new StringFieldValue("bo"));
        values.add(new StringFieldValue("ba"));
        values.add(new StringFieldValue("by"));
        assertEquals(values, doc.getFieldValue("strarray"));
    }

    @Test
    public void testApplyAddMultiWset() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue("strwset"));

        WeightedSet<StringFieldValue> wset = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
        wset.put(new StringFieldValue("this"), 5);
        wset.put(new StringFieldValue("is"), 10);
        wset.put(new StringFieldValue("a"), 15);
        wset.put(new StringFieldValue("test"), 20);
        doc.setFieldValue("strwset", wset);

        docUp.addFieldUpdate(addMultiWset);
        docUp.applyTo(doc);

        WeightedSet<StringFieldValue> values = new WeightedSet<>(doc.getDataType().getField("strwset").getDataType());
        values.put(new StringFieldValue("this"), 5);
        values.put(new StringFieldValue("is"), 143);
        values.put(new StringFieldValue("a"), 157);
        values.put(new StringFieldValue("test"), 20);
        values.put(new StringFieldValue("banana"), 137);
        values.put(new StringFieldValue("great"), 163);
        values.put(new StringFieldValue("fruit"), 189);
        assertEquals(values, doc.getFieldValue("strwset"));
    }

    @Test
    public void testUpdatesToTheSameFieldAreCombining() {
        DocumentType docType = new DocumentType("my_type");
        Field field = new Field("my_int", DataType.INT);
        docType.addField(field);

        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId("id:ns:my_type::foo:"));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new IntegerFieldValue(1)));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new IntegerFieldValue(2)));

        assertEquals(1, update.fieldUpdates().size());
        FieldUpdate fieldUpdate = update.getFieldUpdate(field);
        assertNotNull(fieldUpdate);
        assertEquals(field, fieldUpdate.getField());
        assertEquals(2, fieldUpdate.getValueUpdates().size());
        ValueUpdate valueUpdate = fieldUpdate.getValueUpdate(0);
        assertNotNull(valueUpdate);
        assertTrue(valueUpdate instanceof AssignValueUpdate);
        assertEquals(new IntegerFieldValue(1), valueUpdate.getValue());
        assertNotNull(valueUpdate = fieldUpdate.getValueUpdate(1));
        assertTrue(valueUpdate instanceof AssignValueUpdate);
        assertEquals(new IntegerFieldValue(2), valueUpdate.getValue());
    }

    @Test
    public void testUpdateToWrongField() {
        DocumentType docType = new DocumentType("my_type");
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId("id:ns:my_type::foo:"));
        try {
            update.addFieldUpdate(FieldUpdate.createIncrement(new Field("my_int", DataType.INT), 1));
            fail();
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    @Test
    public void testSerialize() {
        docUp.addFieldUpdate(assignSingle);
        docUp.addFieldUpdate(addMultiList);

        GrowableByteBuffer buf = new GrowableByteBuffer();
        docUp.serialize(DocumentSerializerFactory.create6(buf));
        buf.flip();

        try {
            FileOutputStream fos = new FileOutputStream("src/test/files/updateser.dat");
            fos.write(buf.array(), 0, buf.remaining());
            fos.close();
        } catch (Exception e) {
        }

        assertEquals((17 + 1) //docid id:ns:foobar:bar\0
                     + (6 + 1 + 2) //doctype foobar\0\0\0
                     + 4 //num field updates

                     //field update 1:
                     + (4 //field id
                        + 4 //num valueupdates

                        + (4 //valueUpdateClassID
                           + 1 //valuepresent
                           + (1 + 1 + 9 + 1)) //value

                        //field update 2
                        + (4 //field id
                           + 4 //num valueupdates

                           + (4  //valueUpdateClassID
                              + (4 + 4 + 4 + (1 + 1 + 2 + 1) + 4 + (1 + 1 + 2 + 1) + 4 + (1 + 1 + 2 + 1))))) //value
                        + 4 //num field path updates
                , buf.remaining());

        DocumentUpdate docUpDeser = new DocumentUpdate(DocumentDeserializerFactory.createHead(docMan, buf));
        assertEquals(docUp.getDocumentType(), docUpDeser.getDocumentType());
        assertEquals(docUp, docUpDeser);
    }

    @Ignore
    @Test
    public void testCppDocUpd() throws IOException {
        docMan = DocumentTestCase.setUpCppDocType();
        byte[] data = DocumentTestCase.readFile("src/tests/data/serializeupdatecpp.dat");
        DocumentDeserializer buf = DocumentDeserializerFactory.createHead(docMan, GrowableByteBuffer.wrap(data));

        DocumentType type = docMan.getDocumentType("serializetest");

        DocumentUpdate upd = new DocumentUpdate(buf);

        assertEquals(new DocumentId("id:ns:serializetest::update"), upd.getId());
        assertEquals(type, upd.getType());

        FieldUpdate serAssignFU = upd.getFieldUpdate(type.getField("intfield"));
        assertEquals(type.getField("intfield"), serAssignFU.getField());
        ValueUpdate serAssign = serAssignFU.getValueUpdate(0);
        assertEquals(ValueUpdate.ValueUpdateClassID.ASSIGN, serAssign.getValueUpdateClassID());
        assertEquals(new IntegerFieldValue(4), serAssign.getValue());

        ValueUpdate serArith = serAssignFU.getValueUpdate(1);
        assertEquals(ValueUpdate.ValueUpdateClassID.ARITHMETIC, serArith.getValueUpdateClassID());

        FieldUpdate serAddFU = upd.getFieldUpdate(type.getField("arrayoffloatfield"));
        assertEquals(type.getField("arrayoffloatfield"), serAddFU.getField());
        ValueUpdate serAdd1 = serAddFU.getValueUpdate(0);
        assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd1.getValueUpdateClassID());
        FloatFieldValue addParam1 = (FloatFieldValue)serAdd1.getValue();
        assertEquals(new FloatFieldValue(5.00f), addParam1);
        ValueUpdate serAdd2 = serAddFU.getValueUpdate(1);
        assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd2.getValueUpdateClassID());
        FloatFieldValue addparam2 = (FloatFieldValue)serAdd2.getValue();
        assertEquals(new FloatFieldValue(4.23f), addparam2);
        ValueUpdate serAdd3 = serAddFU.getValueUpdate(2);
        assertEquals(ValueUpdate.ValueUpdateClassID.ADD, serAdd3.getValueUpdateClassID());
        FloatFieldValue addparam3 = (FloatFieldValue)serAdd3.getValue();
        assertEquals(new FloatFieldValue(-1.00f), addparam3);

        FieldUpdate wsetFU = upd.getFieldUpdate(type.getField("wsfield"));
        assertEquals(type.getField("wsfield"), wsetFU.getField());
        assertEquals(2, wsetFU.size());
        ValueUpdate mapUpd = wsetFU.getValueUpdate(0);
        assertEquals(ValueUpdate.ValueUpdateClassID.MAP, mapUpd.getValueUpdateClassID());
        mapUpd = wsetFU.getValueUpdate(1);
        assertEquals(ValueUpdate.ValueUpdateClassID.MAP, mapUpd.getValueUpdateClassID());
    }

    @Test
    public void testGenerateSerializedFile() throws IOException {
        docMan = DocumentTestCase.setUpCppDocType();

        DocumentType type = docMan.getDocumentType("serializetest");
        DocumentUpdate upd = new DocumentUpdate(type, new DocumentId("id:ns:serializetest::update"));
        FieldUpdate serAssign = FieldUpdate.createAssign(type.getField("intfield"), new IntegerFieldValue(4));
        upd.addFieldUpdate(serAssign);
        FieldUpdate serClearField = FieldUpdate.createClearField(type.getField("floatfield"));
        upd.addFieldUpdate(serClearField);
        List<FloatFieldValue> arrayOfFloat = new ArrayList<>();
        arrayOfFloat.add(new FloatFieldValue(5.00f));
        arrayOfFloat.add(new FloatFieldValue(4.23f));
        arrayOfFloat.add(new FloatFieldValue(-1.00f));
        FieldUpdate serAdd = FieldUpdate.createAddAll(type.getField("arrayoffloatfield"), arrayOfFloat);
        upd.addFieldUpdate(serAdd);

        GrowableByteBuffer buf = new GrowableByteBuffer(100, 2.0f);
        upd.serialize(DocumentSerializerFactory.create6(buf));
        buf.flip();

        writeBufferToFile(buf, "src/tests/data/serializeupdatejava.dat");
    }

    private static void writeBufferToFile(GrowableByteBuffer buf, String fileName) throws IOException {
        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(buf.array(), 0, buf.remaining());
        fos.close();
    }

    @Test
    public void testRequireThatAddAllCombinesFieldUpdates() {
        DocumentType docType = new DocumentType("my_type");
        Field field = new Field("my_int", DataType.INT);
        docType.addField(field);

        FieldUpdate fooField = FieldUpdate.createAssign(field, new IntegerFieldValue(1));
        DocumentUpdate fooUpdate = new DocumentUpdate(docType, new DocumentId("id:ns:my_type::foo:"));
        fooUpdate.addFieldUpdate(fooField);

        FieldUpdate barField = FieldUpdate.createAssign(field, new IntegerFieldValue(2));
        DocumentUpdate barUpdate = new DocumentUpdate(docType, new DocumentId("id:ns:my_type::foo:"));
        barUpdate.addFieldUpdate(barField);

        fooUpdate.addAll(barUpdate);
        assertEquals(1, fooUpdate.fieldUpdates().size());
        FieldUpdate fieldUpdate = fooUpdate.getFieldUpdate(field);
        assertNotNull(fieldUpdate);
        assertEquals(field, fieldUpdate.getField());
        assertEquals(2, fieldUpdate.getValueUpdates().size());
        ValueUpdate valueUpdate = fieldUpdate.getValueUpdate(0);
        assertNotNull(valueUpdate);
        assertTrue(valueUpdate instanceof AssignValueUpdate);
        assertEquals(new IntegerFieldValue(1), valueUpdate.getValue());
        assertNotNull(valueUpdate = fieldUpdate.getValueUpdate(1));
        assertTrue(valueUpdate instanceof AssignValueUpdate);
        assertEquals(new IntegerFieldValue(2), valueUpdate.getValue());
    }

    @Test
    public void testGetAndRemoveByName() {
        DocumentType docType = new DocumentType("my_type");
        Field my_int = new Field("my_int", DataType.INT);
        Field your_int = new Field("your_int", DataType.INT);
        docType.addField(my_int);
        docType.addField(your_int);
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId("id:this:my_type::is:a:test"));

        update.addFieldUpdate(FieldUpdate.createAssign(my_int, new IntegerFieldValue(2)));
        assertNull(update.getFieldUpdate("none-existing-field"));
        assertNull(update.removeFieldUpdate("none-existing-field"));
        assertNull(update.getFieldUpdate("your_int"));
        assertEquals(new IntegerFieldValue(2), update.getFieldUpdate("my_int").getValueUpdate(0).getValue());
        assertNull(update.removeFieldUpdate("your_int"));
        assertEquals(new IntegerFieldValue(2), update.removeFieldUpdate("my_int").getValueUpdate(0).getValue());
        assertNull(update.getFieldUpdate("my_int"));

        update.addFieldUpdate(FieldUpdate.createAssign(my_int, new IntegerFieldValue(2)));
        assertNull(update.getFieldUpdate(your_int));
        assertEquals(new IntegerFieldValue(2), update.getFieldUpdate(my_int).getValueUpdate(0).getValue());
        assertNull(update.removeFieldUpdate(your_int));
        assertEquals(new IntegerFieldValue(2), update.removeFieldUpdate(my_int).getValueUpdate(0).getValue());
        assertNull(update.getFieldUpdate(my_int));
    }

    @Test
    public void testInstantiationAndEqualsHashCode() {
        DocumentType type = new DocumentType("doo");
        DocumentUpdate d1 = new DocumentUpdate(type, new DocumentId("id:this:doo::is:a:test"));
        DocumentUpdate d2 = new DocumentUpdate(type, "id:this:doo::is:a:test");

        assertEquals(d1, d2);
        assertEquals(d1, d1);
        assertEquals(d2, d1);
        assertEquals(d2, d2);
        assertFalse(d1.equals(new Object()));
        assertEquals(d1.hashCode(), d2.hashCode());
    }

    @Test
    public void testThatApplyingToWrongTypeFails() {
        DocumentType t1 = new DocumentType("doo");
        DocumentUpdate documentUpdate = new DocumentUpdate(t1, new DocumentId("id:this:doo::is:a:test"));

        DocumentType t2 = new DocumentType("foo");
        Document document = new Document(t2, "id:this:foo::is:another:test");

        try {
            documentUpdate.applyTo(document);
            fail("Should have gotten exception here.");
        } catch (IllegalArgumentException iae) {
            //ok!
        }
    }

    @Test
    public void testFieldUpdatesInDocUp() {
        DocumentType t1 = new DocumentType("doo");
        Field f1 = new Field("field1", DataType.STRING);
        Field f2 = new Field("field2", DataType.STRING);
        t1.addField(f1);
        t1.addField(f2);

        DocumentUpdate documentUpdate = new DocumentUpdate(t1, new DocumentId("id:ns:doo::is:a:test"));

        assertEquals(0, documentUpdate.size());

        FieldUpdate fu1 = FieldUpdate.createAssign(f1, new StringFieldValue("banana"));
        FieldUpdate fu2 = FieldUpdate.createAssign(f2, new StringFieldValue("apple"));
        documentUpdate.addFieldUpdate(fu1);

        assertEquals(1, documentUpdate.size());

        documentUpdate.clearFieldUpdates();

        assertEquals(0, documentUpdate.size());

        documentUpdate.addFieldUpdate(fu1);
        documentUpdate.addFieldUpdate(fu2);

        assertEquals(2, documentUpdate.size());


        assertSame(fu1, documentUpdate.getFieldUpdate(f1));

        try {
            documentUpdate.setFieldUpdates(null);
            fail("Should have gotten NullPointerException");
        } catch (NullPointerException npe) {
            //ok!
        }

        List<FieldUpdate> fus = new ArrayList<>();
        fus.add(fu1);
        fus.add(fu2);

        documentUpdate.setFieldUpdates(fus);
        assertEquals(2, documentUpdate.size());
        assertSame(fu1, documentUpdate.getFieldUpdate(fu1.getField()));
        assertSame(fu2, documentUpdate.getFieldUpdate(fu2.getField()));

        documentUpdate.removeFieldUpdate(fu2.getField());
        assertEquals(1, documentUpdate.size());
        assertSame(fu1, documentUpdate.getFieldUpdate(fu1.getField()));


        documentUpdate.toString();

        assertFalse(documentUpdate.isEmpty());

        Iterator<FieldPathUpdate> fpUpdates = documentUpdate.iterator();
        assertFalse(fpUpdates.hasNext());
    }

    @Test
    public void testAddAll() {
        DocumentType t1 = new DocumentType("doo");
        DocumentType t2 = new DocumentType("foo");
        Field f1 = new Field("field1", DataType.STRING);
        Field f2 = new Field("field2", DataType.STRING);
        t1.addField(f1);
        t2.addField(f2);

        DocumentUpdate du1 = new DocumentUpdate(t1, new DocumentId("id:this:doo::is:a:test"));
        DocumentUpdate du2 = new DocumentUpdate(t2, "id:this:foo::is:another:test");

        FieldUpdate fu1 = FieldUpdate.createAssign(f1, new StringFieldValue("banana"));
        FieldUpdate fu2 = FieldUpdate.createAssign(f2, new StringFieldValue("apple"));
        du1.addFieldUpdate(fu1);
        du2.addFieldUpdate(fu2);

        du1.addAll(null);

        try {
            du1.addAll(du2);
            fail("Should have gotten exception");
        } catch (IllegalArgumentException iae) {
            //ok!
        }

        DocumentUpdate du3 = new DocumentUpdate(t2, new DocumentId("id:this:foo::is:a:test"));

        try {
            du1.addAll(du3);
            fail("Should have gotten exception");
        } catch (IllegalArgumentException iae) {
            //ok!
        }
    }

    private void assertDocumentUpdateFlag(boolean createIfNonExistent, int value) {
        DocumentUpdateFlags f1 = new DocumentUpdateFlags();
        f1.setCreateIfNonExistent(createIfNonExistent);
        assertEquals(createIfNonExistent, f1.getCreateIfNonExistent());
        int combined = f1.injectInto(value);
        System.out.println("createIfNonExistent=" + createIfNonExistent + ", value=" + value + ", combined=" + combined);

        DocumentUpdateFlags f2 = DocumentUpdateFlags.extractFlags(combined);
        int extractedValue = DocumentUpdateFlags.extractValue(combined);
        assertEquals(createIfNonExistent, f2.getCreateIfNonExistent());
        assertEquals(value, extractedValue);
    }

    @Test
    public void testRequireThatDocumentUpdateFlagsIsWorking() {
        { // create-if-non-existent = true
            assertDocumentUpdateFlag(true, 0);
            assertDocumentUpdateFlag(true, 1);
            assertDocumentUpdateFlag(true, 2);
            assertDocumentUpdateFlag(true, 9999);
            assertDocumentUpdateFlag(true, 0xFFFFFFE);
            assertDocumentUpdateFlag(true, 0xFFFFFFF);
        }
        { // create-if-non-existent = false
            assertDocumentUpdateFlag(false, 0);
            assertDocumentUpdateFlag(false, 1);
            assertDocumentUpdateFlag(false, 2);
            assertDocumentUpdateFlag(false, 9999);
            assertDocumentUpdateFlag(false, 0xFFFFFFE);
            assertDocumentUpdateFlag(false, 0xFFFFFFF);
        }
    }

    @Test
    public void testRequireThatCreateIfNonExistentFlagIsSerializedAndDeserialized() {
        docUp.setCreateIfNonExistent(true);

        DocumentSerializer serializer = DocumentSerializerFactory.create6(new GrowableByteBuffer());
        docUp.serialize(serializer);
        serializer.getBuf().flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(docMan, serializer.getBuf());
        DocumentUpdate deserialized = new DocumentUpdate(deserializer);
        assertEquals(docUp, deserialized);
        assertTrue(deserialized.getCreateIfNonExistent());
    }

    @Test
    public void testThatAssignValueUpdateForTensorFieldCanBeApplied() {
        Document doc = createDocument();
        assertNull(doc.getFieldValue(tensorField));

        DocumentUpdate update = createTensorAssignUpdate();
        update.applyTo(doc);

        TensorFieldValue tensor = (TensorFieldValue) doc.getFieldValue(tensorField);
        assertEquals(createTensorFieldValue("{{x:0}:2.0}"), tensor);
    }

    @Test
    public void testThatAssignValueUpdateForTensorFieldCanBeSerializedAndDeserialized() {
        DocumentUpdate serializedUpdate = createTensorAssignUpdate();
        DocumentSerializer serializer = DocumentSerializerFactory.create6(new GrowableByteBuffer());
        serializedUpdate.serialize(serializer);
        serializer.getBuf().flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(docMan, serializer.getBuf());
        DocumentUpdate deserializedUpdate = new DocumentUpdate(deserializer);
        assertEquals(serializedUpdate, deserializedUpdate);
    }

    @Test
    public void testThatClearValueUpdateForTensorFieldCanBeApplied() {
        Document doc = createDocument();
        doc.setFieldValue(docType.getField(tensorField), createTensorFieldValue("{{x:0}:2.0}"));
        assertNotNull(doc.getFieldValue(tensorField));

        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createClear(docType.getField(tensorField)));
        update.applyTo(doc);

        assertNull(doc.getFieldValue(tensorField));
    }

    @Test
    public void testThatNonIdenticalAssignCanNotBePrunedAway() {
        Field field = docType.getField("strfoo");
        String expected = "some other value";
        Document doc = createDocument();
        doc.setFieldValue(field, "some value");
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected)));
        update.prune(doc);
        assertEquals(1, update.size());
        update.applyTo(doc);
        assertEquals(expected, doc.getFieldValue(field).getWrappedValue());
    }

    @Test
    public void testThatIdenticalAssignCanBePrunedAway() {
        Field field = docType.getField("strfoo");
        String expected = "some value";
        Document doc = createDocument();
        doc.setFieldValue(field, "some value");
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createAssign(field,new StringFieldValue(expected)));
        update.prune(doc);
        assertEquals(0, update.size());
        update.applyTo(doc);
        assertEquals(expected, doc.getFieldValue(field).getWrappedValue());
    }

    @Test
    public void testThatIdenticalAssignCanBePrunedAwayIfLast() {
        Field field = docType.getField("strfoo");
        String expected = "some value";
        Document doc = createDocument();
        doc.setFieldValue(field, "some value");
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createClearField(field));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected)));
        update.prune(doc);
        assertEquals(0, update.size());
        update.applyTo(doc);
        assertEquals(expected, doc.getFieldValue(field).getWrappedValue());
    }

    @Test
    public void testThatIdenticalAssignCanNotBePrunedAwayIfNotLast() {
        Field field = docType.getField("strfoo");
        String expected = "some random value";
        Document doc = createDocument();
        doc.setFieldValue(field, "some value");
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue("some value")));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue(expected)));
        update.prune(doc);
        assertEquals(1, update.size());
        update.applyTo(doc);
        assertEquals(expected, doc.getFieldValue(field).getWrappedValue());
    }

    @Test
    public void testThatClearCanBePrunedIfNoneExisting() {
        Field field = docType.getField("strfoo");
        Document doc = createDocument();
        StringFieldValue expected = new StringFieldValue("some value");
        expected.clear();
        doc.setFieldValue(field, expected);
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createClearField(field));
        update.prune(doc);
        assertEquals(0, update.size());
        update.applyTo(doc);
        assertEquals(expected, doc.getFieldValue(field));
    }

    @Test
    public void testThatClearCanBePrunedIfEmpty() {
        Field field = docType.getField("strfoo");
        String expected = "";
        Document doc = createDocument();
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createClearField(field));
        update.prune(doc);
        assertEquals(0, update.size());
        update.applyTo(doc);
        assertNull(doc.getFieldValue(field));
    }

    @Test
    public void testThatClearCanBePrunedIfNoneExistingAndLast() {
        Field field = docType.getField("strfoo");
        String expected = "";
        Document doc = createDocument();
        DocumentUpdate update = new DocumentUpdate(docType, new DocumentId(documentId));
        update.addFieldUpdate(FieldUpdate.createAssign(field, new StringFieldValue("some value")));
        update.addFieldUpdate(FieldUpdate.createClearField(field));
        update.prune(doc);
        assertEquals(0, update.size());
        update.applyTo(doc);
        assertNull(doc.getFieldValue(field));
    }

    private static TensorFieldValue createTensorFieldValue(String tensor) {
        return new TensorFieldValue(Tensor.from(tensor));
    }

    private DocumentUpdate createTensorAssignUpdate() {
        DocumentUpdate result = new DocumentUpdate(docType, new DocumentId(documentId));
        result.addFieldUpdate(FieldUpdate.createAssign(docType.getField(tensorField),
                              createTensorFieldValue("{{x:0}:2.0}")));
        return result;
    }

    private static class TensorUpdateSerializeFixture {
        private DocumentTypeManager docMan;
        private DocumentType docType;

        public TensorUpdateSerializeFixture() {
            docMan = new DocumentTypeManager();
            docType = new DocumentType("test");
            docType.addField("sparse_tensor", new TensorDataType(TensorType.fromSpec("tensor(x{})")));
            docType.addField("dense_tensor", new TensorDataType(TensorType.fromSpec("tensor(x[4])")));
            docMan.registerDocumentType(docType);
        }

        Field getField(String name) {
            return docType.getField(name);
        }

        TensorFieldValue createTensor() {
            return new TensorFieldValue(Tensor.from("tensor(x{})", "{{x:2}:5, {x:3}:7}"));
        }

        DocumentUpdate createUpdate() {
            var result = new DocumentUpdate(docType, "id:test:test::0");

            result.addFieldUpdate(FieldUpdate.create(getField("sparse_tensor"))
                    .addValueUpdate(new AssignValueUpdate(createTensor()))
                    .addValueUpdate(new TensorAddUpdate(createTensor()))
                    .addValueUpdate(new TensorRemoveUpdate(createTensor())));

            result.addFieldUpdate(FieldUpdate.create(getField("dense_tensor"))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, createTensor()))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.ADD, createTensor()))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.MULTIPLY, createTensor()))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, createTensor(), true))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.ADD, createTensor(), true))
                    .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.MULTIPLY, createTensor(), true)));
            return result;
        }

        void serializeUpdateToFile(DocumentUpdate update, String fileName) throws IOException {
            GrowableByteBuffer buf = new GrowableByteBuffer(100, 2.0f);
            update.serialize(DocumentSerializerFactory.createHead(buf));
            buf.flip();

            writeBufferToFile(buf, fileName);
        }

        DocumentUpdate deserializeUpdateFromFile(String fileName) throws IOException {
            byte[] data = DocumentTestCase.readFile(fileName);
            DocumentDeserializer buf = DocumentDeserializerFactory.createHead(docMan, GrowableByteBuffer.wrap(data));
            return new DocumentUpdate(buf);
        }
    }

    @Test
    public void tensor_update_file_cpp_can_be_deserialized() throws IOException {
        var f = new TensorUpdateSerializeFixture();
        var update = f.deserializeUpdateFromFile("src/tests/data/serialize-tensor-update-cpp.dat");
        assertEquals(f.createUpdate(), update);
    }

    @Test
    public void generate_serialized_tensor_update_file_java() throws IOException {
        var f = new TensorUpdateSerializeFixture();
        var update = f.createUpdate();
        f.serializeUpdateToFile(update, "src/tests/data/serialize-tensor-update-java.dat");
    }

}