changeset 2240:88cc0f44c940

Rename C functions and remove functors nested inside modules.
author Ziv Scully <ziv@mit.edu>
date Sun, 19 Jul 2015 19:03:11 -0700
parents f70a91f7810d
children 2b1af5dc6dee
files include/urweb/types_cpp.h include/urweb/urweb_cpp.h src/c/urweb.c src/lru_cache.sml src/option_key_fn.sml src/sources src/sqlcache.sml src/triple_key_fn.sml
diffstat 8 files changed, 96 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb/types_cpp.h	Tue Jul 07 00:07:24 2015 -0700
+++ b/include/urweb/types_cpp.h	Sun Jul 19 19:03:11 2015 -0700
@@ -123,31 +123,31 @@
 
 #include "uthash.h"
 
-typedef struct CacheValue {
+typedef struct uw_sqlcache_CacheValue {
   char *result;
   char *output;
-} CacheValue;
+} uw_sqlcache_CacheValue;
 
-typedef struct CacheEntry {
+typedef struct uw_sqlcache_CacheEntry {
   char *key;
   void *value;
   time_t timeValid;
-  struct CacheEntry *prev;
-  struct CacheEntry *next;
+  struct uw_sqlcache_CacheEntry *prev;
+  struct uw_sqlcache_CacheEntry *next;
   UT_hash_handle hh;
-} CacheEntry;
+} uw_sqlcache_CacheEntry;
 
-typedef struct CacheList {
-  CacheEntry *first;
-  CacheEntry *last;
+typedef struct uw_sqlcache_CacheList {
+  uw_sqlcache_CacheEntry *first;
+  uw_sqlcache_CacheEntry *last;
   int size;
-} CacheList;
+} uw_sqlcache_CacheList;
 
-typedef struct Cache {
-  CacheEntry *table;
+typedef struct uw_sqlcache_Cache {
+  uw_sqlcache_CacheEntry *table;
   time_t timeInvalid;
-  CacheList *lru;
+  uw_sqlcache_CacheList *lru;
   int height;
-} Cache;
+} uw_sqlcache_Cache;
 
 #endif
--- a/include/urweb/urweb_cpp.h	Tue Jul 07 00:07:24 2015 -0700
+++ b/include/urweb/urweb_cpp.h	Sun Jul 19 19:03:11 2015 -0700
@@ -406,8 +406,8 @@
 
 #include "uthash.h"
 
-CacheValue *check(Cache *, char **);
-CacheValue *store(Cache *, char **, CacheValue *);
-CacheValue *flush(Cache *, char **);
+uw_sqlcache_CacheValue *uw_sqlcache_check(uw_sqlcache_Cache *, char **);
+uw_sqlcache_CacheValue *uw_sqlcache_store(uw_sqlcache_Cache *, char **, uw_sqlcache_CacheValue *);
+uw_sqlcache_CacheValue *uw_sqlcache_flush(uw_sqlcache_Cache *, char **);
 
 #endif
--- a/src/c/urweb.c	Tue Jul 07 00:07:24 2015 -0700
+++ b/src/c/urweb.c	Sun Jul 19 19:03:11 2015 -0700
@@ -4500,7 +4500,7 @@
 
 // Sqlcache
 
-void listDelete(CacheList *list, CacheEntry *entry) {
+void uw_sqlcache_listDelete(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) {
   if (list->first == entry) {
     list->first = entry->next;
   }
@@ -4518,7 +4518,7 @@
   --(list->size);
 }
 
-void listAdd(CacheList *list, CacheEntry *entry) {
+void uw_sqlcache_listAdd(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) {
   if (list->last) {
     list->last->next = entry;
     entry->prev = list->last;
@@ -4530,22 +4530,22 @@
   ++(list->size);
 }
 
-void listBump(CacheList *list, CacheEntry *entry) {
-  listDelete(list, entry);
-  listAdd(list, entry);
+void uw_sqlcache_listBump(uw_sqlcache_CacheList *list, uw_sqlcache_CacheEntry *entry) {
+  uw_sqlcache_listDelete(list, entry);
+  uw_sqlcache_listAdd(list, entry);
 }
 
 // TODO: deal with time properly.
 
-time_t getTimeNow() {
+time_t uw_sqlcache_getTimeNow() {
   return time(NULL);
 }
 
-time_t timeMax(time_t x, time_t y) {
+time_t uw_sqlcache_timeMax(time_t x, time_t y) {
   return difftime(x, y) > 0 ? x : y;
 }
 
-void freeCacheValue(CacheValue *value) {
+void uw_sqlcache_freeuw_sqlcache_CacheValue(uw_sqlcache_CacheValue *value) {
   if (value) {
     free(value->result);
     free(value->output);
@@ -4553,83 +4553,83 @@
   }
 }
 
-void delete(Cache *cache, CacheEntry* entry) {
-  //listDelete(cache->lru, entry);
+void uw_sqlcache_delete(uw_sqlcache_Cache *cache, uw_sqlcache_CacheEntry* entry) {
+  //uw_sqlcache_listUw_Sqlcache_Delete(cache->lru, entry);
   HASH_DELETE(hh, cache->table, entry);
-  freeCacheValue(entry->value);
+  uw_sqlcache_freeuw_sqlcache_CacheValue(entry->value);
   free(entry->key);
   free(entry);
 }
 
-CacheValue *checkHelper(Cache *cache, char **keys, int timeInvalid) {
+uw_sqlcache_CacheValue *uw_sqlcache_checkHelper(uw_sqlcache_Cache *cache, char **keys, int timeInvalid) {
   char *key = keys[cache->height];
-  CacheEntry *entry;
+  uw_sqlcache_CacheEntry *entry;
   HASH_FIND(hh, cache->table, key, strlen(key), entry);
-  timeInvalid = timeMax(timeInvalid, cache->timeInvalid);
+  timeInvalid = uw_sqlcache_timeMax(timeInvalid, cache->timeInvalid);
   if (entry && difftime(entry->timeValid, timeInvalid) > 0) {
     if (cache->height == 0) {
       // At height 0, entry->value is the desired value.
-      //listBump(cache->lru, entry);
+      //uw_sqlcache_listBump(cache->lru, entry);
       return entry->value;
     } else {
       // At height n+1, entry->value is a pointer to a cache at heignt n.
-      return checkHelper(entry->value, keys, timeInvalid);
+      return uw_sqlcache_checkHelper(entry->value, keys, timeInvalid);
     }
   } else {
     return NULL;
   }
 }
 
-CacheValue *check(Cache *cache, char **keys) {
-  return checkHelper(cache, keys, 0);
-}
-
-void storeHelper(Cache *cache, char **keys, CacheValue *value, int timeNow) {
-  CacheEntry *entry;
+uw_sqlcache_CacheValue *uw_sqlcache_check(uw_sqlcache_Cache *cache, char **keys) {
+  return uw_sqlcache_checkHelper(cache, keys, 0);
+}
+
+void uw_sqlcache_storeHelper(uw_sqlcache_Cache *cache, char **keys, uw_sqlcache_CacheValue *value, int timeNow) {
+  uw_sqlcache_CacheEntry *entry;
   char *key = keys[cache->height];
   HASH_FIND(hh, cache->table, key, strlen(key), entry);
   if (!entry) {
-    entry = malloc(sizeof(CacheEntry));
+    entry = malloc(sizeof(uw_sqlcache_CacheEntry));
     entry->key = strdup(key);
     entry->value = NULL;
     HASH_ADD_KEYPTR(hh, cache->table, entry->key, strlen(entry->key), entry);
   }
   entry->timeValid = timeNow;
   if (cache->height == 0) {
-    //listAdd(cache->lru, entry);
-    freeCacheValue(entry->value);
+    //uw_sqlcache_listAdd(cache->lru, entry);
+    uw_sqlcache_freeuw_sqlcache_CacheValue(entry->value);
     entry->value = value;
     //if (cache->lru->size > MAX_SIZE) {
-      //delete(cache, cache->lru->first);
+      //uw_sqlcache_delete(cache, cache->lru->first);
       // TODO: return flushed value.
     //}
   } else {
     if (!entry->value) {
-      Cache *newCache = malloc(sizeof(Cache));
-      newCache->table = NULL;
-      newCache->timeInvalid = timeNow;
-      newCache->lru = cache->lru;
-      newCache->height = cache->height - 1;
-      entry->value = newCache;
+      uw_sqlcache_Cache *newuw_sqlcache_Cache = malloc(sizeof(uw_sqlcache_Cache));
+      newuw_sqlcache_Cache->table = NULL;
+      newuw_sqlcache_Cache->timeInvalid = timeNow;
+      newuw_sqlcache_Cache->lru = cache->lru;
+      newuw_sqlcache_Cache->height = cache->height - 1;
+      entry->value = newuw_sqlcache_Cache;
     }
-    storeHelper(entry->value, keys, value, timeNow);
+    uw_sqlcache_storeHelper(entry->value, keys, value, timeNow);
   }
 }
 
-void store(Cache *cache, char **keys, CacheValue *value) {
-  storeHelper(cache, keys, value, getTimeNow());
-}
-
-void flushHelper(Cache *cache, char **keys, int timeNow) {
-  CacheEntry *entry;
+void uw_sqlcache_store(uw_sqlcache_Cache *cache, char **keys, uw_sqlcache_CacheValue *value) {
+  uw_sqlcache_storeHelper(cache, keys, value, uw_sqlcache_getTimeNow());
+}
+
+void uw_sqlcache_flushHelper(uw_sqlcache_Cache *cache, char **keys, int timeNow) {
+  uw_sqlcache_CacheEntry *entry;
   char *key = keys[cache->height];
   if (key) {
     HASH_FIND(hh, cache->table, key, strlen(key), entry);
     if (entry) {
       if (cache->height == 0) {
-        delete(cache, entry);
+        uw_sqlcache_delete(cache, entry);
       } else {
-        flushHelper(entry->value, keys, timeNow);
+        uw_sqlcache_flushHelper(entry->value, keys, timeNow);
       }
     }
   } else {
@@ -4638,6 +4638,6 @@
   }
 }
 
-void flush(Cache *cache, char **keys) {
-  flushHelper(cache, keys, getTimeNow());
-}
+void uw_sqlcache_flush(uw_sqlcache_Cache *cache, char **keys) {
+  uw_sqlcache_flushHelper(cache, keys, uw_sqlcache_getTimeNow());
+}
--- a/src/lru_cache.sml	Tue Jul 07 00:07:24 2015 -0700
+++ b/src/lru_cache.sml	Sun Jul 19 19:03:11 2015 -0700
@@ -64,7 +64,7 @@
 
     in
         Print.box
-            [string ("static Cache cacheStruct" ^ i ^ " = {"),
+            [string ("static uw_sqlcache_Cache cacheStruct" ^ i ^ " = {"),
              newline,
              string "  .table = NULL,",
              newline,
@@ -74,7 +74,7 @@
              newline,
              string ("  .height = " ^ Int.toString (params - 1) ^ "};"),
              newline,
-             string ("static Cache *cache" ^ i ^ " = &cacheStruct" ^ i ^ ";"),
+             string ("static uw_sqlcache_Cache *cache" ^ i ^ " = &cacheStruct" ^ i ^ ";"),
              newline,
              newline,
 
@@ -83,7 +83,7 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string ("  CacheValue *v = check(cache" ^ i ^ ", ks);"),
+             string ("  uw_sqlcache_CacheValue *v = uw_sqlcache_check(cache" ^ i ^ ", ks);"),
              newline,
              string "  if (v) {",
              newline,
@@ -112,7 +112,7 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string ("  CacheValue *v = malloc(sizeof(CacheValue));"),
+             string ("  uw_sqlcache_CacheValue *v = malloc(sizeof(uw_sqlcache_CacheValue));"),
              newline,
              string "  v->result = strdup(s);",
              newline,
@@ -120,7 +120,7 @@
              newline,
              string ("  puts(\"SQLCACHE: stored " ^ i ^ ".\");"),
              newline,
-             string ("  store(cache" ^ i ^ ", ks, v);"),
+             string ("  uw_sqlcache_store(cache" ^ i ^ ", ks, v);"),
              newline,
              string "  return uw_unit_v;",
              newline,
@@ -133,7 +133,7 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string ("  flush(cache" ^ i ^ ", ks);"),
+             string ("  uw_sqlcache_flush(cache" ^ i ^ ", ks);"),
              newline,
              string "  return uw_unit_v;",
              newline,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/option_key_fn.sml	Sun Jul 19 19:03:11 2015 -0700
@@ -0,0 +1,11 @@
+functor OptionKeyFn(K : ORD_KEY) : ORD_KEY = struct
+
+type ord_key = K.ord_key option
+
+val compare =
+ fn (NONE, NONE) => EQUAL
+  | (NONE, _) => LESS
+  | (_, NONE) => GREATER
+  | (SOME x, SOME y) => K.compare (x, y)
+
+end
--- a/src/sources	Tue Jul 07 00:07:24 2015 -0700
+++ b/src/sources	Sun Jul 19 19:03:11 2015 -0700
@@ -172,8 +172,9 @@
 $(SRC)/sql.sml
 
 $(SRC)/union_find_fn.sml
-
 $(SRC)/multimap_fn.sml
+$(SRC)/option_key_fn.sml
+$(SRC)/triple_key_fn.sml
 
 $(SRC)/cache.sml
 $(SRC)/toy_cache.sml
--- a/src/sqlcache.sml	Tue Jul 07 00:07:24 2015 -0700
+++ b/src/sqlcache.sml	Sun Jul 19 19:03:11 2015 -0700
@@ -207,7 +207,7 @@
 
 (* SQL analysis. *)
 
-structure CmpKey : ORD_KEY = struct
+structure CmpKey = struct
 
     type ord_key = Sql.cmp
 
@@ -247,34 +247,6 @@
 end
 *)
 
-functor OptionKeyFn (K : ORD_KEY) : ORD_KEY = struct
-
-    type ord_key = K.ord_key option
-
-    val compare =
-     fn (NONE, NONE) => EQUAL
-      | (NONE, _) => LESS
-      | (_, NONE) => GREATER
-      | (SOME x, SOME y) => K.compare (x, y)
-
-end
-
-functor TripleKeyFn (structure I : ORD_KEY
-                     structure J : ORD_KEY
-                     structure K : ORD_KEY)
-        : ORD_KEY where type ord_key = I.ord_key * J.ord_key * K.ord_key = struct
-
-    type ord_key = I.ord_key * J.ord_key * K.ord_key
-
-    fun compare ((i1, j1, k1), (i2, j2, k2)) =
-        case I.compare (i1, i2) of
-            EQUAL => (case J.compare (j1, j2) of
-                          EQUAL => K.compare (k1, k2)
-                        | ord => ord)
-          | ord => ord
-
-end
-
 val rec chooseTwos : 'a list -> ('a * 'a) list =
  fn [] => []
   | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/triple_key_fn.sml	Sun Jul 19 19:03:11 2015 -0700
@@ -0,0 +1,15 @@
+functor TripleKeyFn (structure I : ORD_KEY
+                     structure J : ORD_KEY
+                     structure K : ORD_KEY)
+        : ORD_KEY where type ord_key = I.ord_key * J.ord_key * K.ord_key = struct
+
+type ord_key = I.ord_key * J.ord_key * K.ord_key
+
+fun compare ((i1, j1, k1), (i2, j2, k2)) =
+    case I.compare (i1, i2) of
+        EQUAL => (case J.compare (j1, j2) of
+                      EQUAL => K.compare (k1, k2)
+                    | ord => ord)
+      | ord => ord
+
+end