From 593b9010a74d9eeba19e30cd20caa0980fff3d93 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <sim.desaulniers@gmail.com>
Date: Tue, 14 Jun 2016 21:41:21 -0400
Subject: [PATCH] python: update cython wrapper and python benchmark

API changed when the keys pec was added. This commit updates python code
according to the new API.
---
 python/opendht.pyx        | 11 +++++++----
 python/opendht_cpp.pxd    |  5 +++--
 python/tools/dht/tests.py | 10 ++++++----
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/python/opendht.pyx b/python/opendht.pyx
index a2f408b2..c1f493c1 100644
--- a/python/opendht.pyx
+++ b/python/opendht.pyx
@@ -406,8 +406,11 @@ cdef class IndexValue(object):
 
 cdef class Pht(object):
     cdef cpp.Pht* thisptr
-    def __cinit__(self, bytes name, DhtRunner dht):
-        self.thisptr = new cpp.Pht(name, dht.thisptr)
+    def __cinit__(self, bytes name, key_spec, DhtRunner dht):
+        cdef cpp.IndexKeySpec cpp_key_spec
+        for kk, size in key_spec.items():
+            cpp_key_spec[bytes(kk, 'utf-8')] = size
+        self.thisptr = new cpp.Pht(name, cpp_key_spec, dht.thisptr)
     property MAX_NODE_ENTRY_COUNT:
         def __get__(self):
             return cpp.PHT_MAX_NODE_ENTRY_COUNT
@@ -424,7 +427,7 @@ cdef class Pht(object):
         ref.Py_INCREF(cb_obj)
         cdef cpp.IndexKey cppk
         for kk, v in key.items():
-            cppk[bytes(kk, 'utf-8')] = cpp.Prefix(bytes(v))
+            cppk[bytes(kk, 'utf-8')] = bytes(v)
         self.thisptr.lookup(
                 cppk,
                 cpp.Pht.bindLookupCb(lookup_callback, <void*>cb_obj),
@@ -441,7 +444,7 @@ cdef class Pht(object):
         ref.Py_INCREF(cb_obj)
         cdef cpp.IndexKey cppk
         for kk, v in key.items():
-            cppk[bytes(kk, 'utf-8')] = cpp.Prefix(bytes(v))
+            cppk[bytes(kk, 'utf-8')] = bytes(v)
         cdef cpp.IndexValue val
         val.first = (<InfoHash>value.getKey())._infohash
         val.second = value.getValueId()
diff --git a/python/opendht_cpp.pxd b/python/opendht_cpp.pxd
index 95b95500..d2f53026 100644
--- a/python/opendht_cpp.pxd
+++ b/python/opendht_cpp.pxd
@@ -167,12 +167,13 @@ cdef extern from "opendht/indexation/pht.h" namespace "dht::indexation":
         Prefix(vector[uint8_t]) except +
         string toString() const
     ctypedef pair[InfoHash, uint64_t] IndexValue "dht::indexation::Value"
-    ctypedef map[string, Prefix] IndexKey "dht::indexation::Pht::Key"
+    ctypedef map[string, vector[uint8_t]] IndexKey "dht::indexation::Pht::Key"
+    ctypedef map[string, uint32_t] IndexKeySpec "dht::indexation::Pht::KeySpec"
     ctypedef void (*LookupCallbackRaw)(vector[shared_ptr[IndexValue]]* values, Prefix* p, void* user_data);
     cdef cppclass Pht:
         cppclass LookupCallback:
             LookupCallback() except +
-        Pht(string, shared_ptr[DhtRunner]) except +
+        Pht(string, IndexKeySpec, shared_ptr[DhtRunner]) except +
         void lookup(IndexKey k, LookupCallback cb, DoneCallbackSimple doneCb);
         void insert(IndexKey k, IndexValue v, DoneCallbackSimple cb)
         @staticmethod
diff --git a/python/tools/dht/tests.py b/python/tools/dht/tests.py
index 67cdd6a4..91eb810f 100644
--- a/python/tools/dht/tests.py
+++ b/python/tools/dht/tests.py
@@ -10,6 +10,7 @@ import string
 import time
 import subprocess
 import re
+import collections
 
 from matplotlib.ticker import FuncFormatter
 import math
@@ -276,17 +277,18 @@ class PhtTest(FeatureTest):
         """
         bootstrap = self._bootstrap
         bootstrap.resize(2)
-
         dht = bootstrap.get(1)
-        pht = Pht(b'foo_index', dht)
+
+        NUM_DIG  = max(math.log(self._num_keys, 2)/4, 5) # at least 5 digit keys.
+        keyspec = collections.OrderedDict([('foo', NUM_DIG)])
+        pht = Pht(b'foo_index', keyspec, dht)
 
         DhtNetwork.log('PHT has',
                        pht.MAX_NODE_ENTRY_COUNT,
                        'node'+ ('s' if pht.MAX_NODE_ENTRY_COUNT > 1 else ''),
                        'per leaf bucket.')
-        NUM_DIG  = max(math.log(self._num_keys, 2)/4, 5) # at least 5 digit keys.
         keys = [{
-            'foo' :
+            [_ for _ in keyspec.keys()][0] :
             ''.join(random.SystemRandom().choice(string.hexdigits)
                 for _ in range(NUM_DIG)).encode()
             } for n in range(self._num_keys)]
-- 
GitLab