Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design and learning of the Algolia API client

Design and learning of the Algolia API client

More Decks by Rémy-Christophe Schermesser

Other Decks in Programming

Transcript

  1. Algolia Today 15 regions 36 data centers 1500+ customers in

    100+ countries 30B+ Write operations per month 12B+ User-generated queries per month
  2. =

  3. sealed trait Of case object of extends Of case object

    clear { def synonyms(of: Of): ClearSynonymsDefinition = ClearSynonymsDefinition() } sealed trait ForwardToSlave case object forwardToSlaves extends ForwardToSlave case class ClearSynonymsDefinition { def and(option: ForwardToSlave) = copy(option = Some(option)) }
  4. “object” is what you upload to Algolia, but a reserved

    keyword in scala def `object`() = ??? index into "toto" `object` MyObject("algolia", 1)
  5. sealed trait Ranking object Ranking { case class asc(attribute: String)

    extends Ranking case class desc(attribute: String) extends Ranking case object typo extends Ranking }
  6. • Futures are trendy • Future → synchronous is easy,

    not the other way around • Scala futures • for { f <- c.myMethod(); g <- f.doSomething() } yield g Why Futures?
  7. GET /1/indexes/index/browse { "cursor": "XXX", "hits":[ ... ] } GET

    /1/indexes/index/browse?cursor=XXX { "cursor": "YYY", "hits":[ ... ] } GET /1/indexes/index/browse?cursor=YYY etc.
  8. Eat your own dog food • use your api, if

    possible before releasing it • tests are a good way to “test” your API
  9. it("should index multiple objects") { batch( index into "test" `object`

    BasicObject("name1", 1), index into "tutu" objects Seq(BasicObject("name2", 2)), index into "test" objectId "oid1" `object` BasicObject("name3", 3), index into "tutu" objects Map("oid2" -> BasicObject("name4", 4)) ) }
  10. A good documentation • README, API documentation • Use common

    vocabulary (object, batch, browse, etc.) • But adapt the naming/idioms/syntax for each language
  11. TCP

  12. DNS

  13. DNS Future<InetAddress> getByName(final String host) { FutureTask<InetAddress> future = new

    FutureTask(new Callable<InetAddress>() { public InetAddress call() throws UnknownHostException { return InetAddress.getByName(host); } }); executor.execute(future); return future; }
  14. networkaddress.cache.ttl Specified in java.security to indicate the caching policy for

    successful name lookups from the name service.. The value is specified as integer to indicate the number of seconds to cache the successful lookup. A value of -1 indicates "cache forever". The default behaviour is to cache forever when a security manager is installed, and to cache for an implementation specific period of time, when a security manager is not installed. No workaround possible in the code
  15. batch( index into "indexToBrowse" `object` Test("algolia1", 10, alien = false),

    index into "indexToBrowse" `object` Test("algolia2", 10, alien = false), index into "indexToBrowse" `object` Test("algolia3", 10, alien = false), index into "indexToBrowse" `object` Test("anything", 10, alien = false) )
  16. client.inIndex("indexToBrowse") { implicit index => index `object` Test("algolia1", 10, alien

    = false) index `object` Test("algolia2", 10, alien = false) index `object` Test("algolia3", 10, alien = false) index `object` Test("anything", 10, alien = false) }