-
Notifications
You must be signed in to change notification settings - Fork 199
/
Hash.js
94 lines (81 loc) · 2.65 KB
/
Hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
define( [
'dojo/_base/declare',
'dojo/_base/array',
'dojo/_base/Deferred',
'dojo/store/JsonRest',
'dojo/store/util/QueryResults',
'JBrowse/Digest/Crc32'
],
function(
declare,
array,
Deferred,
dojoJSONRest,
QueryResults,
digest
) {
return declare( null, {
constructor: function( args ) {
// make sure url has a trailing slash
var url = /\/$/.test( args.url ) ? args.url : args.url + '/';
this.bucketStore = new dojoJSONRest({
target: url
});
this.meta = {};
this.browser = args.browser;
// this.ready is a Deferred that will be resolved when we have
// read the meta.json file with the params of this hashstore
this.ready = this._readMeta();
},
_readMeta: function() {
var thisB = this;
return this.bucketStore.get( 'meta.json' )
.then( function( meta ) {
dojo.mixin( thisB.meta, meta || {} );
thisB.meta.hash_hex_characters = Math.ceil( thisB.meta.hash_bits / 4 );
});
},
query: function( query, options ) {
return this.get( (query.name || '').toString() )
.then( function( value ) {
return QueryResults( (value||{}).exact || [] );
});
},
get: function( key ) {
return this._getBucket(key)
.then( function( bucket ) {
return bucket[key];
});
},
_getBucket: function( key ) {
var thisB = this;
var bucketP = new Deferred();
this.ready.then( function() {
var bucketIdent = thisB._hash( key );
thisB.bucketStore.get( thisB._hexToDirPath( bucketIdent ) )
.then(function(d) { bucketP.resolve(d); } );
});
return bucketP;
},
_hexToDirPath: function( hex ) {
// zero-pad the hex string to be 8 chars if necessary
while( hex.length < 8 )
hex = '0'+hex;
hex = hex.substr( 8-this.meta.hash_hex_characters );
var dirpath = [];
for( var i = 0; i < hex.length; i += 3 ) {
dirpath.push( hex.substring( i, i+3 ) );
}
return dirpath.join('/') + '.json';
},
_hash: function( data ) {
return digest.objectFingerprint( data )
.toString(16)
.toLowerCase()
.replace('-','n');
},
getIdentity: function( object ) {
return object.id;
}
});
});