1 package com.sleepycat.persist.test;
2 
3 import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
4 
5 import java.io.File;
6 import java.util.ArrayList;
7 import java.util.List;
8 
9 import com.sleepycat.je.Environment;
10 import com.sleepycat.je.EnvironmentConfig;
11 import com.sleepycat.persist.EntityStore;
12 import com.sleepycat.persist.PrimaryIndex;
13 import com.sleepycat.persist.StoreConfig;
14 import com.sleepycat.persist.model.Entity;
15 import com.sleepycat.persist.model.KeyField;
16 import com.sleepycat.persist.model.Persistent;
17 import com.sleepycat.persist.model.PrimaryKey;
18 import com.sleepycat.persist.model.SecondaryKey;
19 
20 /*
21  * Create a database which stores StringData classes. This database will be
22  * used in the unit test c.s.persist.test.StringFormatCompatibilityTest.
23  */
24 public class CreateStringDataDB {
25     Environment env;
26     private EntityStore store;
27     private PrimaryIndex<String, StringData> primary;
28 
main(String args[])29     public static void main(String args[]) {
30     	CreateStringDataDB csd = new CreateStringDataDB();
31         csd.open();
32         csd.writeData();
33         csd.close();
34     }
35 
writeData()36     private void writeData() {
37         CompositeKey compK = new CompositeKey("CompKey1_1", "CompKey1_2");
38         CompositeKey compK2 = new CompositeKey("CompKey2_1", "CompKey2_2");
39         String[] f3 = {"f3_1", "f3_2"};
40         List<String> f4 = new ArrayList<String>();
41         f4.add("f4_1");
42         f4.add("f4_2");
43         primary.put
44             (null, new StringData ("pk1", "sk1", compK, "f1", "f2", f3, f4));
45         f4.clear();
46         f4.add("f4_1_2");
47         f4.add("f4_2_2");
48         primary.put
49             (null, new StringData ("pk2", "sk2", compK2, "f1", "f2", f3, f4));
50     }
51 
close()52     private void close() {
53         store.close();
54         store = null;
55 
56         env.close();
57         env = null;
58     }
59 
open()60     private void open() {
61         EnvironmentConfig envConfig = new EnvironmentConfig();
62         envConfig.setAllowCreate(true);
63         File envHome = new File("./");
64         env = new Environment(envHome, envConfig);
65         StoreConfig config = new StoreConfig();
66         config.setAllowCreate(envConfig.getAllowCreate());
67         config.setTransactional(envConfig.getTransactional());
68         store = new EntityStore(env, "test", config);
69         primary = store.getPrimaryIndex(String.class, StringData.class);
70     }
71 
72     @Entity
73     static class StringData {
74         @PrimaryKey
75         private String pk;
76         @SecondaryKey (relate = MANY_TO_ONE)
77         private String sk1;
78         @SecondaryKey (relate = MANY_TO_ONE)
79         private CompositeKey sk2;
80         private String f1;
81         private String f2;
82         private String[] f3;
83         private List<String> f4;
84 
StringData()85         StringData() { }
86 
StringData(String pk, String sk1, CompositeKey sk2, String f1, String f2, String[] f3, List<String> f4)87         StringData(String pk, String sk1, CompositeKey sk2, String f1,
88                    String f2, String[] f3, List<String> f4) {
89             this.pk = pk;
90             this.sk1 = sk1;
91             this.sk2 = sk2;
92             this.f1 = f1;
93             this.f2 = f2;
94             this.f3 = f3;
95             this.f4 = f4;
96         }
97 
getPK()98         String getPK() {
99             return pk;
100         }
101 
getSK1()102         String getSK1() {
103             return sk1;
104         }
105 
getSK2()106         CompositeKey getSK2() {
107             return sk2;
108         }
109 
getF1()110         String getF1() {
111             return f1;
112         }
113 
getF2()114         String getF2() {
115             return f2;
116         }
117 
getF3()118         String[] getF3() {
119             return f3;
120         }
121 
getF4()122         List<String> getF4() {
123             return f4;
124         }
125     }
126 
127     @Persistent
128     static class CompositeKey {
129         @KeyField(2)
130         private String f1;
131         @KeyField(1)
132         private String f2;
133 
CompositeKey()134         private CompositeKey() {}
135 
CompositeKey(String f1, String f2)136         CompositeKey(String f1, String f2) {
137             this.f1 = f1;
138             this.f2 = f2;
139         }
140 
getF1()141         String getF1() {
142             return f1;
143         }
144 
getF2()145         String getF2() {
146             return f2;
147         }
148     }
149 }