preon (j-fall 2008)

40
Err.... Quarks Preon

Upload: wilfred-springer

Post on 24-May-2015

3.745 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Preon (J-Fall 2008)

Err....

QuarksPreon

Page 2: Preon (J-Fall 2008)

Preon

Adeclarativedatabindingframeworkforbinaryencodeddata

Page 3: Preon (J-Fall 2008)

Binary Encoded Data?

Anyfileforwhich'file‐bi{filename}'doesnotreturnamimetypestartingwith'text/'

Non‐binary:text/plain(*.txt)text/html(*.html)text/xml(*.xml)

Binary:application/pdf(*.pdf)application/octet‐stream(*.mp4)image/png(*.png)

Page 4: Preon (J-Fall 2008)

Not only byte stream

Alwaysoctects(8bitvalues)

8bits8bits

Page 5: Preon (J-Fall 2008)

But also bit stream

Alwaysoctects(8bitvalues)

5bits 5bits

Page 6: Preon (J-Fall 2008)

Compressed Data

2kg

1021pages

Page 7: Preon (J-Fall 2008)

Network Traffic

Page 8: Preon (J-Fall 2008)

TomTom Map Files

Approx.300pagesofC++sourcecode,justfordecodingonly...

Page 9: Preon (J-Fall 2008)

Why?

Binary45.71%

Non‐binary54.29%

Binaryvs.non‐binarydistributiononarandomdirectoryonmysystem

Page 10: Preon (J-Fall 2008)

Why?

...andonmywife'ssystem

Binaryfilesonly100%

Page 11: Preon (J-Fall 2008)

Challenges

● Decodingfrombitstreamnotforthefaint‐hearted

● Encodingtobitstreamnotforthefaint‐hearted

● Hardtomaintain

● Hardtoextend

● Javadoesn'thelp(Bug4504839)

● Decoderandencodereasilygooutofsync

● Documentationandsoftwareeasilygooutofsync

Page 12: Preon (J-Fall 2008)

What if....

..this could all be solved easily?

Page 13: Preon (J-Fall 2008)

Preon Ambitions

● Declarativelymapdatastructuretoencodingformat

● Getthedecoder/encoder/documentation,freeofcharge

Page 14: Preon (J-Fall 2008)

5-Second User Guide

01Filefile=newFile(...);02Codec<BitMap>codec=Codecs.create(BitMap.class);03BitMapbitmap=Codecs.decode(codec,file);

DecodingaBitMap:

Page 15: Preon (J-Fall 2008)

What just happened?

01Filefile=newFile(...);02Codec<BitMap>codec=Codecs.create(BitMap.class);03BitMapbitmap=Codecs.decode(codec,file);

CreateaCodecforinstancesofBitMap

...anduseittodecodeaBitMap.

Page 16: Preon (J-Fall 2008)

Find the specification

ThedatastructureISthespecification:classBitMap{@Boundintwidth;@Boundintheight;@BoundintnrColors;@BoundList(size=”nrColors”)Color[];@BoundList(size=”width*height”)byte[]pixels;}

classColor{@Boundintred;@Boundintgreen;@Boundintblue;}

Page 17: Preon (J-Fall 2008)

Demo Decoding

Page 18: Preon (J-Fall 2008)

But what is actually happening?

CodecisnothingbutafacacetoachainofCodecs.

EachCodeconlyunderstandsitsowntask.

CodecswilldelegatetootherCodecs

Page 19: Preon (J-Fall 2008)

Codecs

Encapsulateeverythingthereistoknowaboutthemappingbetweenin‐memoryrepresentationandencodedrepresentation

Page 20: Preon (J-Fall 2008)

Demo Documentation

Page 21: Preon (J-Fall 2008)

So, now....

ForgeteverythingIjusttoldyou

Page 22: Preon (J-Fall 2008)

Preon just works

Annotations

● @Bound● @BoundList● @BoundString● @BoundNumber● @BoundObject● @BoundExplicitly● @If● @LazyLoading● @LengthPrefix● @TypePrefix● @ByteAlign● @Slice

Types

int,byte,short,long,Integer,Byte,Short,Long,boolean,Boolean,String,List<T>,T[],type‐safeenums,Object

Expressions

attributes:.{name}items:[{number}],or[{expr}]arithmetic:+,‐,/,*,^combinatorial:&&,||relational:>=,<=,==,<,>literals:'foobar',0x0f,0b01101

Page 23: Preon (J-Fall 2008)

Convention over Configuration

//DefaultsettingsforintCodec://32‐bits,littleendian@Boundintfoo;

//Readanint,butconstructtheint//from5bitsonly@BoundNumber(size=”5”)intfoo;

Page 24: Preon (J-Fall 2008)

Expressions (1)

/***Aniconofmaximal15*15pixels.Eachcolor*hasacolorintherange0‐255.*/publicclassIcon{@BoundNumber(size=”4”)intheight;@BoundNumber(size=”4”)intwidth;@BoundList(size=”height*width”)byte[]pixels;}

ByacceptingStringsinsteadofbooleansandintegers,Preonallowsyoutopassinexpressions.

Page 25: Preon (J-Fall 2008)

Expressions (2)

@BoundString(size=”{expr}”,...)

