W3C home > Mailing lists > Public > www-jigsaw@w3.org > November to December 1996

Storing objects w/ jdbm

From: Anselm Baird_Smith <abaird@www43.inria.fr>
Date: Mon, 4 Nov 1996 18:50:11 +0100 (MET)
Message-Id: <199611041750.SAA05721@www43.inria.fr>
To: Martijn <martijn@cruiser.vo.com>
Cc: www-jigsaw@w3.org
martijn@cruiser.vo.com writes:
 > Hi,
 > 
 > This isn't really a jigsaw related question, but since jdbm is included
 > with the jigsaw package i'm asking it here anyway.
 > In the original gdbm package in C, it's possible storing/retrieving any
 > datatype. Storing a big structure and retrieving it by casting back to the
 > original datatype is possible. Very handy feature.
 > Would it be possible doing this with jdbm as well? Would there be an easy
 > way to store an object that is an instance of a dataholder class?
 > Right now i see a way to store a String object because the String class
 > defines methods to get a byte array copy of its value and giving it a
 > value using a byte array copy of the desired value. How would i clone a
 > random object to a byte-array so that i can store it using jdbm? And how
 > would i create an instance back from the byte-array to the object?
 > 
 > If anyone already did this, please let me know,

I did: just use the resources base classes along with the
jdbmResourceStore, it does exactly this.

I have attached below an example of such a class...
Anselm.
-----

package w3c.jigsaw.filters;

import java.io.*;
import java.util.*;

import w3c.jigsaw.auth.AuthFilter;
import w3c.jigsaw.http.*;
import w3c.www.http.*;
import w3c.jigsaw.resources.*;

/**
 * We want free pickling, hence the super class.
 */

public class PutedEntry extends AttributeHolder {
    protected static int ATTR_AUTHOR   = -1;
    protected static int ATTR_URL      = -1;
    protected static int ATTR_FILENAME = -1;
    protected static int ATTR_TIME     = -1;

    static {
	Class c = null;
	Attribute a = null;
    
	try {
	    c = Class.forName("w3c.jigsaw.filters.PutedEntry");
	} catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(1);
	}
	// The author of change:
	a = new StringAttribute("author"
				, null
				, Attribute.EDITABLE);
	ATTR_AUTHOR = AttributeRegistry.registerAttribute(c, a);
	// The mandatory url:
	a = new StringAttribute("url"
				, null
				, Attribute.EDITABLE);
	ATTR_URL = AttributeRegistry.registerAttribute(c, a);
	// The optional absolute file name:
	a = new StringAttribute("filename"
				, null
				, Attribute.EDITABLE);
	ATTR_FILENAME = AttributeRegistry.registerAttribute(c, a);
	// The time of modification:
	a = new LongAttribute("time"
			      , null
			      , Attribute.EDITABLE);
	ATTR_TIME = AttributeRegistry.registerAttribute(c, a);
    }

    final String getAuthor() {
	return getString(ATTR_AUTHOR, null);
    }

    final String getURL() {
	return getString(ATTR_URL, null);
    }

    final String getFilename() {
	return getString(ATTR_FILENAME, null);
    }

    final long getTime() {
	return getLong(ATTR_TIME, -1);
    }

    final String getKey() {
	String key = getFilename();
	if ( key == null )
	    return getURL();
	return key;
    }

    synchronized void update(Request request) {
	String author = (String) request.getState(AuthFilter.STATE_AUTHUSER);
	long   time   = System.currentTimeMillis();
	setValue(ATTR_AUTHOR, author);
	setValue(ATTR_TIME, new Long(time));
    }

    static PutedEntry makeEntry(Request request) {
	HTTPResource r = (HTTPResource) request.getTargetResource();
	if ( r != null ) {
	    // Build an entry:
	    PutedEntry e = new PutedEntry();
	    e.setValue(ATTR_URL, request.getURL().toExternalForm());
	    if ( r instanceof FileResource )
		e.setValue(ATTR_FILENAME
			   , ((FileResource) r).getFile().getAbsolutePath());
	    // Update other infos:
	    e.update(request);
	    return e;
	}
	return null;
    }

}
Received on Monday, 4 November 1996 12:50:14 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 9 April 2012 12:13:25 GMT