/** Create a key-only record, allocating blank space for the key */ public Record createKeyOnly() { return create(new byte[keyLength], null); }
/** 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); }
private Record record(ByteArray k, ByteArray v) { return recordFactory.create(k.bytes, v.bytes) ; }
/** Create a key and value record (value uninitialized) */ public Record create(byte[] key) { checkKey(key); byte[] v = null; if ( valueLength > 0 ) v = new byte[valueLength]; return create(key, v); }
private static Record record(int i) { byte b[] = new byte[]{ (byte)((i>>8)&0xFF), (byte)(i&0xFF)} ; Record r = factory.create(b) ; return r ; }
@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) ; }
/** Create a key-only record */ public Record createKeyOnly(Record record) { checkKey(record.getKey()); if ( record.getValue() == null ) return record; return create(record.getKey(), null); }
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 ; }
r = recordFactory.create(r.getKey()) ;