// File: /workspace/github_c_makefile_star2_O3/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, %rcx # imm = 0x7FFFFFFFFFFFFFFF
cmpq %rcx, 0xb8(%rbx)
jne 0x44fbe
addq $0x8, %rbx
leaq 0x2f688(%rip), %rsi     # 0x74631
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3a376
movl $0xfffffff9, %r13d      # imm = 0xFFFFFFF9
jmp 0x45180
movq 0xd8(%rdi), %rcx
movq 0x10(%rcx), %r12
movq (%r12), %rcx
movq 0x8(%rcx), %rcx
cmpq $0x0, 0x38(%rcx)
je 0x4514b
movq %rsi, %r14
movq %rcx, -0x38(%rbp)
movl $0x0, 0x78(%rbx)
movl 0x80(%rbx), %ecx
testb $0x4, %cl
je 0x4500d
movq $0x0, 0x70(%rbx)
movl $0x0, 0x7c(%rbx)
andl $-0x5, %ecx
movl %ecx, 0x80(%rbx)
leaq 0x68(%rbx), %r13
testb $0x40, 0x8(%r14)
je 0x4507f
movq (%r14), %rcx
testb $0x1, 0x40(%rcx)
je 0x4507f
movq 0xe0(%rax), %rax
movq 0xa8(%rbx), %rcx
xorps %xmm0, %xmm0
leaq -0x78(%rbp), %r15
movups %xmm0, 0x8(%r15)
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movq $0x0, 0x38(%r15)
movq %rax, 0x10(%r15)
movups %xmm0, 0x20(%r15)
movq %rax, 0x18(%r15)
movq %rcx, (%r15)
movl $0x2, 0x8(%r15)
leaq 0x2f641(%rip), %rsi     # 0x746ad
movq %r14, %rdi
movq %r15, %rdx
callq 0x30ad3
movq %r15, %rdi
callq 0x30465
leaq 0x8(%rbx), %rdx
movq 0xa8(%rbx), %rcx
leaq 0x2f047(%rip), %rsi     # 0x740d8
movq %r13, %rdi
movq %rdx, -0x30(%rbp)
xorl %eax, %eax
callq 0x3e26d
movl 0x78(%rbx), %r15d
testq %r15, %r15
je 0x45168
movq %r14, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x3de22
movl %eax, %r13d
testl %eax, %eax
jne 0x45180
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 0x4512e
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4066b
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 0x3da9b
testl %eax, %eax
je 0x45180
movl %eax, %r13d
movq (%rbx), %rax
movq (%rax), %rdi
leaq 0x2f574(%rip), %rsi     # 0x746b2
movq -0x30(%rbp), %rdx
xorl %eax, %eax
callq 0x3a376
jmp 0x45180
addq $0x8, %rbx
leaq 0x2f502(%rip), %rsi     # 0x74658
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3a376
movl $0xffffffb5, %r13d      # imm = 0xFFFFFFB5
jmp 0x45180
movq (%rbx), %rax
movq (%rax), %rdi
leaq 0x2e861(%rip), %rsi     # 0x739d6
callq 0x31537
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq