All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsimpledb.cli.func.DumpFunction Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.cli.func;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;

import org.jsimpledb.JObject;
import org.jsimpledb.cli.CliSession;
import org.jsimpledb.core.CounterField;
import org.jsimpledb.core.Field;
import org.jsimpledb.core.FieldSwitchAdapter;
import org.jsimpledb.core.FieldType;
import org.jsimpledb.core.ListField;
import org.jsimpledb.core.MapField;
import org.jsimpledb.core.ObjId;
import org.jsimpledb.core.SetField;
import org.jsimpledb.core.SimpleField;
import org.jsimpledb.core.Transaction;
import org.jsimpledb.parse.ObjInfo;
import org.jsimpledb.parse.expr.EvalException;
import org.jsimpledb.parse.expr.Value;
import org.jsimpledb.parse.func.Function;

@Function
public class DumpFunction extends SimpleCliFunction {

    public DumpFunction() {
        super("dump", 1, 1);
    }

    @Override
    public String getHelpSummary() {
        return "Prints all fields of the given database object to the console";
    }

    @Override
    public String getUsage() {
        return "dump(expr)";
    }

    @Override
    protected Value apply(CliSession session, Value[] params) {

        // Get object
        Object obj = params[0].checkNotNull(session, "dump()");
        if (obj instanceof JObject)
            obj = ((JObject)obj).getObjId();
        else if (!(obj instanceof ObjId))
            throw new EvalException("invalid dump() operation on non-database object of type " + obj.getClass().getName());
        final ObjId id = (ObjId)obj;

        // Dump object
        this.dump(session, id);

        // Done
        return Value.NO_VALUE;
    }

    private void dump(final CliSession session, final ObjId id) {

        // Get transaction and console writer
        final Transaction tx = session.getTransaction();
        final PrintWriter writer = session.getWriter();

        // Verify object exists
        final ObjInfo info = ObjInfo.getObjInfo(session, id);
        if (info == null) {
            writer.println("object " + id + " (does not exist)");
            return;
        }

        // Print headline
        writer.println("object " + info);

        // Calculate indent amount
        int nameFieldSize = 0;
        for (Field field : info.getObjType().getFields().values())
            nameFieldSize = Math.max(nameFieldSize, field.getName().length());
        final char[] ichars = new char[nameFieldSize + 3];
        Arrays.fill(ichars, ' ');
        final String indent = new String(ichars);
        final String eindent = indent + "  ";

        // Display fields
        for (Field field : info.getObjType().getFields().values()) {
            writer.print(String.format("%" + nameFieldSize + "s = ", field.getName()));
            field.visit(new FieldSwitchAdapter() {

                @Override
                public  Void caseSimpleField(SimpleField field) {
                    final FieldType fieldType = field.getFieldType();
                    writer.println(fieldType.toParseableString(
                      fieldType.validate(tx.readSimpleField(id, field.getStorageId(), false))));
                    return null;
                }

                @Override
                public Void caseCounterField(CounterField field) {
                    writer.println("" + tx.readCounterField(id, field.getStorageId(), false));
                    return null;
                }

                @Override
                @SuppressWarnings("unchecked")
                public  Void caseSetField(SetField field) {
                    this.handleCollection((NavigableSet)tx.readSetField(id, field.getStorageId(), false),
                      field.getElementField(), false);
                    return null;
                }

                @Override
                @SuppressWarnings("unchecked")
                public  Void caseListField(ListField field) {
                    this.handleCollection((List)tx.readListField(id, field.getStorageId(), false),
                      field.getElementField(), true);
                    return null;
                }

                private  void handleCollection(Collection items, SimpleField elementField, boolean showIndex) {
                    final FieldType fieldType = elementField.getFieldType();
                    writer.println("{");
                    int count = 0;
                    for (E item : items) {
                        if (count >= session.getLineLimit()) {
                            writer.println(eindent + "...");
                            break;
                        }
                        writer.print(eindent);
                        if (showIndex)
                            writer.print("[" + count + "] ");
                        writer.println(fieldType.toParseableString(item));
                        count++;
                    }
                    writer.println(indent + "}");
                }

                @Override
                @SuppressWarnings("unchecked")
                public  Void caseMapField(MapField field) {
                    final FieldType keyFieldType = field.getKeyField().getFieldType();
                    final FieldType valueFieldType = field.getValueField().getFieldType();
                    writer.println("{");
                    int count = 0;
                    final NavigableMap map = (NavigableMap)tx.readMapField(id, field.getStorageId(), false);
                    for (Map.Entry entry : map.entrySet()) {
                        if (count >= session.getLineLimit()) {
                            writer.println(eindent + "...");
                            break;
                        }
                        writer.println(eindent + keyFieldType.toParseableString(entry.getKey())
                          + " -> " + valueFieldType.toParseableString(entry.getValue()));
                        count++;
                    }
                    writer.println(indent + "}");
                    return null;
                }
            });
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy