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).

!752

lib 1004017 // Berkeley DB

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;

import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;

import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.SecondaryKey;
import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;

public static class SimpleDA {
    // Open the indices
    public SimpleDA(EntityStore store)
        throws DatabaseException {

        // Primary key for ExampleEntity classes
        pIdx = store.getPrimaryIndex(
            String.class, ExampleEntity.class);

        // Secondary key for ExampleEntity classes
        // Last field in the getSecondaryIndex() method must be
        // the name of a class member; in this case, an 
        // ExampleEntity.class data member.
        sIdx = store.getSecondaryIndex(
            pIdx, String.class, "aSecondaryKey");
    }

    // Index Accessors
    PrimaryIndex<String,ExampleEntity> pIdx;
    SecondaryIndex<String,String,ExampleEntity> sIdx;
} 

@Entity
public static class ExampleEntity {
    // The primary key must be unique in the database.
    @PrimaryKey
    private String aPrimaryKey;

    @SecondaryKey(relate=MANY_TO_ONE)
    private String aSecondaryKey;
    
    void setPKey(S x) { aPrimaryKey = x; }
    void setSKey(S x) { aSecondaryKey = x; }
} 

p {
  Environment myEnv;
  EntityStore store;
  
  bool readOnly = false;
  File envHome = mkdir(getProgramDir());
  
  print("Opening Berkely DB in " + envHome.getAbsolutePath());

  EnvironmentConfig myEnvConfig = new EnvironmentConfig();
  StoreConfig storeConfig = new StoreConfig();

  myEnvConfig.setAllowCreate(!readOnly);
  storeConfig.setAllowCreate(!readOnly);

  // Open the environment and entity store
  myEnv = new Environment(envHome, myEnvConfig);
  store = new EntityStore(myEnv, "EntityStore", storeConfig);
  
  print("Saving stuff");
  
  SimpleDA da = new SimpleDA(store);
  
  ExampleEntity sec1 = new ExampleEntity();
  sec1.setPKey("keyone");
  sec1.setSKey("skeyone");

  da.pIdx.put(sec1);
 
  print("Closing");
  
  if (store != null)
    store.close();
  if (myEnv != null)
    myEnv.close();
}

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: 471 / 849
Referenced in: #1004020 - Test Berkeley DB 3 (Retrieving)