gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r161 - GNUnet/src/applications/dht/module


From: grothoff
Subject: [GNUnet-SVN] r161 - GNUnet/src/applications/dht/module
Date: Wed, 2 Feb 2005 04:53:00 -0800 (PST)

Author: grothoff
Date: 2005-02-02 04:52:59 -0800 (Wed, 02 Feb 2005)
New Revision: 161

Modified:
   GNUnet/src/applications/dht/module/datastore_dht_master.c
Log:
making datastore_dht_master compile

Modified: GNUnet/src/applications/dht/module/datastore_dht_master.c
===================================================================
--- GNUnet/src/applications/dht/module/datastore_dht_master.c   2005-02-02 
12:33:55 UTC (rev 160)
+++ GNUnet/src/applications/dht/module/datastore_dht_master.c   2005-02-02 
12:52:59 UTC (rev 161)
@@ -20,7 +20,7 @@
 /**
  * @file applications/dht/module/datastore_dht_master.c
  * @brief provides the implementation of the 
- * DHT_Datastore API for the DHT master table; based on
+ * Blockstore API for the DHT master table; based on
  * datastore_memory.c.
  * @author Simo Viitanen, Christian Grothoff
  *
@@ -28,7 +28,11 @@
  * The main difference between this datastore and the default
  * memory-datastore is that entries have a timestamp and are
  * timed-out after a certain time of inactivity.  Also,
- * duplicate entries are removed.
+ * duplicate entries are removed.<p>
+ *
+ * The type and priorities are ignored, except in get where
+ * the priority is the maximum number of results to return.
+ * The number of keys specified in a GET must always be one.
  */
 
 #include "platform.h"
@@ -65,34 +69,34 @@
  * Lookup an item in the datastore.
  *
  * @param key the value to lookup
