알쓸전컴(알아두면 쓸모있는 전자 컴퓨터)

(LokiJS)javascirpt in memory db 소개 자료 본문

Web

(LokiJS)javascirpt in memory db 소개 자료

백곳 2019. 2. 18. 17:37

(LokiJS)javascirpt in memory db 소개 자료


web program 에서 대용량 데이터를 처리하고 그래프를 그려줄 작업이 필요한데 

 

Parsing 까지는 반복문으로 직접 처리 하겠지만 


대용량에서 filter 메소드나 반복문으로 데이터를 처리하기에는 너무 자원에 무리가 갈것을 예상 하여 


빠르게 처리 하기 위한 노력이 다른 사람들도 있는지 찾아 봤다. 


brower in memory DB로 제법 규모가 있는 오픈 소스 프로젝트는 LokiJs가 유일하다.


그래도 있다는것에 감사하자 



indexedDB 을 사용하려 했지만 보조 디스크(하드 디스크,SSD) 를 사용한 방식이고 데이터가 클라이언트 PC에 저장되어 남아 있다.


이는 대용량 처리시 유저가 정상 종료시에는 DB를 지워 주겠지만 비정상 종료시에는 DB를 지워 주지는 못할시에 


임시파일의 압박이 예상 된다.


그래서 In memory 방식을 채택 했고 


http://lokijs.org/#


을 사용 하기로 계획 했다 .


https://github.com/techfort/LokiJS/


최근에도 계속 업데이트 되고 있고 Commit 수도 1000 이 넘어가는 어느정도 안정성은 보장되지? 않았나 싶다. 


사용방법은 아래 간단히 기초 사용법이 나와 있다. 

Quick Start

Installation

LokiJS is available on npm and bower. Run:
npm install lokijs 
or:
bower install lokijs

Usage

Create the database:

var db = new loki('loki.json')


Pass the filename where to persist data

Create a collection:

var children = db.addCollection('children')

Insert a document:

children.insert({name:'Sleipnir', legs: 8})
children.insert({name:'Jormungandr', legs: 0})
children.insert({name:'Hel', legs: 2})

Retrieve documents:

children.get(1); // returns Sleipnir
children.find( {'name':'Sleipnir'} )
children.find( { legs: { '$gt' : 2 } } )


Create a dynamic view:

var legs = children.addDynamicView('legs');
legs.applyFind( { legs: { '$gt' : 2 } })
legs.applySimpleSort('legs');
legs.data();


MapReduce:

children.mapReduce(
  function( obj ){ return obj.legs; } ,
  function( array ) {
    var sum = 0;
    for (var i=0; i < array.length; i++ ){
      sum += array[i];
    }
    return ( sum / array.length ).toFixed(2);
});


소개 자료를 보면 망고 DB 문법을 사용 할수 있는게 재미 있는점이다. 


In-memory JavaScript Datastore with Persistence

What is LokiJS?

  • In-Browser NoSQL db with syncing and persisting
  • Persistable NoSQL db for Cordova / Phonegap / Nativescript
  • Embeddable NoSQL db with Persistence for node-webkit
  • a Redis-style store an npm install away

LokiJS is an in-memory database which prioritises performance over everything * 
LokiJS supports field indexing for faster document access and performs really well (over 1.1M ops/s on an average dev machine) on those. Its built-in DynamicView class also enables to utilize indexes on data subsets for even faster performance.
DynamicView has the very handy feature of detecting changes in the database and recomputing itself to contain the most up-to-date data, and data is always readily available through the data() method.

* read this to get an idea of LokiJS's performance.

Among the features that make LokiJS attractive are:

  • Fast Performance
  • Replaces SQLite in Cordova, works as a session store and full blown NoSQL db in node.js, works as in-browser database with syncing capabilities
  • Indexing / Secondary Indexing / Unique Indexing
  • The "Dynamic View", a kind of "live filter" (self-materializing views)
  • Resultset with Fluent API
  • Persistence Adapters (with a built-in IndexedDB and node.js FS adapter)
  • Optional periodic autosave
  • Changes API
  • Compound sort for sorting on multiple columns
  • Insert class instances and deserialize/ inflate objects into class instances
  • partial compatibility with MongoDB API
  • RethinkDB-style Joins


LokiJS supports collections, much like MongoDB, and saves data to disk (to resume state across sessions) in JSON, so your data is portable.
LokiJS is compatible with node.js and browser.

Javascript is an easy to learn, versatile language, so database development in javascript can be easy and very productive. You may not want to retire your MongoDB instances just yet but here are some situations in which you may find LokiJS to be the ideal solution:

LokiJS is also going full blown standalone server soon, with http and tcp clients available.

Choose your favourite paradigm

LokiJS leverage the power of javascript by being versatile.
If functional programming is your preferred style then you may want to create views to query your data.
If you prefer object literals in MongoDB shell style, you can use those.

Comments