Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index does not be built correctly. #205

Open
marudedameo2019 opened this issue Aug 1, 2020 · 0 comments
Open

Index does not be built correctly. #205

marudedameo2019 opened this issue Aug 1, 2020 · 0 comments

Comments

@marudedameo2019
Copy link

Which version are you using?
2.3.7

Describe the bug
If registered in parallel, the index will not be built correctly and the select results that use it will be less.

Expected behavior
Even if they are registered in parallel, search results equal to the number of registrations can be obtained.

Example

<!-- save it as index.html and use npx http-server -->
<script src="https://cdn.jsdelivr.net/npm/@nano-sql/core@2.3.7/dist/nano-sql.min.js"
  integrity="sha256-W1pVgKda7GC4fwXqq9jfOrssBDJJXZqck+ultRPVzmc=" crossorigin="anonymous"></script>
<script>
  // table schema with primary key id column and data value column
  const model = {
    "id:string": { pk: true },
    "value:string": {},
  };
  // data value column index definition
  const indexes = {
    "value:string": {},
  };
  // number of rows in table
  const NUM = 100;
  // original data of the table (number from 0 to NUM-1, used for primary key)
  const keys = [...Array(NUM).keys()];
  // query definition for data registration (write data of value '0' with table name and primary key value as arguments)
  const upsert = (table, key) =>
    nSQL(table).query(
      'upsert'
      , {
        "id": String(key),
        "value": '0'
      }
    ).exec();
  // query definition for data search (counts the number of rows of data with value '0' using the table name as an argument)
  const select = (table) =>
    nSQL(table)
      .query('select')
      .where(['value', '=', '0'])
      .exec()
      .then((rows) => rows.length);

  // database generation (create two tables of the same structure in IndexedDB with names sync and async)
  nSQL().createDatabase({
    id: "sample_db",
    mode: "PERM",
    tables: [
      {
        name: "sync",
        model: model,
        indexes: indexes,
      },
      {
        name: "async",
        model: model,
        indexes: indexes,
      },
    ],
    version: 1,
  }).then(() => {
    // register data (keys) in parallel in the async table asynchronously
    return Promise.all(
      keys.map((key) =>
        upsert("async", key)
      ));
  }).then(() => {
    // register data (keys) in serial in the sync table asynchronously
    return keys.reduce(
      (promise, key) => promise.then(() => upsert("sync", key))
      , Promise.resolve());
  }).then(() => {
    console.log("completed!");
    // count number of rows with a value of 0 in the sync table
    return select('sync');
  }).then((count) => {
    console.log('sync=' + count);
    // count number of rows with value 0 in the async table
    return select('async');
  }).then((count) => {
    console.log('async=' + count);
  }).catch((err) => {
    console.log(err);
  });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant