iwrc iwp_flock(HANDLE fd, iwp_lockmode lmode) { assert(!INVALIDHANDLE(fd)); if (lmode == IWP_NOLOCK) { return 0; } struct flock lock = { .l_type = (lmode & IWP_WLOCK) ? F_WRLCK : F_RDLCK, .l_whence = SEEK_SET }; while (fcntl(fd, (lmode & IWP_NBLOCK) ? F_SETLK : F_SETLKW, &lock) == -1) { if (errno != EINTR) { return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); } } return 0; } iwrc iwp_unlock(HANDLE fd) { assert(!INVALIDHANDLE(fd)); struct flock lock = { .l_type = F_UNLCK, .l_whence = SEEK_SET }; while (fcntl(fd, F_SETLKW, &lock) == -1) { if (errno != EINTR) { return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); } } return 0; }
iwrc iwp_closefh(HANDLE fh) { if (INVALIDHANDLE(fh)) { return 0; } if (close(fh) == -1) { return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); } return 0; }
iwrc iwp_sleep(uint64_t ms) { iwrc rc = 0; struct timespec req; req.tv_sec = ms / 1000UL; req.tv_nsec = (ms % 1000UL) * 1000UL * 1000UL; if (nanosleep(&req, NULL)) { rc = iwrc_set_errno(IW_ERROR_THREADING_ERRNO, errno); } return rc; }
iwrc iwp_write(HANDLE fh, off_t off, const void *buf, size_t siz, size_t *sp) { assert(buf && sp); ssize_t ws = pwrite(fh, buf, siz, off); if (ws == -1) { *sp = 0; return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); } else { *sp = ws; return 0; } }
iwrc iwp_read(HANDLE fh, off_t off, void *buf, size_t siz, size_t *sp) { assert(buf && sp); ssize_t rs = pread(fh, buf, siz, off); if (rs == -1) { *sp = 0; return iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); } else { *sp = rs; return 0; } }
static iwrc jbn_init_pctx(JNIEnv *env, JBN_JSPRINT_CTX *pctx, jobject thisObj, jobject osObj) { memset(pctx, 0, sizeof(*pctx)); iwrc rc = 0; jclass osClazz = (*env)->GetObjectClass(env, osObj); jmethodID writeMid = (*env)->GetMethodID(env, osClazz, "write", "([B)V"); IWXSTR *xstr = iwxstr_new(); if (!xstr) { return iwrc_set_errno(rc, IW_ERROR_ALLOC); } pctx->xstr = xstr; pctx->flush_buffer_sz = JBN_JSON_FLUSH_BUFFER_SZ; pctx->env = env; pctx->osClazz = osClazz; pctx->osObj = osObj; pctx->write_mid = writeMid; pctx->flushFn = jbn_flush_to_stream; return rc; }
static iwrc jbn_exec_visitor(struct _EJDB_EXEC *ux, EJDB_DOC doc, int64_t *step) { iwrc rc = 0; jstring json = 0; JBN_EXEC_CTX *ectx = ux->opaque; JNIEnv *env = ectx->env; IWXSTR *xstr = iwxstr_new2(jbl_size(doc->raw) * 2); if (!xstr) { return iwrc_set_errno(IW_ERROR_ALLOC, errno); } if (doc->node) { rc = jbl_node_as_json(doc->node, jbl_xstr_json_printer, xstr, 0); } else { rc = jbl_as_json(doc->raw, jbl_xstr_json_printer, xstr, 0); } RCGO(rc, finish); json = (*env)->NewStringUTF(env, iwxstr_ptr(xstr)); if (!json) { if (!(*env)->ExceptionOccurred(env)) { rc = JBN_ERROR_CREATION_OBJ; } goto finish; } int64_t llv = (*env)->CallLongMethod(env, ectx->cbObj, ectx->cbMid, (jlong) doc->id, json); if (llv < -2) { *step = 0; } else { *step = llv; } finish: if (json) { (*env)->DeleteLocalRef(env, json); } if (xstr) { iwxstr_destroy(xstr); } return rc; }
// JQL EXECUTE JNIEXPORT void JNICALL Java_com_softmotions_ejdb2_JQL__1execute(JNIEnv *env, jobject thisObj, jobject dbObj, jobject cbObj, jobject logStreamObj) { iwrc rc; EJDB db; JQL q; IWXSTR *log = 0; if (!dbObj) { jbn_throw_rc_exception(env, IW_ERROR_INVALID_ARGS, 0); return; } rc = jbn_jql_q(env, thisObj, &q); RCGO(rc, finish); rc = jbn_db(env, dbObj, &db); RCGO(rc, finish); JBN_EXEC_CTX ectx = { .env = env, .cbObj = cbObj }; if (cbObj) { ectx.cbClazz = (*env)->GetObjectClass(env, cbObj); ectx.cbMid = (*env)->GetMethodID(env, ectx.cbClazz, "onRecord", "(JLjava/lang/String;)J"); if (!ectx.cbMid) { goto finish; } } jlong skip = (*env)->GetLongField(env, thisObj, k_JQL_skip_fid); jlong limit = (*env)->GetLongField(env, thisObj, k_JQL_limit_fid); if (logStreamObj) { log = iwxstr_new(); if (!log) { rc = iwrc_set_errno(IW_ERROR_ALLOC, errno); goto finish; } } EJDB_EXEC ux = { .db = db, .q = q, .skip = skip > 0 ? skip : 0, .limit = limit > 0 ? limit : 0, .opaque = &ectx, .visitor = cbObj ? jbn_exec_visitor : 0, .log = log }; rc = ejdb_exec(&ux); RCGO(rc, finish); if (log) { // Send query execution log size_t xsz = iwxstr_size(log); jclass logStreamClazz = (*env)->GetObjectClass(env, logStreamObj); jmethodID writeMid = (*env)->GetMethodID(env, logStreamClazz, "write", "([B)V"); if (!writeMid) goto finish; jbyteArray arr = (*env)->NewByteArray(env, xsz); if (!arr) goto finish; (*env)->SetByteArrayRegion(env, arr, 0, xsz, (void *) iwxstr_ptr(log)); (*env)->CallVoidMethod(env, logStreamObj, writeMid, arr); } finish: if (log) { iwxstr_destroy(log); } if (rc) { jbn_throw_rc_exception(env, rc, 0); } } // JQL EXECUTE SCALAR LONG JNIEXPORT jlong JNICALL Java_com_softmotions_ejdb2_JQL__1execute_1scalar_1long(JNIEnv *env, jobject thisObj, jobject dbObj, jobject logStreamObj) { iwrc rc; EJDB db; JQL q; IWXSTR *log = 0; jlong ret = 0; if (!dbObj) { jbn_throw_rc_exception(env, IW_ERROR_INVALID_ARGS, 0); return 0; } rc = jbn_jql_q(env, thisObj, &q); RCGO(rc, finish); rc = jbn_db(env, dbObj, &db); RCGO(rc, finish); jlong skip = (*env)->GetLongField(env, thisObj, k_JQL_skip_fid); jlong limit = (*env)->GetLongField(env, thisObj, k_JQL_limit_fid); if (logStreamObj) { log = iwxstr_new(); if (!log) { rc = iwrc_set_errno(IW_ERROR_ALLOC, errno); goto finish; } } EJDB_EXEC ux = { .db = db, .q = q, .skip = skip > 0 ? skip : 0, .limit = limit > 0 ? limit : 0, .log = log }; rc = ejdb_exec(&ux); RCGO(rc, finish); if (log) { // Send query execution log size_t xsz = iwxstr_size(log); jclass logStreamClazz = (*env)->GetObjectClass(env, logStreamObj); jmethodID writeMid = (*env)->GetMethodID(env, logStreamClazz, "write", "([B)V"); if (!writeMid) goto finish; jbyteArray arr = (*env)->NewByteArray(env, xsz); if (!arr) goto finish; (*env)->SetByteArrayRegion(env, arr, 0, xsz, (void *) iwxstr_ptr(log)); (*env)->CallVoidMethod(env, logStreamObj, writeMid, arr); } ret = ux.cnt; finish: if (log) { iwxstr_destroy(log); } if (rc) { jbn_throw_rc_exception(env, rc, 0); } return ret; } static void jbn_free_json_node(void *ptr, void *op) { IWPOOL *pool = op; if (pool) iwpool_destroy(pool); }
iwrc iwp_ftruncate(HANDLE fh, off_t len) { int rv = ftruncate(fh, len); return !rv ? 0 : iwrc_set_errno(IW_ERROR_IO_ERRNO, errno); }