voyage
play

Voyage NoSQL Object Database Damien Cassou, Stphane Ducasse and Luc - PowerPoint PPT Presentation

Voyage NoSQL Object Database Damien Cassou, Stphane Ducasse and Luc Fabresse W4S11 http://www.pharo.org Goal To let you build a real little application Show you a nice way to store objects W4S11 2 / 27 MongoDB Document


  1. Voyage NoSQL Object Database Damien Cassou, Stéphane Ducasse and Luc Fabresse W4S11 http://www.pharo.org

  2. Goal � To let you build a real little application � Show you a nice way to store objects W4S11 2 / 27

  3. MongoDB � Document oriented, open-source NoSQL database � Powerful query language � Most popular document database so far W4S11 3 / 27

  4. What is Voyage? � Object-Document Mapper for MongoDB � Think on Hibernate for MongoDB � For Pharo, obviously ;) W4S11 4 / 27

  5. Voyage Feature � Simple � Ensure object identity � Provide error-handling � Implement a connection pool W4S11 5 / 27

  6. Setting a Connexion | repository | repository := VOMongoRepository host: 'localhost' database: 'demo'. repository enableSingleton. W4S11 6 / 27

  7. Setting a In Memory Connexion | repository | repository := VOMemoryRepository new. repository enableSingleton. � Really nice to prototype � Then can easily switch to a MongoDB W4S11 7 / 27

  8. A Simple Model W4S11 8 / 27

  9. A Simpler Model W4S11 9 / 27

  10. A Simpler Model Object subclass: #Hero instanceVariableNames: 'name level powers' classVariableNames: '' package: 'SuperHeroes' Hero >> name ^ name Hero >> name: aString name := aString Hero >> level ^ level Hero >> level: anObject level := anObject Hero >> powers ^ powers ifNil: [ powers := Set new ] Hero >> addPower: aPower self powers add: aPower W4S11 10 / 27

  11. A Simpler Model Object subclass: #Power instanceVariableNames: 'name' classVariableNames: '' package: 'SuperHeroes'. Power>>name ^ name Power>>name: aString name := aString W4S11 11 / 27

  12. Root Classes � Entry-point for our database � Can be any class in the system � Marked as root by isVoyageRoot class method W4S11 12 / 27

  13. Root Classes Hero class >> isVoyageRoot ^ true W4S11 13 / 27

  14. Some Heroes Hero new name: 'Spiderman'; level: #epic; addPower: (Power new name: 'Super − strength'); addPower: (Power new name: 'Wall − climbing'); addPower: (Power new name: 'Spider instinct'); save. Hero new name: 'Wolverine'; level: #epic; addPower: (Power new name: 'Regeneration'); addPower: (Power new name: 'Adamantium claws'); save. W4S11 14 / 27

  15. In DB > db.Hero.find()[0] { "_id" : ObjectId("d847065c56d0ad09b4000001"), "#version" : 688076276, "#instanceOf" : "Hero", "level" : "epic", "name" : "Spiderman", "powers" : [ { "#instanceOf" : "Power", "name" : "Spider instinct" }, { "#instanceOf" : "Power", "name" : "Super − strength" }, { "#instanceOf" : "Power", "name" : "Wall − climbing" W4S11 15 / 27

  16. Querying Hero selectAll. > an OrderedCollection(a Hero, a Hero) Hero selectOne: [ :each | each name = 'Spiderman' ]. > a Hero Hero selectMany: [ :each | each level = #epic ]. > an OrderedCollection(a Hero, a Hero) W4S11 16 / 27

  17. Querying 2 Hero selectOne: { #name − > 'Spiderman' } asDictionary. > a Hero Hero selectMany: { #level − > #epic } asDictionary. > an OrderedCollection(a Hero, a Hero) W4S11 17 / 27

  18. Querying 3 Hero selectMany: { #level − > #epic } asDictionary sortBy: { #name − > VOOrder ascending } limit: 10 offset: 0. > an OrderedCollection(a Hero, a Hero) W4S11 18 / 27

  19. Some Operations Hero count. > 2 Hero count: [ :each | each name = 'Spiderman' ] > 1. Hero removeAll. "Beware of this!" > Hero class hero := Hero selectAll anyOne. hero remove. > a Hero W4S11 19 / 27

  20. Defining Document Roots � If you want to query them � If you want to share objects between roots W4S11 20 / 27

  21. Defining Document Roots � Yes, Hero is a root on the example � Power can be a root too (you can “share” powers between heroes) � Any class can be declared as root, Voyage will manage it automatically W4S11 21 / 27

  22. Defining Document Roots Power class >> isVoyageRoot ^ true Power new name: 'Fly'; save. Power new name: 'Super − strength'; save. fly := Power selectOne: [ :each | each name = 'Fly']. superStrength := Power selectOne: [ :each | each name = 'Super − strength']. Hero new name: 'Superman'; level: #epic; addPower: fly; addPower: superStrength; save. W4S11 22 / 27

  23. Important Reset repository when changing the schema VORepository current reset. W4S11 23 / 27

  24. In DB > db.Hero.find()[0] { "_id" : ObjectId("d8474983421aa909b4000008"), "#version" : NumberLong("3874503784"), "#instanceOf" : "Hero", "level" : "epic", "name" : "Superman", "powers" : [ { "#collection" : "Power", "#instanceOf" : "Power", "__id" : ObjectId("d84745dd421aa909b4000005") }, { "#collection" : "Power", "#instanceOf" : "Power", "__id" : ObjectId("d84745dd421aa909b4000006") } ] W4S11 24 / 27

  25. Relationships � Root references (kind of “foreign keys”) � Voyage handles cyclic references of root objects � Beware, Voyage does not support cyclic references of embedded objects (yet) W4S11 25 / 27

  26. Conclusion � We can easily save objects in MongoDB � More information in Entreprise Pharo: a Web Perspective W4S11 26 / 27

  27. A course by and in collaboration with Inria 2016 Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France https://creativecommons.org/licenses/by-nc-nd/3.0/fr/

Recommend


More recommend