July 10, 2005

RDBMS Tables vs Entity-Attribute-Value Modeling

Question: Being a relational db guy, I'm still trying to understand the B-Tree Model in practical use. For example, say I have several factories, each with several machines. I want to write an app to periodically collect quality metrics from each machine at each factory and store it with a timestamp. Here's what an RDBMS table might look like:
create table metrics {
       when           timestamp,
       factoryId     varchar(8),
       machineId  varchar(8),
       metric          integer
}
I'd probably create indexes on when and a compound index on factoryId And machineId to support my lookup SQL: select metric, when from metrics where factoryId='Atlanta' and machineId='lathe2' where when between and now() order by when desc (Offhand I forget how to specify in SQL, but you get the idea. I want to retrieve the last 24 hrs of data for the given machine.) Machines can have non-unique names, but must be unique within a factory. Ok... long example, but hopefully illustrative. How could I use your product to model something like this?

Our Answer:

Please read ItemSpace Data Structures.html or ItemSpace DataStructures.pdf for the basics on the Entity-Attribute-Value model we will use here. You could also structure this as a relational system, but EAV is more fun. You would define an EntityClass, which is just the EAV name for a table:

    static final EntityClass METRICS = new EntityClass(0); // the 'table'
and an Attribute, which is just the EAV name for a column:
    static final Attribute METRIC = new Attribute(0); // the 'column'
As you know, to insert the data for a metric report or get results, you need considerable JDBC code to bind the parameters and get the results and so on. The InfinityDB code below is all there is. Here's the InfinityDB 'INSERT' statement:
    static void insertMetricReport(String factoryId,
	            String machineId, Date date, long metric) throws IOException {
	    Cu cu = Cu.alloc(); // get a work cursor temporarily

	    // create a new METRIC report entry if necessary, identified by 
	    // factoryId, machine id, and date and attach the metric to it.
	    cu.append(METRICS) // the EntityClass component
	    .append(factoryId) // next three components are the composite key
	    .append(machineId)
	    .append(date)
	    .append(METRIC) // the Attibute component is like a column name
	    .append(metric); // the Value component
	    db.insert(cu); // put all of them into db in one atomic action
	
	    Cu.dispose(cu); // Give back cursor for speed. no strictly necessary
	}
Then the 'SELECT' iterates over the report dates. This is almost entirely cookie-cutter code and follows the design patterns given in ItemSpace Data Structures.html. The cursor (a 'Cu') is initialized with the known part of the key, which are the METRICS EntityClass, the factoryId and the MachineId. While iterating, these components are protected, and only the components later in the Cu change, including the date and other information.
    void selectReports(String factoryId, String machineId,  
            Date startDate) throws IOException {
	    Cu cu = Cu.alloc()
	    	.append(METRICS)
	    	.append(factoryId)
	    	.append(machineId);
	    int protectedPrefixLength = cu.length(); // The current contents of the Cu is protected
	    cu.append(startDate); // start the scan here. This part is not protected, so it changes
	    while (db.next(cu, protectedPrefixLength)) { // iterate over report dates
	        printMetric(factoryId, machineId, cu.dateAt(protectedPrefixLength));
                // This cookie-cutter code allows iteration over a part of an Entity.
                // More often, this is unnecessary because we iterate over multi-value Attributes.
                cu.setLength(cu.skipComponent(protectedPrefixLength));
                cu.incrementSuffix(protectedPrefixLength);
	    }
	    cu.dispose();
    }
And now we print each report:
    void printMetric(String factoryId, String machineID, Date reportDate) throws IOException {
        Cu cu = Cu.alloc()
        	.append(METRICS)
        	.append(factoryId)
        	.append(machineID)
        	.append(reportDate)
        	.append(METRIC);
        int protectedPrefix = cu.length();
        if (db.next(cu, protectedPrefix)) { // got a report
            System.out.print("date=" + reportDate + " metric=" + cu.longAt(protectedPrefix));
        }
        cu.dispose();
    }
This can actually be done faster and with less code by inlining printMetric() but this is hopefully clearer.
Posted 8 years, 7 months ago on July 10, 2005
The trackback url for this post is http://boilerbay.com/infinitydb/forum/bblog/trackback.php/5/

louis vuitton outlet online usa
Hey I am producing which you very selection of great performers in addition to listen to you the great knowledge a person stunning noises disappear jointly once I had fashioned a chance to ever before head over to America as well as speak to it would be easiest my 1 large fantasy is usually to satisfy anyone all over again in case zero I possibly could be around you actually perhaps enjoy thus i might fill my very own wish. I do definitely not learn when should you come back to European countries but think that some although not almost therefore conceivably sometimes even more so but me and if a person at any time find a way to visit America, and certainly Outlined on our site possibly be content if we possess your own performances on the less active at any time have experienced. If only you all the best for the entire team each accomplishment and also gives excellent actions specifically great inside your management. All over again If only all the best to get a wonderful time.
Trackback from louis vuitton outlet online usa Posted 1 day, 9 hours ago • Reply
Comment Trackback URL : http://boilerbay.com/infinitydb/forum/bblog/trackback.php/5/14521/
louis vuitton
Hi there Therefore i'm producing that you simply extremely gang of great vocalists and also listen to the fantastic expertise you gorgeous voices disappear jointly and when I had developed an opportunity to actually head over to America and meet with less complicated this 1 huge wish is always to meet you all over again if simply no I possibly could be with you actually actually have fun with therefore i can complete my wish. I actually do not really understand when is it best to go back to The european countries however think that some however, not nearly therefore conceivably perhaps even also although my family and if an individual actually have the ability to go to North america, as well as surely I may possibly be pleased when we get your personal performances about the non-active ever have observed. If only an individual good luck for your group every single accomplishment along with presents superb performances mainly excellent inside your government. Again If only all the best for any fine day.
Trackback from louis vuitton Posted 12 hours, 21 minutes ago • Reply
Comment Trackback URL : http://boilerbay.com/infinitydb/forum/bblog/trackback.php/5/14544/

Comments have now been turned off for this post