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

92
LINES

< > BotCompany Repo | #1004020 // Test Berkeley DB 3 (Retrieving)

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

Uses 3057K of libraries. Click here for Pure Java version (1044L/7K/24K).

!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("#1004019"));
  
  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("Retrieving stuff");
  
  SimpleDA da = new SimpleDA(store);
  
  ExampleEntity sec1 = da.pIdx.get("keyone");
  ExampleEntity sec2 = da.pIdx.get("keytwo");
  
  print("keyone => " + structure(sec1));
  print("keytwo => " + structure(sec2));
        
  print("Closing");
  
  if (store != null)
    store.close();
  if (myEnv != null)
    myEnv.close();
}

Author comment

Began life as a copy of #1004019

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

1 comment(s) hidden. show

Snippet ID: #1004020
Snippet name: Test Berkeley DB 3 (Retrieving)
Eternal ID of this version: #1004020/1
Text MD5: eccdc799ecf1a1be08fd005b3f0baf2c
Transpilation MD5: 79c7d85b672e3cc7ec6804366cc83ddb
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:02:55
Source code size: 2639 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 763 / 834
Referenced in: [show references]