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

Queries with inline data (p2p only) #88

Merged
merged 15 commits into from
Nov 21, 2016
Merged

Queries with inline data (p2p only) #88

merged 15 commits into from
Nov 21, 2016

Conversation

yusefnapora
Copy link
Contributor

This adds a remoteQueryWithData method to the MediachainNode class, which will fetch multihash object references in query results from the remote peer and replace the multihash with {key: multihashValue, data: actualObject}

An example at the repl
א > remote.queryWithData('SELECT * FROM scratch.* LIMIT 1')
[ { simple: { stmt: [Object] } } ]
א > var res = _[0].simple.stmt
א > console.dir(res, {depth: 100})
{ id: '4XTTM6XcpMB8N8Tnkhc66DqvAotgKzRJgrZbF7SBByihtPnV8:1478207997:34006',
  publisher: '4XTTM6XcpMB8N8Tnkhc66DqvAotgKzRJgrZbF7SBByihtPnV8',
  namespace: 'scratch.moma',
  body: 
   { simple: 
      { object: 
         { key: 'QmPEeMwnRhJB1YV4sZsJV3wNtHz8jsEw1WgzjC8quqxfqU',
           data: 
            { schema: { '/': 'QmXciNhn4P6veuojsR6uRGHBu27pAztp5o1rtMkkmsTtUf' },
              data: 
               { ArtistBio: 'American, 1930–1992',
                 BeginDate: 1930,
                 ConstituentID: 1,
                 DisplayName: 'Robert Arneson',
                 EndDate: 1992,
                 Gender: 'Male',
                 Nationality: 'American',
                 ULAN: null,
                 'Wiki QID': null } } },
        refs: [ 'moma:artist:1' ],
        tags: [],
        deps: [ 'QmXciNhn4P6veuojsR6uRGHBu27pAztp5o1rtMkkmsTtUf' ] } },
  timestamp: 1478207997,
  signature: 
   Buffer [ ... ] }

TODO:

I'd like to add a new subcommand to the aleph cli that executes a query string against a remote peer and pretty prints the results, so we can get a nice one-liner to use in the Attribution Engine.

@parkan
Copy link
Contributor

parkan commented Nov 17, 2016

could add an invocation like aleph exec "remote.queryWithData('SELECT * FROM scratch.* LIMIT 1')"?

@yusefnapora
Copy link
Contributor Author

yusefnapora commented Nov 17, 2016

That could work, although the repl's default printing of the query results isn't too great. For the example above you'd just get [ { simple: { stmt: [Object] } } ]

That's something we should probably tweak regardless though, to make working at the repl more useful.

@@ -65,12 +65,20 @@ function loadOrGenerateIdentity (filePath: string): Promise<PeerId> {
}

function inflateMultiaddr (multiaddrString: string): PeerInfo {
// total hack to patch in support for /p2p/ multiaddrs, which should
// be supported upstream soon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's actively reference this multiformats/multiaddr#27

@@ -15,16 +28,18 @@ require('yargs')
// this is the only way to get useful config in subcommands
'config': true,
'global': true,
'default': home + 'config.json'
'default': path.join(home, 'config.json')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call

},
'remotePeer': {
'global': true,
'type': 'string',
'describe': 'The remote peer to pair with (multiaddress)'
}
})
.coerce('config', filePath => ensureDirExists(filePath, 'config file'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't want to just put this into the config key above as coerce : { filepath: ... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sure, that does sound better.

}
}

return _.cloneDeepWith(result, replacer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the lodash pieces are broken out so you can just import lodash.clonedeepwith but it's not critical https://www.npmjs.com/package/lodash.clonedeepwith

@@ -177,6 +179,66 @@ function promiseTimeout<T> (timeout: number, promise: Promise<T>): Promise<T> {
})])
}

function flatMap<T, U> (array: Array<T>, f: (x: T) => Array<U>): Array<U> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if only Thenables were monadic 😞

try {
val = decode(obj.data)
} catch (err) {
val = obj.data.toString('base64')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to add a comment here explaining the logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, actually I might remove the base64 conversion here. After I wrote this, I added a pass to convert all Buffers to base64 just before printing the results. That's a better place for it than here, I think

const key = _.get(value, 'object')
const data = objectMap[key]
if (data != null) {
return _.set(value, 'object', {key, data})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I guess we have all these other lodash calls too, let's take the whole package then

@yusefnapora yusefnapora merged commit 12bcab6 into master Nov 21, 2016
@yusefnapora yusefnapora deleted the yn-query-with-data branch November 21, 2016 19:57
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

Successfully merging this pull request may close these issues.

2 participants