~jgillis/flatbuffers-read-writing

2bb0af373114ddc9d7f759192bf7128ad438f8d1 — Joris Gillis 4 years ago 8945fb0
Writing out a list of people to file
4 files changed, 66 insertions(+), 10 deletions(-)

M pom.xml
M src/main/fbs/person.fbs
M src/main/java/com/example/flatbuffers/FlatbuffersApplication.java
R src/main/java/com/example/flatbuffers/{WorkingOnFlatBuffers.java => GeneratingAndWriting.java}
M pom.xml => pom.xml +5 -0
@@ 35,6 35,11 @@
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>

    <build>

M src/main/fbs/person.fbs => src/main/fbs/person.fbs +4 -0
@@ 1,5 1,9 @@
namespace com.example.flatbuffers.model;

table People {
    people:[Person];
}

table Person {
    firstName:string;
    lastName:string;

M src/main/java/com/example/flatbuffers/FlatbuffersApplication.java => src/main/java/com/example/flatbuffers/FlatbuffersApplication.java +10 -1
@@ 1,8 1,17 @@
package com.example.flatbuffers;

import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FlatbuffersApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlatbuffersApplication.class);

    public static void main(String[] args) {
        new WorkingOnFlatBuffers().test();
        StopWatch stopWatch = StopWatch.createStarted();
        int numberOfPeople = 1_047_031;
        new GeneratingAndWriting().writingPeople(numberOfPeople);
        stopWatch.stop();
        LOGGER.info("Generating and writig took: {}", stopWatch);
    }
}

R src/main/java/com/example/flatbuffers/WorkingOnFlatBuffers.java => src/main/java/com/example/flatbuffers/GeneratingAndWriting.java +47 -9
@@ 1,29 1,59 @@
package com.example.flatbuffers;

import com.example.flatbuffers.model.Address;
import com.example.flatbuffers.model.People;
import com.example.flatbuffers.model.Person;
import com.google.flatbuffers.FlatBufferBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.ThreadLocalRandom;

import static com.example.flatbuffers.Util.pickRandom;

public class WorkingOnFlatBuffers {
public class GeneratingAndWriting {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkingOnFlatBuffers.class);
    private static final Logger LOGGER = LoggerFactory.getLogger(GeneratingAndWriting.class);

    private static final String[] FIRST_NAMES = new String[]{"John", "Jane", "Gerard", "Aubrey", "Amelia"};
    private static final String[] LAST_NAMES = new String[]{"Smith", "Parker", "Phillips", "Jones"};
    private static final String[] STREETS = new String[]{
            "Halloway", "Sunset Boulvard", "Wall Street", "Secret Passageway"};
    private static final String[] CITIES = new String[]{"Brussels", "Paris", "London", "Amsterdam"};
    public static final String PERSON_LINE_TEMPLATE = " * {}";
    public static final String ADDRESS_LINE_TEMPLATE = "   * {} {}";
    private static final String PERSON_LINE_TEMPLATE = " * {}";
    private static final String ADDRESS_LINE_TEMPLATE = "   * {} {}";

    public void writingPeople(int numberOfPeople) {
        // Writing out data to file
        try (FileOutputStream fd = new FileOutputStream("people.flatbuffers")) {
            fd.write(createRandomPeople(numberOfPeople));
        } catch (IOException e) {
            LOGGER.error("Failed to write FlatBuffer data");
        }
    }

    private byte[] createRandomPeople(int numberOfPeople) {
        FlatBufferBuilder builder = new FlatBufferBuilder();

        int[] personOffsets = new int[numberOfPeople];
        for (int i = 0; i < numberOfPeople; i++) {
            personOffsets[i] = buildRandomPerson(builder);
        }

        int peopleVector = People.createPeopleVector(builder, personOffsets);

        People.startPeople(builder);
        People.addPeople(builder, peopleVector);
        int peopleOffset = People.endPeople(builder);

        builder.finish(peopleOffset);
        return builder.sizedByteArray();
    }

    public void test() {
    public void writingAndReadingAPerson() {
        // Writing data into buffer
        ByteBuffer data = createPerson();



@@ 49,7 79,18 @@ public class WorkingOnFlatBuffers {
     */
    private ByteBuffer createPerson() {
        FlatBufferBuilder builder = new FlatBufferBuilder();
        int person = buildRandomPerson(builder);
        builder.finish(person);
        return builder.dataBuffer();
    }

    /**
     * Random person created in the builder
     *
     * @param builder FlatBufferBuilder to construct person
     * @return offset of created person
     */
    private int buildRandomPerson(FlatBufferBuilder builder) {
        int street = builder.createString(randomStreet());
        int city = builder.createString(randomCity());
        int address = Address.createAddress(builder, street, randomStreetNumber(), city, randomPostalCode());


@@ 61,10 102,7 @@ public class WorkingOnFlatBuffers {
        Person.addFirstName(builder, firstName);
        Person.addLastName(builder, lastName);
        Person.addAddress(builder, address);
        int person = Person.endPerson(builder);

        builder.finish(person);
        return builder.dataBuffer();
        return Person.endPerson(builder);
    }

    private String randomFirstName() {