54
STANDARDIZINGOURDRIVERSTHROUGHSPECS: A LOOK AT THE CRUD API OPENING TUESDAY, JUNE 2 ND IN SELECT CONFERENCE ROOMS. Jeremy Mikola jmikola

Standardizing Our Drivers Through Specifications: A Look at the CRUD API

  • Upload
    mongodb

  • View
    145

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

STANDARDIZING OUR DRIVERS THROUGH SPECS:

A LOOK AT THE CRUD APIOPENING TUESDAY, JUNE 2ND

IN SELECT CONFERENCE ROOMS.

Jeremy Mikolajmikola

Page 2: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

What’s the deal with specs?

Page 3: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

A Cursory IntroductionSHAMELESSLY BORROWED FROM…

Page 4: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

MongoDB has a lot of drivers…

   

           

   

Page 5: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Drivers Today

APIs evolved over many yearsIdiomatic for their languageInconsistent with each otherSubtle behavioral differences

Our support team loves this!

Page 6: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Specifications

AuthenticationSDAM, server selectionWire protocol, write commandsUser-facing APIs

CRUD, enumeration, $out

Page 7: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

End Goals

Consistency across driversBehavior and API

More intuitive documentationIncreased developer productivityGuidance for third-party drivers

Page 8: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

End Goals (Continued)

Mitigate Parkinson’s law of triviality

Page 9: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Available on GitHub

Internal WIPs → Released SpecsSee: mongodb/specifications

Mailing lists are still in vogue: and mongodb-drivers mongodb-dev

Page 10: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

CRUD Specification

Page 11: Standardizing Our Drivers Through Specifications: A Look at the CRUD API
Page 12: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

What are we addressing?

Collection-level read/write methods.

Variations in naming and semantics.

Inconsistent functionality and APIs.

Page 13: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Default Behaviors (Exhibit A)

// Updates one documentdb.things.update( { "type": "human" }, { "$set": { "organic": true } });

// Removes all matching documentsdb.things.remove( { "type": "robot" });

Page 14: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Option Names (Exhibit B)

// Updates all documentsdb.things.update( { "type": "human" }, { "$set": { "organic": true } } { "multi": true });

// Removes one matching documentdb.things.remove( { "type": "robot" }, { "justOne": true });

Page 15: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Method Signatures (Exhibit C)

// According to the documentationfunction (query, update, options) { // ...}

// Actual implementationfunction (query, update, upsert, multi) { if ( typeof(upsert) === "object" ) { // Unpack options... }

// ...}

Legacy baggage with update()

Page 16: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

CRUD

CreateReadUpdateDelete

insert()find()

update()remove()

Page 17: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

But Wait… There’s More!

Page 18: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

CRUDCRADBOoMFaM

CreateReadUpdateDeleteCountReplaceAggregateDistinctBulk, One or ManyFind and Modify

Page 19: Standardizing Our Drivers Through Specifications: A Look at the CRUD API
Page 20: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Our entirely reasonable andlevel-headed approach…

BEAR WITH ME.

Page 21: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Terminology

Collection: class or interface representing a collection.Spec defines methods to be implemented on this object.

Iterable: some iterable object or structure.Cursor or cursor-like abstraction for reads.

Vector or array for write method arguments.

Page 22: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Operations

Methods to be implemented on the Collection object.

These have required and optional parameters.

Page 23: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Deviations

Spec is flexible with naming and option handling.

This permits idiomaticity. (real word)☜

Page 24: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Naming Deviations

“Root” words are non-negotiable.

batchSize, batch_size ʕ•ᴥ•ʔbatchCount щ(゚Д゚щ)

maxTimeMS, maxTime °͡ ʖ͜ °͡maximumTime? (╯°□°)╯︵ ┻━┻

FindOptions, FindArgs ح(゚ヮ゚)ノQueryParams? (ノಠ益ಠ)ノ彡┻━┻

ordered, isOrdered ᕕ( ᐛ )ᕗ

Page 25: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Option Handling

Required options are positional arguments.

For optional options, you have some options:

Named parametersDictionary or hash objectsOption classes (e.g. UpdateOptions)

May be consolidated, sharedFluent builders for find(), aggregate()

Document when order of operations applies!

Page 26: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

What we’re not doing

Method overloading.

Encapsulating params in “Model” classes.

Codifying inconsistent APIs for BC.

Page 27: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Diving into the API

Chapter 1: Reads

Page 28: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Queryingfind(filter: Document, options: FindOptions): Iterable<Document>;

filter is criteria (i.e. $query meta operator).

Support other operators through options.

Iterable is obviously a cursor here.

Page 29: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

FindOptions

