<commit_before>// redisjsonstore.js
//
// implementation of JSONStore interface using redis
//
// Copyright 2011, StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var store = require('./jsonstore');

var JSONStore = store.JSONStore;
var JSONStoreError = store.JSONStoreError;
var AlreadyExistsError = store.AlreadyExistsError;
var NoSuchThingError = store.NoSuchThingError;

var redis = require('redis');

function RedisJSONStore() {
    this.client = null;
}

RedisJSONStore.prototype = new JSONStore();

RedisJSONStore.prototype.toKey = function(type, id) {
    return type + ':' + id;
}

RedisJSONStore.prototype.connect = function(params, onCompletion) {
    this.client = redis.createClient();
    this.client.on('error', function(err) {
	if (onCompletion) {
	    onCompletion(new JSONStoreError(err));
	}
    })
    this.client.on('connect', function() {
	if (onCompletion) {
	    onCompletion(null);
	}
    })
};

RedisJSONStore.prototype.disconnect = function(onCompletion) {
    this.client.quit(function(err) {
	if (err) {
	    onCompletion(new JSONStoreError());
	} else {
	    onCompletion(null);
	}
    });
};

RedisJSONStore.prototype.create = function(type, id, value, onCompletion) {
    this.client.setnx(this.toKey(type, id), value, function(err, result) {
	if (err) {
	    onCompletion(new JSONStoreError(err));
	} else if (result == 0) {
	    onCompletion(new AlreadyExistsError(type, id));
	} else {
	    onCompletion(null, value);
	}
    });
};

RedisJSONStore.prototype.read = function(type, id, onCompletion) {
    this.client.get(this.toKey(type, id), function(err, value) {
	if (err) {
	    onCompletion(new JSONStoreError(err), null);
	} else if (value == null) {
	    onCompletion(new NoSuchThingError(type, id), null);
	} else {
	    onCompletion(null, value);
	}
    });
};

RedisJSONStore.prototype.update = function(type, id, value, onCompletion) {
    this.client.set(this.toKey(type, id), value, function(err) {
	if (err) {
	    onCompletion(new JSONStoreError(err), null);
	} else {
	    onCompletion(null, value);
	}
    });
};

RedisJSONStore.prototype.del = function(type, id, onCompletion) {
    this.client.del(this.toKey(type, id), function(err, count) {
	if (err) {
	    onCompletion(new JSONStoreError(err));
	} else {<commit_msg>JSON-encode stuff before sticking it in redis<commit_after>// redisjsonstore.js
//
// implementation of JSONStore interface using redis
//
// Copyright 2011, StatusNet Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var store = require('./jsonstore');

var JSONStore = store.JSONStore;
var JSONStoreError = store.JSONStoreError;
var AlreadyExistsError = store.AlreadyExistsError;
var NoSuchThingError = store.NoSuchThingError;

var redis = require('redis');

function RedisJSONStore() {
    this.client = null;
}

RedisJSONStore.prototype = new JSONStore();

RedisJSONStore.prototype.toKey = function(type, id) {
    return type + ':' + id;
}

RedisJSONStore.prototype.connect = function(params, onCompletion) {
    this.client = redis.createClient();
    this.client.on('error', function(err) {
	if (onCompletion) {
	    onCompletion(new JSONStoreError(err));
	}
    })
    this.client.on('connect', function() {
	if (onCompletion) {
	    onCompletion(null);
	}
    })
};

RedisJSONStore.prototype.disconnect = function(onCompletion) {
    this.client.quit(function(err) {
	if (err) {
	    onCompletion(new JSONStoreError());
	} else {
	    onCompletion(null);
	}
    });
};

RedisJSONStore.prototype.create = function(type, id, value, onCompletion) {
    this.client.setnx(this.toKey(type, id), JSON.stringify(value), function(err, result) {
	if (err) {
	    onCompletion(new JSONStoreError(err));
	} else if (result == 0) {
	    onCompletion(new AlreadyExistsError(type, id));
	} else {
	    onCompletion(null, value);
	}
    });
};

RedisJSONStore.prototype.read = function(type, id, onCompletion) {
    this.client.get(this.toKey(type, id), function(err, value) {
	if (err) {
	    onCompletion(new JSONStoreError(err), null);
	} else if (value == null) {
	    onCompletion(new NoSuchThingError(type, id), null);
	} else {
	    onCompletion(null, JSON.parse(value));
	}
    });
};

RedisJSONStore.prototype.update = function(type, id, value, onCompletion) {
    this.client.set(this.toKey(type, id), JSON.stringify(value), function(err) {
	if (err) {
	    onCompletion(new JSONStoreError(err), null);
	} else {
	    onCompletion(null, value);
	}
    });
};

RedisJSONStore.prototype.del = function(type, id, onCompletion) {
    this.client.del(this.toKey(type, id), function(err, count) {
	if (err) {
	    onCompletion(new JSONStoreError(err));
	} else {
{'fraction_non_alphanumeric': 0.13575458392101553, 'fraction_numerical': 0.003173483779971791, 'mean_word_length': 3.9287576020851436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3780259', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1765, 'n_words': 454}