Package jdbm

Examples of jdbm.RecordManager


        throws Exception
    {
      Properties props = new Properties();
      props.put(RecordManagerOptions.COMPRESS, "TRUE");
         
        RecordManager recman = RecordManagerFactory. createRecordManager(
              newTestFile(), props);
       
        //create huge list to be compressed
        ArrayList l1 = new ArrayList();
        for(int i = 0;i<100000;i++) l1.add(i);
       
        long id = recman.insert(l1);
        recman.commit();
        ArrayList l2 = (ArrayList) recman.fetch(id);
        assertEquals(l1,l2);

       
        recman.update(id, l2);
        recman.commit();
        ArrayList l3 = (ArrayList) recman.fetch(id);
        assertEquals(l1,l3);

    }
View Full Code Here


     *  a bug in the stress test involving 0 record lengths.
     */
    public void testDeleteAndReuse()
        throws Exception
    {
        RecordManager recman;

        recman = newRecordManager();

        // insert a 1500 byte record.
        byte[] data = UtilTT.makeRecord(1500, (byte) 1);
        long rowid = recman.insert(data);
        assertTrue("check data1",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid), 1500, (byte) 1) );


        // delete the record
        recman.delete(rowid);

        // insert a 0 byte record. Should have the same rowid.
        data = UtilTT.makeRecord(0, (byte) 2);
        long rowid2 = recman.insert(data);
        assertEquals("old and new rowid", rowid, rowid2);
        assertTrue("check data2",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid2), 0, (byte) 2) );

        // now make the record a bit bigger
        data = UtilTT.makeRecord(10000, (byte) 3);
        recman.update(rowid, data);
        assertTrue("check data3",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid), 10000, (byte) 3) );

        // .. and again
        data = UtilTT.makeRecord(30000, (byte) 4);
        recman.update(rowid, data);
        assertTrue("check data3",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid), 30000, (byte) 4) );

        // close the file
        recman.close();
    }
