[GRASS-SVN] r73498 - in grass/trunk: include lib/segment

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 8 09:20:41 PDT 2018


Author: mmetz
Date: 2018-10-08 09:20:41 -0700 (Mon, 08 Oct 2018)
New Revision: 73498

Modified:
   grass/trunk/include/segment.h
   grass/trunk/lib/segment/close.c
   grass/trunk/lib/segment/format.c
   grass/trunk/lib/segment/get.c
   grass/trunk/lib/segment/get_row.c
   grass/trunk/lib/segment/open.c
   grass/trunk/lib/segment/put.c
   grass/trunk/lib/segment/put_row.c
Log:
libsegment: + all in memory cache

Modified: grass/trunk/include/segment.h
===================================================================
--- grass/trunk/include/segment.h	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/include/segment.h	2018-10-08 16:20:41 UTC (rev 73498)
@@ -58,6 +58,8 @@
     int nseg;			/* number of segments in memory */
     int cur;			/* last accessed segment */
     int offset;			/* offset of data past header */
+
+    char *cache;		/* all in memory cache */
 } SEGMENT;
 
 #include <grass/defs/segment.h>

Modified: grass/trunk/lib/segment/close.c
===================================================================
--- grass/trunk/lib/segment/close.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/close.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -8,7 +8,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2012
+ * \date 2018
  */
 
 #include <unistd.h>
@@ -35,12 +35,19 @@
     if (SEG->open != 1)
 	return -1;
 
-    Segment_release(SEG);
-    close(SEG->fd);
-    unlink(SEG->fname);
+    if (SEG->scb) {
+	Segment_release(SEG);
+	close(SEG->fd);
+	unlink(SEG->fname);
 
-    SEG->fd = -1;
-    SEG->fname = NULL;
+	SEG->fd = -1;
+	SEG->fname = NULL;
+    }
+    else {
+	G_free(SEG->cache);
+    }
 
+    SEG->open = 0;
+
     return 1;
 }

Modified: grass/trunk/lib/segment/format.c
===================================================================
--- grass/trunk/lib/segment/format.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/format.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -105,9 +105,8 @@
 }
 
 
-static int seg_format(int fd,
-			   off_t nrows, off_t ncols,
-			   int srows, int scols, int len, int fill)
+static int seg_format(int fd, off_t nrows, off_t ncols,
+		      int srows, int scols, int len, int fill)
 {
     off_t nbytes;
     int spr, size;

Modified: grass/trunk/lib/segment/get.c
===================================================================
--- grass/trunk/lib/segment/get.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/get.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <string.h>
@@ -40,6 +40,12 @@
 {
     int index, n, i;
 
+    if (!SEG->scb) {
+	memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len, SEG->len);
+	
+	return 1;
+    }
+
     SEG->address(SEG, row, col, &n, &index);
     if ((i = seg_pagein(SEG, n)) < 0)
 	return -1;

Modified: grass/trunk/lib/segment/get_row.c
===================================================================
--- grass/trunk/lib/segment/get_row.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/get_row.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -47,6 +47,12 @@
     int scols;
     int n, index;
 
+    if (!SEG->scb) {
+	memcpy(buf, SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, SEG->len * SEG->ncols);
+	
+	return 1;
+    }
+
     ncols = SEG->ncols - SEG->spill;
     scols = SEG->scols;
     size = scols * SEG->len;

Modified: grass/trunk/lib/segment/open.c
===================================================================
--- grass/trunk/lib/segment/open.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/open.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2012
+ * \date 2018
  */
 
 #include <unistd.h>
@@ -48,7 +48,27 @@
              int srows, int scols, int len, int nseg)
 {
     int ret;
+    int nseg_total;
 
+    nseg_total = ((nrows + srows - 1) / srows) * 
+                 ((ncols + scols - 1) / scols);
+
+    if (nseg >= nseg_total) {
+	G_verbose_message(_("Using memory cache"));
+
+	SEG->nrows = nrows;
+	SEG->ncols = ncols;
+	SEG->len = len;
+	SEG->nseg = nseg;
+	SEG->cache = G_malloc(sizeof(char) * SEG->nrows * SEG->ncols * SEG->len);
+	SEG->scb = NULL;
+	SEG->open = 1;
+	
+	return 1;
+    }
+
+    G_verbose_message(_("Using disk cache"));
+
     if (!fname) {
 	G_warning(_("Segment file name is NULL"));
 	return -1;

Modified: grass/trunk/lib/segment/put.c
===================================================================
--- grass/trunk/lib/segment/put.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/put.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <string.h>
@@ -46,6 +46,12 @@
 {
     int index, n, i;
 
+    if (!SEG->scb) {
+	memcpy(SEG->cache + ((size_t)row * SEG->ncols + col) * SEG->len, buf, SEG->len);
+	
+	return 1;
+    }
+
     SEG->address(SEG, row, col, &n, &index);
     if ((i = seg_pagein(SEG, n)) < 0) {
 	G_warning("segment lib: put: pagein failed");

Modified: grass/trunk/lib/segment/put_row.c
===================================================================
--- grass/trunk/lib/segment/put_row.c	2018-10-08 16:15:42 UTC (rev 73497)
+++ grass/trunk/lib/segment/put_row.c	2018-10-08 16:20:41 UTC (rev 73498)
@@ -9,7 +9,7 @@
  *
  * \author GRASS GIS Development Team
  *
- * \date 2005-2009
+ * \date 2005-2018
  */
 
 #include <stdio.h>
@@ -50,6 +50,12 @@
     int result;
     off_t col;
 
+    if (!SEG->scb) {
+	memcpy(SEG->cache + ((size_t)row * SEG->ncols) * SEG->len, buf, SEG->len * SEG->ncols);
+	
+	return 1;
+    }
+
     ncols = SEG->ncols - SEG->spill;
     scols = SEG->scols;
     size = scols * SEG->len;



More information about the grass-commit mailing list