summaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/simplememfileiobuffertest.cpp
blob: ffe7972552f42356292b88789ee7a7e0c959bb69 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/memfilepersistence/mapper/simplememfileiobuffer.h>
#include <vespa/vdstestlib/cppunit/macros.h>
#include <tests/spi/memfiletestutils.h>
#include <tests/spi/options_builder.h>

namespace storage {
namespace memfile {

class SimpleMemFileIOBufferTest : public SingleDiskMemFileTestUtils
{
    CPPUNIT_TEST_SUITE(SimpleMemFileIOBufferTest);
    CPPUNIT_TEST(testAddAndReadDocument);
    CPPUNIT_TEST(testNonExistingLocation);
    CPPUNIT_TEST(testCopy);
    CPPUNIT_TEST(testCacheLocation);
    CPPUNIT_TEST(testPersist);
    CPPUNIT_TEST(testGetSerializedSize);
    CPPUNIT_TEST(testRemapLocations);
    CPPUNIT_TEST(testAlignmentUtilFunctions);
    CPPUNIT_TEST(testCalculatedCacheSize);
    CPPUNIT_TEST(testSharedBuffer);
    CPPUNIT_TEST(testSharedBufferUsage);
    CPPUNIT_TEST(testHeaderChunkEncoderComputesSizesCorrectly);
    CPPUNIT_TEST(testHeaderChunkEncoderSerializesIdCorrectly);
    CPPUNIT_TEST(testHeaderChunkEncoderSerializesHeaderCorrectly);
    CPPUNIT_TEST(testRemovesCanBeWrittenWithBlankDefaultDocument);
    CPPUNIT_TEST(testRemovesCanBeWrittenWithIdInferredDoctype);
    CPPUNIT_TEST(testRemovesWithInvalidDocTypeThrowsException);
    CPPUNIT_TEST_SUITE_END();

    using BufferType = SimpleMemFileIOBuffer::BufferType;
    using BufferSP = BufferType::SP;
    using BufferAllocation = SimpleMemFileIOBuffer::BufferAllocation;
    using HeaderChunkEncoder = SimpleMemFileIOBuffer::HeaderChunkEncoder;
    using SimpleMemFileIOBufferUP = std::unique_ptr<SimpleMemFileIOBuffer>;

    BufferAllocation allocateBuffer(size_t sz) {
        return BufferAllocation(BufferSP(new BufferType(sz)), 0, sz);
    }

    /**
     * Create an I/O buffer instance with for a dummy bucket. If removeDocType
     * is non-empty, remove entries will be written in backwards compatible
     * mode.
     */
    SimpleMemFileIOBufferUP createIoBufferWithDummySpec(
            vespalib::stringref removeDocType = "");

public:
    class DummyFileReader : public VersionSerializer {
    public:
        virtual FileVersion getFileVersion() override { return FileVersion(); }
        virtual void loadFile(MemFile&, Environment&, Buffer&, uint64_t ) override {}
        virtual FlushResult flushUpdatesToFile(MemFile&, Environment&) override {
            return FlushResult::TooSmall;
        }
        virtual void rewriteFile(MemFile&, Environment&) override {}
        virtual bool verify(MemFile&, Environment&,
                            std::ostream&, bool, uint16_t) override { return false; };
        virtual void cacheLocations(MemFileIOInterface&,
                                    Environment&,
                                    const Options&,
                                    DocumentPart,
                                    const std::vector<DataLocation>&) override {}
    };

    DummyFileReader dfr;

