Created
May 8, 2015 02:26
-
-
Save gythialy/c1cfc626705426223d8b to your computer and use it in GitHub Desktop.
test db4o for java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.wescon.db4o; | |
| import java.io.File; | |
| import java.util.List; | |
| import com.db4o.Db4oEmbedded; | |
| import com.db4o.EmbeddedObjectContainer; | |
| import com.db4o.ObjectSet; | |
| import com.db4o.config.EmbeddedConfiguration; | |
| import com.db4o.diagnostic.DiagnosticToConsole; | |
| import com.db4o.query.Query; | |
| import com.db4o.query.QueryComparator; | |
| import com.google.common.base.Strings; | |
| import com.google.common.collect.ComparisonChain; | |
| import com.google.common.collect.Lists; | |
| import com.wescon.db4o.model.SystemPoint; | |
| public class SodaTest { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| String fileName = "test.db4o"; | |
| new File(fileName).delete(); | |
| EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); | |
| configuration.common().diagnostic().addListener(new DiagnosticToConsole()); | |
| // ObjectClass objectClass = | |
| // configuration.common().objectClass(SystemPoint.class); | |
| // objectClass.objectField("no").indexed(true); | |
| // objectClass.objectField("belongTo").indexed(true); | |
| // objectClass.objectField("channel").indexed(true); | |
| // objectClass.generateUUIDs(true); | |
| // objectClass.cascadeOnDelete(true); | |
| // objectClass.cascadeOnDelete(true); | |
| EmbeddedObjectContainer db = Db4oEmbedded.openFile(configuration, fileName); | |
| List<SystemPoint> points = buildTestDatas(); | |
| db.store(points); | |
| ObjectSet<SystemPoint> all = db.query(SystemPoint.class); | |
| System.err.println("origin"); | |
| display(all); | |
| db.store(new SystemPoint().setDescription("W047 - " + 4) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| QueryComparator<SystemPoint> queryComparator = new QueryComparator<SystemPoint>() { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1L; | |
| @Override | |
| public int compare(SystemPoint first, SystemPoint second) { | |
| return ComparisonChain.start() | |
| .compare(first.getBelongTo(), second.getBelongTo()) | |
| .compare(Strings.nullToEmpty(first.getChannel()), | |
| Strings.nullToEmpty(second.getChannel())) | |
| .result(); | |
| } | |
| }; | |
| System.err.println("sort"); | |
| Query query = db.query(); | |
| query.constrain(SystemPoint.class); | |
| query.sortBy(queryComparator); | |
| all = query.execute(); | |
| display(all); | |
| System.err.println("commit"); | |
| db.commit(); | |
| query = db.query(); | |
| query.constrain(SystemPoint.class); | |
| query.descend("belongTo") | |
| .constrain("W047-0") | |
| .and(query.descend("channel").constrain("channel1")); | |
| ObjectSet<SystemPoint> tobeRemove = query.execute(); | |
| for (SystemPoint systemPoint : tobeRemove) { | |
| db.delete(systemPoint); | |
| System.out.println("remove >>> " + systemPoint); | |
| } | |
| query = db.query(); | |
| query.constrain(SystemPoint.class); | |
| query.sortBy(queryComparator); | |
| all = query.execute(); | |
| display(all); | |
| System.err.println("rollback"); | |
| db.rollback(); | |
| query = db.query(); | |
| query.constrain(SystemPoint.class); | |
| query.sortBy(queryComparator); | |
| all = query.execute(); | |
| display(all); | |
| db.close(); | |
| } | |
| private static void display(ObjectSet<SystemPoint> all) { | |
| for (SystemPoint systemPoint : all) { | |
| System.out.println(systemPoint); | |
| } | |
| System.out.println(Strings.repeat("*", 20)); | |
| } | |
| private static List<SystemPoint> buildTestDatas() { | |
| List<SystemPoint> points = Lists.newArrayList(); | |
| points.add(new SystemPoint().setDescription("W029 - " + 1).setBelongTo("W029-0")); | |
| points.add(new SystemPoint().setDescription("e066 - " + 1) | |
| .setBelongTo("e066-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 测试 - " + 0) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 1) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W029 - " + 0).setBelongTo("W029-0")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 2) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel2")); | |
| points.add(new SystemPoint().setDescription("e066 - " + 0) | |
| .setBelongTo("e066-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 3) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel2")); | |
| return points; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.wescon.db4o.model; | |
| import com.db4o.config.annotations.Indexed; | |
| import com.google.common.base.Objects; | |
| import com.google.gson.Gson; | |
| public class SystemPoint { | |
| private long no; | |
| private String description; | |
| @Indexed | |
| private String belongTo; | |
| private String channel; | |
| public SystemPoint() {} | |
| public long getNo() { | |
| return no; | |
| } | |
| public SystemPoint setNo(long no) { | |
| this.no = no; | |
| return this; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public SystemPoint setDescription(String description) { | |
| this.description = description; | |
| return this; | |
| } | |
| public String getBelongTo() { | |
| return belongTo; | |
| } | |
| public SystemPoint setBelongTo(String belongTo) { | |
| this.belongTo = belongTo; | |
| return this; | |
| } | |
| public String getChannel() { | |
| return channel; | |
| } | |
| public SystemPoint setChannel(String channel) { | |
| this.channel = channel; | |
| return this; | |
| } | |
| @Override | |
| public boolean equals(Object arg0) { | |
| if (arg0 != null && arg0 instanceof SystemPoint) { | |
| SystemPoint point = (SystemPoint) arg0; | |
| return Objects.equal(point.no, this.no) | |
| && Objects.equal(point.description, this.description) | |
| && Objects.equal(point.channel, this.channel) | |
| && Objects.equal(point.belongTo, this.belongTo); | |
| } | |
| return false; | |
| } | |
| @Override | |
| public String toString() { | |
| return new Gson().toJson(this); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.wescon.db4o; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.List; | |
| import com.db4o.Db4oEmbedded; | |
| import com.db4o.EmbeddedObjectContainer; | |
| import com.db4o.ObjectSet; | |
| import com.db4o.config.EmbeddedConfiguration; | |
| import com.db4o.defragment.Defragment; | |
| import com.db4o.defragment.DefragmentConfig; | |
| import com.db4o.defragment.DefragmentInfo; | |
| import com.db4o.defragment.DefragmentListener; | |
| import com.db4o.diagnostic.DiagnosticToConsole; | |
| import com.db4o.query.Predicate; | |
| import com.db4o.query.QueryComparator; | |
| import com.db4o.ta.DeactivatingRollbackStrategy; | |
| import com.db4o.ta.TransparentActivationSupport; | |
| import com.db4o.ta.TransparentPersistenceSupport; | |
| import com.google.common.base.Strings; | |
| import com.google.common.collect.ComparisonChain; | |
| import com.google.common.collect.Lists; | |
| import com.wescon.db4o.model.SystemPoint; | |
| public class Test { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| String fileName = "test.db4o"; | |
| new File(fileName).delete(); | |
| EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); | |
| configuration.common().diagnostic().addListener(new DiagnosticToConsole()); | |
| // Transparent Activation | |
| // This adds transparent activation, which automatically activates | |
| // objects. | |
| configuration.common().add(new TransparentActivationSupport()); | |
| //Transparent Persistence | |
| configuration.common().add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy())); | |
| // ObjectClass objectClass = | |
| // configuration.common().objectClass(SystemPoint.class); | |
| // objectClass.objectField("no").indexed(true); | |
| // objectClass.objectField("belongTo").indexed(true); | |
| // objectClass.objectField("channel").indexed(true); | |
| // objectClass.generateUUIDs(true); | |
| // objectClass.cascadeOnDelete(true); | |
| // objectClass.cascadeOnDelete(true); | |
| EmbeddedObjectContainer db = Db4oEmbedded.openFile(configuration, fileName); | |
| List<SystemPoint> points = buildTestDatas(); | |
| db.store(points); | |
| ObjectSet<SystemPoint> all = db.query(SystemPoint.class); | |
| System.err.println("origin"); | |
| display(all); | |
| db.store(new SystemPoint().setDescription("W047 - " + 4) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| Predicate<SystemPoint> allPredicate = new Predicate<SystemPoint>() { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1L; | |
| @Override | |
| public boolean match(SystemPoint candidate) { | |
| return true; | |
| } | |
| }; | |
| QueryComparator<SystemPoint> queryComparator = new QueryComparator<SystemPoint>() { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1L; | |
| @Override | |
| public int compare(SystemPoint first, SystemPoint second) { | |
| return ComparisonChain.start() | |
| .compare(first.getBelongTo(), second.getBelongTo()) | |
| .compare(Strings.nullToEmpty(first.getChannel()), | |
| Strings.nullToEmpty(second.getChannel())) | |
| .result(); | |
| } | |
| }; | |
| all = db.query(allPredicate, queryComparator); | |
| System.err.println("sort"); | |
| display(all); | |
| System.err.println("commit"); | |
| db.commit(); | |
| ObjectSet<SystemPoint> tobeRemove = db.queryByExample(new SystemPoint().setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| // | |
| // ObjectSet<SystemPoint> tobeRemove = db.query(new | |
| // Predicate<SystemPoint>() { | |
| // | |
| // private static final long serialVersionUID = 1L; | |
| // private static final String W047_STRING = "W047-0"; | |
| // private static final String CHANNEL_STRING = "channel1"; | |
| // | |
| // @Override | |
| // public boolean match(SystemPoint candidate) { | |
| // return W047_STRING.equals(candidate.getBelongTo()) | |
| // && CHANNEL_STRING.equals(candidate.getChannel()); | |
| // } | |
| // }); | |
| for (SystemPoint systemPoint : tobeRemove) { | |
| db.delete(systemPoint); | |
| System.out.println("remove >>> " + systemPoint); | |
| } | |
| all = db.query(allPredicate, queryComparator); | |
| display(all); | |
| System.err.println("rollback"); | |
| db.rollback(); | |
| all = db.query(allPredicate, queryComparator); | |
| display(all); | |
| db.backup("test.back.db4o"); | |
| ObjectSet<Object> objects = db.queryByExample(null); | |
| for (Object object : objects) { | |
| db.delete(object); | |
| } | |
| db.close(); | |
| try { | |
| DefragmentConfig defragmentConfig = new DefragmentConfig(fileName, | |
| fileName | |
| + DefragmentConfig.BACKUP_SUFFIX); | |
| defragmentConfig.forceBackupDelete(true); | |
| Defragment.defrag(defragmentConfig, new DefragmentListener() { | |
| @Override | |
| public void notifyDefragmentInfo(DefragmentInfo info) { | |
| System.out.println("info " + info); | |
| } | |
| }); | |
| } | |
| catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private static void display(ObjectSet<SystemPoint> all) { | |
| for (SystemPoint systemPoint : all) { | |
| System.out.println(systemPoint); | |
| } | |
| System.out.println(Strings.repeat("*", 20)); | |
| } | |
| private static List<SystemPoint> buildTestDatas() { | |
| List<SystemPoint> points = Lists.newArrayList(); | |
| points.add(new SystemPoint().setDescription("W029 - " + 1).setBelongTo("W029-0")); | |
| points.add(new SystemPoint().setDescription("e066 - " + 1) | |
| .setBelongTo("e066-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 测试 - " + 0) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 1) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W029 - " + 0).setBelongTo("W029-0")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 2) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel2")); | |
| points.add(new SystemPoint().setDescription("e066 - " + 0) | |
| .setBelongTo("e066-0") | |
| .setChannel("channel1")); | |
| points.add(new SystemPoint().setDescription("W047 - " + 3) | |
| .setBelongTo("W047-0") | |
| .setChannel("channel2")); | |
| return points; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment