Skip to content

Commit

Permalink
Merge pull request runbox#3 from petersalomonsen/npm-lib-setup
Browse files Browse the repository at this point in the history
get document id from unique id-term and npmjs library setup
  • Loading branch information
petersalomonsen authored Jun 25, 2019
2 parents 804da1a + 2c89a6d commit f9f5a1d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
node_modules
xapianasm.*
.vscode
.vscode
dist
7 changes: 6 additions & 1 deletion compilermmxapianapi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const execSync = require('child_process').execSync;
const { mkdirSync, existsSync } = require('fs');

const xapianDirArgName = '--xapiandir=';
const xapianDirArg = process.argv.find(arg => arg.indexOf(xapianDirArgName)===0);

if (xapianDirArg) {

process.env.XAPIAN = xapianDirArg.substr(xapianDirArgName.length);
Expand All @@ -13,11 +15,14 @@ if(!process.env.XAPIAN) {
} else {
try {
console.log('Building Runbox Xapian webassembly library');
if(!existsSync('dist')) {
mkdirSync('dist');
}
execSync(`em++ -Oz -s DISABLE_EXCEPTION_CATCHING=0 -s USE_ZLIB=1 ` +
`-s "EXTRA_EXPORTED_RUNTIME_METHODS=['FS','cwrap','stringToUTF8','UTF8ToString','getValue']" ` +
`-std=c++11 -s DEMANGLE_SUPPORT=1 -s ALLOW_MEMORY_GROWTH=1 ` +
`-I$XAPIAN/include -I$XAPIAN -I$XAPIAN/common rmmxapianapi.cc $XAPIAN/.libs/libxapian-1.5.a ` +
`-o xapianasm.js`, { stdio: 'inherit' });
`-o dist/xapianasm.js`, { stdio: 'inherit' });
console.log('Successful build of xapianasm.wasm and xapianasm.js');
} catch(e) {
console.error('Compile failed');
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"scripts": {
"build": "node compilermmxapianapi.js",
"test": "mocha-typescript-watch -p tsconfig.json build/test/test.js",
"test-no-watch": "tsc -p tsconfig.json && mocha build/test/test.js"
},
"dependencies": {
"runbox7lib": "^0.2.0",
"rxjs": "^6.3.3"
"test-no-watch": "tsc -p tsconfig.json && mocha build/test/test.js",
"preparelib": "node preparelibrary.js"
},
"devDependencies": {
"runbox7lib": "^0.2.0",
"rxjs": "^6.3.3",
"@types/node": "^10.12.18",
"mocha": "^5.2.0",
"mocha-typescript": "^1.1.17",
Expand Down
8 changes: 8 additions & 0 deletions preparelibrary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { readFileSync, writeFileSync } = require('fs');

const packageJSON = JSON.parse(readFileSync('package.json').toString());

delete packageJSON['scripts'];
delete packageJSON['devDependencies'];

writeFileSync('dist/package.json', JSON.stringify(packageJSON, null, 1));
10 changes: 10 additions & 0 deletions rmmxapianapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ extern "C" {
dbc->clearValueRange();
}

int EMSCRIPTEN_KEEPALIVE getDocIdFromUniqueIdTerm(char * unique_id_term) {
Xapian::PostingIterator p = dbc->db.postlist_begin(unique_id_term);

if (p != dbc->db.postlist_end(unique_id_term)) {
return *p;
} else {
return 0;
}
}

int EMSCRIPTEN_KEEPALIVE documentTermList(int docid) {
Xapian::Document doc = dbc->db.get_document(docid);
int numterms = 0;
Expand Down
23 changes: 23 additions & 0 deletions ts/xapian/modifydocterms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const contents = [
let addTermToDocument: (idterm: string, termname: string) => void;
let removeTermFromDocument: (idterm: string, termname: string) => void;
let addTextToDocument: (idterm: string, withoutpositions: boolean, text: string) => void;
let getDocIdFromUniqueIdTerm: (idterm: string) => number;

/**
* Change folder in xapian test
Expand All @@ -49,6 +50,7 @@ let addTextToDocument: (idterm: string, withoutpositions: boolean, text: string)
addTermToDocument = global['Module'].cwrap('addTermToDocument', null, ['string', 'string']);
removeTermFromDocument = global['Module'].cwrap('removeTermFromDocument', null, ['string', 'string']);
addTextToDocument = global['Module'].cwrap('addTextToDocument', null, ['string', 'boolean', 'string']);
getDocIdFromUniqueIdTerm = global['Module'].cwrap('getDocIdFromUniqueIdTerm', 'number', ['string']);
done();
});
}
Expand Down Expand Up @@ -246,4 +248,25 @@ let addTextToDocument: (idterm: string, withoutpositions: boolean, text: string)
results = xapian.sortedXapianQuery(`folder:"Otherpartition" AND "brequrianis xulaxis"`, 0, 0, 0, 100000, -1);
equal(0, results.length);
}

@test() getDocumentByIdTermAndModify() {
const xapian = new XapianAPI();
let results = xapian.sortedXapianQuery(`folder:"Otherpartition"`, 0, 0, 0, 1, -1);

const unique_id_term = xapian.getDocumentData(results[0][0]).split('\t')[0];
const docid = getDocIdFromUniqueIdTerm(unique_id_term);
equal(results[0][0], docid);

xapian.documentXTermList(docid);
let termlistresult: string[] = global['Module']['documenttermlistresult'];
equal(1, termlistresult.filter(term => term === 'XFOLDER:Otherpartition').length);
xapian.changeDocumentsFolder(unique_id_term, 'TestFolder1523');

xapian.documentXTermList(docid);
termlistresult = global['Module']['documenttermlistresult'];

equal(1, xapian.sortedXapianQuery(`folder:"TestFolder1523"`, 0, 0, 0, 1, -1).length);
equal(1, termlistresult.filter(term => term === 'XFOLDER:TestFolder1523').length);
equal(0, termlistresult.filter(term => term === 'XFOLDER:Otherpartition').length);
}
}
2 changes: 1 addition & 1 deletion ts/xapian/xapian.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function loadXapian(): AsyncSubject<boolean> {
if(!xapianLoadedSubject) {
xapianLoadedSubject = new AsyncSubject();

const xapian = require(`${process.cwd()}/xapianasm.js`);
const xapian = require(`${process.cwd()}/dist/xapianasm.js`);

global.termlistresult = [];
global.Module = xapian;
Expand Down

0 comments on commit f9f5a1d

Please sign in to comment.