{
if ( properties == null || properties.isEmpty() )
{
return Record.NO_NEXT_PROPERTY.intValue();
}
PropertyStore propStore = getPropertyStore();
List<PropertyRecord> propRecords = new ArrayList<PropertyRecord>();
PropertyRecord currentRecord = new PropertyRecord( propStore.nextId() );
currentRecord.setInUse( true );
currentRecord.setCreated();
propRecords.add( currentRecord );
for ( Entry<String, Object> entry : properties.entrySet() )
{
int keyId = indexHolder.getKeyId( entry.getKey() );
if ( keyId == -1 )
{
keyId = createNewPropertyIndex( entry.getKey() );
}
PropertyBlock block = new PropertyBlock();
propStore.encodeValue( block, keyId, entry.getValue() );
if ( currentRecord.size() + block.getSize() > PropertyType.getPayloadSize() )
{
// Here it means the current block is done for
PropertyRecord prevRecord = currentRecord;
// Create new record
long propertyId = propStore.nextId();
currentRecord = new PropertyRecord( propertyId );
currentRecord.setInUse( true );
currentRecord.setCreated();
// Set up links
prevRecord.setNextProp( propertyId );
currentRecord.setPrevProp( prevRecord.getId() );
propRecords.add( currentRecord );
// Now current is ready to start picking up blocks
}
currentRecord.addPropertyBlock( block );
}
/*
* Add the property records in reverse order, which means largest
* id first. That is to make sure we expand the property store file
* only once.
*/
for ( int i = propRecords.size() - 1; i >= 0; i-- )
{
propStore.updateRecord( propRecords.get( i ) );
}
/*
* 0 will always exist, if the map was empty we wouldn't be here
* and even one property will create at least one record.
*/