    void testAddAndReadDocument();
    void testNonExistingLocation();
    void testCopy();
    void testCacheLocation();
    void testPersist();
    void testGetSerializedSize();
    void testRemapLocations();
    void testAlignmentUtilFunctions();
    void testCalculatedCacheSize();
    void testSharedBuffer();
    void testSharedBufferUsage();
    void testHeaderChunkEncoderComputesSizesCorrectly();
    void testHeaderChunkEncoderSerializesIdCorrectly();
    void testHeaderChunkEncoderSerializesHeaderCorrectly();
    void testRemovesCanBeWrittenWithBlankDefaultDocument();
    void testRemovesCanBeWrittenWithIdInferredDoctype();
    void testRemovesWithInvalidDocTypeThrowsException();
};

CPPUNIT_TEST_SUITE_REGISTRATION(SimpleMemFileIOBufferTest);


void
SimpleMemFileIOBufferTest::testAddAndReadDocument()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");
    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       456,
                                       789,
                                       1234));

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 std::unique_ptr<FileInfo>(new FileInfo),
                                 fileSpec,
                                 env());

    DataLocation h = buffer.addHeader(*doc);
    DataLocation b = buffer.addBody(*doc);

    Document::UP newDoc = buffer.getDocumentHeader(*getTypeRepo(), h);
    buffer.readBody(*getTypeRepo(), b, *newDoc);

    CPPUNIT_ASSERT_EQUAL(*doc, *newDoc);
    CPPUNIT_ASSERT_EQUAL(true, buffer.isCached(h, HEADER));
    CPPUNIT_ASSERT_EQUAL(true, buffer.isCached(b, BODY));
    CPPUNIT_ASSERT_EQUAL(false, buffer.isCached(h, BODY));
    CPPUNIT_ASSERT_EQUAL(false, buffer.isCached(b, HEADER));
    CPPUNIT_ASSERT_EQUAL(doc->getId(), buffer.getDocumentId(h));
}

void
SimpleMemFileIOBufferTest::testPersist()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");
    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       456,
                                       789,
                                       1234));

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 std::unique_ptr<FileInfo>(new FileInfo),
                                 fileSpec,
                                 env());

    DataLocation h = buffer.addHeader(*doc);
    DataLocation b = buffer.addBody(*doc);

    CPPUNIT_ASSERT(!buffer.isPersisted(h, HEADER));
    CPPUNIT_ASSERT(!buffer.isPersisted(b, BODY));

    buffer.persist(HEADER, h, DataLocation(1000, h.size()));
    buffer.persist(BODY, b, DataLocation(5000, b.size()));

    Document::UP newDoc = buffer.getDocumentHeader(*getTypeRepo(), DataLocation(1000, h.size()));
    buffer.readBody(*getTypeRepo(), DataLocation(5000, b.size()), *newDoc);

    CPPUNIT_ASSERT(buffer.isPersisted(DataLocation(1000, h.size()), HEADER));
    CPPUNIT_ASSERT(buffer.isPersisted(DataLocation(5000, b.size()), BODY));

    CPPUNIT_ASSERT_EQUAL(*doc, *newDoc);
}

void
SimpleMemFileIOBufferTest::testCopy()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");
    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 std::unique_ptr<FileInfo>(new FileInfo),
                                 fileSpec,
                                 env());

    for (uint32_t i = 0; i < 10; ++i) {
        document::Document::SP doc(createRandomDocumentAtLocation(
                                           123,
                                           456,
                                           789,
                                           1234));

        DataLocation h = buffer.addHeader(*doc);
        DataLocation b = buffer.addBody(*doc);

        SimpleMemFileIOBuffer buffer2(dfr,
                                      vespalib::LazyFile::UP(),
                                      std::unique_ptr<FileInfo>(new FileInfo),
                                      fileSpec,
                                      env());

        DataLocation h2 = buffer2.copyCache(buffer, HEADER, h);
        DataLocation b2 = buffer2.copyCache(buffer, BODY, b);

        Document::UP newDoc = buffer2.getDocumentHeader(*getTypeRepo(), h2);
        buffer2.readBody(*getTypeRepo(), b2, *newDoc);

        CPPUNIT_ASSERT_EQUAL(*doc, *newDoc);
    }
}

