{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "from btree import Node, BTree, NodeKey\n",
    "\n",
    "class DQKV(BTree):\n",
    "    def __init__(self, type_, values=None):\n",
    "        self.type = type_\n",
    "        super().__init__(10)\n",
    "\n",
    "    def get(self, key):\n",
    "        value = self.search(self.root, key)\n",
    "        if value is None:\n",
    "            raise KeyError('There is no value for key \"{}\"'.format(key))\n",
    "        return value\n",
    "    \n",
    "    def set(self, key, value):\n",
    "        if value is None:\n",
    "            raise ValueError('Cannot store None values')\n",
    "        if not isinstance(key, self.type):\n",
    "            raise KeyError('Key must be of type {}'.format(self.type))\n",
    "        exists = self.search(self.root, key)\n",
    "        if exists is not None:\n",
    "            raise ValueError('Cannot store duplicate key values')\n",
    "            \n",
    "        node = NodeKey(key, value)\n",
    "        self.insert(node)\n",
    "    \n",
    "    def range_query(self, interval, inclusive=False):\n",
    "        if not isinstance(interval, (list, tuple)) and len(interval) != 2:\n",
    "            raise ValueError('The first argument must be a list or tuple of length 2')\n",
    "        \n",
    "        lower, upper = interval\n",
    "        if lower is None:\n",
    "            return self.less_than(self.root, upper, inclusive=inclusive)\n",
    "        return self.greater_than(self.root, lower, upper_bound=upper, inclusive=inclusive)\n",
    "    \n",
    "    def save(self, filename):\n",
    "        filename = filename + '.dqdb'\n",
    "        with open(filename, 'wb') as f:\n",
    "            pickle.dump(self, f)\n",
    "            return True\n",
    "        return False\n",
    "    \n",
    "    def load_from_dict(self, dictionary):\n",
    "        for key, value in dictionary.items():\n",
    "            self.set(key, value)\n",
    "    \n",
    "    @staticmethod\n",
    "    def load(filename):\n",
    "        filename = filename + '.dqdb'\n",
    "        with open(filename, 'rb') as f:\n",
    "            return pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dq = DQKV(int)\n",
    "dq.set(1, 'hello')\n",
    "dq.set(2, 'world')\n",
    "dq.set(3, 'this')\n",
    "dq.set(4, 'is')\n",
    "print(dq.range_query([1,3]))\n",
    "\n",
    "dq.save('sample_store')\n",
    "dqkv = DQKV.load('sample_store')\n",
    "\n",
    "print(dqkv.range_query([1,3]))\n",
    "additional_keys = {\n",
    "    5: 'a',\n",
    "    6: 'simple',\n",
    "    7: 'kv store'\n",
    "}\n",
    "dqkv.load_from_dict(additional_keys)\n",
    "print(dqkv.range_query([4,8]))\n",
    "print(dqkv.get(5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
