Skip to content

Java11 Lambda Index

Fast ACID NoSQL Application Database edited this page Sep 27, 2019 · 1 revision

Java Style One Line Query

package example;

import com.db4o.Db4oEmbedded;
import com.db4o.config.ConfigScope;
import com.db4o.config.annotations.Indexed;
import com.db4o.io.MemoryStorage;
import com.db4o.ta.TransparentActivationSupport;
import com.db4o.ta.TransparentPersistenceSupport;
import java.util.Random;
 
public class LambdaIndex {

    public static class Detail {

        @Indexed
        public long type;

        public String memo;

        public Record record;
    }

    public static class Record {

        @Indexed
        public String name;

        public String noName;

        @Indexed
        public double indexField;

        public double noIndexField;

        public Detail detail;
    }

    public static void main(String[] args) throws InterruptedException {

        String dbname = "index.j.db";
        var ecfg = Db4oEmbedded.newConfiguration();
        var memory = new MemoryStorage();
        ecfg.file().storage(memory);
        ecfg.common().add(new TransparentActivationSupport());
        ecfg.common().add(new TransparentPersistenceSupport());
        ecfg.file().generateUUIDs(ConfigScope.GLOBALLY);

        try (var oc = Db4oEmbedded.openFile(ecfg, dbname)) {
            //Test Data
            try (var see = oc.ext().openSession()) {
                for (int i = 0; i < 1000; i++) {
                    for (int j = 0; j < 50; j++) {
                        var r = new Record();
                        r.name = "Name-" + i;
                        r.noName = r.name;
                        r.indexField = (double) i;
                        r.noIndexField = i;
                        r.detail = new Detail();
                        r.detail.record = r;
                        r.detail.type = j;
                        r.detail.memo = r.name + " on meno";
                        see.store(r);
                    }
                }
                see.commit();
            }

            var ran = new Random();
            for (int i = 0; i < 2; i++) {
                double value = (double) ran.nextInt(1000);
                String name = "Name-" + (int) value;
                System.out.println("======Value: " + value + " =====");

                try (var see = oc.ext().openSession()) {
                    long st = System.currentTimeMillis();

                    var rs = see.query((Detail r) -> r.type == 10);

                    System.out.println(rs.get(1).memo);
                    System.out.println(rs.get(1).record.name);
                    System.out.println("Size: " + rs.size());
                    long end = System.currentTimeMillis();
                    System.out.println("Detail Type Index Time: " + (end - st) + "\r\n");
                }

                try (var see = oc.ext().openSession()) {
                    long st = System.currentTimeMillis();

                    var rs = see.query((Record r) -> r.name.equals(name));

                    System.out.println("Size: " + rs.size());
                    long end = System.currentTimeMillis();
                    System.out.println("Recrod Index Time: " + (end - st) + "\r\n");
                }

                try (var see = oc.ext().openSession()) {
                    long st = System.currentTimeMillis();

                    var rs = see.query((Record r) -> r.indexField == value && r.noName.equals(name));

                    System.out.println(rs.get(1).name);
                    System.out.println("Size: " + rs.size());
                    long end = System.currentTimeMillis();
                    System.out.println("Recrod Index Time: " + (end - st) + "\r\n");
                }

                try (var see = oc.ext().openSession()) {
                    long st = System.currentTimeMillis();

                    var rs = see.query((Record r) -> r.noIndexField == value);

                    System.out.println("Size: " + rs.size());
                    long end = System.currentTimeMillis();
                    System.out.println("NoIndex Time: " + (end - st) + "\r\n");
                }
                try (var see = oc.ext().openSession()) {
                    long st = System.currentTimeMillis();

                    var rs = see.query((Record r) -> r.noName.equals(name));

                    System.out.println("Size: " + rs.size());
                    long end = System.currentTimeMillis();
                    System.out.println("NoIndex Time: " + (end - st) + "\r\n");
                }

            }

        }

        System.out.println("End.");
    }
}
Clone this wiki locally