void
SimpleMemFileIOBufferTest::testNonExistingLocation()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");
    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       456,
                                       789,
                                       1234));

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 std::unique_ptr<FileInfo>(new FileInfo),
                                 fileSpec,
                                 env());

    DataLocation h = buffer.addHeader(*doc);
    DataLocation b = buffer.addBody(*doc);

    buffer.clear(HEADER);

    try {
        Document::UP newDoc = buffer.getDocumentHeader(*getTypeRepo(), h);
        CPPUNIT_ASSERT(false);
    } catch (SimpleMemFileIOBuffer::PartNotCachedException& e) {
    }

    buffer.clear(BODY);

    try {
        document::Document newDoc;
        buffer.readBody(*getTypeRepo(), b, newDoc);
        CPPUNIT_ASSERT(false);
    } catch (SimpleMemFileIOBuffer::PartNotCachedException& e) {
    }
}

void
SimpleMemFileIOBufferTest::testCacheLocation()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 FileInfo::UP(new FileInfo(100, 10000, 50000)),
                                 fileSpec,
                                 env());

    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       456,
                                       789,
                                       1234));

    BufferAllocation headerBuf = buffer.serializeHeader(*doc);
    BufferAllocation bodyBuf = buffer.serializeBody(*doc);

    DataLocation hloc(1234, headerBuf.getSize());
    DataLocation bloc(5678, bodyBuf.getSize());

    buffer.cacheLocation(HEADER, hloc, headerBuf.getSharedBuffer(), 0);
    buffer.cacheLocation(BODY, bloc, bodyBuf.getSharedBuffer(), 0);

    Document::UP newDoc = buffer.getDocumentHeader(*getTypeRepo(), hloc);
    buffer.readBody(*getTypeRepo(), bloc, *newDoc);

    CPPUNIT_ASSERT_EQUAL(*doc, *newDoc);
}

void
SimpleMemFileIOBufferTest::testGetSerializedSize()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 FileInfo::UP(new FileInfo(100, 10000, 50000)),
                                 fileSpec,
                                 env());

    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       456,
                                       789,
                                       1234));

    BufferAllocation headerBuf = buffer.serializeHeader(*doc);
    BufferAllocation bodyBuf = buffer.serializeBody(*doc);

    DataLocation hloc(1234, headerBuf.getSize());
    DataLocation bloc(5678, bodyBuf.getSize());

    buffer.cacheLocation(HEADER, hloc, headerBuf.getSharedBuffer(), 0);
    buffer.cacheLocation(BODY, bloc, bodyBuf.getSharedBuffer(), 0);

    vespalib::nbostream serializedHeader;
    doc->serializeHeader(serializedHeader);

    vespalib::nbostream serializedBody;
    doc->serializeBody(serializedBody);

    CPPUNIT_ASSERT_EQUAL(uint32_t(serializedHeader.size()),
                         buffer.getSerializedSize(HEADER, hloc));
    CPPUNIT_ASSERT_EQUAL(uint32_t(serializedBody.size()),
                         buffer.getSerializedSize(BODY, bloc));
}

// Test that remapping does not overwrite datalocations that it has
// already updated
void
SimpleMemFileIOBufferTest::testRemapLocations()
{
    FileSpecification fileSpec(BucketId(16, 123), env().getDirectory(), "testfile.0");

    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 FileInfo::UP(new FileInfo(100, 10000, 50000)),
                                 fileSpec,
                                 env());

    document::Document::SP doc(createRandomDocumentAtLocation(
                                       123,
                                       100,
                                       100));
    BufferAllocation headerBuf = buffer.serializeHeader(*doc);
    BufferAllocation bodyBuf = buffer.serializeBody(*doc);

    document::Document::SP doc2(createRandomDocumentAtLocation(
                                       123,
                                       100,
                                       100));

    BufferAllocation headerBuf2 = buffer.serializeHeader(*doc2);
    BufferAllocation bodyBuf2 = buffer.serializeBody(*doc2);

    DataLocation hloc(30000, headerBuf.getSize());
    DataLocation hloc2(0, headerBuf2.getSize());
    DataLocation hloc3(10000, hloc2._size);

    buffer.cacheLocation(HEADER, hloc, headerBuf.getSharedBuffer(), 0);
    buffer.cacheLocation(HEADER, hloc2, headerBuf2.getSharedBuffer(), 0);

    std::map<DataLocation, DataLocation> remapping;
    remapping[hloc2] = hloc;
    remapping[hloc] = hloc3;

    buffer.remapAndPersistAllLocations(HEADER, remapping);

    Document::UP newDoc = buffer.getDocumentHeader(*getTypeRepo(), hloc3);
    document::ByteBuffer bbuf(bodyBuf.getBuffer(), bodyBuf.getSize());
    newDoc->deserializeBody(*getTypeRepo(), bbuf);

    CPPUNIT_ASSERT_EQUAL(*doc, *newDoc);

    Document::UP newDoc2 = buffer.getDocumentHeader(*getTypeRepo(), hloc);
    document::ByteBuffer bbuf2(bodyBuf.getBuffer(), bodyBuf.getSize());
    newDoc2->deserializeBody(*getTypeRepo(), bbuf2);
    CPPUNIT_ASSERT_EQUAL(*doc2, *newDoc2);
}

/**
 * Not technically a part of SimpleMemFileIOBuffer, but used by it and
 * currently contained within its header file. Move test somewhere else
 * if the code itself is moved.
 */
void
SimpleMemFileIOBufferTest::testAlignmentUtilFunctions()
{
    using namespace util;
    CPPUNIT_ASSERT_EQUAL(size_t(0), alignUpPow2<4096>(0));
    CPPUNIT_ASSERT_EQUAL(size_t(4096), alignUpPow2<4096>(1));
    CPPUNIT_ASSERT_EQUAL(size_t(4096), alignUpPow2<4096>(512));
    CPPUNIT_ASSERT_EQUAL(size_t(4096), alignUpPow2<4096>(4096));
    CPPUNIT_ASSERT_EQUAL(size_t(8192), alignUpPow2<4096>(4097));
    CPPUNIT_ASSERT_EQUAL(size_t(32), alignUpPow2<16>(20));
    CPPUNIT_ASSERT_EQUAL(size_t(32), alignUpPow2<32>(20));
    CPPUNIT_ASSERT_EQUAL(size_t(64), alignUpPow2<64>(20));
    CPPUNIT_ASSERT_EQUAL(size_t(128), alignUpPow2<128>(20));

    CPPUNIT_ASSERT_EQUAL(uint32_t(0), nextPow2(0));
    CPPUNIT_ASSERT_EQUAL(uint32_t(1), nextPow2(1));
    CPPUNIT_ASSERT_EQUAL(uint32_t(4), nextPow2(3));
    CPPUNIT_ASSERT_EQUAL(uint32_t(16), nextPow2(15));
    CPPUNIT_ASSERT_EQUAL(uint32_t(64), nextPow2(40));
    CPPUNIT_ASSERT_EQUAL(uint32_t(64), nextPow2(64));
}

/**
 * Test that allocated buffers are correctly reported with their sizes
 * rounded up to account for mmap overhead.
 */
void
SimpleMemFileIOBufferTest::testCalculatedCacheSize()
{
    FileSpecification fileSpec(BucketId(16, 123),
                               env().getDirectory(), "testfile.0");
    SimpleMemFileIOBuffer buffer(dfr,
                                 vespalib::LazyFile::UP(),
                                 std::unique_ptr<FileInfo>(new FileInfo),
                                 fileSpec,
                                 env());

    CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.getCachedSize(HEADER));
    CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.getCachedSize(BODY));

    // All buffers are on a 4k page granularity.
    BufferAllocation sharedHeaderBuffer(allocateBuffer(1500)); // -> 4096
    buffer.cacheLocation(HEADER, DataLocation(0, 85),
                         sharedHeaderBuffer.getSharedBuffer(), 0);
    CPPUNIT_ASSERT_EQUAL(size_t(4096), buffer.getCachedSize(HEADER));

    buffer.cacheLocation(HEADER, DataLocation(200, 100),
                         sharedHeaderBuffer.getSharedBuffer(), 85);
    CPPUNIT_ASSERT_EQUAL(size_t(4096), buffer.getCachedSize(HEADER));

    BufferAllocation singleHeaderBuffer(allocateBuffer(200)); // -> 4096
    buffer.cacheLocation(HEADER, DataLocation(0, 100),
                         singleHeaderBuffer.getSharedBuffer(), 0);
    CPPUNIT_ASSERT_EQUAL(size_t(8192), buffer.getCachedSize(HEADER));

    BufferAllocation singleBodyBuffer(allocateBuffer(300)); // -> 4096
    buffer.cacheLocation(BODY, DataLocation(0, 100),
                         singleBodyBuffer.getSharedBuffer(), 0);
    CPPUNIT_ASSERT_EQUAL(size_t(4096), buffer.getCachedSize(BODY));

    buffer.clear(HEADER);
    CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.getCachedSize(HEADER));

    buffer.clear(BODY);
    CPPUNIT_ASSERT_EQUAL(size_t(0), buffer.getCachedSize(BODY));
}

void
SimpleMemFileIOBufferTest::testSharedBuffer()
{
    typedef SimpleMemFileIOBuffer::SharedBuffer SharedBuffer;

    {
        SharedBuffer buf(1024);
        CPPUNIT_ASSERT_EQUAL(size_t(1024), buf.getSize());
        CPPUNIT_ASSERT_EQUAL(size_t(1024), buf.getFreeSize());
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.getUsedSize());
        CPPUNIT_ASSERT(buf.hasRoomFor(1024));
        CPPUNIT_ASSERT(!buf.hasRoomFor(1025));

        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.allocate(13));
        // Allocation should be rounded up to nearest alignment.
        // TODO: is this even necessary?
        CPPUNIT_ASSERT_EQUAL(size_t(16), buf.getUsedSize());
        CPPUNIT_ASSERT_EQUAL(size_t(1008), buf.getFreeSize());
        CPPUNIT_ASSERT(buf.hasRoomFor(1008));
        CPPUNIT_ASSERT(!buf.hasRoomFor(1009));
        CPPUNIT_ASSERT_EQUAL(size_t(16), buf.allocate(1));
        CPPUNIT_ASSERT_EQUAL(size_t(24), buf.getUsedSize());

        CPPUNIT_ASSERT_EQUAL(size_t(24), buf.allocate(999));
        CPPUNIT_ASSERT(!buf.hasRoomFor(1));
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.getFreeSize());
        CPPUNIT_ASSERT_EQUAL(size_t(1024), buf.getUsedSize());
    }
    // Test exact fit.
    {
        SharedBuffer buf(1024);
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.allocate(1024));
        CPPUNIT_ASSERT(!buf.hasRoomFor(1));
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.getFreeSize());
        CPPUNIT_ASSERT_EQUAL(size_t(1024), buf.getUsedSize());
    }
    // Test 512-byte alignment.
    {
        SharedBuffer buf(1024);
        CPPUNIT_ASSERT(buf.hasRoomFor(1000, SharedBuffer::ALIGN_512_BYTES));
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.allocate(10));
        CPPUNIT_ASSERT(!buf.hasRoomFor(1000, SharedBuffer::ALIGN_512_BYTES));
        CPPUNIT_ASSERT(!buf.hasRoomFor(513, SharedBuffer::ALIGN_512_BYTES));
        CPPUNIT_ASSERT(buf.hasRoomFor(512, SharedBuffer::ALIGN_512_BYTES));
        CPPUNIT_ASSERT_EQUAL(size_t(512), buf.allocate(512, SharedBuffer::ALIGN_512_BYTES));
        CPPUNIT_ASSERT_EQUAL(size_t(0), buf.getFreeSize());
        CPPUNIT_ASSERT_EQUAL(size_t(1024), buf.getUsedSize());
    }
}

void
SimpleMemFileIOBufferTest::testSharedBufferUsage()
{
    FileSpecification fileSpec(BucketId(16, 123),
                               env().getDirectory(), "testfile.0");
    SimpleMemFileIOBuffer ioBuf(dfr,
                                vespalib::LazyFile::UP(),
                                std::unique_ptr<FileInfo>(new FileInfo),
                                fileSpec,
                                env());

    const size_t threshold = SimpleMemFileIOBuffer::WORKING_BUFFER_SIZE;

    // Brand new allocation
    BufferAllocation ba(ioBuf.allocateBuffer(HEADER, 1));
    CPPUNIT_ASSERT(ba.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(0), ba.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(1), ba.size);
    // Should reuse buffer, but get other offset
    BufferAllocation ba2(ioBuf.allocateBuffer(HEADER, 500));
    CPPUNIT_ASSERT_EQUAL(ba.buf.get(), ba2.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(8), ba2.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(500), ba2.size);
    CPPUNIT_ASSERT_EQUAL(size_t(512), ba2.buf->getUsedSize());

    // Allocate a buffer so big that it should get its own buffer instance
    BufferAllocation ba3(ioBuf.allocateBuffer(HEADER, threshold));
    CPPUNIT_ASSERT(ba3.buf.get() != ba2.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(0), ba3.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(threshold), ba3.size);

    // But smaller allocs should still be done from working buffer
    BufferAllocation ba4(ioBuf.allocateBuffer(HEADER, 512));
    CPPUNIT_ASSERT_EQUAL(ba.buf.get(), ba4.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(512), ba4.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(512), ba4.size);
    CPPUNIT_ASSERT_EQUAL(size_t(1024), ba4.buf->getUsedSize());

    // Allocate lots of smaller buffers from the same buffer until we run out.
    while (true) {
        BufferAllocation tmp(ioBuf.allocateBuffer(HEADER, 1024));
        CPPUNIT_ASSERT_EQUAL(ba.buf.get(), tmp.buf.get());
        if (!tmp.buf->hasRoomFor(2048)) {
            break;
        }
    }
    BufferAllocation ba5(ioBuf.allocateBuffer(HEADER, 2048));
    CPPUNIT_ASSERT(ba5.buf.get() != ba.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(0), ba5.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(2048), ba5.size);

    // Allocating for different part should get different buffer.
    BufferAllocation ba6(ioBuf.allocateBuffer(BODY, 128));
    CPPUNIT_ASSERT(ba6.buf.get() != ba5.buf.get());
    CPPUNIT_ASSERT_EQUAL(uint32_t(0), ba6.pos);
    CPPUNIT_ASSERT_EQUAL(uint32_t(128), ba6.size);
}

void
SimpleMemFileIOBufferTest::testHeaderChunkEncoderComputesSizesCorrectly()
{
    document::Document::SP doc(createRandomDocumentAtLocation(123, 100, 100));

    std::string idString = doc->getId().toString();
    HeaderChunkEncoder encoder(doc->getId());
    // Without document, payload is: 3x u32 + doc id string (no zero term).
    CPPUNIT_ASSERT_EQUAL(sizeof(uint32_t)*3 + idString.size(),
                         static_cast<size_t>(encoder.encodedSize()));

    encoder.bufferDocument(*doc);
    vespalib::nbostream stream;
    doc->serializeHeader(stream);
    // With document, add size of serialized document to the mix.
    CPPUNIT_ASSERT_EQUAL(sizeof(uint32_t)*3 + idString.size() + stream.size(),
                         static_cast<size_t>(encoder.encodedSize()));
}

SimpleMemFileIOBufferTest::SimpleMemFileIOBufferUP
SimpleMemFileIOBufferTest::createIoBufferWithDummySpec(
        vespalib::stringref removeDocType)
{
    FileSpecification fileSpec(BucketId(16, 123),
                               env().getDirectory(), "testfile.0");
    // Override config.
    auto options = env().acquireConfigReadLock().options();
    env().acquireConfigWriteLock().setOptions(
            OptionsBuilder(*options)
                .defaultRemoveDocType(removeDocType)
                .build());

    SimpleMemFileIOBufferUP ioBuf(
            new SimpleMemFileIOBuffer(
                dfr,
                vespalib::LazyFile::UP(),
                std::unique_ptr<FileInfo>(new FileInfo),
                fileSpec,
                env()));
    return ioBuf;
}

