package pstore.examples;

import java.util.ArrayList;
import java.util.List;
import pstore.*;

import static pstore.PStoreFileMode.READ_ONLY;
import static pstore.PStoreFileMode.WRITE_ONLY;

public class PStore {
  static { System.loadLibrary("pstore-java"); }

  private static final String FILENAME = "/tmp/pstore-" + System.currentTimeMillis() + ".pstore";

  public static void main(String[] args) {
    writeDatabase();
    printDatabase();
  }

  private static void writeDatabase() {
    PStoreFile file = new PStoreFile(FILENAME, WRITE_ONLY);
    writeTable(table(), rows(), file);
    file.close();
  }

  private static void writeTable(Table table, List<Row> rows, PStoreFile output) {
    Header header = createHeader(table);
    header.write(output);
    IteratorState state = new IteratorState(rows.iterator());
    table.importValues(output, state);
    header.write(output);
    state.release();
    header.release();
  }

  private static void printDatabase() {
    PStoreFile input = new PStoreFile(FILENAME, READ_ONLY);
    Header header = Header.read(input);
    for (Table table : header.getTables())
      printTable(table, input);
  }

  private static void printTable(Table table, PStoreFile input) {
    for (Column col : table.getColumns())
      printColumn(col, input);
  }

  private static void printColumn(Column col, PStoreFile input) {
    Segment segment = Segment.read(col, input);
    for (;;) {
      String value = segment.next();
      if (value == null)
        break;
      System.out.println(value);
    }
    segment.release();
  }

  private static Table table() {
    Table table = new Table("table", 0);
    for (Column column : columns())
      table.add(column);
    return table;
  }

  private static List<Column> columns() {
    List<Column> cols = new ArrayList<Column>();
    cols.add(new Column("Column1", 0, ValueType.STRING));
    cols.add(new Column("Column2", 1, ValueType.STRING));
    cols.add(new Column("Column3", 2, ValueType.STRING));
    return cols;
  }

  private static List<Row> rows() {
    List<Row> rows = new ArrayList<Row>();
    rows.add(new Row("col1.1", "col1.2", "col1.3"));
    rows.add(new Row("col2.1", "col2.2", "col2.3"));
    return rows;
  }

  private static Header createHeader(Table table) {
    Header header = new Header();
    header.insertTable(table);
    return header;
  }
}

Add example that uses P-Store Java API
java: Add example that uses P-Store Java API

Signed-off-by: Karim Osman <94396faf6cc817be76165477038b3cfa832e89a4@reaktor.fi>
Signed-off-by: Kare Nuorteva <9ddc85c347529f7b35a7cd6b44bef03c7222a8be@reaktor.fi>
Signed-off-by: Riku Hurmalainen <6c06622681729c81e7a2d921b037b07ad9f1070c@reaktor.fi>
Signed-off-by: Pekka Enberg <add4fcd06328a394f0ad91feda7ee057316dc5ed@kernel.org>
