{"size": 1486, "ext": "js", "max_stars_repo_path": "src/dht/components/kv-store/KVStore.js", "max_stars_repo_name": "shaunpersad/learn2p2p", "max_stars_repo_stars_event_min_datetime": "2020-10-04T05:36:10.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-02T04:43:24.000Z", "max_issues_repo_path": "src/dht/components/kv-store/KVStore.js", "max_issues_repo_name": "shaunpersad/learn2p2p", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/dht/components/kv-store/KVStore.js", "max_forks_repo_name": "shaunpersad/learn2p2p", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.186440678, "max_line_length": 80, "alphanum_fraction": 0.5874831763}
const { Readable, Writable } = require('stream');
const StringStream = require('../../../utils/StringStream');
const Value = require('./components/Value');
const PartialValue = require('./components/PartialValue');
const PartialValueWriteStream = require('./components/PartialValueWriteStream');

class KVStore {

    createDataReadStream(key, streamOptions = null) {

        return new Readable(streamOptions);
    }

    createDataWriteStream(key) {

        return new Writable();
    }

    createPartialValue(key, length) {

        return Promise.resolve(new PartialValue(key, length));
    }

    /**
     *
     * @param {string} key
     * @returns {Promise<Value>}
     */
    getValue(key) {

        return Value.createFromReadStream(this.createDataReadStream(key));
    }

    saveRawValueData(key, data) {

        return new Promise((resolve, reject) => {

            (new StringStream(data))
                .on('error', reject)
                .pipe(this.createDataWriteStream(key))
                .on('error', reject)
                .on('finish', resolve);
        });
    }

    forEachPartialValueChunk(key, only, forEachCallback) {

        return new Promise((resolve, reject) => {

            this.createDataReadStream(key)
                .on('error', reject)
                .pipe(new PartialValueWriteStream(only, forEachCallback))
                .on('error', reject)
                .on('finish', resolve);
        });
    }
}

module.exports = KVStore;