I got the solution for this, I have created a vertex counterAM with property counter and initiated it with value 0. then create the below given functions in orientdb one javascript and another sql funtion.
run the sql on command prompt or on the browser.
select jf_autogen("counterAM ")
Created a Java function ( this is generic function which you have use to call different counter tables , you just have to pass table name , this java function calls the sql funtion with the table name and it delete the older record and insert the incremented record.
jf_autogen()
var db = orient.getDatabase();
var incrid = db.query("select sum(count,1) as count from "+tname+"");
db.begin();
for(var i=0; i<incrid.length; i++){
var c = incrid[i];
var delid = c.field("count");
var delincr= db.query("select sf_delete"+tname+"("+delid+")");
db.save({
"@class":""+tname+"",
count: c.field("count")
});
}
db.commit();
return delid;
Create a SQL function ( which delete older record from the counter table)
delete vertex counterAM where count <> $vcount.asString()