-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c984c3c
commit b9e206c
Showing
15 changed files
with
341 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
|
||
static void dir_fdw_begin(ForeignScanState* f, int eflags){ | ||
switch(eflags & EXEC_FLAG_EXPLAIN_ONLY){ | ||
case 0: break; | ||
default: return; | ||
} | ||
|
||
DirFdwExecState* e = palloc(sizeof(DirFdwExecState)); | ||
f->fdw_state = e; | ||
|
||
DirFdwOption o = {0}; | ||
dir_fdw_options(RelationGetRelid(f->ss.ss_currentRelation), &o); | ||
switch(NULL==o.dirname){ | ||
case false: break; | ||
default: | ||
elog(ERROR, "Invalid dirname."); | ||
return; | ||
} | ||
|
||
e->dirname = pstrdup(o.dirname); | ||
e->d = AllocateDir(e->dirname); | ||
switch(NULL == e->d){ | ||
case false: return; | ||
default: break; | ||
} | ||
elog(ERROR, "Unable to get fd."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
static void dir_fdw_end(ForeignScanState* f){ | ||
DirFdwExecState* e = f->fdw_state; | ||
switch(FreeDir(e->d)){ | ||
case 0: return; | ||
default: break; | ||
} | ||
elog(WARNING, "Unable to free dir."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <commands/explain.h> | ||
#include <utils/rel.h> | ||
|
||
#include <unistd.h> | ||
|
||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
|
||
static void dir_fdw_explain(ForeignScanState* f, ExplainState* e){ | ||
DirFdwOption o = {0}; | ||
dir_fdw_options(RelationGetRelid(f->ss.ss_currentRelation), &o); | ||
ExplainPropertyText("Foreign dirname", o.dirname, e); | ||
|
||
switch(!! e->costs){ | ||
case false: return; | ||
default: break; | ||
} | ||
|
||
struct stat s = {0}; | ||
switch(stat(o.dirname, &s)){ | ||
case -1: return; | ||
default: break; | ||
} | ||
ExplainPropertyInteger("Foreign dir size", "b", s.st_size, e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
static Datum b2pgb8(const void* bytes){ | ||
static const uint16_t bufsz = 256; | ||
int sz = bufsz + VARHDRSZ; | ||
bytea* ba = palloc(sz); | ||
memcpy(VARDATA(ba), bytes, bufsz); | ||
SET_VARSIZE(ba, sz); | ||
return PointerGetDatum(ba); | ||
} | ||
|
||
static TupleTableSlot* dir_fdw_iterate(ForeignScanState* f){ | ||
DirFdwExecState* e = f->fdw_state; | ||
TupleTableSlot* s = f->ss.ss_ScanTupleSlot; | ||
|
||
TupleDesc t = s->tts_tupleDescriptor; | ||
int n = t->natts; | ||
|
||
ExecClearTuple(s); | ||
|
||
switch(n){ | ||
case 3: break; | ||
default: | ||
return s; | ||
} | ||
|
||
switch(NULL == e->dirname){ | ||
case false: break; | ||
default: | ||
elog(ERROR, "invalid dirname."); | ||
return s; | ||
} | ||
|
||
struct dirent* d = ReadDir(e->d, e->dirname); | ||
switch(NULL == d){ | ||
case true: return s; | ||
default: break; | ||
} | ||
|
||
Datum* values = s->tts_values; | ||
bool* nulls = s->tts_isnull; | ||
|
||
values[0] = d->d_ino; | ||
values[1] = UInt8GetDatum(d->d_type); | ||
values[2] = b2pgb8(d->d_name); | ||
nulls[0] = false; | ||
nulls[1] = false; | ||
nulls[2] = false; | ||
ExecStoreVirtualTuple(s); | ||
|
||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <foreign/foreign.h> | ||
#include <commands/defrem.h> | ||
|
||
typedef struct DirFdwOption { | ||
const char* dirname; | ||
char filter; | ||
} DirFdwOption; | ||
|
||
static void dir_fdw_options(Oid tableid, DirFdwOption* o){ | ||
ForeignTable* t = GetForeignTable(tableid); | ||
ListCell* i = NULL; | ||
//char* filter = NULL; | ||
|
||
foreach(i, t->options){ | ||
DefElem* d = lfirst(i); | ||
switch(d->defname[0]){ | ||
case 'd': | ||
o->dirname = defGetString(d); | ||
break; | ||
/* | ||
case 'f': | ||
filter = defGetString(d); | ||
o->filter = NULL == filter ? '\0' : filter[0]; | ||
break; | ||
*/ | ||
default: continue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <optimizer/paths.h> | ||
#include <optimizer/pathnode.h> | ||
|
||
static void dir_fdw_estimate_cost( | ||
PlannerInfo* p, RelOptInfo* r, DirFdwPlanState* s, Cost* c, Cost* t | ||
){ | ||
Cost cpu = cpu_tuple_cost * 10 + r->baserestrictcost.per_tuple; | ||
Cost run = seq_page_cost * s->pages; | ||
run += cpu * s->ntup; | ||
*c = r->baserestrictcost.startup; | ||
*t = *c + run; | ||
} | ||
|
||
static void dir_fdw_path(PlannerInfo* p, RelOptInfo* r, Oid o){ | ||
DirFdwPlanState* s = r->fdw_private; | ||
Cost start; | ||
Cost total; | ||
dir_fdw_estimate_cost(p, r, s, &start, &total); | ||
add_path(r, (Path*)create_foreignscan_path( | ||
p, | ||
r, | ||
NULL, // default pathtarget | ||
r->rows, | ||
start, | ||
total, | ||
NIL, // no pathkeys | ||
r->lateral_relids, | ||
NULL, // no extra plan | ||
NIL // | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <optimizer/planmain.h> | ||
|
||
static ForeignScan* dir_fdw_plan( | ||
PlannerInfo* p, | ||
RelOptInfo* r, | ||
Oid o, | ||
ForeignPath* f, | ||
List* l, | ||
List* s, | ||
Plan* outer | ||
){ | ||
return make_foreignscan( | ||
l, | ||
s, | ||
r->relid, | ||
NIL, // no expressions to evaluate | ||
f->fdw_private, | ||
NIL, // no custom tlist | ||
NIL, // no remote quals | ||
outer | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <optimizer/optimizer.h> | ||
|
||
static double dir_fdw_estimate( | ||
PlannerInfo* p, RelOptInfo* r, DirFdwPlanState* s | ||
){ | ||
BlockNumber pages = 8; | ||
s->pages = pages; | ||
for(; r && 0 <= r->tuples && 0 < r->pages;){ | ||
double density = (double)r->tuples / (double)r->pages; | ||
s->ntup = clamp_row_est(density * (double)pages); | ||
break; | ||
} | ||
for(; NULL == r || r->tuples < 0 || r->pages <= 0;){ | ||
int w = | ||
MAXALIGN(r->reltarget->width) + MAXALIGN(SizeofHeapTupleHeader); | ||
s->ntup = clamp_row_est((double) BLCKSZ * 10.0 / (double)w); | ||
break; | ||
} | ||
double nrow = s->ntup * clauselist_selectivity( | ||
p, | ||
r->baserestrictinfo, | ||
0, | ||
JOIN_INNER, | ||
NULL | ||
); | ||
return clamp_row_est(nrow); | ||
} | ||
|
||
static void dir_fdw_relsize(PlannerInfo* p, RelOptInfo* r, Oid o){ | ||
DirFdwPlanState* s = palloc(sizeof(DirFdwPlanState)); | ||
r->fdw_private = s; | ||
dir_fdw_options(o, &s->options); | ||
r->rows = dir_fdw_estimate(p, r, s); | ||
switch(NULL == s->options.dirname){ | ||
case false: return; | ||
default: | ||
elog(ERROR, "invalid dirname."); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
static void dir_fdw_rescan(ForeignScanState* f){ | ||
DirFdwExecState* e = f->fdw_state; | ||
switch(NULL == e->d){ | ||
case true: return; | ||
default: break; | ||
} | ||
rewinddir(e->d); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
typedef struct DirFdwPlanState { | ||
DirFdwOption options; | ||
|
||
BlockNumber pages; | ||
double ntup; | ||
} DirFdwPlanState; | ||
|
||
typedef struct DirFdwExecState { | ||
const char* dirname; | ||
int fd; | ||
DIR* d; | ||
} DirFdwExecState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters