sample.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. const MongoClient = require("mongodb").MongoClient;
  2. const ObjectID = require("mongodb").ObjectID;
  3. const mm = require('./index.js').mm
  4. const delay = ms => new Promise(r => setTimeout(r.bind(ms), ms))
  5. ;(async () => {
  6. const mongoClient = new MongoClient("mongodb://localhost:27017/", { useNewUrlParser: true });
  7. const client = await mongoClient.connect()
  8. const db = client.db('mm')
  9. const Savable = mm(db).Savable
  10. const SlicedSavable = mm(db).sliceSavable([ObjectID("5c9571219be797377361c65a"), 'user', 'admin'])
  11. //const SlicedSavable = mm(db).sliceSavable([])
  12. //
  13. class Notebook extends SlicedSavable{
  14. static get relations(){
  15. return {
  16. owner: "notebook"
  17. }
  18. }
  19. }
  20. class User extends SlicedSavable{
  21. static get relations(){
  22. return {
  23. children: "parent",
  24. parent: ["children"],
  25. friends: ["friends"],
  26. notebook: "owner",
  27. }
  28. }
  29. }
  30. Savable.addClass(Notebook)
  31. Savable.addClass(User)
  32. let names = ['Ivan', 'Petro', 'Mykola', 'Sashko']
  33. let surnames = ['Ivanopulo', 'Petrov', 'Mykolyiv', 'Alexandrov']
  34. let rndItem = arr => arr[Math.floor(Math.random()*arr.length)]
  35. let person = new User({
  36. name: 'Mykola',
  37. surname: 'Silniy',
  38. phones: ['105', '1'],
  39. children: [
  40. new User({
  41. name: 'Marina',
  42. surname: 'Silnaya',
  43. phones: ['105', '1000503'],
  44. }),
  45. new User({
  46. name: 'Andrey',
  47. surname: 'Silniy',
  48. phones: ['103', '1000502'],
  49. }),
  50. new User({
  51. name: 'Fedor',
  52. surname: 'Ivanova',
  53. phones: ['102', '1000504'],
  54. notebook: new Notebook({
  55. brand: 'dubovo'
  56. })
  57. })
  58. ]
  59. })
  60. await person.save()
  61. console.log(person.children.length)
  62. person.children[0].parent = null;
  63. await person.children[0].save()
  64. console.log(person.children.length)
  65. //let orphan = new User({
  66. //name: 'Left',
  67. //surname: 'Guy',
  68. //parent: person //check for push into and $addToSet
  69. //})
  70. //await orphan.save()
  71. //person.parent = orphan //check for arrize
  72. //await person.save()
  73. //console.log(orphan.children)
  74. //person.parent = null //check for arrize
  75. //await person.save()
  76. //person.children.pop();
  77. //person.children.pop();
  78. //console.log(person.children.length)
  79. //await person.save()
  80. let stamp = (new Date()).getTime()
  81. let prevI = 0;
  82. const persons = []
  83. //for (var i=0;i<1e10;i++){
  84. //let person = new User({
  85. //name: rndItem(names),
  86. //surname: rndItem(surnames),
  87. //phones: ['105', '1'],
  88. //friends: persons.slice(-(Math.random()*80))
  89. //})
  90. //await person.save(true)
  91. //persons.push(person)
  92. //if (persons.length > 200){
  93. //await (Math.random() > 0.5 ? persons.shift() : persons.pop()).save(true)
  94. //}
  95. //let now = (new Date()).getTime()
  96. //if (stamp < now - 1000){
  97. ////results:
  98. ////objects w/o relations: 2500 writes per second
  99. ////objects w relations: pessimistic backrelations sync, 0..80 friends of 200 latest created, ~25 per second due 0..80 saves of other friends with new one relation
  100. ////objects w relations: ~500 per second, save w/o backref save (but it updated in object), than, when object removed from random buffer, re-save it with updated relations
  101. //console.log(i, i - prevI)
  102. //prevI = i
  103. //stamp = now
  104. //}
  105. //}
  106. async function walker(limit=10) {
  107. let start = (new Date()).getTime()
  108. let stamp = start
  109. let now = start
  110. let prevI = 0
  111. let person
  112. for (let uP of Savable.m.User.find({},null,{sort:['_id', -1], limit: [1]})){
  113. person = await uP
  114. console.log(person)
  115. }
  116. //let person = [...Savable.m.User.find({},null,{sort:['_id', 1], limit: [1]})][0]
  117. //console.log('order-huyorder', person)
  118. let prevPerson;
  119. for (var i=0;i<limit;i++){
  120. prevPerson = person || prevPerson
  121. try {
  122. person = person.friends ? await rndItem(person.friends) : await Savable.m.User.findOne() //walking in graph: go to random friend
  123. }
  124. catch(e){
  125. person = await Savable.m.User.findOne()
  126. }
  127. //console.log(prevPerson && prevPerson._id)
  128. //await prevPerson.delete()
  129. //if (persons.includes(person)){
  130. //console.log('WAS HERE',person._id, person.name, person.surname, person.createdAt)
  131. //}
  132. //for (let friend of person.friends){
  133. //await friend
  134. //}
  135. //persons.push(person)
  136. now = (new Date()).getTime()
  137. if (stamp < now - 1000){
  138. //results:
  139. //walking: 100-200 per second, not so fun...
  140. //loops in graph: near 0-5 on 100 steps between nodes in graph
  141. console.log(i, i - prevI, person._id, person.name, person.surname, person.createdAt)
  142. prevI = i
  143. stamp = now
  144. }
  145. }
  146. return now - start
  147. }
  148. //await walker(8531)
  149. //console.log(await Promise.all([walker(), walker()]))
  150. //console.log(await Promise.all([walker(), walker()]))
  151. //for (let child of father.children){
  152. //console.log(await child)
  153. //console.log(child.name, child.dirty)
  154. //}
  155. //let father = await Savable.m.User.findOne(ObjectID("5c9571219be797377361c65a"))
  156. //console.log(father);
  157. //(await father.children[0]).parent = null;
  158. //await (await father.children[0]).save();
  159. //console.log(father);
  160. //let notik = await Savable.m.Notebook.findOne(ObjectID('5c7c064d2ed0f4c9ab4cba4e'))
  161. //let SilniyeMans = await Savable.m.Savable.find({ $or: [{surname: 'Silniy'}, {surname: 'Silnaya'}]})
  162. //for (let manPromise of SilniyeMans){
  163. //let man = await manPromise;
  164. //console.log('man', man.name, man.surname, man.createdAt)
  165. //notik.owner = man
  166. ////notik.owner = [man]
  167. ////notik.owner = new Set([man])
  168. //break;
  169. //}
  170. //await notik.save()
  171. //console.log(notik)
  172. //notik.ram = 4;
  173. //notik.resolution = {width: 1920, height: 1080}
  174. //await notik.save()
  175. //console.log(await Savable.m.Notebook.findOne(ObjectID('5c7c064d2ed0f4c9ab4cba4e')))
  176. //while(true){
  177. //await (new Savable({timestamp: (new Date).getTime(), r: Math.random()})).save()
  178. //console.log(person)
  179. //await delay(1000)
  180. ////}
  181. ////let person = new Savable()
  182. ////person._id = ObjectID('5c7bd603ce3cbc409978203e');
  183. ////console.log(person)
  184. //let child = new Savable({
  185. //name: 'New One Child',
  186. //surname: 'Silniy',
  187. //phones: ['105', '1000506']
  188. //});
  189. ////console.log(await person)
  190. ////console.log(await person.children[1])
  191. //person.children.push(child)
  192. //child.father = person
  193. ////console.log(person)
  194. ////console.log(child)
  195. //await person.save()
  196. ////console.log(await person.children[3])
  197. //let p2 =new Savable({_id: ObjectID('5c7bf8f04a3a3299f7deda0d' )}, true) //check for cache hit
  198. //;(await new Savable({_id: ObjectID('5c7bf8f04a3a3299f7deda0d' )}, true)) //check for cache hit
  199. //;(await p2)
  200. //console.log('parent 2', p2)
  201. //console.log(await p2.children[3]) //check for other hit
  202. //console.log(await person.children[3].father)
  203. //console.log(await person.children[3].father.children[1])
  204. ////let obj = {
  205. ////then(cb){
  206. ////process.nextTick(() => cb(obj))
  207. ////}
  208. ////}
  209. ////console.log(await obj)
  210. ////console.log('empty await', await person)//.then(p => console.log(p))
  211. ////console.log('sub await', (await person.children[0]))//.then(p => console.log(p))
  212. client.close();
  213. })()