< Back

Contents

Script Storage

Storage is an easy way to store persistent data.

Example

Record is a helper class to make it easier to handle object-like data (key-value).

Full Example to store and read records:

RecordDef recordDef = new RecordDef()
  .add("firstname")
  .addString("lastname")
  .addLong("age")
  .addDouble("height")
  .addBoolean("promoted")
  .addDate("created");

Record record = new Record()
  .set("firstname", "John")
  .set("lastname", "Woods")
  .set("age", 48L)
  .set("height", 1.92)
  .set("promoted", true)
  .set("created", new Date());

// Write
STORAGE.set("managers", Arrays.asList(record), recordDef);

// Read
List<Record> managerRecords = STORAGE.get("managers", recordDef);

// Display Information
System.out.println(managerRecords);

Available data types:

The class is located in, it is imported automatically: recordlib.Record

Read values

Also showns alternative method names, with same result.

Record record1 = new Record()
  .set("title", "Learn to get up late")
  .set("count", 16)
  .set("price", 11.99)
  .set("promoted", true)
  .set("created", new Date());

// Get Values
String title = record1.get("title");
String title2 = record1.getString("title");
Long count = record1.getLong("count");
Double price = record1.getDouble("price");
Boolean promoted = record1.getBoolean("promoted");
Date date = record1.getDate("created");

// Or:
String title = record1.getStringValue("title");
Long count = record1.getLongValue("count");
Double price = record1.getDoubleValue("price");
Boolean promoted = record1.getBooleanValue("promoted");
Date date = record1.getDateValue("created");

Separate Script: "shared.groovy"

It can be helpful to handle the reading/writing of data in a separate file that can be included, with e.g.

// include shared.groovy

shared.groovy:

class Shared {
    private static RecordDef recordDef;
    public static List<Record> records;
    
    static {
        recordDef = new RecordDef()
		  .add("firstname")
          .add("lastname")
          .addLong("age")
          .addDouble("height_cm");
    
        readData();
    }
    
    private static void readData() {
        records = STORAGE.get("records", recordDef);
    }
    
    private static void writeData() {
        STORAGE.set("records", records, recordDef);
    }
    
    public static void printAll() {
        for (Record record : records) {
            System.out.println(record);
        }
    }
}

show.groovy: (any other script)

// include shared.groovy

Shared.records.add(new Record().set("firstname", "Robert"));

Shared.writeData();
Shared.printAll();

Definition

The definition sets data types and parameter names.

Doing this it is similar to a SQL column definition.

RecordDef recordDef = new RecordDef();

// Define column of type String (Default)
recordDef.add("firstname");

// More data types: Long, Double, Boolean, Date
recordDef.addLong("age");
recordDef.addDouble("height_cm");
recordDef.addBoolean("premium");
recordDef.addDate("registered");