| Home >> All >> org >> vrspace >> [ server Javadoc ] |
Source code: org/vrspace/server/DB.java
1 package org.vrspace.server; 2 import java.io.*; 3 import java.net.*; 4 import org.vrspace.util.*; 5 import org.vrspace.server.db.DBCache; 6 /** 7 * Database class 8 * 9 */ 10 public abstract class DB { 11 12 /** this keeps reference to cache */ 13 public DBCache cache; 14 15 /** 16 create a new database if does not exist 17 @param name Database name 18 @return String to use as parameter to connect() 19 @see #connect 20 */ 21 public abstract String create( String name ) throws Exception; 22 23 /** connect to the database */ 24 public abstract void connect ( String url ) throws Exception; 25 26 /** Disconnect from the database **/ 27 public abstract void disconnect(); 28 29 /** 30 Commit changes. Should NOT be synchronized. 31 */ 32 public abstract void commit(); 33 34 /** Return database object from table <b>obj</b>.getClass().getName() having 35 ** db_id = <b>obj</b>.db_id 36 */ 37 public abstract Object get( Object obj ) throws Exception; 38 39 /** Returns the object having id == <b>obj</b>.db_id 40 */ 41 public abstract Object get( String className, long id ) throws Exception; 42 43 /** Returns the object of <b>className</b> class having <b>field</b> == <b>value</b> */ 44 public abstract Object get( String className, String field, Object value ) 45 throws Exception; 46 47 /** Returns the object of <b>className</b> class having <b>field</b> == <b>value</b> */ 48 public abstract Object[] getRange( String className, String field, Object value ) 49 throws Exception; 50 51 /** 52 Returns Object[] between o1 and o2 53 Class must have comparator() method to be searchable. 54 */ 55 public abstract Object[] getRange( Object o1, Object o2 ) 56 throws Exception; 57 58 /** 59 Returns all members of the class 60 */ 61 public abstract Object[] getAll( String className ) throws Exception; 62 63 /** From the table <b>obj</b>.getClass().getName() deletes the row having 64 ** db_id == <b>obj</b>.db_id 65 */ 66 public abstract void delete( Object obj ) throws Exception; 67 68 /** 69 Stores obj into database 70 Logic: 71 - all the public fields are stored 72 - database table: obj.getClass().getName() 73 - create table if does not exist 74 */ 75 public abstract void put( Object obj ) throws Exception; 76 77 /** 78 A request is a change to <b>one</b> field. This method allows 79 optimal database update. 80 */ 81 public abstract void update( Request r ) throws Exception; 82 83 /** 84 Loads file content into the db. Default protocol for <b>url</b> is 'file' 85 */ 86 public void load( String url ) throws Exception { 87 URL u; 88 try { 89 u = new URL( url ); 90 } catch (MalformedURLException e) { 91 url = "file:"+url; 92 try { 93 u = new URL( url ); 94 } catch ( MalformedURLException e1 ) { 95 throw e; 96 } 97 } 98 InputStream in = u.openStream(); 99 load( new InputStreamReader(in) ); 100 } 101 /** 102 Loads stream content into the database 103 */ 104 public void load( Reader in ) throws Exception { 105 BufferedReader reader = new BufferedReader( in ); 106 String line; 107 StringBuffer text = new StringBuffer(); 108 long time = System.currentTimeMillis(); 109 while ( ( line = reader.readLine() ) != null ) { 110 text.append(line + "\n"); 111 } 112 Object[] objects = VRObject.fromText( text.toString() ); 113 time = System.currentTimeMillis()-time; 114 Logger.logDebug( "DB: "+objects.length+" objects loaded in "+time/100+" seconds" ); 115 for ( int i = 0; i < objects.length; i++ ) { 116 //Logger.logDebug( objects[i].toString() ); 117 ((VRObject)objects[i]).isNew = true; 118 if ( (cache != null ) ) { 119 cache.put( objects[ i ] ); 120 } else { 121 put( objects[ i ] ); 122 } 123 ((VRObject)objects[i]).isNew = false; 124 } 125 } 126 /** 127 Loads file content into the database 128 */ 129 public void load( File file ) throws Exception { 130 load( new FileReader( file )); 131 } 132 }