allowPartialResults: BooleanbatchSize: Int32comment: StringcursorType: CursorTypelimit: Int32maxTimeMS: Int64modifiers: DocumentnoCursorTimeout: BooleanoplogReplay: Booleanprojection: Documentskip: Int32sort: Document

Page 30: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Abstracting Internal Details

CursorType enum may be normal,tailable, or tailable and awaitable.

Today’s wire protocol flags aretomorrow’s command options.

Users shouldn’t care andit’s not worth future API breaks.

Page 31: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Other Read Methodsaggregate(pipeline: Document[], options: AggregateOptions): Iterable<Document>;

count(filter: Document, options: CountOptions): Int64;

distinct(fieldName: string, filter: Document, options: DistinctOptions): Iterable<any>;

Page 32: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

AggregateOptions

allowDiskUse: BooleanbatchSize: Int32maxTimeMS: Int64useCursor: Boolean

useCursor default varies by server version.

May affect the kind of Iterable returned.

Page 33: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Diving into the API

Chapter 2: Writes

We’re basically 50% done at this point…

Page 34: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Write MethodsinsertOne(document: Document): InsertOneResult;

insertMany(Iterable<Document> documents, options: InsertManyOptions): InsertManyResult;

deleteOne(filter: Document): DeleteResult;

deleteMany(filter: Document): DeleteResult;

One or many behavior is explicit andfacilitates self-documenting code.

Eliminates inconsistency between multi and justOnedefaults for update() and remove(), respectively.

Page 35: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

insertMany()

insertMany is syntactic sugarfor bulkWrite() with inserts.

Spec doesn’t address legacybatch OP_INSERT operations.

Page 36: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

InsertManyOptions

ordered: Boolean

Page 37: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Write Methods (Continued)

replaceOne(filter: Document, replacement: Document, options: UpdateOptions): UpdateResult;

updateOne(filter: Document, update: Document, options: UpdateOptions): UpdateResult;

updateMany(filter: Document, update: Document, options: UpdateOptions): UpdateResult;

Same points about explicit,self-documenting code apply.

Trivial to validate if replacement orupdate documents contain operators.

Page 38: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

UpdateOptions

upsert: Boolean

Page 39: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Bulk WritesbulkWrite(requests: WriteModel[], options: BulkWriteOptions): BulkWriteResult;

Remember initializeOrderedBulkOp(),or the really old fluent API from 2013?

Page 40: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

WriteModel

Also, remember when we said we weren’t doing“model” classes that encapsulate all arguments?

Page 41: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

WriteModel

Models include required and optional (if any)arguments from single write methods.

bulkWrite()’s requests argumentallows users to specify all of their writes

at once and in a single method call.

Trivial to make a fluent API atop this,although it’s not in the spec.

Page 42: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

UpdateManyModel (f.e.)

filter: Document requiredupdate: Document requiredupsert: Boolean optional

Page 43: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

BulkWriteOptions

ordered: Boolean

Basically the same thing as InsertManyOptions.

Page 44: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Result Classes

Insert results may report driver-generated IDsDelete results include countsUpdate results include counts andserver-generated IDs from upsertsBulk results aggregate all of the above

Results are optional for unacknowledged writes.(e.g. Optional<BulkWriteResult>, isAcknowledged boolean)

Since all fields within insert results are optional,insertOne() and insertMany() may be void!

Page 45: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Write Errors

Spec is flexible on how errors are reportedMainly concerned that info is accessibleunder consistent fields and names.Doesn’t address merging errors

We have a bulk write spec for that

Page 46: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Diving into the API

Chapter 3: Find and Modify

I’ll keep this short…

Page 47: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Find and Modify MethodsfindOneAndDelete( filter: Document, options: FindOneAndDeleteOptions): Document;

findOneAndReplace( filter: Document, replacement: Document, options: FindOneAndReplaceOptions): Document;

findOneAndUpdate( filter: Document, update: Document, options: FindOneAndUpdateOptions): Document;

Option classes contain onlythe relevant command options.

Page 48: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Preemptive Q&A

Page 49: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Read Preferences?

Generally, queries on same collectionwill use the same read preference.

Spec assumes it’s set onclient, database, or collection.

Per-operation read preferences are permitted;the spec simply doesn’t define it.

Page 50: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Write Concerns?

Everything we just said about read preferences…

Page 51: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

findOne() et al.

Drivers are free to keep existing methodsand add new ones, too.

Please keep options and naming consistent.

Page 52: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Thanks!

Page 53: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

FIN.

Questions?

Page 54: Standardizing Our Drivers Through Specifications: A Look at the CRUD API

Image Credits

and http://www.instructables.com/id/Egg-Cream/Giphy Reaction Gifshttps://octodex.github.com/wheres-waldocat/