1657169100
Inverted Index for levelup.
With prewired defaults!
var indexDb = InvertedIndex(db, 'index')
//get list of documents containing 'search' and 'term'
indexDb.query(['search', 'term'], function (err, docs){
console.log(docs)
})
//stream stubs of documents
indexDb.createQueryStream(['search', 'term'])
.on('data', console.log)
Configurable!
var indexDb = InvertedIndex(db, 'index',
//extract words from documents
function map(key, value, index) {
//parse, and pull out any bits of text you want,
//call index with an index and a rank!
//here we split by anything that is not a letter
//or a number
index(value.split(/[^\w\d]+/))
},
//when quering with createQueryStream,
//stub converts from each doc, to a short preview.
//Possibly, highlighting matches with the query!
function stub(doc, query) {
return doc.substring(0, 140) + '...\n'
})
insert documents into the database like you normally would.
fs.readFile('readme.md', function (err, value) {
db.put('readme.md', value, function () {
//and so on!
})
})
//run an index batch
indexDb.start()
add realtime update to level-map-merge
and get them in level-inverted-index
for free.
https://github.com/dominictarr/level-map-merge/blob/master/index.js#L106-L109
Author: Dominictarr
Source Code: https://github.com/dominictarr/level-inverted-index
License: MIT license
1657169100
Inverted Index for levelup.
With prewired defaults!
var indexDb = InvertedIndex(db, 'index')
//get list of documents containing 'search' and 'term'
indexDb.query(['search', 'term'], function (err, docs){
console.log(docs)
})
//stream stubs of documents
indexDb.createQueryStream(['search', 'term'])
.on('data', console.log)
Configurable!
var indexDb = InvertedIndex(db, 'index',
//extract words from documents
function map(key, value, index) {
//parse, and pull out any bits of text you want,
//call index with an index and a rank!
//here we split by anything that is not a letter
//or a number
index(value.split(/[^\w\d]+/))
},
//when quering with createQueryStream,
//stub converts from each doc, to a short preview.
//Possibly, highlighting matches with the query!
function stub(doc, query) {
return doc.substring(0, 140) + '...\n'
})
insert documents into the database like you normally would.
fs.readFile('readme.md', function (err, value) {
db.put('readme.md', value, function () {
//and so on!
})
})
//run an index batch
indexDb.start()
add realtime update to level-map-merge
and get them in level-inverted-index
for free.
https://github.com/dominictarr/level-map-merge/blob/master/index.js#L106-L109
Author: Dominictarr
Source Code: https://github.com/dominictarr/level-inverted-index
License: MIT license
1667425440
Perl script converts PDF files to Gerber format
Pdf2Gerb generates Gerber 274X photoplotting and Excellon drill files from PDFs of a PCB. Up to three PDFs are used: the top copper layer, the bottom copper layer (for 2-sided PCBs), and an optional silk screen layer. The PDFs can be created directly from any PDF drawing software, or a PDF print driver can be used to capture the Print output if the drawing software does not directly support output to PDF.
The general workflow is as follows:
Please note that Pdf2Gerb does NOT perform DRC (Design Rule Checks), as these will vary according to individual PCB manufacturer conventions and capabilities. Also note that Pdf2Gerb is not perfect, so the output files must always be checked before submitting them. As of version 1.6, Pdf2Gerb supports most PCB elements, such as round and square pads, round holes, traces, SMD pads, ground planes, no-fill areas, and panelization. However, because it interprets the graphical output of a Print function, there are limitations in what it can recognize (or there may be bugs).
See docs/Pdf2Gerb.pdf for install/setup, config, usage, and other info.
#Pdf2Gerb config settings:
#Put this file in same folder/directory as pdf2gerb.pl itself (global settings),
#or copy to another folder/directory with PDFs if you want PCB-specific settings.
#There is only one user of this file, so we don't need a custom package or namespace.
#NOTE: all constants defined in here will be added to main namespace.
#package pdf2gerb_cfg;
use strict; #trap undef vars (easier debug)
use warnings; #other useful info (easier debug)
##############################################################################################
#configurable settings:
#change values here instead of in main pfg2gerb.pl file
use constant WANT_COLORS => ($^O !~ m/Win/); #ANSI colors no worky on Windows? this must be set < first DebugPrint() call
#just a little warning; set realistic expectations:
#DebugPrint("${\(CYAN)}Pdf2Gerb.pl ${\(VERSION)}, $^O O/S\n${\(YELLOW)}${\(BOLD)}${\(ITALIC)}This is EXPERIMENTAL software. \nGerber files MAY CONTAIN ERRORS. Please CHECK them before fabrication!${\(RESET)}", 0); #if WANT_DEBUG
use constant METRIC => FALSE; #set to TRUE for metric units (only affect final numbers in output files, not internal arithmetic)
use constant APERTURE_LIMIT => 0; #34; #max #apertures to use; generate warnings if too many apertures are used (0 to not check)
use constant DRILL_FMT => '2.4'; #'2.3'; #'2.4' is the default for PCB fab; change to '2.3' for CNC
use constant WANT_DEBUG => 0; #10; #level of debug wanted; higher == more, lower == less, 0 == none
use constant GERBER_DEBUG => 0; #level of debug to include in Gerber file; DON'T USE FOR FABRICATION
use constant WANT_STREAMS => FALSE; #TRUE; #save decompressed streams to files (for debug)
use constant WANT_ALLINPUT => FALSE; #TRUE; #save entire input stream (for debug ONLY)
#DebugPrint(sprintf("${\(CYAN)}DEBUG: stdout %d, gerber %d, want streams? %d, all input? %d, O/S: $^O, Perl: $]${\(RESET)}\n", WANT_DEBUG, GERBER_DEBUG, WANT_STREAMS, WANT_ALLINPUT), 1);
#DebugPrint(sprintf("max int = %d, min int = %d\n", MAXINT, MININT), 1);
#define standard trace and pad sizes to reduce scaling or PDF rendering errors:
#This avoids weird aperture settings and replaces them with more standardized values.
#(I'm not sure how photoplotters handle strange sizes).
#Fewer choices here gives more accurate mapping in the final Gerber files.
#units are in inches
use constant TOOL_SIZES => #add more as desired
(
#round or square pads (> 0) and drills (< 0):
.010, -.001, #tiny pads for SMD; dummy drill size (too small for practical use, but needed so StandardTool will use this entry)
.031, -.014, #used for vias
.041, -.020, #smallest non-filled plated hole
.051, -.025,
.056, -.029, #useful for IC pins
.070, -.033,
.075, -.040, #heavier leads
# .090, -.043, #NOTE: 600 dpi is not high enough resolution to reliably distinguish between .043" and .046", so choose 1 of the 2 here
.100, -.046,
.115, -.052,
.130, -.061,
.140, -.067,
.150, -.079,
.175, -.088,
.190, -.093,
.200, -.100,
.220, -.110,
.160, -.125, #useful for mounting holes
#some additional pad sizes without holes (repeat a previous hole size if you just want the pad size):
.090, -.040, #want a .090 pad option, but use dummy hole size
.065, -.040, #.065 x .065 rect pad
.035, -.040, #.035 x .065 rect pad
#traces:
.001, #too thin for real traces; use only for board outlines
.006, #minimum real trace width; mainly used for text
.008, #mainly used for mid-sized text, not traces
.010, #minimum recommended trace width for low-current signals
.012,
.015, #moderate low-voltage current
.020, #heavier trace for power, ground (even if a lighter one is adequate)
.025,
.030, #heavy-current traces; be careful with these ones!
.040,
.050,
.060,
.080,
.100,
.120,
);
#Areas larger than the values below will be filled with parallel lines:
#This cuts down on the number of aperture sizes used.
#Set to 0 to always use an aperture or drill, regardless of size.
use constant { MAX_APERTURE => max((TOOL_SIZES)) + .004, MAX_DRILL => -min((TOOL_SIZES)) + .004 }; #max aperture and drill sizes (plus a little tolerance)
#DebugPrint(sprintf("using %d standard tool sizes: %s, max aper %.3f, max drill %.3f\n", scalar((TOOL_SIZES)), join(", ", (TOOL_SIZES)), MAX_APERTURE, MAX_DRILL), 1);
#NOTE: Compare the PDF to the original CAD file to check the accuracy of the PDF rendering and parsing!
#for example, the CAD software I used generated the following circles for holes:
#CAD hole size: parsed PDF diameter: error:
# .014 .016 +.002
# .020 .02267 +.00267
# .025 .026 +.001
# .029 .03167 +.00267
# .033 .036 +.003
# .040 .04267 +.00267
#This was usually ~ .002" - .003" too big compared to the hole as displayed in the CAD software.
#To compensate for PDF rendering errors (either during CAD Print function or PDF parsing logic), adjust the values below as needed.
#units are pixels; for example, a value of 2.4 at 600 dpi = .0004 inch, 2 at 600 dpi = .0033"
use constant
{
HOLE_ADJUST => -0.004 * 600, #-2.6, #holes seemed to be slightly oversized (by .002" - .004"), so shrink them a little
RNDPAD_ADJUST => -0.003 * 600, #-2, #-2.4, #round pads seemed to be slightly oversized, so shrink them a little
SQRPAD_ADJUST => +0.001 * 600, #+.5, #square pads are sometimes too small by .00067, so bump them up a little
RECTPAD_ADJUST => 0, #(pixels) rectangular pads seem to be okay? (not tested much)
TRACE_ADJUST => 0, #(pixels) traces seemed to be okay?
REDUCE_TOLERANCE => .001, #(inches) allow this much variation when reducing circles and rects
};
#Also, my CAD's Print function or the PDF print driver I used was a little off for circles, so define some additional adjustment values here:
#Values are added to X/Y coordinates; units are pixels; for example, a value of 1 at 600 dpi would be ~= .002 inch
use constant
{
CIRCLE_ADJUST_MINX => 0,
CIRCLE_ADJUST_MINY => -0.001 * 600, #-1, #circles were a little too high, so nudge them a little lower
CIRCLE_ADJUST_MAXX => +0.001 * 600, #+1, #circles were a little too far to the left, so nudge them a little to the right
CIRCLE_ADJUST_MAXY => 0,
SUBST_CIRCLE_CLIPRECT => FALSE, #generate circle and substitute for clip rects (to compensate for the way some CAD software draws circles)
WANT_CLIPRECT => TRUE, #FALSE, #AI doesn't need clip rect at all? should be on normally?
RECT_COMPLETION => FALSE, #TRUE, #fill in 4th side of rect when 3 sides found
};
#allow .012 clearance around pads for solder mask:
#This value effectively adjusts pad sizes in the TOOL_SIZES list above (only for solder mask layers).
use constant SOLDER_MARGIN => +.012; #units are inches
#line join/cap styles:
use constant
{
CAP_NONE => 0, #butt (none); line is exact length
CAP_ROUND => 1, #round cap/join; line overhangs by a semi-circle at either end
CAP_SQUARE => 2, #square cap/join; line overhangs by a half square on either end
CAP_OVERRIDE => FALSE, #cap style overrides drawing logic
};
#number of elements in each shape type:
use constant
{
RECT_SHAPELEN => 6, #x0, y0, x1, y1, count, "rect" (start, end corners)
LINE_SHAPELEN => 6, #x0, y0, x1, y1, count, "line" (line seg)
CURVE_SHAPELEN => 10, #xstart, ystart, x0, y0, x1, y1, xend, yend, count, "curve" (bezier 2 points)
CIRCLE_SHAPELEN => 5, #x, y, 5, count, "circle" (center + radius)
};
#const my %SHAPELEN =
#Readonly my %SHAPELEN =>
our %SHAPELEN =
(
rect => RECT_SHAPELEN,
line => LINE_SHAPELEN,
curve => CURVE_SHAPELEN,
circle => CIRCLE_SHAPELEN,
);
#panelization:
#This will repeat the entire body the number of times indicated along the X or Y axes (files grow accordingly).
#Display elements that overhang PCB boundary can be squashed or left as-is (typically text or other silk screen markings).
#Set "overhangs" TRUE to allow overhangs, FALSE to truncate them.
#xpad and ypad allow margins to be added around outer edge of panelized PCB.
use constant PANELIZE => {'x' => 1, 'y' => 1, 'xpad' => 0, 'ypad' => 0, 'overhangs' => TRUE}; #number of times to repeat in X and Y directions
# Set this to 1 if you need TurboCAD support.
#$turboCAD = FALSE; #is this still needed as an option?
#CIRCAD pad generation uses an appropriate aperture, then moves it (stroke) "a little" - we use this to find pads and distinguish them from PCB holes.
use constant PAD_STROKE => 0.3; #0.0005 * 600; #units are pixels
#convert very short traces to pads or holes:
use constant TRACE_MINLEN => .001; #units are inches
#use constant ALWAYS_XY => TRUE; #FALSE; #force XY even if X or Y doesn't change; NOTE: needs to be TRUE for all pads to show in FlatCAM and ViewPlot
use constant REMOVE_POLARITY => FALSE; #TRUE; #set to remove subtractive (negative) polarity; NOTE: must be FALSE for ground planes
#PDF uses "points", each point = 1/72 inch
#combined with a PDF scale factor of .12, this gives 600 dpi resolution (1/72 * .12 = 600 dpi)
use constant INCHES_PER_POINT => 1/72; #0.0138888889; #multiply point-size by this to get inches
# The precision used when computing a bezier curve. Higher numbers are more precise but slower (and generate larger files).
#$bezierPrecision = 100;
use constant BEZIER_PRECISION => 36; #100; #use const; reduced for faster rendering (mainly used for silk screen and thermal pads)
# Ground planes and silk screen or larger copper rectangles or circles are filled line-by-line using this resolution.
use constant FILL_WIDTH => .01; #fill at most 0.01 inch at a time
# The max number of characters to read into memory
use constant MAX_BYTES => 10 * M; #bumped up to 10 MB, use const
use constant DUP_DRILL1 => TRUE; #FALSE; #kludge: ViewPlot doesn't load drill files that are too small so duplicate first tool
my $runtime = time(); #Time::HiRes::gettimeofday(); #measure my execution time
print STDERR "Loaded config settings from '${\(__FILE__)}'.\n";
1; #last value must be truthful to indicate successful load
#############################################################################################
#junk/experiment:
#use Package::Constants;
#use Exporter qw(import); #https://perldoc.perl.org/Exporter.html
#my $caller = "pdf2gerb::";
#sub cfg
#{
# my $proto = shift;
# my $class = ref($proto) || $proto;
# my $settings =
# {
# $WANT_DEBUG => 990, #10; #level of debug wanted; higher == more, lower == less, 0 == none
# };
# bless($settings, $class);
# return $settings;
#}
#use constant HELLO => "hi there2"; #"main::HELLO" => "hi there";
#use constant GOODBYE => 14; #"main::GOODBYE" => 12;
#print STDERR "read cfg file\n";
#our @EXPORT_OK = Package::Constants->list(__PACKAGE__); #https://www.perlmonks.org/?node_id=1072691; NOTE: "_OK" skips short/common names
#print STDERR scalar(@EXPORT_OK) . " consts exported:\n";
#foreach(@EXPORT_OK) { print STDERR "$_\n"; }
#my $val = main::thing("xyz");
#print STDERR "caller gave me $val\n";
#foreach my $arg (@ARGV) { print STDERR "arg $arg\n"; }
Author: swannman
Source Code: https://github.com/swannman/pdf2gerb
License: GPL-3.0 license
1657258020
Reference every value in your leveldb to its parent, e.g. by setting value.parentKey
to the key of the parent, then level-tree-index will keep track of the full path for each value, allow you to look up parents and children, stream the entire tree or a part thereof and even perform streaming search queries on the tree.
This is useful for implementing e.g. nested comments.
level-tree-index works for all keyEncodings. It works for the json valueEncoding automatically and works for other valueEncodings if you provide custom functions for the opts.pathProp
and opts.parentProp
options. level-tree-index works equally well with string and buffer paths.
level-tree-index automatically keeps the tree updated as you add, change or delete from the database.
Usage
// db contains your data and idb is used to store the index
var tree = treeIndexer(db, idb);
db.put('1', {name: "foo"}, function(err) {
if(err) fail(err);
db.put('2', {parentKey: '1', name: "bar"}, function(err) {
if(err) fail(err);
db.put('3', {parentKey: '2', name: "baz"}, function(err) {
if(err) fail(err);
// wait for index to finish building
setTimeout(function() {
// stream child-paths of 'foo' recursively
var s = tree.stream('foo');
s.on('data', function(data) {
console.log(data.path, data.key, data.value);
});
}, 500);
});
});
});
Read the unit tests in tests/
for more.
API
opts:
pathProp: 'name' // property used to construct the path
parentProp: 'parentKey' // property that references key of parent
sep: 0x1f, // path separator. can be string or unicode/ascii character code
pathArray: false, // for functions that output paths, output paths as arrays
ignore: false, // set to a function to selectively ignore
listen: true, // listen for changes on db and update index automatically
uniquefy: false, // add uniqueProp to end of pathProp to ensure uniqueness
uniqProp: 'unique', // property used for uniqueness
uniqSep: 0x1e, // like `sep` but separates pathProp from uniqProp
levelup: false // if true, returns a levelup instance instead
orphanPath: 'orphans' // parent path of orphans
Both pathProp
and parentProp
can be either a string, a buffer or a function.
If a function is used then the function will be passed a value from your database as the only argument. The pathProp
function is expected to return a string or buffer that will be used to construct the path by joining multiple returned pathProp
values with the opts.sep
value as separator. The parentProp
function is expected to return the key in db
of the parent.
opts.sep
can be a buffer of a string and is used as a separator to construct the path to each node in the tree.
opts.ignore
can be set to a function which will receive the key and value for each change and if it returns something truthy then that value will be ignored by the tree indexer, e.g:
Setting orphanPath
to a string, buffer or array will cause all orphaned rows to have orphanPath
as their parent path. Setting orphanPath
to null
will cause orphaned rows to be ignored (not indexed). An orphan is defined as a row with its parentProp
set to a non-falsy value but where the referenced parent does not exist in the database. This can happen e.g. if a parent is deleted but its children are left in the database.
// ignore items where the .name property starts with an underscore
ignore: function(key, value) {
if(typeof value === 'object') {
if(typeof value.name === 'string') {
if(value.name[0] === '_') {
return true;
}
}
}
return false;
}
If opts.listen
is true then level-tree-index will listen to operations on db and automatically update the index. Otherwise the index will only be updated when .put/.del/batch is called directly on the level-tree-index instance. This option is ignored when opts.levelup
is true.
If opts.levelup
is true then instead of a level-tree-index instance a levelup instance will be returned with all of the standard levelup API + the level-tree-index API. All calls to .put, .del or .batch will operate on the database given as the db
argument and only call their callbacks once the tree index has been updated.
Limitations when using levelup:true
:
opts.pathProp
and opts.parentProp
must be set to functions and if you're using valueEncoding:'json'
then those functions will receive the stringified json data.See tests/levelup.js
for how to use the levelup:true
mode.
Get the path and key of the root element. E.g:
tree.getRoot(function(err, path, key) {
console.log("Path of root element:", path);
console.log("Key of root element:", key);
});
Recursively stream descendants starting from parentPath
. If parentPath
is falsy then the entire tree will be streamed to the specified depth.
Opts:
depth: 0, // how many (grand)children deep to go. 0 means infinite
paths: true, // output the path for each child
keys: true, // output the key for each child
values: true, // output the value for each child
pathArray: undefined, // output paths as arrays
ignore: false, // optional function that returns true for values to ignore
match: null, // Stream only matching elements. A string, buffer or function.
matchAncestors: false, // include ancestors of matches if true
gt: undefined, // specify gt directly, must then also specify lt or lte
gte: undefined, // specify gte directly, must then also specify lt or lte
lt: undefined, // specify lt directly, must then also specify gt or gte
lte: undefined // specify lte directly, must then also specify lt or gte
If parentPath
is not specified then .gt/.gte
and .lt/.lte
must be specified.
opts.depth
is currently not usable at the same time as opts.match
.
If more than one of opts.paths
, opts.keys
and opts.values
is true then the stream will output objects with these as properties.
opts.ignore
can be set to a function. This function will receive whatever the stream is about to output (which depends on opts.paths
, opts.keys
and opts.values
) and if the function returns true then those values will not be emitted by the stream.
opts.match
allows for streaming search queries on the tree. If set to a string or buffer it will match any path that contains that string or buffer. If set to a RegEx then it will run a .match on the path with that RegEx (only works for string paths). If set to a function then that function will be called with the path as first argument and with the second argument depending on the values of opts.paths
, opts.keys
and opts.values
, e.g:
match: function(path, o) {
if(o.value.name.match("cattens")) {
return true;
}
return false;
}
Setting opts.matchAncestors
to true modifies the behaviour of opts.match
to also match all ancestors of matched elements. Ancestors of matched elements will then be streamed in the correct order before the matched element. This requires some buffering so may slow down matches on very large tree indexes.
When using opts.lt/opts.lte
you can use the convenience function .lteKey(key)
. E.g. to stream all paths that begin with 'foo.bar' you could run:
levelTree.stream({
gte: 'foo.bar',
lte: levelTree.lteKey('foo.bar')
});
Keep in mind that the above example would also return paths like 'foo.barbar'.
Convenience function that, according to leveldb alphabetical ordering, returns the last possible string or buffer that begins with the specified string or buffer.
Stream tree index ancestor paths starting from path
. Like .stream()
but traverses ancestors instead of descendants.
Opts:
height: 0, // how many (grand)children up to go. 0 means infinite
includeCurrent: true, // include the node specified by path in the stream
paths: true, // output the path for each child
keys: true, // output the key for each child
values: true, // output the value for each child
pathArray: undefined, // output paths as arrays
Same as .parentStream
but calls back with the results as an array.
Get key and value from path.
Callback: cb(err, key, value)
Get tree path given a key.
opts.pathArray: undefined // if true, split path into array
Callback: cb(err, path)
Get parent value given a value.
Callback: cb(err, parentValue)
Get parent path given a key.
opts.pathArray: undefined // if true, split path into array
Callback: cb(err, parentPath)
Get parent path given a value.
opts.pathArray: undefined // if true, split path into array
Callback: cb(err, parentPath)
Get parent value given a path.
Callback: cb(err, parentValue)
Get parent path given a path.
opts.pathArray: undefined // if true, split path into array
Note: this function can be called synchronously
Callback: cb(err, parentPath)
Get array of children given a value.
Same usage as .stream
but this version isn't streaming.
Callback: cb(err, childArray)
Same as .children
but takes a key as input.
Same usage as .stream
but this version isn't streaming.
Callback: cb(err, childArray)
Same as .stream with only opts.paths set to true.
Same as .stream with only opts.keys set to true.
Same as .stream with only opts.values set to true.
Clear the index. Deletes all of the index's data in the index db.
Build the index from scratch.
Note: You will likely want to .clear the index first or call .rebuild instead.
Clear and then build the index.
If you need to wait for the tree index to update after a .put
operation then you can use .put directly on the level-tree-index instance and give it a callback. Calling .put
this way is much less efficient so if you are planning to use this feature most of the time then you should look into using level-tree-index with the levelup:true
option instead.
Allows you to wait for the tree index to finish building using a callback. Same as .put
above but for deletion.
Uniqueness
The way level-tree-index
works requires that each indexed database entry has a globally unique path. In other words no two siblings can share the same pathProp
.
You might get into a situation where you really need multiple siblings with an identical pathProp
. Then you might wonder if you coulds just append e.g. a random string to each pathProp
before passing it to level-tree-index
and then strip it away again before e.g. showing the data to users.
Well, level-tree-index
provides helpers for exactly that. You can set opts.uniquefy
to true
in the constructor. You will then need database each entry to have a property that, combined with its pathProp
, makes it unique. This can be as simple as a long randomly generated string. As with pathProp
you will have to inform level-tree-index
about this property with uniqProp
.
You will then run into the problem that you no longer know the actual path names since they have the uniqueness added. You can either get the actual path name using the synchronous function .getPathName(val)
where val
is the value from the key-value pair for which you want the path. Or you can call .put
or .batch
directly on your level-tree-index
instance and they will pass your callback a second argument which for .put
is the actual path name and for .batch
is an array of path names for all put
operations.
When uniqefy
is turned on any functions returning paths will now be returning paths with the uniqueness data appended. You can use the convenience function .nicify(path)
to convert these paths into normal paths without the uniqueness data. For .stream
and any functions described as "same as .stream but ..." you can add set opts.nicePaths
to true and in you will receive the nicified paths back with each result.
Async quirks
Note that when you call .put, .del or .batch on your database level-tree-index will not be able to delay the callback so you cannot expect the tree index to be up to date when the callback is called. That is why you see the setTimeout used in the usage example above. You can instead call .put, .del or .batch directly on the level-tree-index instance and your callback will not be called until the index has finished building. This works but if opts.listen
is set to true then an inefficient and inelegant workaround is used (in order to prevent the change listener from attempting to update the already updated index) which could potentially slow things down.
If you want to wait for the index to update most of the time then you should probably either set opts.listen
to false or use the levelup mode by calling the constructor with opts.levelup
set to true, though that has its own drawbacks, especially if using valueEncoding:'json'
. See the constructor API documentation for more.
I normal operation (opts.levelup == false)
level-tree-index will listen for any changes on your database and update its index every time a change occurs. This is implemented using leveup change event listeners which run after the database operation has already completed.
When running .put
or .del
directly on level-tree-index the operation is performed on the underlying database then the tree index is updated and then the callback is called. Since we can't turn off the change event listeners for a specific operation, level-tree-index has to remember the operations performed directly through .put
or .del
on the level-tree-index instance such that the change event listener can ignore them to prevent the tree-index update operation from being called twice. This is done by hashing the entire operation, saving the hash and then checking the hash of each operation picked up by the change event listeners agains the saved hash. This is obviously inefficient. If this feature is never used then nothing is ever hashed nor compared so performance will not be impacted.
ToDo
opts.depth
working with opts.match
.Author: Biobricks
Source Code: https://github.com/biobricks/level-tree-index
License: AGPLv3
1598750220
Getting a performance boost with the best usage of indexes, by understanding what’s the data structure, how it works’s/stored, how is it loaded into memory. How Query optimization make’s decision to select indexes.
_Basic understanding of indexes is required i.e what are indexes, index types, creating them. _https://docs.mongodb.com/manual/indexes/
Index on a filed/fields is stored in order that we specify using B-Tree data structure. Stored in ordered Let see what does it mean’s and how it help’s.
snipped from: MognoDB university
snipped from: MognoDB university
snipped from: MognoDB university
Let’s see/visualize how the index are stored on disk. Index stored on disk is managed by the database storage engine itself.
db.getCollection("movieTicket")
.ensureIndex({"showDate":1, "seatNo":1, "status":1});
How the index ({“showDate”:1, “seatNo”:1, “status”:1}) is stored on disk.
showDate_1_seatNo_1_status_1
#index #mongodb #indexing #mongo
1657178700
npm install [--save/--save-dev] inverted-index
var inverted = require('inverted-index')
var level = require('level')('/path/to/my/db')
var sublevel = require('sublevel')
var index = inverted(sublevel(db, 'index'), {
idf: true,
stem: true,
rank: true,
rank_algorithm: 'cosine',
facets: true
}, function(id, options, fn){
level.get(id, options, fn)
})
Any level API-compatible instance is accepted.
The exemplified options is the default configuration.
idf
When idf
is flagged as true, for each token indexed an idf
(term frequency–inverse document frequency) is calculated. When querying the index, the terms with lowest idf
are fetched first. Example:
"Julie loves me more than Linda loves me"
[
{
"word": "julie",
"idf": 1.791759469228055
},
{
"word": "linda",
"idf": 1.791759469228055
},
{
"word": "loves",
"idf": 1.0986122886681098
}
]
Notice that "me", "more" and "than" are not indexed, because those are considered stopwords
.
stem
Whether the text should be stemmed or not. When true, the text is stemmed with the Porter stemming algorithm using NaturalNode/natural. Example:
"Fishing is a way of catching cats, he argued in his arguments"
is tokenized into:
["fishing", "is", "a", "way", "of", "catching", "cats", "he", "argued", "in", "his", "arguments"]
and stemmed into:
["fish", "is", "a", "wai", "of", "catch", "cat", "he", "argued", "in", "his", "argum"]
rank
With ranking enabled, when querying it ranks the results based on a defined algorithm. The rank is done AFTER the fetch, so it only ranks using the result set (that can be parcial depending on the size of matching results) comparing the query with the original indexed text, to the tokens.
So, idf
is used to fetch tokens ordered by idf
and then ranking is done with the original text of each token's correspondent document comparing with the query text. The "problem" with ranking is that if you have 100000 tokens that match the query tokens, only 100
(can be set on the query options) are fetched for each page and THEN the rank is done. Example:
{
"1": "Fishing is a way of catching cats, Julie argued in her arguments",
"2": "Julie loves me more than Linda loves me"
}
querying Julie loves
would fetch:
[
{
"word": "loves",
"idf": 1.0986122886681098,
"id": "2"
},
{
"word": "julie",
"idf": 1.791759469228055,
"id": "2"
},
{
"word": "julie",
"idf": 2.4849066497880004,
"id": "1"
}
]
and then rank them:
["2", "1"]
rank_algorithm
Only takes effect when rank
is set to true. Valid options are cosine
or sift3
using ramitos/cosine and ramitos/sift3.
Haven't made any benchmarks on that, but sift3
should be faster. Will get data on that soon.
facets
Enabling facets
is useful to query based on types of models. Example:
{
"1": {
"text": "Hank Green",
"facets": ["user"]
},
"2": {
"text": "John Green",
"facets": ["user"]
},
"3": {
"text": "Johnnie Walker",
"facets": ["user"]
},
"b": {
"text": "Johnnie Walker",
"facets": ["brand"]
}
}
You can then query "Johnnie" with facets
["brand"]
and only get:
["b"]
Notice how the result don't include the user 3
because it doesn't have the brand facet
.
You can also combine facets
with id
's to provide property based queries:
{
"3": {
"text": "Johnnie Walker johnnie@walker.com",
"facets": ["user"]
},
"3-name": {
"text": "Johnnie Walker",
"facets": ["user-name"]
},
"3-email": {
"text": "johnnie@walker.com",
"facets": ["user-email"]
}
}
And then query the facets
["user-name"]
with the text "johnnie" and get:
["3-name"]
And with that you can just split the results to get the id
's.
For ranking results, we need to store the original text. When indexing large amounts of data this can have an impact on disk usage. To prevent that, a function can be passed that receives id
, options
, and callback
as the arguments to fetch the original indexed text for that id
.
index.index('john green', 1, ['user'], function(err){
assert(!err)
})
index.put('Fishing is a way of catching cats, he argued in his arguments', 'b', function(err){
assert(!err)
})
index.link('Julie loves me more than Linda loves me', '1436ebc684b-c1039c76bdb2b054670f3a1256c98650', ['message'], function(err){
assert(!err)
})
index.remove(1, function(err){
assert(!err)
})
index.del('b', function(err){
assert(!err)
})
index.unlink('1436ebc684b-c1039c76bdb2b054670f3a1256c98650', function(err){
assert(!err)
})
index.search('Fishing', function(err, result){
assert(!err)
assert(result.last)
assert(result.results)
})
index.query('Green', ['user'], function(err, result){
assert(!err)
assert(result.last)
assert(result.results)
})
index.search('Green', 'user', function(err, result){
assert(!err)
assert(result.last)
assert(result.results)
})
index.search('Green', {
limit: 100,
ttl: 1000 * 60 * 60
}, function(err, result){
assert(!err)
assert(result.last)
assert(result.results)
})
index.search({
last: '1436ec2e069-bf55e1ed64540b925e13d6bfd21a543c'
}, function(err, result){
assert(!err)
assert(result.last)
assert(result.results)
})
Every query returns a last parameter. That can be passed to the query
/search
function to get the next results. When you pass last
, you don't need to pass the search query again, because it is saved in the db.
Note that pagination expires in 1h
, so if you do a query now, and 2 hours later you want to retrieve the next page, you'll get an error.
The ttl
can, however, be tuned in the query
options.
Author: Sergioramos
Source Code: https://github.com/sergioramos/inverted
License: MIT license