Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Renamed unique serializers to be easier to locate
Browse files Browse the repository at this point in the history
Added serializer to create row format necessary for unique ledger
  • Loading branch information
Todd Nine committed Feb 24, 2015
1 parent 8c581a4 commit c2989fc
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*
*/

package org.apache.usergrid.persistence.collection.serialization.impl;


import java.util.UUID;

import org.apache.usergrid.persistence.model.entity.Id;
import org.apache.usergrid.persistence.model.field.Field;


/**
* Class that encapsulates a unique field with the entity and version that own it
*/
public class UniqueFieldEntry {

private final UUID version;
private final Field field;


/**
* Create a unique field with the specified version and field
* @param version
* @param field
*/
public UniqueFieldEntry( final UUID version, final Field field ) {
this.version = version;
this.field = field;
}


public UUID getVersion() {
return version;
}


public Field getField() {
return field;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
*
* *
* * Licensed to the Apache Software Foundation (ASF) under one
* * or more contributor license agreements. See the NOTICE file
* * distributed with this work for additional information
* * regarding copyright ownership. The ASF licenses this file
* * to you under the Apache License, Version 2.0 (the
* * "License"); you may not use this file except in compliance
* * with the License. You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing,
* * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* * KIND, either express or implied. See the License for the
* * specific language governing permissions and limitations
* * under the License.
* *
*
*/
package org.apache.usergrid.persistence.collection.serialization.impl;


import java.nio.ByteBuffer;
import java.util.UUID;

import org.apache.usergrid.persistence.core.astyanax.DynamicCompositeParserImpl;
import org.apache.usergrid.persistence.model.field.BooleanField;
import org.apache.usergrid.persistence.model.field.DoubleField;
import org.apache.usergrid.persistence.model.field.Field;
import org.apache.usergrid.persistence.model.field.FieldTypeName;
import org.apache.usergrid.persistence.model.field.FloatField;
import org.apache.usergrid.persistence.model.field.IntegerField;
import org.apache.usergrid.persistence.model.field.LongField;
import org.apache.usergrid.persistence.model.field.StringField;
import org.apache.usergrid.persistence.model.field.UUIDField;

import com.netflix.astyanax.model.CompositeBuilder;
import com.netflix.astyanax.model.CompositeParser;
import com.netflix.astyanax.model.Composites;
import com.netflix.astyanax.model.DynamicComposite;
import com.netflix.astyanax.serializers.AbstractSerializer;
import com.netflix.astyanax.serializers.DynamicCompositeSerializer;


/**
* Serialize a unique field into a column name
*/
public class UniqueFieldEntrySerializer extends AbstractSerializer<UniqueFieldEntry> {


private static final UniqueFieldEntrySerializer INSTANCE = new UniqueFieldEntrySerializer();




@Override
public ByteBuffer toByteBuffer( final UniqueFieldEntry value ) {

CompositeBuilder builder = Composites.newDynamicCompositeBuilder();


final UUID version = value.getVersion();
final Field<?> field = value.getField();

final FieldTypeName fieldType = field.getTypeName();
final String fieldValue = field.getValue().toString().toLowerCase();


builder.addUUID( version );
builder.addString( field.getName() );
builder.addString( fieldValue );
builder.addString( fieldType.name() );

return builder.build();
}


@Override
public UniqueFieldEntry fromByteBuffer( final ByteBuffer byteBuffer ) {

final CompositeParser composite = new DynamicCompositeParserImpl( byteBuffer );

final UUID version = composite.readUUID();

final String name = composite.readString();

final String value = composite.readString();

final String typeString = composite.readString();


final FieldTypeName fieldType = FieldTypeName.valueOf( typeString );

final Field<?> field;

switch ( fieldType ) {
case BOOLEAN:
field = new BooleanField( name, Boolean.parseBoolean( value ) );
break;
case DOUBLE:
field = new DoubleField( name, Double.parseDouble( value ) );
break;
case FLOAT:
field = new FloatField( name, Float.parseFloat( value ));
break;
case INTEGER:
field = new IntegerField( name, Integer.parseInt( value ) );
break;
case LONG:
field = new LongField( name, Long.parseLong( value ) );
break;
case STRING:
field = new StringField( name, value );
break;
case UUID:
field = new UUIDField( name, UUID.fromString( value ) );
break;
default:
throw new RuntimeException( "Unknown unique field type" );
}

return new UniqueFieldEntry( version, field );
}


/**
* Get the singleton serializer
*/
public static UniqueFieldEntrySerializer get() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.usergrid.persistence.model.field.DoubleField;
import org.apache.usergrid.persistence.model.field.Field;
import org.apache.usergrid.persistence.model.field.FieldTypeName;
import org.apache.usergrid.persistence.model.field.FloatField;
import org.apache.usergrid.persistence.model.field.IntegerField;
import org.apache.usergrid.persistence.model.field.LongField;
import org.apache.usergrid.persistence.model.field.StringField;
Expand All @@ -39,10 +40,10 @@
/**
* Serialize Field for use as part of row-key in Unique Values Column Family.
*/
public class FieldSerializer implements CompositeFieldSerializer<Field> {
public class UniqueFieldRowKeySerializer implements CompositeFieldSerializer<Field> {


private static final FieldSerializer INSTANCE = new FieldSerializer();
private static final UniqueFieldRowKeySerializer INSTANCE = new UniqueFieldRowKeySerializer();


@Override
Expand All @@ -58,14 +59,15 @@ public void toComposite( final CompositeBuilder builder, final Field field ) {
switch ( fieldType ) {
case BOOLEAN:
case DOUBLE:
case FLOAT:
case INTEGER:
case LONG:
case STRING:
case UUID:
break;
default:
throw new RuntimeException(
String.format( "Type %s is not a supported type for unique values", fieldType ) );
String.format( "Type %s is not a supported type for unique values", fieldType ) );
}


Expand Down Expand Up @@ -95,6 +97,8 @@ public Field fromComposite( final CompositeParser composite ) {
return new BooleanField( name, Boolean.parseBoolean( value ) );
case DOUBLE:
return new DoubleField( name, Double.parseDouble( value ) );
case FLOAT:
return new FloatField( name, Float.parseFloat( value ) );
case INTEGER:
return new IntegerField( name, Integer.parseInt( value ) );
case LONG:
Expand All @@ -112,7 +116,7 @@ public Field fromComposite( final CompositeParser composite ) {
/**
* Get the singleton serializer
*/
public static FieldSerializer get() {
public static UniqueFieldRowKeySerializer get() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
import org.apache.usergrid.persistence.collection.serialization.UniqueValueSerializationStrategy;
import org.apache.usergrid.persistence.collection.serialization.UniqueValueSet;
import org.apache.usergrid.persistence.core.astyanax.ColumnTypes;
import org.apache.usergrid.persistence.core.astyanax.FieldBufferSerializer;
import org.apache.usergrid.persistence.core.astyanax.IdRowCompositeSerializer;
import org.apache.usergrid.persistence.core.astyanax.MultiTennantColumnFamily;
import org.apache.usergrid.persistence.core.astyanax.MultiTennantColumnFamilyDefinition;
import org.apache.usergrid.persistence.core.astyanax.ScopedRowKey;
import org.apache.usergrid.persistence.core.migration.schema.Migration;
import org.apache.usergrid.persistence.core.util.ValidationUtils;
import org.apache.usergrid.persistence.model.entity.Id;
import org.apache.usergrid.persistence.model.field.Field;
Expand All @@ -51,6 +52,7 @@
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.Row;
import com.netflix.astyanax.serializers.UUIDSerializer;
import com.netflix.astyanax.util.RangeBuilder;


Expand All @@ -61,16 +63,32 @@ public class UniqueValueSerializationStrategyImpl implements UniqueValueSerializ

private static final Logger log = LoggerFactory.getLogger( UniqueValueSerializationStrategyImpl.class );

// TODO: use "real" field serializer here instead once it is ready

private static final CollectionScopedRowKeySerializer<Field> ROW_KEY_SER =
new CollectionScopedRowKeySerializer<>( FieldSerializer.get() );
new CollectionScopedRowKeySerializer<>( UniqueFieldRowKeySerializer.get() );

private static final EntityVersionSerializer ENTITY_VERSION_SER = new EntityVersionSerializer();

private static final MultiTennantColumnFamily<ScopedRowKey<CollectionPrefixedKey<Field>>, EntityVersion> CF_UNIQUE_VALUES =
new MultiTennantColumnFamily<>( "Unique_Values", ROW_KEY_SER,
ENTITY_VERSION_SER );

private static final IdRowCompositeSerializer ID_SER = IdRowCompositeSerializer.get();




private static final CollectionScopedRowKeySerializer<Id> ENTITY_ROW_KEY_SER =
new CollectionScopedRowKeySerializer<>( ID_SER );


private static final MultiTennantColumnFamily<ScopedRowKey<CollectionPrefixedKey<Id>>, UUID> CF_ENTITY_UNIQUE_VALUE =
new MultiTennantColumnFamily<>( "Entity_Unique_Values", ENTITY_ROW_KEY_SER, UUIDSerializer.get() );

private static final FieldBufferSerializer FIELD_BUFFER_SERIALIZER = FieldBufferSerializer.get();



protected final Keyspace keyspace;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@
import org.junit.Assert;
import org.junit.Test;

import org.apache.usergrid.persistence.collection.serialization.impl.FieldSerializer;
import org.apache.usergrid.persistence.collection.serialization.impl.UniqueFieldRowKeySerializer;
import org.apache.usergrid.persistence.model.field.Field;
import org.apache.usergrid.persistence.model.field.IntegerField;

import com.netflix.astyanax.model.CompositeBuilder;
import com.netflix.astyanax.model.CompositeParser;
import com.netflix.astyanax.model.Composites;

public class FieldSerializerTest {
public class UniqueFieldRowKeySerializerTest {

@Test
public void testBasicOperation() {

Field original = new IntegerField( "count", 5 );

CompositeBuilder builder = Composites.newCompositeBuilder();
FieldSerializer fs = new FieldSerializer();
UniqueFieldRowKeySerializer fs = new UniqueFieldRowKeySerializer();
fs.toComposite( builder, original );
ByteBuffer serializer = builder.build();

CompositeParser parser = Composites.newCompositeParser( serializer );

Field deserialized = fs.fromComposite( parser );

Assert.assertEquals( original, deserialized );
}
}
}
Loading

0 comments on commit c2989fc

Please sign in to comment.