npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spatial-db-ngt

v0.0.113

Published

n-dimensional spatial database for JSON docs with vectors, combining NGT vector search with leveldb

Downloads

8

Readme

spatial-db-ngt

JSON db with n-dimensional nearest neighbors search, combining NGT vector search with JSON stored in leveldb.

Basically ngt-tool with JSON payloads. Similar to spatial-db but faster.

  • This tool is experimental

Installation

First install ngt.

Then install with npm.

npm i spatial-db-ngt

Usage

var SpatialDbNgt = require('spatial-db-ngt');
var ngtt = require('ngt-tool');

//create new spatial-db-ngt
var numDimensions = 5;
var sdb = new SpatialDbNgt('mytest',numDimensions);

//add 100 random items with json - one by one
for(var i=0;i<100;i++){
    var itemKey = "my_item"+i;
    var itemJSON = {"something":"stuff"+i};
    var randomVector = ngtt.generateVector(numDimensions); //array of random floats
    sdb.addItemToDb_sync(itemKey, itemJSON, randomVector);

    //note - adding multiple items with the same key will produce
    //multiple vectors associated with the same json object.
    //eg the json data is overwritten but vectors are permanent.
}

//add items, bulk
var keys = ["key1","key2"];
var jsonItems = [{"some1": "json1"},{"some2":"json2"}];
var vectors = ngtt.generateVectorList(numDimensions,jsonItems.length); //array of vectors; each vector is array of floats
sdb.addItemsToDb_sync(keys, jsonItems, vectors);

//update item json
sdb.updateItemJson_sync("my_item32", {"newstuff":"is here now"});

//get item from db
console.log(sdb.getItemFromDb_sync("my_item32"));

//result
/*
{
  vector: [
    0.5486296150852892,
    0.5829361650362115,
    0.5297864223424096,
    0.49587874692720013,
    0.8087344422687666
  ],
  json: { newstuff: 'is here now' },
  ngtId: 35,
  key: 'my_item32'
}
 */


//search for nearest neighbors 

var randomSearchVector = ngtt.generateVector(numDimensions); //array of random floats
var numberOfResults = 2;
var radius = 10.0;

console.log(sdb.searchDb_nearestNeighbors_sync(randomSearchVector, numberOfResults, radius));

//result:
/*
[
    {
        vector: [
            0.18625948240313495,
            0.7975673245218937,
            0.3348365678471359,
            0.16693183181406512,
            0.29184773086562577
        ],
        json: { some1: 'json1' },
        ngtId: 1,
        distance: 0.819544
    },
    {
        vector: [
            0.055458120172628966,
            0.9540835249844943,
            0.2664088896698613,
            0.16366906181043261,
            0.7774182161406313
        ],
        json: { some2: 'json2' },
        ngtId: 2,
        distance: 0.82182
    }
]*/

Async functions

Remove _sync from the end of function names to get async versions with callback(err, result) as the final parameter.

Where is the data stored?

If you named your database "mytest", then ngt data is stored in a folder called ./mytest_ngt_db and leveldb data goes to a folder named ./mytest_level_db.

What is NGT?

Yahoo Japan's Neighborhood Graph and Tree for Indexing High-dimensional Data

Note: spatial-db-ngt is not affiliated with Yahoo or Yahoo Japan.

See Also

stonks