Skip to content

Commit

Permalink
support persistent_load
Browse files Browse the repository at this point in the history
  • Loading branch information
ewfian committed Apr 15, 2023
1 parent 02fc330 commit 7b3ea12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
28 changes: 28 additions & 0 deletions examples/persistent_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import pickle
from collections import namedtuple

MemoRecord = namedtuple("MemoRecord", "key, task")

class MyPickler(pickle.Pickler):

def persistent_id(self, obj):
# Instead of pickling MemoRecord as a regular class instance, we emit a
# persistent ID.
if isinstance(obj, MemoRecord):
# Here, our persistent ID is simply a tuple, containing a tag and a
# key, which refers to a specific record in the database.
print('persistent_id set')
return ("MemoRecord", obj.key)
else:
# If obj does not have a persistent ID, return None. This means obj
# needs to be pickled as usual.
return None

def main():
memo = MemoRecord("aa", "bb")
filehandler = open(b"persistent_id.pkl", "wb")
MyPickler(filehandler).dump(memo)

if __name__ == '__main__':
main()
18 changes: 15 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { Registry } from './registry';

export class Parser {
private _reader: Reader;
public persistent_load: (pid: string) => any;

registry: Registry = new Registry();

constructor(buffer: Uint8Array | Int8Array | Uint8ClampedArray) {
constructor(buffer: Uint8Array | Int8Array | Uint8ClampedArray, persistent_load?: (pid: string) => any) {
this._reader = new Reader(buffer);
this.persistent_load =
persistent_load ||
((pid: string) => {
throw new Error(`Unsupported persistent id: \`${pid}\`.`);
});
}

load() {
Expand All @@ -20,6 +26,8 @@ export class Parser {
while (reader.hasNext()) {
const opcode = reader.byte();
// console.log(`${(reader.position - 1).toString()} ${opcode}`);
// console.log('metastack:', metastack, '\nstack:', stack);
// console.log('\nmemo:', Array.from(memo.entries()));
switch (opcode) {
// Structural
case OP.PROTO: {
Expand Down Expand Up @@ -296,6 +304,12 @@ export class Parser {
stack.push(obj);
break;
}
case OP.PERSID:
stack.push(this.persistent_load(reader.line()));
break;
case OP.BINPERSID:
stack.push(this.persistent_load(stack.pop()));
break;
case OP.BUILD: {
const state = stack.pop();
const obj = stack[stack.length - 1];
Expand All @@ -318,8 +332,6 @@ export class Parser {
}

default:
// console.log('metastack:', metastack, '\nstack:', stack);
// console.log('\nmemo:', Array.from(memo.entries()));
throw new Error(`Unsupported opcode '${opcode}'.`);
}
}
Expand Down

0 comments on commit 7b3ea12

Please sign in to comment.