/** Create a record, allocating space for the key and value (if any) */ public Record create() { return create(new byte[keyLength], (valueLength > 0) ? new byte[valueLength] : null); }
/** Return a RecordFactory that makes key-only records of the same key size */ public RecordFactory keyFactory() { return new RecordFactory(keyLength, 0); }
@Override public String toString() { return String.format("Order=%d : Records [key=%d, value=%d] : records=[%d,%d] : pointers=[%d,%d] : split=%d", order, keyFactory.keyLength() , recordFactory.valueLength() , MinRec, MaxRec, MinPtr, MaxPtr, SplitIndex ) ; }
@Override public Record map(ByteBuffer bb, int idx, byte[] keyBytes, RecordFactory factory) { byte[] key = new byte[factory.keyLength]; byte[] value = (factory.hasValue() ? new byte[factory.valueLength] :null ); // Is it better to avoid the synchronize needed for bb.position./bb.get // but not use a (relative) bulk bb.get(byte[],,) which may be a native operation? // int posnKey = idx*slotLen; // // Avoid using position() so we can avoid needing synchronized. // copyInto(key, bb, posnKey, keyLength); // if ( value != null ) // { // int posnValue = idx*slotLen+keyLength; // copyInto(value, bb, posnValue, valueLength); // } // Using bb.get(byte[],,) may be potentially faster but requires the synchronized // There's no absolute version. synchronized(bb) { bb.position(idx*factory.slotLen); bb.get(key, 0, factory.keyLength); if ( value != null ) bb.get(value, 0, factory.valueLength); } if ( keyBytes != null ) System.arraycopy(key, 0, keyBytes, 0, factory.keyLength); return factory.create(key, value); } };
public static Record intToRecord(int v, RecordFactory factory) { byte[] vb = Bytes.packInt(v) ; int recLen = factory.recordLength() ; byte[] bb = new byte[recLen] ; int x = 0 ; // Start point in bb. if ( recLen > 4 ) x = recLen-4 ; int len = Math.min(4, recLen) ; int z = 4-len ; // Start point in vb // Furthest right bytes. for ( int i = len-1 ; i >= 0 ; i-- ) bb[x+i] = vb[z+i] ; return factory.create(bb) ; }
public RecordBuffer(ByteBuffer bb, RecordFactory recFactory, int num) { super(bb, recFactory.recordLength(), num) ; this.factory = recFactory ; }
@Override public boolean contains(Record record) { Record r = find(record) ; if ( r == null ) return false ; if ( !recordFactory.hasValue() ) return true ; return Bytes.compare(record.getValue(), r.getValue()) == 0 ; }
private RecordRangeIterator(int id, Record fromRec, Record toRec, RecordBufferPageMgr pageMgr, RecordMapper<X> mapper) { currentIdx = 0 ; this.pageMgr = pageMgr; this.minRec = fromRec ; this.maxRec = toRec ; this.mapper = mapper ; this.keySlot = new byte[pageMgr.getRecordFactory().keyLength()] ; if ( toRec != null && fromRec != null && Record.keyLE(toRec, fromRec) ) { currentPage = null ; return ; } pageMgr.startRead(); currentPage = pageMgr.getRead(id) ; if ( currentPage.getCount() == 0 ) { // Empty page. close() ; return ; } if ( fromRec != null ) { currentIdx = currentPage.getRecordBuffer().find(fromRec) ; if ( currentIdx < 0 ) currentIdx = decodeIndex(currentIdx) ; } }
public <X> X _access(int idx, byte[] key, RecordMapper<X> mapper) { return factory.access(bb, idx, key, mapper) ; }
public Record _get(int idx) { return factory.buildFrom(bb, idx) ; }
static List<Record> createData(int N, RecordFactory recordFactory) { List<Record> originaldata = new ArrayList<>(N) ; for ( int i = 0; i < N ; i++ ) { Record record = recordFactory.create() ; Bytes.setInt(i+1, record.getKey()) ; if ( recordFactory.hasValue() ) Bytes.setInt(10*i+1, record.getValue()) ; originaldata.add(record) ; } return originaldata ; }
public int getRecordLength() { return recordFactory.recordLength() ; }
public Record buildFrom(ByteBuffer bb, int idx) { return access(bb, idx, null, mapperRecord); }
/** Create a key-only record, allocating blank space for the key */ public Record createKeyOnly() { return create(new byte[keyLength], null); }
public static RecordFactory makeRecordFactory(int keyLen, int valueLen) { return new RecordFactory(keyLen, valueLen) ; }
public int getKeyLength() { return keyFactory.recordLength() ; }
public void addToMetaData(MetaFile mf) { mf.setProperty(ParamOrder, order) ; mf.setProperty(ParamKeyLength, recordFactory.keyLength()) ; mf.setProperty(ParamValueLength, recordFactory.valueLength()) ; mf.flush() ; }