From dffb1316a43fc5a7e21964041c9e6cbddb8be2e4 Mon Sep 17 00:00:00 2001
From: dilip kumar <dilipbalaut@localhost.localdomain>
Date: Tue, 18 Aug 2020 13:44:53 +0530
Subject: [PATCH v2] bufile_test

---
 contrib/buffile_test/.gitignore            |   4 +
 contrib/buffile_test/Makefile              |  22 +++++
 contrib/buffile_test/buffile_test--1.0.sql |  13 +++
 contrib/buffile_test/buffile_test.c        | 132 +++++++++++++++++++++++++++++
 contrib/buffile_test/buffile_test.control  |   5 ++
 5 files changed, 176 insertions(+)
 create mode 100644 contrib/buffile_test/.gitignore
 create mode 100644 contrib/buffile_test/Makefile
 create mode 100644 contrib/buffile_test/buffile_test--1.0.sql
 create mode 100644 contrib/buffile_test/buffile_test.c
 create mode 100644 contrib/buffile_test/buffile_test.control

diff --git a/contrib/buffile_test/.gitignore b/contrib/buffile_test/.gitignore
new file mode 100644
index 0000000..5dcb3ff
--- /dev/null
+++ b/contrib/buffile_test/.gitignore
@@ -0,0 +1,4 @@
+# Generated subdirectories
+/log/
+/results/
+/tmp_check/
diff --git a/contrib/buffile_test/Makefile b/contrib/buffile_test/Makefile
new file mode 100644
index 0000000..96da192
--- /dev/null
+++ b/contrib/buffile_test/Makefile
@@ -0,0 +1,22 @@
+# contrib/amcheck/Makefile
+
+MODULE_big	= buffile_test
+OBJS = \
+	$(WIN32RES) \
+	buffile_test.o
+
+EXTENSION = buffile_test
+DATA = buffile_test--1.0.sql
+PGFILEDESC = "buffile_test"
+
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/amcheck
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/buffile_test/buffile_test--1.0.sql b/contrib/buffile_test/buffile_test--1.0.sql
new file mode 100644
index 0000000..6305f3e
--- /dev/null
+++ b/contrib/buffile_test/buffile_test--1.0.sql
@@ -0,0 +1,13 @@
+/* contrib/amcheck/amcheck--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION buffile_test" to load this file. \quit
+
+--
+-- bt_index_check()
+--
+CREATE FUNCTION buffile_test()
+RETURNS VOID
+AS 'MODULE_PATHNAME', 'buffile_test'
+LANGUAGE C STRICT PARALLEL RESTRICTED;
+
diff --git a/contrib/buffile_test/buffile_test.c b/contrib/buffile_test/buffile_test.c
new file mode 100644
index 0000000..44bf157
--- /dev/null
+++ b/contrib/buffile_test/buffile_test.c
@@ -0,0 +1,132 @@
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "access/nbtree.h"
+#include "access/table.h"
+#include "access/tableam.h"
+#include "access/transam.h"
+#include "access/xact.h"
+#include "catalog/index.h"
+#include "catalog/pg_am.h"
+#include "commands/tablecmds.h"
+#include "lib/bloomfilter.h"
+#include "miscadmin.h"
+#include "storage/buf_internals.h"
+#include "storage/buffile.h"
+#include "storage/fd.h"
+#include "utils/memutils.h"
+#include "utils/snapmgr.h"
+
+
+PG_MODULE_MAGIC;
+
+PG_FUNCTION_INFO_V1(buffile_test);
+
+/* test truncate */
+static void
+buffile_test1(SharedFileSet *fileset)
+{
+	BufFile    *fd;
+	int			fileno = 0;
+	off_t			offset = 0;
+	size_t  nread = 0;
+	char		readbuf[100];
+
+	fd = BufFileCreateShared(fileset, "test_file");
+	BufFileWrite(fd, "aaaaaaaaaa", 10);
+	BufFileTell(fd, &fileno, &offset);
+	BufFileWrite(fd, "bbbbbbbbbb", 10);
+	BufFileTruncateShared(fd, fileno, offset);
+	BufFileWrite(fd, "ccccc", 5);
+	BufFileSeek(fd, 0, 0, SEEK_SET);
+	nread = BufFileRead(fd, readbuf, 20);
+
+	if (nread != 15)
+		elog(ERROR, "FAILED: unexpected bytes read");
+	else if (strncmp(readbuf, "aaaaaaaaaaccccc", 15) != 0)
+		elog(ERROR, "FAILED: unexpected data read");
+	else
+		elog(WARNING, "PASSED: expected bytes read");
+	BufFileClose(fd);
+
+	BufFileDeleteShared(fileset, "test_file");
+}
+
+#define MAX_PHYSICAL_FILESIZE	0x40000000
+#define BUFFILE_SEG_SIZE		(MAX_PHYSICAL_FILESIZE / BLCKSZ)
+
+/* test truncate on multiple files*/
+static void
+buffile_test2(SharedFileSet *fileset)
+{
+	BufFile    *fd;
+	int			fileno = 0;
+	off_t		offset = 0;
+	size_t  	size = 0;
+	char		buf[BLCKSZ] = {'b'};
+	int			i;
+
+	fd = BufFileCreateShared(fileset, "test_file");
+	BufFileWrite(fd, "aaaaaaaaaa", 10);
+	BufFileTell(fd, &fileno, &offset);
+
+	/* create 3 files */
+	for (i = 0; i < 3* BUFFILE_SEG_SIZE; i++)
+	{
+		BufFileWrite(fd, buf, BLCKSZ);
+	}
+
+	/* seek to some location in the first file */
+	BufFileSeek(fd, 0, 10, SEEK_SET);
+	BufFileWrite(fd, "aaaaa", 5);
+
+	/* truncate within the first file and in same buffer */
+	BufFileTruncateShared(fd, fileno, offset + 7);
+	size = BufFileSize(fd);
+	if (size == 17)
+		elog(WARNING, "PASSED: expected file size");
+	else
+		elog(WARNING, "FAILED: unexpected file size");
+
+	BufFileClose(fd);
+
+	BufFileDeleteShared(fileset, "test_file");
+}
+
+static void
+buffile_test3(SharedFileSet *fileset)
+{
+	BufFile    *fd;
+	int			fileno = 0;
+	off_t			offset = 0;
+	size_t  nread = 0;
+	char		readbuf[100];
+	SharedFileSet *fileset1;
+
+	fileset1 = palloc0(sizeof(SharedFileSet));
+	fd = BufFileCreateShared(fileset, "test_file");
+	BufFileWrite(fd, "aaaaaaaaaa", 10);
+	SharedFileSetInit(fileset1, NULL);
+	BufFileCreateShared(fileset1, "test_file1");
+	SharedFileSetDeleteAll(fileset1);
+	proc_exit(1);
+}
+
+Datum
+buffile_test(PG_FUNCTION_ARGS)
+{
+	SharedFileSet *fileset;
+
+	MemoryContextSwitchTo(TopMemoryContext);
+	fileset = palloc(sizeof(SharedFileSet));
+	SharedFileSetInit(fileset, NULL);
+
+	buffile_test1(fileset);
+	//buffile_test2(fileset);
+	buffile_test3(fileset);
+
+
+	SharedFileSetDeleteAll(fileset);
+
+	PG_RETURN_VOID();
+}
diff --git a/contrib/buffile_test/buffile_test.control b/contrib/buffile_test/buffile_test.control
new file mode 100644
index 0000000..a7c6fa2
--- /dev/null
+++ b/contrib/buffile_test/buffile_test.control
@@ -0,0 +1,5 @@
+# buffile_test extension
+comment = 'test buffile'
+default_version = '1.0'
+module_pathname = '$libdir/buffile_test'
+relocatable = true
-- 
1.8.3.1