View Full Code Here

   
  }
   
  public static void main(String[] args) throws IOException {
    //init Record Manager and dao
    RecordManager recman = RecordManagerFactory.createRecordManager("persons1");

    PrimaryTreeMap<String,Person> personsByName = recman.treeMap("personsByName");

    SecondaryTreeMap<String, String, Person> personsByTown =
        personsByName.secondaryTreeMap("personsByTown",
            new SecondaryKeyExtractor<String, String, Person>() {
              public String extractSecondaryKey(String key,Person value) {
View Full Code Here

  }
 

 
  public static void main(String[] args) throws IOException {
    RecordManager recman = RecordManagerFactory.
        createRecordManager("HelloWorld2");
    /**
     * Create primary map which is used to store records. 
     * Data can be also stored in PrimaryTreeMap and PrimaryHashMap,
     * but this is more efficient for large objects.
     */
    PrimaryStoreMap<Long,Person> main = recman.storeMap("recman");
   
    /**
     * Create secondary index which points to Person name.
     * SecondaryMap is readonly, it is updated by PrimaryMap.
     * 
     * Secondary map have three types,
     * first is type of secondary index (string),
     * second and third are copied from primary map.
     */
    SecondaryTreeMap<String, Long, Person> nameIndex = main.secondaryTreeMap("nameIndex",
            new SecondaryKeyExtractor<String, Long, Person>() {
              public String extractSecondaryKey(Long key, Person value) {
                return value.name;
              }         
            });
   
    /**
     * Another secondary map which points to town and country
     */
    SecondaryTreeMap<String, Long, Person> townIndex = main.secondaryTreeMap("townIndex",
        new SecondaryKeyExtractor<String, Long, Person>() {
          public String extractSecondaryKey(Long key, Person value) {
            /**
             * Note format of map key
             */
            return value.country+"/"+value.town;
          }         
        });
   
    /**
     * And very simple index of Evil family members
     */
    SecondaryTreeMap<Boolean, Long, Person> evilIndex = main.secondaryTreeMap("evilIndex",
        new SecondaryKeyExtractor<Boolean, Long, Person>() {
          public Boolean extractSecondaryKey(Long key, Person value) {
            return value.name.contains("Evil");
          }         
        });
   
    /**
     * Clean up, if this example was run more times.
     * All secondary indexes are updated automatically.
     */
    main.clear();
   
   
    /**
     * Add some data.
     * StoreMap does not have usuall 'put' method.
     * Key is generated by Store, so use 'putValue' instead
     */
    main.putValue(new Person("James Bond","London","UK"));
    main.putValue(new Person("Austin Powers","London","UK"));
    main.putValue(new Person("Dr Evil","Vulcano Island","Ocean"));
    main.putValue(new Person("Scott Evil","Vulcano Island","Ocean"));   
    main.putValue(new Person("Vanessa Kensington","London","UK"));
    main.putValue(new Person("Alotta Fagina","Las Vegas","USA"));
       
    /**
     * Persists inserted values
     */
    recman.commit();
   
    /**
     * Get persons with name Austin Powers
     */
    System.out.println();
    System.out.println("Austin Powers: ");
    for(Person person:nameIndex.getPrimaryValues("Austin Powers")){
      System.out.println("  "+person);
    }
   
    /**
     * Print all Persons who lives on Vulcano Island.
     * First we must obtain key from primary map,
     * then
     */
    System.out.println();
    System.out.println("Persons on Vulcano Island: ");
    for(Person person:townIndex.getPrimaryValues("Ocean/Vulcano Island")){
      System.out.println("  "+person);
    }
   

    /**
     * Get everyone who is Evil
     */
    System.out.println();
    System.out.println("Evil family: ");
    for(Person person:evilIndex.getPrimaryValues(true)){
      System.out.println("  "+person);
    }   
   
    recman.close();
  }
View Full Code Here

public class HelloWorld {
  public static void main(String[] args) throws IOException {

    /** create (or open existing) database */
    String fileName = "helloWorld";
    RecordManager recMan = RecordManagerFactory.createRecordManager(fileName);
   
    /** Creates TreeMap which stores data in database. 
     *  Constructor method takes recordName (something like SQL table name)*/
    String recordName = "firstTreeMap";
    PrimaryTreeMap<Integer,String> treeMap = recMan.treeMap(recordName);

    /** add some stuff to map*/
    treeMap.put(1, "One");
    treeMap.put(2, "Two");
    treeMap.put(3, "Three");
   
    System.out.println(treeMap.keySet());
    // > [1, 2, 3]
   
    /** Map changes are not persisted yet, commit them (save to disk) */
    recMan.commit();

    System.out.println(treeMap.keySet());
    // > [1, 2, 3]

    /** Delete one record. Changes are not commited yet, but are visible. */
    treeMap.remove(2);

    System.out.println(treeMap.keySet());
    // > [1, 3]
   
    /** Did not like change. Roolback to last commit (undo record remove). */
    recMan.rollback();
   
    /** Key 2 was recovered */
    System.out.println(treeMap.keySet());
    // > [1, 2, 3]
   
    /** close record manager */
    recMan.close();

  }
 
View Full Code Here

*/
public class HugeData {
  public static void main(String[] args) throws IOException {

    /** open db */
        RecordManager recman = RecordManagerFactory.createRecordManager( "hugedata");       
        PrimaryTreeMap<Long, String> m = recman.treeMap("hugemap");
       
        /** insert 1e7 records */
        for(long i = 0;i<1e8;i++){
          m.put(i, "aa"+i);       
          if(i%1e5==0){
            /** Commit periodically, otherwise program would run out of memory */             
            recman.commit();
            System.out.println(i);           
          }
           
        }
       
        recman.commit();
        recman.close();
        System.out.println("DONE");
       
  }
View Full Code Here

  }
 
  public static void main(String[] args) throws IOException {
    //init Record Manager and dao
    RecordManager recman = RecordManagerFactory.createRecordManager("persons2");
    PersonDao dao = new PersonDao(recman);
   
    //create a few persons
    Person patrick = new Person("Patrick Moore",
        new Address("First street", "Athlone","Ireland"),
View Full Code Here

     *  operations.
     */
    public void testRollback()
        throws Exception
    {
        RecordManager recman;

        // Note: We start out with an empty file
        recman = newRecordManager();

        // insert a 150000 byte record.
        byte[] data1 = UtilTT.makeRecord(150000, (byte) 1);
        long rowid1 = recman.insert(data1);
        assertTrue("check data1",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid1), 150000, (byte) 1) );

        // rollback transaction, should revert to previous state
        recman.rollback();

        // insert same 150000 byte record.
        byte[] data2 = UtilTT.makeRecord(150000, (byte) 1);
        long rowid2 = recman.insert(data2);
        assertTrue("check data2",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid2), 150000, (byte) 1) );

        assertEquals("old and new rowid", rowid1, rowid2);

        recman.commit();

        // insert a 150000 byte record.
        data1 = UtilTT.makeRecord(150000, (byte) 2);
        rowid1 = recman.insert(data1);
        assertTrue("check data1",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid1), 150000, (byte) 2) );

        // rollback transaction, should revert to previous state
        recman.rollback();

        // insert same 150000 byte record.
        data2 = UtilTT.makeRecord(150000, (byte) 2);
        rowid2 = recman.insert(data2);
        assertTrue("check data2",
               UtilTT.checkRecord((byte[]) recman.fetch(rowid2), 150000, (byte) 2) );

        assertEquals("old and new rowid", rowid1, rowid2);

        // close the file
        recman.close();
    }
View Full Code Here

        recman.close();
    }

   
    public void testNonExistingRecid() throws IOException{
      RecordManager recman = newRecordManager();
     
      Object obj = recman.fetch(6666666);
      assertTrue(obj == null);
     
      try{
        recman.update(6666666, obj);
        recman.commit();
        fail();
      }catch(IOException expected){

      }
    }
View Full Code Here

      public void serialize(SerializerOutput out, String obj) throws IOException {
        i.incrementAndGet();
        out.writeUTF(obj);
      }};
     
    RecordManager recman = newRecordManager();
    PrimaryTreeMap<Long,String> t =  recman.treeMap("test",ser);
    t.put(1l, "hopsa hejsa1");
    t.put(2l, "hopsa hejsa2");
    recman.commit();
    assertEquals(t.get(2l),"hopsa hejsa2");
    assertTrue(i.intValue()>0);
    }
View Full Code Here

TOP

Related Classes of jdbm.RecordManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.