@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 void insertInto(Record record, ByteBuffer bb, int idx) { check(record); bb.position(idx * slotLen); bb.put(record.getKey(), 0, keyLength); if ( hasValue() && record.getValue() != null ) bb.put(record.getValue(), 0, valueLength); }
@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 ; }
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 ; }