You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Myrna van Lunteren <m....@gmail.com> on 2014/08/25 05:14:32 UTC

How to get Derby folder size from remote Server?

This message was stuck in moderation also; my attempt to moderate it
through failed.
---------------

From: Nguyen Hai Dong <do...@yahoo.com>
To: derby-user@db.apache.org
Cc:
Date: Mon, 21 Jul 2014 00:26:59 -0700 (PDT)
Subject: How to get Derby folder size from remote Server?
Hi,

My java application and Database is running in different servers. How can i
get whole *database folder size* in java program. Thanks.

Re: How to get Derby folder size from remote Server?

Posted by Rick Hillegas <ri...@oracle.com>.
On 8/24/14 8:14 PM, Myrna van Lunteren wrote:
> This message was stuck in moderation also; my attempt to moderate it 
> through failed.
> ---------------
>
> From: Nguyen Hai Dong <dongngh@yahoo.com <ma...@yahoo.com>>
> To: derby-user@db.apache.org <ma...@db.apache.org>
> Cc:
> Date: Mon, 21 Jul 2014 00:26:59 -0700 (PDT)
> Subject: How to get Derby folder size from remote Server?
> Hi,
>
> My java application and Database is running in different servers. How 
> can i
> get whole *database folder size* in java program. Thanks.
I don't know of a supported way to do this. The following technique 
relies on interfaces which are not part of Derby's public api. Those 
interfaces may change in the future, although they've been pretty stable 
over the last ten years. This technique returns an answer which is close 
to what you want. For instance, when I use this technique on a newly 
created Derby database, I get the value 1,874,595. That's pretty close 
to 1,874,553, the value reported by my operating system.

First compile this program:

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.derby.iapi.reference.Property;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.iapi.store.raw.data.DataFactory;
import org.apache.derby.impl.jdbc.EmbedConnection;
import org.apache.derby.io.StorageFactory;

public class DBSize
{
     public  static  long    getDBSize() throws Exception
     {
         Connection  conn = DriverManager.getConnection( 
"jdbc:default:connection" );
         Object monitor = Monitor.findService
             ( Property.DATABASE_MODULE, ((EmbedConnection) 
conn).getDBName() ) ;
         DataFactory dataFactory = (DataFactory) 
Monitor.findServiceModule( monitor, DataFactory.MODULE );
         StorageFactory  storageFactory = dataFactory.getStorageFactory();
         String  canonicalName = storageFactory.getCanonicalName();
         File    dbDirectory = new File( canonicalName );

         return getSize( dbDirectory );
     }

     private static long getSize( File baseFile )
     {
         long size = 0;

         if ( baseFile.isDirectory() )
         {
             for ( File file : baseFile.listFiles() )
             {
                 if ( file.isFile() )    { size += file.length(); }
                 else { size += getSize( file ); }
             }
         }

         return size;
     }

}

Then run this script:

connect 'jdbc:derby:db;create=true';

create function getDBSize() returns bigint
language java parameter style java no sql
external name 'DBSize.getDBSize';

values getDBSize();

Hope this helps,
-Rick