Skip to content

Commit a147bd3

Browse files
committed
replace gsGetRowFieldGeneral() as gsGetRowFieldAsXXX()
1 parent cda5ba7 commit a147bd3

File tree

3 files changed

+262
-37
lines changed

3 files changed

+262
-37
lines changed

src/Store.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,21 @@ namespace griddb {
155155
* muti_get method. Using gsGetMultipleContainerRows C-API
156156
*/
157157
void Store::multi_get(const GSRowKeyPredicateEntry* const * predicateList,
158-
size_t predicateCount, GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList) {
158+
size_t predicateCount, GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList, GSType*** typeList) {
159159
// get number of column of rows in each container.
160160
*colNumList = new int[predicateCount]; //will be free in argout
161-
for (int i = 0; i < predicateCount; i++) {
161+
*typeList = new GSType*[predicateCount]; //will be free in argout
162+
int length = (int)predicateCount;
163+
for (int i = 0; i < length; i++) {
162164
Container *tmpContainer = this->get_container((*predicateList)[i].containerName);
163165
if (tmpContainer == NULL) {
164166
throw GSException(mStore, "Not found container");
165167
}
166168
(*colNumList)[i] = tmpContainer->getColumnCount();
169+
(*typeList)[i] = (GSType*) malloc(sizeof(GSType) * (*colNumList)[i]);
170+
for (int j = 0; j < (*colNumList)[i]; j++) {
171+
(*typeList)[i][j] = tmpContainer->getGSTypeList()[j];
172+
}
167173
delete tmpContainer;
168174
}
169175
// Get data for entryList

src/Store.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Store {
4646
void multi_put(GSRow*** listRow, const int *listRowContainerCount,
4747
const char ** listContainerName, size_t containerCount);
4848
void multi_get(const GSRowKeyPredicateEntry* const * predicateList,
49-
size_t predicateCount, GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList);
49+
size_t predicateCount, GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList, GSType*** typeList);
5050

5151
ContainerInfo* get_container_info(const char *name);
5252
PartitionController* partition_info();

src/gstype_python.i

+253-34
Original file line numberDiff line numberDiff line change
@@ -1577,26 +1577,234 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
15771577
$1 = NULL;
15781578
}
15791579

1580-
%typemap(argout, fragment = "convertFieldToObject") (GSRow *rowdata) {
1580+
/**
1581+
* Support convert data from GSRow* row to Python list
1582+
*/
1583+
%fragment("getRowFields", "header",
1584+
fragment = "convertStrToObj", fragment = "convertTimestampToObject") {
1585+
static bool getRowFields(GSRow *row, int columnCount, GSType* typeList, bool timestampOutput, int* columnError, GSType* fieldTypeError, PyObject* outList) {
1586+
GSResult ret;
1587+
GSValue mValue;
1588+
bool retVal = true;
1589+
for (int i = 0; i < columnCount; i++) {
1590+
//Check NULL value
1591+
GSBool nullValue;
1592+
%#if GS_COMPATIBILITY_SUPPORT_3_5
1593+
ret = gsGetRowFieldNull(row, (int32_t) i, &nullValue);
1594+
if (ret != GS_RESULT_OK) {
1595+
*columnError = i;
1596+
retVal = false;
1597+
*fieldTypeError = GS_TYPE_NULL;
1598+
return retVal;
1599+
}
1600+
if (nullValue) {
1601+
Py_INCREF(Py_None);
1602+
PyList_SetItem(outList, i, Py_None);
1603+
continue;
1604+
}
1605+
%#endif
1606+
switch(typeList[i]) {
1607+
case GS_TYPE_LONG: {
1608+
int64_t longValue;
1609+
ret = gsGetRowFieldAsLong(row, (int32_t) i, &longValue);
1610+
PyList_SetItem(outList, i, PyLong_FromLong(longValue));
1611+
break;
1612+
}
1613+
case GS_TYPE_STRING: {
1614+
GSChar* stringValue;
1615+
ret = gsGetRowFieldAsString(row, (int32_t) i, (const GSChar **)&stringValue);
1616+
PyList_SetItem(outList, i, convertStrToObj(stringValue));
1617+
break;
1618+
}
1619+
case GS_TYPE_BLOB: {
1620+
GSBlob blobValue;
1621+
ret = gsGetRowFieldAsBlob(row, (int32_t) i, &blobValue);
1622+
PyList_SetItem(outList, i, PyByteArray_FromStringAndSize((const char*)blobValue.data, blobValue.size));
1623+
break;
1624+
}
1625+
case GS_TYPE_BOOL: {
1626+
GSBool boolValue;
1627+
ret = gsGetRowFieldAsBool(row, (int32_t) i, &boolValue);
1628+
PyList_SetItem(outList, i, PyBool_FromLong(boolValue));
1629+
break;
1630+
}
1631+
case GS_TYPE_INTEGER: {
1632+
int32_t intValue;
1633+
ret = gsGetRowFieldAsInteger(row, (int32_t) i, &intValue);
1634+
PyList_SetItem(outList, i, PyInt_FromLong(intValue));
1635+
break;
1636+
}
1637+
case GS_TYPE_FLOAT: {
1638+
float floatValue;
1639+
ret = gsGetRowFieldAsFloat(row, (int32_t) i, &floatValue);
1640+
PyList_SetItem(outList, i, PyFloat_FromDouble(floatValue));
1641+
break;
1642+
}
1643+
case GS_TYPE_DOUBLE: {
1644+
double doubleValue;
1645+
ret = gsGetRowFieldAsDouble(row, (int32_t) i, &doubleValue);
1646+
PyList_SetItem(outList, i, PyFloat_FromDouble(doubleValue));
1647+
break;
1648+
}
1649+
case GS_TYPE_TIMESTAMP: {
1650+
GSTimestamp timestampValue;
1651+
ret = gsGetRowFieldAsTimestamp(row, (int32_t) i, &timestampValue);
1652+
PyList_SetItem(outList, i, convertTimestampToObject(&timestampValue, timestampOutput));
1653+
break;
1654+
}
1655+
case GS_TYPE_BYTE: {
1656+
int8_t byteValue;
1657+
ret = gsGetRowFieldAsByte(row, (int32_t) i, &byteValue);
1658+
PyList_SetItem(outList, i, PyInt_FromLong(byteValue));
1659+
break;
1660+
}
1661+
case GS_TYPE_SHORT: {
1662+
int16_t shortValue;
1663+
ret = gsGetRowFieldAsShort(row, (int32_t) i, &shortValue);
1664+
PyList_SetItem(outList, i, PyInt_FromLong(shortValue));
1665+
break;
1666+
}
1667+
case GS_TYPE_GEOMETRY: {
1668+
GSChar* geoValue;
1669+
ret = gsGetRowFieldAsGeometry(row, (int32_t) i, (const GSChar **)&geoValue);
1670+
PyList_SetItem(outList, i, convertStrToObj(geoValue));
1671+
break;
1672+
}
1673+
case GS_TYPE_INTEGER_ARRAY: {
1674+
int32_t* intArr;
1675+
size_t size;
1676+
ret = gsGetRowFieldAsIntegerArray (row, (int32_t) i, (const int32_t **)&intArr, &size);
1677+
PyObject* list = PyList_New(size);
1678+
for (int j = 0; j < size; j++) {
1679+
PyList_SetItem(list, j, PyInt_FromLong(intArr[j]));
1680+
}
1681+
PyList_SetItem(outList, i, list);
1682+
break;
1683+
}
1684+
case GS_TYPE_STRING_ARRAY: {
1685+
GSChar** stringArrVal;
1686+
size_t size;
1687+
ret = gsGetRowFieldAsStringArray (row, (int32_t) i, ( const GSChar *const **)&stringArrVal, &size);
1688+
PyObject* list = PyList_New(size);
1689+
for (int j = 0; j < size; j++) {
1690+
PyList_SetItem(list, j, convertStrToObj(stringArrVal[j]));
1691+
}
1692+
PyList_SetItem(outList, i, list);
1693+
break;
1694+
}
1695+
case GS_TYPE_BOOL_ARRAY: {
1696+
GSBool* boolArr;
1697+
size_t size;
1698+
ret = gsGetRowFieldAsBoolArray(row, (int32_t) i, (const GSBool **)&boolArr, &size);
1699+
PyObject* list = PyList_New(size);
1700+
for (int j = 0; j < size; j++) {
1701+
PyList_SetItem(list, j, PyBool_FromLong(boolArr[j]));
1702+
}
1703+
PyList_SetItem(outList, i, list);
1704+
break;
1705+
}
1706+
case GS_TYPE_BYTE_ARRAY: {
1707+
int8_t* byteArr;
1708+
size_t size;
1709+
ret = gsGetRowFieldAsByteArray(row, (int32_t) i, (const int8_t **)&byteArr, &size);
1710+
PyObject* list = PyList_New(size);
1711+
for (int j = 0; j < size; j++) {
1712+
PyList_SetItem(list, j, PyInt_FromLong(byteArr[j]));
1713+
}
1714+
PyList_SetItem(outList, i, list);
1715+
break;
1716+
}
1717+
case GS_TYPE_SHORT_ARRAY: {
1718+
int16_t* shortArr;
1719+
size_t size;
1720+
ret = gsGetRowFieldAsShortArray(row, (int32_t) i, (const int16_t **)&shortArr, &size);
1721+
PyObject* list = PyList_New(size);
1722+
for (int j = 0; j < size; j++) {
1723+
PyList_SetItem(list, j, PyInt_FromLong(shortArr[j]));
1724+
}
1725+
PyList_SetItem(outList, i, list);
1726+
break;
1727+
}
1728+
case GS_TYPE_LONG_ARRAY: {
1729+
int64_t* longArr;
1730+
size_t size;
1731+
ret = gsGetRowFieldAsLongArray(row, (int32_t) i, (const int64_t **)&longArr, &size);
1732+
PyObject* list = PyList_New(size);
1733+
for (int j = 0; j < size; j++) {
1734+
PyList_SetItem(list, j, PyLong_FromLong(longArr[j]));
1735+
}
1736+
PyList_SetItem(outList, i, list);
1737+
break;
1738+
}
1739+
case GS_TYPE_FLOAT_ARRAY: {
1740+
float* floatArr;
1741+
size_t size;
1742+
ret = gsGetRowFieldAsFloatArray(row, (int32_t) i, (const float **)&floatArr, &size);
1743+
PyObject* list = PyList_New(size);
1744+
for (int j = 0; j < size; j++) {
1745+
PyList_SetItem(list, j, PyFloat_FromDouble(static_cast<double>(floatArr[j])));
1746+
}
1747+
PyList_SetItem(outList, i, list);
1748+
break;
1749+
}
1750+
case GS_TYPE_DOUBLE_ARRAY: {
1751+
double* doubleArr;
1752+
size_t size;
1753+
ret = gsGetRowFieldAsDoubleArray(row, (int32_t) i, (const double **)&doubleArr, &size);
1754+
PyObject* list = PyList_New(size);
1755+
for (int j = 0; j < size; j++) {
1756+
PyList_SetItem(list, j, PyFloat_FromDouble(doubleArr[j]));
1757+
}
1758+
PyList_SetItem(outList, i, list);
1759+
break;
1760+
}
1761+
case GS_TYPE_TIMESTAMP_ARRAY: {
1762+
GSTimestamp* timestampArr;
1763+
size_t size;
1764+
ret = gsGetRowFieldAsTimestampArray(row, (int32_t) i, (const GSTimestamp **)&timestampArr, &size);
1765+
PyObject* list = PyList_New(size);
1766+
for (int j = 0; j < size; j++) {
1767+
PyList_SetItem(list, j, convertTimestampToObject(&timestampArr[j], timestampOutput));
1768+
}
1769+
PyList_SetItem(outList, i, list);
1770+
break;
1771+
}
1772+
default: {
1773+
// NOT OK
1774+
ret = -1;
1775+
break;
1776+
}
1777+
}
1778+
if (ret != GS_RESULT_OK) {
1779+
*columnError = i;
1780+
*fieldTypeError = typeList[i];
1781+
retVal = false;
1782+
return retVal;
1783+
}
1784+
}
1785+
return retVal;
1786+
}
1787+
}
1788+
1789+
%typemap(argout, fragment = "getRowFields") (GSRow *rowdata) {
15811790
GSRow* row = arg1->getGSRowPtr();
15821791
PyObject *outList = PyList_New(arg1->getColumnCount());
15831792
if (outList == NULL) {
15841793
PyErr_SetString(PyExc_ValueError, "Memory allocation for row is error");
15851794
SWIG_fail;
15861795
}
1587-
15881796
GSValue mValue;
15891797
GSType mType;
15901798
GSResult ret;
1591-
for (int i = 0; i < arg1->getColumnCount(); i++) {
1592-
ret = gsGetRowFieldGeneral(row, i, &mValue, &mType);
1593-
if (ret != GS_RESULT_OK) {
1594-
char errorMsg[60];
1595-
sprintf(errorMsg, "Can't get data for field %d", i);
1596-
PyErr_SetString(PyExc_ValueError, errorMsg);
1597-
SWIG_fail;
1598-
}
1599-
PyList_SetItem(outList, i, convertFieldToObject(&mValue, mType, arg1->timestamp_output_with_float));
1799+
bool retVal;
1800+
int errorColumn;
1801+
GSType errorType;
1802+
retVal = getRowFields(row, arg1->getColumnCount(), arg1->getGSTypeList(),arg1->timestamp_output_with_float, &errorColumn, &errorType, outList);
1803+
if (retVal == false) {
1804+
char errorMsg[60];
1805+
sprintf(errorMsg, "Can't get data for field %d with type%d", errorColumn, errorType);
1806+
PyErr_SetString(PyExc_ValueError, errorMsg);
1807+
SWIG_fail;
16001808
}
16011809
$result = outList;
16021810
}
@@ -1840,20 +2048,24 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
18402048
/**
18412049
* Typemaps output for Store.multi_get() function
18422050
*/
1843-
%typemap(in, numinputs = 0) (GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList)
1844-
(GSContainerRowEntry *tmpEntryList, size_t tmpContainerCount, int *tmpcolNumList) {
2051+
%typemap(in, numinputs = 0) (GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList, GSType*** typeList)
2052+
(GSContainerRowEntry *tmpEntryList, size_t tmpContainerCount, int *tmpcolNumList, GSType** tmpTypeList) {
18452053
$1 = &tmpEntryList;
18462054
$2 = &tmpContainerCount;
18472055
$3 = &tmpcolNumList;
2056+
$4 = &tmpTypeList;
18482057
}
18492058

1850-
%typemap(argout, numinputs = 0, fragment = "convertStrToObj", fragment = "convertFieldToObject") (GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList) () {
2059+
%typemap(argout, numinputs = 0, fragment = "convertStrToObj", fragment = "getRowFields") (GSContainerRowEntry **entryList, size_t* containerCount, int **colNumList, GSType*** typeList) () {
18512060
PyObject* dict = PyDict_New();
18522061
griddb::Container *tmpContainer;
18532062
GSRow* row;
18542063
GSValue mValue;
18552064
GSType mType;
18562065
GSResult ret;
2066+
bool retVal;
2067+
int errorColumn;
2068+
GSType errorType;
18572069
for (int i = 0; i < *$2; i++) {
18582070
PyObject* key = convertStrToObj((*$1)[i].containerName);
18592071
PyObject* list = PyList_New((*$1)[i].rowCount);
@@ -1864,15 +2076,12 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
18642076
PyErr_SetString(PyExc_ValueError, "Memory allocation for row is error");
18652077
SWIG_fail;
18662078
}
1867-
for (int k = 0; k < (*$3)[i]; k++) {
1868-
ret = gsGetRowFieldGeneral(row, k, &mValue, &mType);
1869-
if (ret != GS_RESULT_OK) {
1870-
char errorMsg[60];
1871-
sprintf(errorMsg, "Can't get data for field %d", i);
1872-
PyErr_SetString(PyExc_ValueError, errorMsg);
1873-
SWIG_fail;
1874-
}
1875-
PyList_SetItem(outList, k, convertFieldToObject(&mValue, mType, arg1->timestamp_output_with_float));
2079+
retVal = getRowFields(row, (*$3)[i], (*$4)[i], arg1->timestamp_output_with_float, &errorColumn, &errorType, outList);
2080+
if (retVal == false) {
2081+
char errorMsg[60];
2082+
sprintf(errorMsg, "Can't get data for field %d with type %d", errorColumn, errorType);
2083+
PyErr_SetString(PyExc_ValueError, errorMsg);
2084+
SWIG_fail;
18762085
}
18772086
PyList_SetItem(list, j, outList);
18782087
}
@@ -1882,7 +2091,17 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
18822091
Py_DECREF(key);
18832092
Py_DECREF(list);
18842093
}
1885-
delete (*$3);
2094+
if (*$4) {
2095+
for (int j = 0; j < *$2;j++) {
2096+
if ((*$4)[j]) {
2097+
free ((void*) (*$4)[j]);
2098+
}
2099+
}
2100+
delete (*$4);
2101+
}
2102+
if (*$3) {
2103+
delete (*$3);
2104+
}
18862105
$result = dict;
18872106
}
18882107

@@ -2207,7 +2426,7 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
22072426
$4 = &aggResultTmp;
22082427
}
22092428

2210-
%typemap(argout, fragment = "convertFieldToObject") (GSRowSetType* type, bool* hasNextRow,
2429+
%typemap(argout, fragment = "getRowFields") (GSRowSetType* type, bool* hasNextRow,
22112430
griddb::QueryAnalysisEntry** queryAnalysis, griddb::AggregationResult** aggResult) {
22122431

22132432
PyObject *resultobj;
@@ -2217,6 +2436,8 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
22172436
GSValue mValue;
22182437
GSType mType;
22192438
GSResult ret;
2439+
bool retVal;
2440+
int errorColumn;
22202441
switch (*$1) {
22212442
case (GS_ROW_SET_CONTAINER_ROWS):
22222443
if (*$2 == false) {
@@ -2229,15 +2450,13 @@ static GSChar** convertObjectToStringArray(PyObject* value, size_t* size) {
22292450
PyErr_SetString(PyExc_ValueError, "Memory allocation for row is error");
22302451
SWIG_fail;
22312452
}
2232-
for (int i = 0; i < arg1->getColumnCount(); i++) {
2233-
ret = gsGetRowFieldGeneral(row, i, &mValue, &mType);
2234-
if (ret != GS_RESULT_OK) {
2235-
char errorMsg[60];
2236-
sprintf(errorMsg, "Can't get data for field %d", i);
2237-
PyErr_SetString(PyExc_ValueError, errorMsg);
2238-
SWIG_fail;
2239-
}
2240-
PyList_SetItem(outList, i, convertFieldToObject(&mValue, mType, arg1->timestamp_output_with_float));
2453+
GSType errorType;
2454+
retVal = getRowFields(row, arg1->getColumnCount(), arg1->getGSTypeList(),arg1->timestamp_output_with_float, &errorColumn, &errorType, outList);
2455+
if (retVal == false) {
2456+
char errorMsg[60];
2457+
sprintf(errorMsg, "Can't get data for field %d with type%d", errorColumn, errorType);
2458+
PyErr_SetString(PyExc_ValueError, errorMsg);
2459+
SWIG_fail;
22412460
}
22422461
$result = outList;
22432462
}

0 commit comments

Comments
 (0)