- * @param maxResults maximum number of results
- * @param results where to store the result; must point to
- *        an array of maxResuls containers; if the containers
- *        point to allocated memory, it will be used by lookup;
- *        otherwise lookup will allocate the data pointer;
- *        Note that dataLength must either be 0 or the
- *        size of HashCode160.
- * @return number of results, SYSERR on error
+ * @param prio is interpreted as the maximum number
+ *  of results to return
+ * @return number of results available, SYSERR on error
  */
 static int lookup(void * closure,
-                 const HashCode160 * key,
-                 unsigned int maxResults,
-                 DHT_DataContainer * results) {
+                 unsigned int type,
+                 unsigned int prio,
+                 unsigned int keyCount,
+                 const HashCode160 * keys,
+                 DataProcessor resultCallback,
+                 void * resCallbackClosure) {
   MemoryDatastore * ds = (MemoryDatastore*) closure;
   HT_Entry * pos;
   int count;
   int i;
+  DataContainer * data;
 
+  GNUNET_ASSERT(keyCount == 1);
   if (ds == NULL)
     return SYSERR;
   MUTEX_LOCK(&ds->lock);
   pos = ds->first;
   while (pos != NULL) {
-    if (equalsHashCode160(key, &pos->key)) {
+    if (equalsHashCode160(&keys[0], &pos->key)) {
       int * perm;
 
-      if (pos->count > maxResults)
-       count = maxResults;
+      if (pos->count > prio)
+       count = prio;
       else
        count = pos->count;
       if (count < pos->count) 
@@ -106,18 +110,17 @@
          j = i;
        else
          j = perm[i];
-       if (results[j].dataLength > 0) {
-         GNUNET_ASSERT(results[j].dataLength == sizeof(HashCode160));
-         memcpy(results[j].data,
-                &pos->values[j].hash,
-                sizeof(HashCode160));
-       } else {
-         results[j].dataLength = sizeof(HashCode160);
-         results[j].data = MALLOC(sizeof(HashCode160));
-         memcpy(results[j].data,
-                &pos->values[j].hash,
-                sizeof(HashCode160));
-       }
+       data = MALLOC(sizeof(DataContainer) + 
+                     sizeof(HashCode160));
+       data->size = htonl(sizeof(DataContainer) + 
+                          sizeof(HashCode160));
+       memcpy(&data[1],
+              &pos->values[j].hash,
+              sizeof(HashCode160));    
+       resultCallback(NULL,
+                      data,
+                      resCallbackClosure);
+       FREE(data);
       }
       FREENONNULL(perm);
       MUTEX_UNLOCK(&ds->lock);
@@ -135,18 +138,22 @@
  * @param key the key of the item
  * @param value the value to store, must be of size HashCode160 for 
  *        the master table!
- * @return OK if the value could be stored, DHT_ERRORCODE or SYSERR if not 
(i.e. out of space)
+ * @return OK if the value could be stored, SYSERR if not, 
+ *         NO for out of space)
  */
 static int store(void * closure,
                 const HashCode160 * key,
-                const DHT_DataContainer * value) {
+                unsigned int type,
+                const DataContainer * value,
+                unsigned int prio) {
   MemoryDatastore * ds = (MemoryDatastore*) closure;
   HT_Entry * pos;
   int i;
 
-  if (ds == NULL)
+  if ( (ds == NULL) || (value == NULL) )
     return SYSERR;
-  if (value->dataLength != sizeof(HashCode160))
+  if (ntohl(value->size) - sizeof(DataContainer) 
+      != sizeof(HashCode160))
     return SYSERR;
 
   MUTEX_LOCK(&ds->lock);
@@ -155,22 +162,22 @@
     if (equalsHashCode160(key, &pos->key)) {
       for (i=0;i<pos->count;i++)
        if (equalsHashCode160(&pos->values[i].hash,
-                             (HashCode160*)value->data)) {
+                             (HashCode160*)&value[1])) {
          pos->values[i].lastRefreshTime = cronTime(NULL);
          MUTEX_UNLOCK(&ds->lock);
          return OK; /* already present */
        }
       if (ds->max_memory < sizeof(MasterEntry)) {
        MUTEX_UNLOCK(&ds->lock);
-       return DHT_ERRORCODES__OUT_OF_SPACE;    
+       return NO;
       }
-      ds->max_memory -= value->dataLength;
+      ds->max_memory -= sizeof(MasterEntry);
       GROW(pos->values,
           pos->count,
           pos->count+1);      
       pos->values[pos->count-1].lastRefreshTime = cronTime(NULL);
       memcpy(&pos->values[pos->count-1].hash,
-            value->data,
+            &value[1],
             sizeof(HashCode160));
       MUTEX_UNLOCK(&ds->lock);  
       return OK;
@@ -178,17 +185,17 @@
     pos = pos->next;
   }
   /* no key matched, create fresh entry */
-  if (ds->max_memory < sizeof(HT_Entry) + sizeof(DHT_DataContainer) + 
sizeof(MasterEntry)) {
+  if (ds->max_memory < sizeof(HT_Entry) + sizeof(MasterEntry)) {
     MUTEX_UNLOCK(&ds->lock);
-    return DHT_ERRORCODES__OUT_OF_SPACE;
+    return NO;
   }
-  ds->max_memory -= sizeof(HT_Entry) + sizeof(DHT_DataContainer) + 
sizeof(MasterEntry);
+  ds->max_memory -= sizeof(HT_Entry) + sizeof(MasterEntry);
   pos = MALLOC(sizeof(HT_Entry));
   pos->key = *key;
   pos->count = 1;
   pos->values = MALLOC(sizeof(MasterEntry));
   memcpy(&pos->values[0].hash,
-        value->data,
+        &value[1],
         sizeof(HashCode160));
   pos->values[0].lastRefreshTime = cronTime(NULL);
   pos->next = ds->first;
@@ -205,7 +212,8 @@
  */
 static int ds_remove(void * closure,
                     const HashCode160 * key,
-                    const DHT_DataContainer * value) {
+                    unsigned int type,
+                    const DataContainer * value) {
   MemoryDatastore * ds = (MemoryDatastore*) closure;
   HT_Entry * pos;
   HT_Entry * prev;
@@ -214,7 +222,8 @@
   if (ds == NULL)
     return SYSERR;
   if ( (value != NULL) &&
-       (value->dataLength != sizeof(HashCode160)) )
+       (ntohl(value->size) - sizeof(DataContainer) 
+       != sizeof(HashCode160)) )
     return SYSERR;
 
   MUTEX_LOCK(&ds->lock);
@@ -225,7 +234,7 @@
       if (value != NULL) {
        for (i=0;i<pos->count;i++) {
          if (0 == memcmp(&pos->values[i].hash,
-                         value->data,
+                         &value[1],
                          sizeof(HashCode160))) {
            pos->values[i] = pos->values[pos->count-1];
            GROW(pos->values,
@@ -276,13 +285,13 @@
  * @return number of results, SYSERR on error
  */
 static int iterate(void * closure,              
-                  DHT_DataProcessor processor,
+                  DataProcessor processor,
                   void * cls) {
   MemoryDatastore * ds = (MemoryDatastore*) closure;
   int ret;
   HT_Entry * pos;
   int i;
-  DHT_DataContainer cont;
+  DataContainer * cont;
 
   if (ds == NULL)
     return SYSERR;
@@ -290,16 +299,20 @@
   MUTEX_LOCK(&ds->lock);
   pos = ds->first;
   ret = 0;
-  cont.dataLength = 20;
+  cont = MALLOC(sizeof(HashCode160) + sizeof(DataContainer));
+  cont->size = htonl(sizeof(HashCode160) + sizeof(DataContainer));
   while (pos != NULL) {
     for (i=0;i<pos->count;i++) {
       ret++;
       if (processor != NULL) {
-       cont.data = &pos->values[i].hash;
+       memcpy(&cont[1],
+              &pos->values[i].hash,
+              sizeof(HashCode160));
        if (OK != processor(&pos->key,
-                           &cont,
+                           cont,
                            cls)) {
          MUTEX_UNLOCK(&ds->lock);
+         FREE(cont);
          return ret;
        }
       }
@@ -307,6 +320,7 @@
     pos = pos->next;
   }
   MUTEX_UNLOCK(&ds->lock);
+  FREE(cont);
   return SYSERR;
 }
 
@@ -350,8 +364,8 @@
  * Create a DHT Datastore (in memory)
  * @param max_memory do not use more than max_memory memory.
  */
-DHT_Datastore * create_datastore_dht_master(size_t max_memory) {
-  DHT_Datastore * res;
+Blockstore * create_datastore_dht_master(size_t max_memory) {
+  Blockstore * res;
   MemoryDatastore * md;
 
   md = MALLOC(sizeof(MemoryDatastore));
@@ -359,10 +373,10 @@
   md->first = NULL;
   MUTEX_CREATE_RECURSIVE(&md->lock);
 
-  res = MALLOC(sizeof(DHT_Datastore));
-  res->lookup  = &lookup;
-  res->store   = &store;
-  res->remove  = &ds_remove;
+  res = MALLOC(sizeof(Blockstore));
+  res->get  = &lookup;
+  res->put   = &store;
+  res->del  = &ds_remove;
   res->iterate = &iterate;
   res->closure = md;
   addCronJob((CronJob) &expirationJob,
@@ -377,7 +391,7 @@
  * @param ds the Datastore to destroy; must have been
  *  created by create_datastore_memory.
  */
-void destroy_datastore_dht_master(DHT_Datastore * ds) {
+void destroy_datastore_dht_master(Blockstore * ds) {
   MemoryDatastore * md;
   HT_Entry * pos;
   HT_Entry * next;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]