// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionStore
static int CollectionStore(
	unqlite_col *pCol, /* Target collection */
	jx9_value *pValue  /* JSON value to be stored */
	)
{
	SyBlob *pWorker = &pCol->sWorker;
	unqlite_kv_methods *pMethods;
	unqlite_kv_engine *pEngine;
	sxu32 nKeyLen;
	int rc;	
	/* Point to the underlying KV store */
	pEngine = unqlitePagerGetKvEngine(pCol->pVm->pDb);
	pMethods = pEngine->pIo->pMethods;
	if( pCol->nTotRec >= SXI64_HIGH ){
		/* Collection limit reached. No more records */
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Collection '%z': Records limit reached",
				&pCol->sName
			);
		return UNQLITE_LIMIT;
	}
	if( pMethods->xReplace == 0 ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"Cannot store record into collection '%z' due to a read-only Key/Value storage engine",
				&pCol->sName
			);
		return UNQLITE_READ_ONLY;
	}
	/* Reset the working buffer */
	SyBlobReset(pWorker);
	if( jx9_value_is_json_object(pValue) ){
		jx9_value sId;
		/* If the given type is a JSON object, then add the special __id field */
		jx9MemObjInitFromInt(pCol->pVm->pJx9Vm,&sId,pCol->nLastid);
		jx9_array_add_strkey_elem(pValue,"__id",&sId);
		jx9MemObjRelease(&sId);
	}
	/* Prepare the unique ID for this record */
	SyBlobFormat(pWorker,"%z_%qd",&pCol->sName,pCol->nLastid);
	nKeyLen = SyBlobLength(pWorker);
	if( nKeyLen < 1 ){
		unqliteGenOutofMem(pCol->pVm->pDb);
		return UNQLITE_NOMEM;
	}
	/* Turn to FastJson */
	rc = FastJsonEncode(pValue,pWorker,0);
	if( rc != UNQLITE_OK ){
		return rc;
	}
	/* Finally perform the insertion */
	rc = pMethods->xReplace(
		pEngine,
		SyBlobData(pWorker),nKeyLen,
		SyBlobDataAt(pWorker,nKeyLen),SyBlobLength(pWorker)-nKeyLen
		);
	if( rc == UNQLITE_OK ){
		/* Save the value in the cache */
		CollectionCacheInstallRecord(pCol,pCol->nLastid,pValue);
		/* Increment the unique __id */
		pCol->nLastid++;
		pCol->nTotRec++;
		/* Reflect the change */
		rc = CollectionSetHeader(0,pCol,pCol->nLastid,pCol->nTotRec,0);
	}
	if( rc != UNQLITE_OK ){
		unqliteGenErrorFormat(pCol->pVm->pDb,
				"IO error while storing record into collection '%z'",
				&pCol->sName
			);
		return rc;
	}
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
movq (%rax), %rdi
movabsq $0x7fffffffffffffff, %rax # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rax, 0xb8(%rbx)
jne 0x3bf4f
addq $0x8, %rbx
leaq 0x286d3(%rip), %rsi     # 0x64611
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32836
pushq $-0x7
jmp 0x3c0b9
movq 0xd8(%rdi), %rax
movq 0x10(%rax), %r12
movq (%r12), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x38(%rax)
je 0x3c093
movq %rsi, %r14
movq %rax, -0x38(%rbp)
leaq 0x68(%rbx), %r13
movq %r13, %rdi
callq 0x31b1e
testb $0x40, 0x8(%r14)
je 0x3bfc7
movq (%r14), %rax
testb $0x1, 0x40(%rax)
je 0x3bfc7
movq (%rbx), %rax
movq 0xa8(%rbx), %rdx
movq 0xe0(%rax), %rdi
leaq -0x78(%rbp), %r15
movq %r15, %rsi
callq 0x3c0cd
leaq 0x286d9(%rip), %rsi     # 0x6468d
movq %r14, %rdi
movq %r15, %rdx
callq 0x29c6c
movq %r15, %rdi
callq 0x296af
leaq 0x8(%rbx), %rdx
movq 0xa8(%rbx), %rcx
leaq 0x280df(%rip), %rsi     # 0x640b8
movq %r13, %rdi
movq %rdx, -0x30(%rbp)
xorl %eax, %eax
callq 0x35d7b
movl 0x78(%rbx), %r15d
testq %r15, %r15
je 0x3c0ac
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x359ef
movl %eax, %r13d
testl %eax, %eax
jne 0x3c0bb
movq 0x70(%rbx), %rsi
movq %r15, %rcx
addq %rsi, %rcx
movl 0x78(%rbx), %r8d
subl %r15d, %r8d
movq %r12, %rdi
movl %r15d, %edx
movq -0x38(%rbp), %rax
callq *0x38(%rax)
testl %eax, %eax
jne 0x3c076
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x37fe5
movq 0xa8(%rbx), %rdx
movq 0xb8(%rbx), %rcx
incq %rdx
movq %rdx, 0xa8(%rbx)
incq %rcx
movq %rcx, 0xb8(%rbx)
xorl %r13d, %r13d
xorl %edi, %edi
movq %rbx, %rsi
xorl %r8d, %r8d
callq 0x357a9
testl %eax, %eax
je 0x3c0bb
movl %eax, %r13d
movq (%rbx), %rax
movq (%rax), %rdi
leaq 0x2860c(%rip), %rsi     # 0x64692
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x32836
jmp 0x3c0bb
addq $0x8, %rbx
leaq 0x2859a(%rip), %rsi     # 0x64638
movq %rbx, %rdx
xorl %eax, %eax
callq 0x32836
pushq $-0x4b
jmp 0x3c0b9
movq (%rbx), %rax
movq (%rax), %rdi
callq 0x32825
pushq $-0x1
popq %r13
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq