Writing serial elements

To write a serial element, call the following function on a ILssTransaction:


    ICloseableOutputStream* WriteSerialElement(Seid seid);

This function is used to write new serial elements and also to re-write existing serial elements.

It returns an output stream that can be used to write the content as a byte stream. Note that the entire serial element must always be written. Then the output stream must be closed. The interface pointer must not be deleted.

It is very important to close the output stream before calling WriteSerialElement() again to write another serial element, and also before closing the transaction.

Only the thread that opened the transaction is allowed to write a serial element.

Example

The following code either opens an existing store and reads the root serial element or creates a new store if it doesn't exist and writes the root serial element. Then the store is closed. For simplicity error handling is not shown.


#include "Ceda/cxLss/ILogStructuredStore.h"
#include <assert>
#include <iostream>

void WriteRootSerialElementExample()
{
    const char* path = "myfile";

    bool createdNew;
    ceda::LssSettings settings;
    ceda::ILogStructuredStore* lss = ceda::CreateOrOpenLSS(path, nullptr, createdNew, ceda::OM_OPEN_ALWAYS, settings);
    if (createdNew)
    {
        // Create a 32 bit Seid space for all Seid allocations
        ceda::SeidHigh seidHigh = lss->CreateSeidSpace();
                
        // Allocate Seid for root object
        ceda::Seid seid = lss->AllocateSeid(seidHigh);
        assert(seid == ceda::ROOT_SEID);

        const char* buffer = "Hello world";

        ceda::ILssTransaction* txn = lss->OpenTransaction();

        // Write root object
        ceda::ICloseableOutputStream* os = txn->WriteSerialElement(seid);
        os->WriteStream(buffer, strlen(buffer)+1);
        os->Close();

        txn->Close();
    }
    else
    {
        ceda::Seid seid = ceda::ROOT_SEID;

        const int BUFSIZE = 100;
        char buffer[BUFSIZE];

        // Read root object
        ceda::ICloseableInputStream* is = lss->ReadSerialElement(seid);
		ceda::ssize_t numBytesRead = is->ReadStream(buffer, BUFSIZE);
        is->Close();

        std::cout << "Read: " << buffer << '\n';
    }

    lss->Close();
}