Skip to content

Implement deletion #5

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

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const st = createFile('benchmark.txt')
st.open(tinyWrites)

function tinyWrites () {
var offset = 0
let offset = 0
const buf = Buffer.alloc(1)
console.time('10000 tiny writes')
st.write(0, buf, function onwrite (err) {
Expand All @@ -19,7 +19,7 @@ function tinyWrites () {
}

function tinyReads () {
var offset = 0
let offset = 0
console.time('10000 tiny reads')
st.read(0, 1, function onread (err) {
if (err) throw err
Expand All @@ -33,7 +33,7 @@ function tinyReads () {
}

function benchRead () {
var offset = 0
let offset = 0
console.time('512mb read')
st.read(0, 65536, function onread (err, buf) {
if (err) throw err
Expand All @@ -43,7 +43,7 @@ function benchRead () {
}

function benchWrite () {
var offset = 0
let offset = 0
const buf = Buffer.alloc(65536).fill('hi')
console.time('512mb write')
st.write(offset, buf, function onwrite (err) {
Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const createFile = require('./')

const st = createFile('/some/folder/hello-world.txt')
var missing = 2
let missing = 2

st.write(0, Buffer.from('hello '), done)
st.write(6, Buffer.from('world'), done)
Expand Down
92 changes: 78 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ras = require('random-access-storage')

const TYPE = {type: 'octet/stream'}
const TYPE = { type: 'octet/stream' }
const requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem
const persistentStorage = navigator.persistentStorage || navigator.webkitPersistentStorage
const FileReader = window.FileReader
Expand All @@ -27,13 +27,14 @@ function createFile (name, opts) {
const maxSize = opts.maxSize || createFile.DEFAULT_MAX_SIZE
const mutex = new Mutex()

var fs = null
var entry = null
var toDestroy = null
var readers = []
var writers = []
let fs = null
let entry = null
let toDestroy = null
let readers = []
let writers = []
let deleters = []

return ras({read, write, open, stat, close, destroy})
return ras({ read, write, del, open, stat, close, destroy })

function read (req) {
const r = readers.pop() || new ReadRequest(readers, entry, mutex)
Expand All @@ -45,8 +46,13 @@ function createFile (name, opts) {
w.run(req)
}

function del (req) {
const d = deleters.pop() || new DeleteRequest(deleters, entry, mutex)
d.run(req)
}

function close (req) {
readers = writers = entry = fs = null
readers = writers = deleters = entry = fs = null
req.callback(null)
}

Expand Down Expand Up @@ -76,7 +82,7 @@ function createFile (name, opts) {
requestFileSystem(window.PERSISTENT, granted, function (res) {
fs = res
mkdirp(parentFolder(name), function () {
fs.root.getFile(name, {create: true}, function (e) {
fs.root.getFile(name, { create: true }, function (e) {
entry = toDestroy = e
req.callback(null)
}, onerror)
Expand All @@ -86,9 +92,9 @@ function createFile (name, opts) {

function mkdirp (name, ondone) {
if (!name) return ondone()
fs.root.getDirectory(name, {create: true}, ondone, function () {
fs.root.getDirectory(name, { create: true }, ondone, function () {
mkdirp(parentFolder(name), function () {
fs.root.getDirectory(name, {create: true}, ondone, ondone)
fs.root.getDirectory(name, { create: true }, ondone, ondone)
})
})
}
Expand Down Expand Up @@ -166,10 +172,10 @@ WriteRequest.prototype.lock = function () {
WriteRequest.prototype.run = function (req) {
this.entry.file(file => {
this.req = req

if (!this.writer || this.writer.length !== file.size) return this.makeWriter()

const end = req.offset + req.size
if (end > file.size && !this.lock()) return
if (req.offset + req.size > file.size && !this.lock()) return

if (req.offset > this.writer.length) {
if (req.offset > file.size) return this.truncate()
Expand All @@ -188,7 +194,7 @@ function Mutex () {
Mutex.prototype.release = function () {
const queued = this.queued
this.queued = null
for (var i = 0; i < queued.length; i++) {
for (let i = 0; i < queued.length; i++) {
queued[i].run(queued[i].req)
}
}
Expand Down Expand Up @@ -258,3 +264,61 @@ ReadRequest.prototype.run = function (req) {
this.reader.readAsArrayBuffer(file.slice(req.offset, end))
}, err => req.callback(err))
}

function DeleteRequest (pool, entry, mutex) {
this.pool = pool
this.entry = entry
this.mutex = mutex
this.writer = null
this.req = null
this.locked = false
}

DeleteRequest.prototype.makeWriter = function () {
const self = this
this.entry.createWriter(function (writer) {
self.writer = writer

writer.onwriteend = function () {
self.onwrite(null)
}

writer.onerror = function (err) {
self.onwrite(err)
}

self.run(self.req)
})
}

DeleteRequest.prototype.onwrite = function (err) {
const req = this.req
this.req = null

if (this.locked) {
this.locked = false
this.mutex.release()
}

this.pool.push(this)
req.callback(err, null)
}

DeleteRequest.prototype.lock = function () {
if (this.locked) return true
this.locked = this.mutex.lock(this)
return this.locked
}

DeleteRequest.prototype.run = function (req) {
this.entry.file(file => {
this.req = req

if (req.offset + req.size < file.size) return req.callback(null)

if (!this.writer) return this.makeWriter()
if (!this.lock()) return

this.writer.truncate(req.offset)
}, err => req.callback(err))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"random-access-storage": "^1.3.0"
},
"devDependencies": {
"standard": "^11.0.1"
"standard": "^16.0.4"
},
"scripts": {
"test": "standard"
Expand Down