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

Clear is not clearing #365

Open
benzmuircroft opened this issue Jan 26, 2024 · 2 comments
Open

Clear is not clearing #365

benzmuircroft opened this issue Jan 26, 2024 · 2 comments

Comments

@benzmuircroft
Copy link

benzmuircroft commented Jan 26, 2024

;(async function() {
  const Corestore = require('corestore');
  const Hyperdrive = require('hyperdrive');
  const folder = 'animals';
  const store = new Corestore(`./db/${folder}`);
  await store.ready();
  const drive = new Hyperdrive(store);
  await drive.ready();
  await drive.put(`/dogs/husky`, Buffer.from(JSON.stringify({ _id: 'husky', job: 'sled dog' })));
  await drive.put(`/dogs/husky`, Buffer.from(JSON.stringify({ _id: 'husky', job: 'sled dog', origin: 'siberia' })));
  await drive.put(`/dogs/husky`, Buffer.from(JSON.stringify({ _id: 'husky', job: 'sled dog', origin: 'siberia', size: 'medium' })));
  console.log(JSON.parse((await drive.get(`/dogs/husky`)).toString()));
  const trim = drive.db.core.length; // keep tiny on startup
  console.log(1, trim);
  const view = drive.db.createReadStream();
  const blobs = await drive.getBlobs();
      
  for await (const entry of view) {
    console.log(entry);
    const key = entry.key.replace('files\x00', '');
    const value = await blobs.get(entry.value.blob);
    console.log(key, value.toString());
    await drive.put(key, value);
  }
  await drive.db.core.clear(1, trim);
  console.log(JSON.parse((await drive.get(`/dogs/husky`)).toString()));
  console.log((await drive.db.core.get(0, {wait: false})).toString()); // hyperbee
  console.log((await drive.db.core.get(1, {wait: false})).toString()); // files/dogs/husky|{"exec... // I expected null
  console.log((await drive.db.core.get(2, {wait: false})).toString()); // files/dogs/husky}{"exec... // I expected null
  console.log((await drive.db.core.get(3, {wait: false})).toString()); // files/dogs/husky}{"exec... // I expected null
  console.log((await drive.db.core.get(4, {wait: false})).toString()); // files/dogs/husky~{"exec...
})();
@benzmuircroft
Copy link
Author

For example this works well on a hyperbee

;(async function() {
  const Hyperbee = require('hyperbee');
  const Hypercore = require('hypercore');
  const fs = require('fs').promises;
  await fs.rm('./db', { recursive: true });
  let Toyota = {
    make: "Toyota",
    model: "Camry",
    owner: "John Doe"
  };
  let Honda = {
    make: "Honda",
    model: "Civic",
    owner: "Mark smith"
  };
  const core = new Hypercore('./db');
  const car = new Hyperbee(core);
  await car.ready();
  await car.put('Toyota', JSON.stringify(Toyota));
  console.log((await car.core.get(0)).toString()); // dont remove this
  console.log(JSON.parse((await car.get('Toyota')).value.toString()));
  await car.put('Honda', JSON.stringify(Honda));
  Honda.owner = 'benz';
  await car.put('Honda', JSON.stringify(Honda));
  const trim = car.core.length;
  console.log(trim); // 4
  const view = car.createReadStream();
  for await (const entry of view) {
    await car.put(entry.key.toString(), entry.value.toString());
  }
  await car.core.clear(1, trim);
  console.log(JSON.parse((await car.get('Toyota')).value.toString()));
  console.log(JSON.parse((await car.get('Honda')).value.toString()));
  console.log(car.core.length); // 6
  console.log('#0', await car.core.get(0, { wait: false })); // hyperbee
  console.log('#1', await car.core.get(1, { wait: false })); // null
  console.log('#2', await car.core.get(2, { wait: false })); // null
  console.log('#3', await car.core.get(3, { wait: false })); // null
  console.log('#4', await car.core.get(4, { wait: false })); // Toyota buffer
  console.log('#5', await car.core.get(5, { wait: false })); // Honda buffer
  await car.close();
})();

@mafintosh
Copy link
Contributor

Use drive.clear(filename) to clear content on the drive

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