@BoundList(size=”{expr}”,offset=”{expr}”,...)

@BoundNumber(size=”{expr}”,...)

@Slice(“{expr}”)

...

Page 26: Preon (J-Fall 2008)

Boolean expressions

@BoundprivateintmapVersion;

@If(“mapVersion>=700”)@BoundprivateMapFlagsflags;

//Butalso://mapVersion+1>=700//mapVersion>3&&mapVersion<300//etc.

Page 27: Preon (J-Fall 2008)

References

Backwardreferencesonly

Referencethe“outer”object

addresses[1].streetouter.driversLicenses[1].statedriveresLicenses[nrDriverLicenses‐1].state

Page 28: Preon (J-Fall 2008)

Variable Introductions

@Boundint[]offsets;@BoundList(offset=”offsets[index]”,...)List<Node>nodes;

● PreonwillinjectListimplementation

● ListimplementationwillloadNodeslazily,ondemand

● Callingnodes.get(3)willcausetheListimplementationto

– Calculatethenode'sposition:offsets[index=3]=120

– CallCodec<Node>tostartdecodingfromthatpoint

Introduction

Page 29: Preon (J-Fall 2008)

Inheritance

JavaClasses PreonPerspective

Page 30: Preon (J-Fall 2008)

Codecs class is your friend

static <T> T decode(Codec<T> codec, byte[] buffer)static <T> T decode(Codec<T> codec, ByteBuffer buffer)static <T> T decode(Codec<T> codec, File file)

static <T> Codec<T> create(Class<T> type) static <T> Codec<T> create(Class<T> type, CodecFactory...

factories)static <T> Codec<T> create(Class<T> type, CodecDecorator...

decorators)

static <T> void document(Codec<T> codec, ArticleDocument document)

static <T> void document(Codec<T> codec, DocumentType type, OutputStream out)

static <T> void document(Codec<T> codec, DocumentType type, File file)

Page 31: Preon (J-Fall 2008)

Preon Layers

Anexpressionlanguagecapableofrenderingitselftohumanreadabletext.(limbo.sourceforge.net)

Afluentinterfaceforgeneratingdocuments.(pecia.sourceforget.net)

BitBufferabstractions

Databinding

Page 32: Preon (J-Fall 2008)

Preon License

Apache2.0

Apache2.0

GPL+ClasspathException

GPL+ClasspathException

Page 33: Preon (J-Fall 2008)

If not Preon, then what else?

Declarativeapproachisnotnew:– BSDL(BitstreamSyntaxDescriptionLanguage)– Flavor(http://flavor.sourceforge.net/)– XFlavor– BFlavor(http://multimedialab.elis.ugent.be/bflavor/)

Noneofthemwouldworkedinourcase:– Overlycomplicated– Nosolidimplementation– Assumptionsaresurreal:

● “everythingwilljustfitinmemory”● “therewillonlybeasinglethread”● “ourcurrentfeaturesetisallyoueverneed”

Page 34: Preon (J-Fall 2008)

The Preon Answer

“everythingwilljustfitinmemory”

Preoniscapableofusingmemory‐mappeddata.(DefaultBitBufferimplementationwrapsaroundByteBuffer,andhencealsoMappedByteBuffer.)

“therewillonlybeasinglethread”

InPreon,everythreadcanhaveitsownreferencetothecurrentpositionintheBitBuffer.

“ourcurrentfeaturesetisallyoueverneed”

Preonhasincrediblyopen:implementCodecFactoryorCodecDecoratortocreateyourowncodecsbasedontypeinformationorannotations.

Page 35: Preon (J-Fall 2008)

CodecFactory

interface CodecFactory { <T> Codec<T> create(AnnotatedElement metadata, Class<T> type, ResolverContext context);}

AnnotationsontheobjectexpectingdatatobedecodedbytheCodec.

Thetypeofobjecttobedecoded.

Theobjectforconstructingreferences.

returnsnullifitdoesnothaveawaytoconstructaCodec

Page 36: Preon (J-Fall 2008)

CodecFactory Usage

● CodecFactoriescanbepassedtoCodecs.create(...)

● ...whichwillcausetheCodecsclasstoconsiderthesefactorieswhenconstructingthevariousCodecs.

● Internally,abigchainofcommands

Page 37: Preon (J-Fall 2008)

Current Status

● http://preon.sourceforge.net/

● Interfacesfairlystable

● Currentversion1.0‐SNAPSHOT

● CompleteJavaclassexamplebeforefirstreleasecandidate

● Bugs...

● Iwantyou

Page 38: Preon (J-Fall 2008)

Future work

● Theencodeoperation(after1.0)

● Betterdebugging

● Annotationdrivensupportforothercompressiontechniques

● Morehyperlinkinginthedocumentation

● Betteralgebraicsimplificationofexpressions

● DescriptionsfromJavaDoc

Page 39: Preon (J-Fall 2008)

If there is only a couple of things...

● XMLisjustalamewayofbinaryencoding

● PreoncutsouttheInfosetmodel,preventingunnecessarytransformations

● Preonmakesbinaryencodingeasy

● Preonisextensible

● Preon(probably)scalesquitewell

● PreonisfriendlytoJavadevelopers

Page 40: Preon (J-Fall 2008)

Confused?