void
SimpleMemFileIOBufferTest::testHeaderChunkEncoderSerializesIdCorrectly()
{
    document::Document::SP doc(createRandomDocumentAtLocation(123, 100, 100));
    HeaderChunkEncoder encoder(doc->getId());

    SimpleMemFileIOBufferUP ioBuf(createIoBufferWithDummySpec());

    BufferAllocation buf(ioBuf->allocateBuffer(HEADER, encoder.encodedSize()));
    encoder.writeTo(buf);
    DataLocation newLoc = ioBuf->addLocation(HEADER, buf);
    document::DocumentId checkId = ioBuf->getDocumentId(newLoc);

    CPPUNIT_ASSERT_EQUAL(doc->getId(), checkId);
}

void
SimpleMemFileIOBufferTest::testHeaderChunkEncoderSerializesHeaderCorrectly()
{
    document::Document::SP doc(createRandomDocumentAtLocation(123, 100, 100));
    HeaderChunkEncoder encoder(doc->getId());
    encoder.bufferDocument(*doc);

    SimpleMemFileIOBufferUP ioBuf(createIoBufferWithDummySpec());
    BufferAllocation buf(ioBuf->allocateBuffer(HEADER, encoder.encodedSize()));
    encoder.writeTo(buf);
    DataLocation newLoc = ioBuf->addLocation(HEADER, buf);
    Document::UP checkDoc = ioBuf->getDocumentHeader(*getTypeRepo(), newLoc);

    CPPUNIT_ASSERT_EQUAL(doc->getId(), checkDoc->getId());
    CPPUNIT_ASSERT_EQUAL(doc->getType(), checkDoc->getType());
}

void
SimpleMemFileIOBufferTest::testRemovesCanBeWrittenWithBlankDefaultDocument()
{
    SimpleMemFileIOBufferUP ioBuf(createIoBufferWithDummySpec("testdoctype1"));

    document::DocumentId id("userdoc:yarn:12345:fluff");
    DataLocation loc(ioBuf->addDocumentIdOnlyHeader(id, *getTypeRepo()));
    // Despite adding with document id only, we should now actually have a
    // valid document header. Will fail with a DeserializeException if no
    // header has been written.
    Document::UP removeWithHeader(
            ioBuf->getDocumentHeader(*getTypeRepo(), loc));
    CPPUNIT_ASSERT_EQUAL(removeWithHeader->getId(), id);
    CPPUNIT_ASSERT_EQUAL(removeWithHeader->getType(),
                         *getTypeRepo()->getDocumentType("testdoctype1"));
}

void
SimpleMemFileIOBufferTest::testRemovesCanBeWrittenWithIdInferredDoctype()
{
    SimpleMemFileIOBufferUP ioBuf(createIoBufferWithDummySpec("testdoctype1"));

    document::DocumentId id("id:yarn:testdoctype2:n=12345:fluff");
    DataLocation loc(ioBuf->addDocumentIdOnlyHeader(id, *getTypeRepo()));
    // Since document id contains an explicit document type, the blank remove
    // document header should be written with that type instead of the one
    // provided as default via config.
    Document::UP removeWithHeader(
            ioBuf->getDocumentHeader(*getTypeRepo(), loc));
    CPPUNIT_ASSERT_EQUAL(removeWithHeader->getId(), id);
    CPPUNIT_ASSERT_EQUAL(removeWithHeader->getType(),
                         *getTypeRepo()->getDocumentType("testdoctype2"));
}

void
SimpleMemFileIOBufferTest::testRemovesWithInvalidDocTypeThrowsException()
{
    SimpleMemFileIOBufferUP ioBuf(createIoBufferWithDummySpec("testdoctype1"));

    document::DocumentId id("id:yarn:nosuchtype:n=12345:fluff");
    try {
        DataLocation loc(ioBuf->addDocumentIdOnlyHeader(id, *getTypeRepo()));
        CPPUNIT_FAIL("No exception thrown on bad doctype");
    } catch (const vespalib::Exception& e) {
        CPPUNIT_ASSERT(e.getMessage().find("Could not serialize document "
                                           "for remove with unknown doctype "
                                           "'nosuchtype'")
                       != std::string::npos);
    }
}

} // memfile
} // storage