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

The operation delete not working ($inp['__dbid'] order mismatch) #4

Closed
CharlesKumar opened this issue Jun 21, 2018 · 3 comments
Closed

Comments

@CharlesKumar
Copy link

I experienced an issue after deleting a few records, inserting a few and again tried to delete

nothing happened...

inspected through the code

 private function tweak($input){
      $counter = 0;
      foreach($input as $inp){
        array_push($this->uids, $counter);
        $inp['__dbid'] = $counter;
        $this->data[] = $inp;
        $counter++;
      }
      $this->rawd = $this->data;
    }

The delete operation is based on ['__dbid'] which is based on $counter created in tweak function

So I temporarily reordered using array_values in delete function (like below)

 public function delete(){
      $this->base->tbdata['data'] = array_values($this->base->tbdata['data']);
      foreach($this->data as $d) {
        unset($this->base->tbdata['data'][$d['__dbid']]);
      }
      $this->base->tbdata['data'] = array_values($this->base->tbdata['data']);
      $this->base->_saveData();
      return true;
    }

kindly look through this when you go through crunchdb

@cybrox
Copy link
Owner

cybrox commented Jun 22, 2018

Thanks a lot for reporting this issue and providing such detailed information!

I'll see if I can reproduce this, as soon as possible. - I think I'll be able to look into it by mid next week.

@cybrox
Copy link
Owner

cybrox commented Jul 1, 2018

Hi, sorry for the late reply.

I ran a little test script and was able to reproduce your issue:

$cdb->table('names')->create());
$cdb->table('names')->count());

$cdb->table('names')->insert(array("name" => "Name A"));
$cdb->table('names')->insert(array("name" => "Name B"));
$cdb->table('names')->insert(array("name" => "Name C"));
$cdb->table('names')->select(["name", "==", "Name A"])->delete();
$cdb->table('names')->select(["name", "==", "Name B"])->delete();
$cdb->table('names')->select(["name", "==", "Name C"])->delete();

// Database should be empty but contains "C"
$cdb->table('names')->select('*')->fetch();
$cdb->table('names')->count());

$cdb->table('names')->drop();

Thank you for providing a fitting solution. Although I don't think re-aligning the table twice is necessary. The bug is only caused by shifts from previous deletes, so using array_values() once after the delete (unset) operation should suffice.

      // working delete();
      foreach($this->data as $d) {
        unset($this->base->tbdata['data'][$d['__dbid']]);
      }
      $this->base->tbdata['data'] = array_values($this->base->tbdata['data']);
      $this->base->_saveData();
      return true;

In Conclusion, if you want, you can file a PR to have these changes merged into the library under your name. Otherwise, I'll just commit them manually and reference this issue :)


For reference:
Another method of solving this, would be modifying _saveData instead, so the table is aligned after every mutation. I don't think there's any other use case than the delete operation that requires this fix, though, so your solution should work fine

$aligned_table = array("data" => array_values($this->tbdata["data"]));
file_put_contents($this->tbpath, json_encode($aligned_table));

@CharlesKumar
Copy link
Author

Hi Sven,

thanks for getting into this and also for keeping this nice project (still a unique project in this category json database system)

Although I've been using github for years yet to do a PR

next time i'll do

@cybrox cybrox closed this as completed in 7262305 Jul 15, 2018
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

2 participants