Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

92
LINES

< > BotCompany Repo | #1004019 // Test Berkeley DB 2 (Storing)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 3057K of libraries. Click here for Pure Java version (532L/4K/13K).

1  
!752
2  
3  
lib 1004017 // Berkeley DB
4  
5  
import com.sleepycat.je.DatabaseException;
6  
import com.sleepycat.je.Environment;
7  
import com.sleepycat.je.EnvironmentConfig;
8  
9  
import com.sleepycat.persist.EntityStore;
10  
import com.sleepycat.persist.StoreConfig;
11  
12  
import com.sleepycat.persist.model.Entity;
13  
import com.sleepycat.persist.model.PrimaryKey;
14  
import com.sleepycat.persist.model.SecondaryKey;
15  
import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
16  
17  
import com.sleepycat.je.DatabaseException;
18  
import com.sleepycat.persist.EntityStore;
19  
import com.sleepycat.persist.PrimaryIndex;
20  
import com.sleepycat.persist.SecondaryIndex;
21  
22  
public static class SimpleDA {
23  
    // Open the indices
24  
    public SimpleDA(EntityStore store)
25  
        throws DatabaseException {
26  
27  
        // Primary key for ExampleEntity classes
28  
        pIdx = store.getPrimaryIndex(
29  
            String.class, ExampleEntity.class);
30  
31  
        // Secondary key for ExampleEntity classes
32  
        // Last field in the getSecondaryIndex() method must be
33  
        // the name of a class member; in this case, an 
34  
        // ExampleEntity.class data member.
35  
        sIdx = store.getSecondaryIndex(
36  
            pIdx, String.class, "aSecondaryKey");
37  
    }
38  
39  
    // Index Accessors
40  
    PrimaryIndex<String,ExampleEntity> pIdx;
41  
    SecondaryIndex<String,String,ExampleEntity> sIdx;
42  
} 
43  
44  
@Entity
45  
public static class ExampleEntity {
46  
    // The primary key must be unique in the database.
47  
    @PrimaryKey
48  
    private String aPrimaryKey;
49  
50  
    @SecondaryKey(relate=MANY_TO_ONE)
51  
    private String aSecondaryKey;
52  
    
53  
    void setPKey(S x) { aPrimaryKey = x; }
54  
    void setSKey(S x) { aSecondaryKey = x; }
55  
} 
56  
57  
p {
58  
  Environment myEnv;
59  
  EntityStore store;
60  
  
61  
  bool readOnly = false;
62  
  File envHome = mkdir(getProgramDir());
63  
  
64  
  print("Opening Berkely DB in " + envHome.getAbsolutePath());
65  
66  
  EnvironmentConfig myEnvConfig = new EnvironmentConfig();
67  
  StoreConfig storeConfig = new StoreConfig();
68  
69  
  myEnvConfig.setAllowCreate(!readOnly);
70  
  storeConfig.setAllowCreate(!readOnly);
71  
72  
  // Open the environment and entity store
73  
  myEnv = new Environment(envHome, myEnvConfig);
74  
  store = new EntityStore(myEnv, "EntityStore", storeConfig);
75  
  
76  
  print("Saving stuff");
77  
  
78  
  SimpleDA da = new SimpleDA(store);
79  
  
80  
  ExampleEntity sec1 = new ExampleEntity();
81  
  sec1.setPKey("keyone");
82  
  sec1.setSKey("skeyone");
83  
84  
  da.pIdx.put(sec1);
85  
 
86  
  print("Closing");
87  
  
88  
  if (store != null)
89  
    store.close();
90  
  if (myEnv != null)
91  
    myEnv.close();
92  
}

Author comment

Began life as a copy of #1004018

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004019
Snippet name: Test Berkeley DB 2 (Storing)
Eternal ID of this version: #1004019/1
Text MD5: e7a0bbb8a5696fd92f61a443558d0dc4
Transpilation MD5: c837ed7b03f76ecb3543460a7df590f1
Author: stefan
Category: javax / databases
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-03 18:01:03
Source code size: 2560 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 472 / 851
Referenced in: [show references]