How to use serialize method of db Package

Best Syzkaller code snippet using db.serialize

accessors_chain.go

Source:accessors_chain.go Github

copy

Full Screen

...4 "encoding/binary"5 "fmt"6 "math/big"7 "github.com/QuarkChain/goquarkchain/core/types"8 "github.com/QuarkChain/goquarkchain/serialize"9 "github.com/ethereum/go-ethereum/common"10 "github.com/ethereum/go-ethereum/log"11 "github.com/ethereum/go-ethereum/rlp"12)13const DBLOG = "db-operation"14type minorBlockHeaders struct {15 Headers []*types.MinorBlockHeader `bytesizeofslicelen:"4"`16}17type HashList struct {18 HList []common.Hash `bytesizeofslicelen:"4"`19}20// ReadCanonicalHash retrieves the hash assigned to a canonical block number.21func ReadCanonicalHash(db DatabaseReader, chainType ChainType, number uint64) common.Hash {22 data, _ := db.Get(headerHashKey(chainType, number))23 if len(data) == 0 {24 return common.Hash{}25 }26 return common.BytesToHash(data)27}28// WriteCanonicalHash stores the hash assigned to a canonical block number.29func WriteCanonicalHash(db DatabaseWriter, chainType ChainType, hash common.Hash, number uint64) {30 if err := db.Put(headerHashKey(chainType, number), hash.Bytes()); err != nil {31 log.Crit("Failed to store number to hash mapping", "err", err)32 }33}34// DeleteCanonicalHash removes the number to hash canonical mapping.35func DeleteCanonicalHash(db DatabaseDeleter, chainType ChainType, number uint64) {36 if err := db.Delete(headerHashKey(chainType, number)); err != nil {37 log.Crit("Failed to delete number to hash mapping", "err", err)38 }39}40// ReadHeaderNumber returns the header number assigned to a hash.41func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 {42 data, _ := db.Get(headerNumberKey(hash))43 if len(data) != 8 {44 return nil45 }46 number := binary.BigEndian.Uint64(data)47 return &number48}49// ReadHeadHeaderHash retrieves the hash of the current canonical head header.50func ReadHeadHeaderHash(db DatabaseReader) common.Hash {51 data, _ := db.Get(headHeaderKey)52 if len(data) == 0 {53 return common.Hash{}54 }55 return common.BytesToHash(data)56}57// WriteHeadHeaderHash stores the hash of the current canonical head header.58func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) {59 if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {60 log.Crit("Failed to store last header's hash", "err", err)61 }62}63// ReadHeadBlockHash retrieves the hash of the current canonical head block.64func ReadHeadBlockHash(db DatabaseReader) common.Hash {65 data, _ := db.Get(headBlockKey)66 if len(data) == 0 {67 return common.Hash{}68 }69 return common.BytesToHash(data)70}71// WriteHeadBlockHash stores the head block's hash.72func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) {73 if err := db.Put(headBlockKey, hash.Bytes()); err != nil {74 log.Crit("Failed to store last block's hash", "err", err)75 }76}77// ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.78func ReadHeadFastBlockHash(db DatabaseReader) common.Hash {79 data, _ := db.Get(headFastBlockKey)80 if len(data) == 0 {81 return common.Hash{}82 }83 return common.BytesToHash(data)84}85// WriteHeadFastBlockHash stores the hash of the current fast-sync head block.86func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) {87 if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil {88 log.Crit("Failed to store last fast block's hash", "err", err)89 }90}91// ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow92// reporting correct numbers across restarts.93func ReadFastTrieProgress(db DatabaseReader) uint64 {94 data, _ := db.Get(fastTrieProgressKey)95 if len(data) == 0 {96 return 097 }98 return new(big.Int).SetBytes(data).Uint64()99}100// WriteFastTrieProgress stores the fast sync trie process counter to support101// retrieving it across restarts.102func WriteFastTrieProgress(db DatabaseWriter, count uint64) {103 if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil {104 log.Crit("Failed to store fast sync trie progress", "err", err)105 }106}107// HasHeader verifies the existence of a block header corresponding to the hash.108func HasHeader(db DatabaseReader, hash common.Hash) bool {109 if has, err := db.Has(headerKey(hash)); !has || err != nil {110 return false111 }112 return true113}114// ReadMinorBlockHeader retrieves the block header corresponding to the hash.115func ReadMinorBlockHeader(db DatabaseReader, hash common.Hash) *types.MinorBlockHeader {116 data, _ := db.Get(headerKey(hash))117 if len(data) == 0 {118 return nil119 }120 header := new(types.MinorBlockHeader)121 if err := serialize.Deserialize(serialize.NewByteBuffer(data), header); err != nil {122 log.Error("Invalid block header Deserialize", "hash", hash, "err", err)123 return nil124 }125 return header126}127func WriteMinorBlockHeader(db DatabaseWriter, header *types.MinorBlockHeader) {128 // Write the hash -> number mapping129 var (130 hash = header.Hash()131 encoded = encodeBlockNumber(header.Number)132 )133 key := headerNumberKey(hash)134 if err := db.Put(key, encoded); err != nil {135 log.Crit("Failed to store hash to number mapping", "err", err)136 }137 // Write the encoded header138 data, err := serialize.SerializeToBytes(header)139 if err != nil {140 log.Crit("Failed to Serialize header", "err", err)141 }142 key = headerKey(hash)143 if err := db.Put(key, data); err != nil {144 log.Crit("Failed to store header", "err", err)145 }146}147// DeleteMinorBlockHeader removes all block header data associated with a hash.148func DeleteMinorBlockHeader(db DatabaseDeleter, hash common.Hash) {149 if err := db.Delete(headerKey(hash)); err != nil {150 log.Crit("Failed to delete header", "err", err)151 }152 if err := db.Delete(headerNumberKey(hash)); err != nil {153 log.Crit("Failed to delete hash to number mapping", "err", err)154 }155}156func ReadLatestMinorBlockHeaders(db DatabaseReader, hash common.Hash) []*types.MinorBlockHeader {157 data, _ := db.Get(latestMHeaderKey(hash))158 if len(data) == 0 {159 return nil160 }161 headers := new(minorBlockHeaders)162 if err := serialize.Deserialize(serialize.NewByteBuffer(data), headers); err != nil {163 log.Error("Invalid block header Deserialize", "hash", hash, "err", err)164 return nil165 }166 return headers.Headers167}168func WriteLatestMinorBlockHeaders(db DatabaseWriter, hash common.Hash, headers []*types.MinorBlockHeader) {169 // Write the encoded header170 hs := minorBlockHeaders{headers}171 data, err := serialize.SerializeToBytes(hs)172 if err != nil {173 log.Crit("Failed to Serialize header", "err", err)174 }175 key := latestMHeaderKey(hash)176 if err := db.Put(key, data); err != nil {177 log.Crit("Failed to store header", "err", err)178 }179}180func DeleteLatestMinorBlockHeaders(db DatabaseDeleter, hash common.Hash) {181 if err := db.Delete(latestMHeaderKey(hash)); err != nil {182 log.Crit("Failed to delete header", "err", err)183 }184}185// ReadRootBlockHeader retrieves the block header corresponding to the hash.186func ReadRootBlockHeader(db DatabaseReader, hash common.Hash) *types.RootBlockHeader {187 data, _ := db.Get(headerKey(hash))188 if len(data) == 0 {189 return nil190 }191 header := new(types.RootBlockHeader)192 if err := serialize.Deserialize(serialize.NewByteBuffer(data), header); err != nil {193 log.Error("Invalid block header Deserialize", "hash", hash, "err", err)194 return nil195 }196 return header197}198// WriteRootBlockHeader stores a block header into the database and also stores the hash-199// to-number mapping.200func WriteRootBlockHeader(db DatabaseWriter, header *types.RootBlockHeader) {201 // Write the hash -> number mapping202 var (203 hash = header.Hash()204 encoded = encodeBlockNumber(header.NumberU64())205 )206 key := headerNumberKey(hash)207 if err := db.Put(key, encoded); err != nil {208 log.Crit("Failed to store hash to number mapping", "err", err)209 }210 // Write the encoded header211 data, err := serialize.SerializeToBytes(header)212 if err != nil {213 log.Crit("Failed to Serialize header", "err", err)214 }215 key = headerKey(hash)216 if err := db.Put(key, data); err != nil {217 log.Crit("Failed to store header", "err", err)218 }219}220// DeleteRootBlockHeader removes all block header data associated with a hash.221func DeleteRootBlockHeader(db DatabaseDeleter, hash common.Hash) {222 if err := db.Delete(headerKey(hash)); err != nil {223 log.Crit("Failed to delete header", "err", err)224 }225 if err := db.Delete(headerNumberKey(hash)); err != nil {226 log.Crit("Failed to delete hash to number mapping", "err", err)227 }228}229// HasBlock verifies the existence of a block body corresponding to the hash.230func HasBlock(db DatabaseReader, hash common.Hash) bool {231 if has, err := db.Has(blockKey(hash)); !has || err != nil {232 return false233 }234 return true235}236// ReadMinorBlock retrieves the block body corresponding to the hash.237func ReadMinorBlock(db DatabaseReader, hash common.Hash) *types.MinorBlock {238 data, _ := db.Get(blockKey(hash))239 if len(data) == 0 {240 return nil241 }242 block := new(types.MinorBlock)243 if err := serialize.Deserialize(serialize.NewByteBuffer(data), block); err != nil {244 log.Error("Invalid block body Deserialize", "hash", hash, "err", err)245 return nil246 }247 return block248}249// WriteMinorBlock storea a block body into the database.250func WriteMinorBlock(db DatabaseWriter, block *types.MinorBlock) {251 data, err := serialize.SerializeToBytes(block)252 if err != nil {253 log.Crit("Failed to serialize body", "err", err)254 }255 log.Info(DBLOG+" Write MinorBlock", "branch", fmt.Sprintf("%x", block.Branch().Value), "height", block.NumberU64(), "hash", block.Hash().TerminalString(), "len(tx)", len(block.Transactions()))256 if err := db.Put(blockKey(block.Hash()), data); err != nil {257 log.Crit("Failed to store minor block body", "err", err)258 }259}260// ReadRootBlock retrieves the block rootBlockBody corresponding to the hash.261func ReadRootBlock(db DatabaseReader, hash common.Hash) *types.RootBlock {262 data, _ := db.Get(blockKey(hash))263 if len(data) == 0 {264 return nil265 }266 block := new(types.RootBlock)267 if err := serialize.Deserialize(serialize.NewByteBuffer(data), block); err != nil {268 log.Error("Invalid block rootBlockBody Deserialize", "hash", hash, "err", err)269 return nil270 }271 return block272}273// WriteRootBlock storea a block rootBlockBody into the database.274func WriteRootBlock(db DatabaseWriter, block *types.RootBlock) {275 data, err := serialize.SerializeToBytes(block)276 if err != nil {277 log.Crit("Failed to serialize RootBlock", "err", err)278 }279 log.Info(DBLOG+" Write RootBlock", "height", block.NumberU64(), "hash", block.Hash())280 if err := db.Put(blockKey(block.Hash()), data); err != nil {281 log.Crit("Failed to store RootBlock", "err", err)282 }283}284// DeleteBlock removes block data associated with a hash.285func DeleteBlock(db DatabaseDeleter, hash common.Hash) {286 if err := db.Delete(blockKey(hash)); err != nil {287 log.Crit("Failed to delete block", "err", err)288 }289}290// HasReceipts verifies the existence of all the transaction receipts belonging291// to a block.292func HasReceipts(db DatabaseReader, hash common.Hash) bool {293 if has, err := db.Has(blockReceiptsKey(hash)); !has || err != nil {294 return false295 }296 return true297}298// ReadReceipts retrieves all the transaction receipts belonging to a block.299func ReadReceipts(db DatabaseReader, hash common.Hash) types.Receipts {300 // Retrieve the flattened receipt slice301 data, _ := db.Get(blockReceiptsKey(hash))302 if len(data) == 0 {303 return nil304 }305 // Convert the receipts from their storage form to their internal representation306 storageReceipts := []*types.ReceiptForStorage{}307 if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {308 log.Error("Invalid receipt array RLP", "hash", hash, "err", err)309 return nil310 }311 receipts := make(types.Receipts, len(storageReceipts))312 for i, receipt := range storageReceipts {313 receipts[i] = (*types.Receipt)(receipt)314 }315 return receipts316}317// WriteReceipts stores all the transaction receipts belonging to a block.318func WriteReceipts(db DatabaseWriter, hash common.Hash, receipts types.Receipts) {319 // Convert the receipts into their storage form and serialize them320 storageReceipts := make([]*types.ReceiptForStorage, len(receipts))321 for i, receipt := range receipts {322 storageReceipts[i] = (*types.ReceiptForStorage)(receipt)323 }324 bytes, err := rlp.EncodeToBytes(storageReceipts)325 if err != nil {326 log.Crit("Failed to encode block receipts", "err", err)327 }328 // Store the flattened receipt slice329 if err := db.Put(blockReceiptsKey(hash), bytes); err != nil {330 log.Crit("Failed to store block receipts", "err", err)331 }332}333// DeleteReceipts removes all receipt data associated with a block hash.334func DeleteReceipts(db DatabaseDeleter, hash common.Hash) {335 if err := db.Delete(blockReceiptsKey(hash)); err != nil {336 log.Crit("Failed to delete block receipts", "err", err)337 }338}339// DeleteBlock removes all block data associated with a hash.340func DeleteMinorBlock(db DatabaseDeleter, hash common.Hash) {341 DeleteReceipts(db, hash)342 DeleteBlock(db, hash)343 DeleteMinorBlockCommitStatus(db, hash)344}345// DeleteRootBlock removes all block data associated with a hash.346func DeleteRootBlock(db DatabaseDeleter, hash common.Hash) {347 DeleteLatestMinorBlockHeaders(db, hash)348 DeleteBlock(db, hash)349}350func WriteRootBlockCommittingHash(db DatabaseWriter, hash common.Hash) {351 // use write-ahead log so if crashed the root block can be re-broadcasted352 if err := db.Put(rbCommittingKey, hash.Bytes()); err != nil {353 log.Crit("Failed to store rb committing block's hash", "err", err)354 }355}356func ReadRbCommittingHash(db DatabaseReader) common.Hash {357 data, _ := db.Get(rbCommittingKey)358 if len(data) == 0 {359 return common.Hash{}360 }361 return common.BytesToHash(data)362}363func DeleteRbCommittingHash(db DatabaseDeleter) {364 if err := db.Delete(rbCommittingKey); err != nil {365 log.Crit("Failed to delete block receipts", "err", err)366 }367}368// FindCommonMinorAncestor returns the last common ancestor of two block headers369func FindCommonMinorAncestor(db DatabaseReader, a, b *types.MinorBlockHeader) *types.MinorBlockHeader {370 for bn := b.Number; a.Number > bn; {371 a = ReadMinorBlockHeader(db, a.ParentHash)372 if a == nil {373 return nil374 }375 }376 for an := a.Number; an < b.Number; {377 b = ReadMinorBlockHeader(db, b.ParentHash)378 if b == nil {379 return nil380 }381 }382 for a.Hash() != b.Hash() {383 a = ReadMinorBlockHeader(db, a.ParentHash)384 if a == nil {385 return nil386 }387 b = ReadMinorBlockHeader(db, b.ParentHash)388 if b == nil {389 return nil390 }391 }392 return a393}394// FindCommonRootAncestor returns the last common ancestor of two block headers395func FindCommonRootAncestor(db DatabaseReader, a, b *types.RootBlockHeader) *types.RootBlockHeader {396 for bn := b.NumberU64(); a.NumberU64() > bn; {397 a = ReadRootBlockHeader(db, a.ParentHash)398 if a == nil {399 return nil400 }401 }402 for an := a.NumberU64(); an < b.NumberU64(); {403 b = ReadRootBlockHeader(db, b.ParentHash)404 if b == nil {405 return nil406 }407 }408 for a.Hash() != b.Hash() {409 a = ReadRootBlockHeader(db, a.ParentHash)410 if a == nil {411 return nil412 }413 b = ReadRootBlockHeader(db, b.ParentHash)414 if b == nil {415 return nil416 }417 }418 return a419}420func WriteTotalTx(db DatabaseWriter, hash common.Hash, txCount uint32) {421 data := encodeUint32(txCount)422 if err := db.Put(totalTxCountKey(hash), data); err != nil {423 log.Crit("Failed to store total Tx", "err", err)424 }425}426func ReadTotalTx(db DatabaseReader, hash common.Hash) *uint32 {427 data, _ := db.Get(totalTxCountKey(hash))428 if len(data) == 0 {429 return nil430 }431 number := binary.BigEndian.Uint32(data)432 return &number433}434func WriteGenesisBlock(db DatabaseWriter, rHash common.Hash, block *types.MinorBlock) {435 data, err := serialize.SerializeToBytes(block)436 if err != nil {437 log.Crit("can not serilalize Minor block")438 }439 key := makeGenesisKey(rHash)440 if err := db.Put(key, data); err != nil {441 log.Crit("Failed to store genesis", "err", err)442 }443}444func ReadGenesis(db DatabaseReader, hash common.Hash) *types.MinorBlock {445 data, _ := db.Get(makeGenesisKey(hash))446 if len(data) == 0 {447 return nil448 }449 res := new(types.MinorBlock)450 if err := serialize.DeserializeFromBytes(data, res); err != nil {451 return nil452 }453 return res454}455func WriteConfirmedCrossShardTxList(db DatabaseWriter, hash common.Hash, list *types.CrossShardTransactionDepositList) {456 data, err := serialize.SerializeToBytes(list)457 if err != nil {458 log.Crit("can not serialize CrossShardTransactionDepositList")459 }460 key := makeConfirmedXShardKey(hash)461 if err := db.Put(key, data); err != nil {462 log.Crit("Failed to store header", "err", err)463 }464}465func ReadConfirmedCrossShardTxList(db DatabaseReader, hash common.Hash) *types.CrossShardTransactionDepositList {466 data, _ := db.Get(makeConfirmedXShardKey(hash))467 if len(data) == 0 {468 return nil469 }470 list := new(types.CrossShardTransactionDepositList)471 if err := serialize.Deserialize(serialize.NewByteBuffer(data), list); err != nil {472 log.Error("Invalid block header Deserialize", "hash", hash, "err", err)473 return nil474 }475 return list476}477func WriteCrossShardTxList(db DatabaseWriter, hash common.Hash, list *types.CrossShardTransactionDepositList) {478 data, err := serialize.SerializeToBytes(list)479 if err != nil {480 log.Crit("can not serialize CrossShardTransactionDepositList")481 }482 key := makeXShardTxList(hash)483 if err := db.Put(key, data); err != nil {484 log.Crit("Failed to store header", "err", err)485 }486}487func ReadCrossShardTxList(db DatabaseReader, hash common.Hash) *types.CrossShardTransactionDepositList {488 data, _ := db.Get(makeXShardTxList(hash))489 if len(data) == 0 {490 return nil491 }492 list := new(types.CrossShardTransactionDepositList)493 if err := serialize.Deserialize(serialize.NewByteBuffer(data), list); err != nil {494 log.Error("Invalid block header Deserialize", "hash", hash, "err", err)495 return nil496 }497 return list498}499func WriteLastConfirmedMinorBlockHeaderAtRootBlock(db DatabaseWriter, rHash common.Hash, mHash common.Hash) {500 if err := db.Put(makeRLastMHash(rHash), mHash.Bytes()); err != nil {501 log.Crit("failed to store last confirmed minot block at root block")502 }503}504func ReadLastConfirmedMinorBlockHeaderAtRootBlock(db DatabaseReader, rHash common.Hash) common.Hash {505 data, _ := db.Get(makeRLastMHash(rHash))506 if len(data) == 0 {507 return common.Hash{}508 }509 return common.BytesToHash(data)510}511func WriteMinorBlockCnt(db DatabaseWriter, fullShardID uint32, height uint32, data []byte) {512 if err := db.Put(makeMinorCount(fullShardID, height), data); err != nil {513 log.Crit("failed to store minor block cnt")514 }515}516func GetMinorBlockCnt(db DatabaseReader, fullShardID uint32, height uint32) []byte {517 data, _ := db.Get(makeMinorCount(fullShardID, height))518 if len(data) == 0 {519 return []byte{}520 }521 return data522}523func WriteMinorBlockCoinbase(db DatabaseWriter, mHash common.Hash, coinbaseToken *types.TokenBalances) {524 tokenBytes, err := serialize.SerializeToBytes(coinbaseToken)525 if err != nil {526 log.Crit("failed to put minor block coinbase", "serialize err", err)527 }528 if err := db.Put(makeMinorBlockCoinbase(mHash), tokenBytes); err != nil {529 log.Crit("failed to put minor block coinbase ", "put err", err)530 }531}532func GetMinorBlockCoinbaseToken(db DatabaseReader, hash common.Hash) *types.TokenBalances {533 data, _ := db.Get(makeMinorBlockCoinbase(hash))534 if len(data) == 0 {535 return nil536 }537 tokens := new(types.TokenBalances)538 if err := serialize.DeserializeFromBytes(data, tokens); err != nil {539 log.Error("GetMinorBlockCoinbaseToken", "deserialize err", err)540 return nil541 }542 return tokens543}544func ContainMinorBlockByHash(db DatabaseReader, hash common.Hash) bool {545 if has, err := db.Has(makeMinorBlockCoinbase(hash)); !has || err != nil {546 return false547 }548 return true549}550func PutRootBlockConfirmingMinorBlock(db DatabaseWriter, mBlockID []byte, rHash common.Hash) {551 if err := db.Put(makeRootBlockConfirmingMinorBlock(mBlockID), rHash.Bytes()); err != nil {552 log.Crit("failed to put rootBlock confirming minor block", "err", err)553 }554}555func GetRootBlockConfirmingMinorBlock(db DatabaseReader, mBlockID []byte) common.Hash {556 data, _ := db.Get(makeRootBlockConfirmingMinorBlock(mBlockID))557 if len(data) == 0 {558 return common.Hash{}559 }560 return common.BytesToHash(data)561}562func PutXShardDepositHashList(db DatabaseWriter, h common.Hash, hList *HashList) {563 bytes, err := serialize.SerializeToBytes(hList)564 if err != nil {565 log.Crit("can not serialize HashList")566 }567 if err := db.Put(makeXShardDepositHashList(h), bytes); err != nil {568 log.Crit("failed to put xshard deposit hash list err", err)569 }570}571func GetXShardDepositHashList(db DatabaseReader, h common.Hash) *HashList {572 data, _ := db.Get(makeXShardDepositHashList(h))573 hList := new(HashList)574 if err := serialize.DeserializeFromBytes(data, hList); err != nil {575 log.Error("GetXShardDepositHashList", "DeserializeFromBytes err", err)576 return nil577 }578 return hList579}580func WriteCommitMinorBlock(db DatabaseWriter, h common.Hash) {581 if err := db.Put(makeCommitMinorBlock(h), []byte{1}); err != nil { // value must not empty582 log.Crit("failed to write commit minor block", "err", err)583 }584}585func HasCommitMinorBlock(db DatabaseReader, h common.Hash) bool {586 if has, err := db.Has(makeCommitMinorBlock(h)); !has || err != nil {587 return false588 }589 return true...

Full Screen

Full Screen

expression_test.go

Source:expression_test.go Github

copy

Full Screen

1// Copyright 2015 CoreOS, Inc. All rights reserved.2// Copyright 2014 Dropbox, Inc. All rights reserved.3// Use of this source code is governed by the BSD 3-Clause license,4// which can be found in the LICENSE file.5package sqlbuilder6import (7 "bytes"8 gc "gopkg.in/check.v1"9)10type ExprSuite struct {11}12var _ = gc.Suite(&ExprSuite{})13func (s *ExprSuite) TestConjunctExprEmptyList(c *gc.C) {14 dbName := "db"15 d := NewMySQLDialect(&dbName)16 expr := And()17 buf := &bytes.Buffer{}18 err := expr.SerializeSql(d, buf)19 c.Assert(err, gc.NotNil)20}21func (s *ExprSuite) TestConjunctExprNilInList(c *gc.C) {22 dbName := "db"23 d := NewMySQLDialect(&dbName)24 expr := And(nil, EqL(table1Col1, 1))25 buf := &bytes.Buffer{}26 err := expr.SerializeSql(d, buf)27 c.Assert(err, gc.NotNil)28}29func (s *ExprSuite) TestConjunctExprSingleElement(c *gc.C) {30 dbName := "db"31 d := NewMySQLDialect(&dbName)32 expr := And(EqL(table1Col1, 1))33 buf := &bytes.Buffer{}34 err := expr.SerializeSql(d, buf)35 c.Assert(err, gc.IsNil)36 sql := buf.String()37 c.Assert(sql, gc.Equals, "`table1`.`col1`=1")38}39func (s *ExprSuite) TestTupleExpr(c *gc.C) {40 dbName := "db"41 d := NewMySQLDialect(&dbName)42 expr := Tuple()43 buf := &bytes.Buffer{}44 err := expr.SerializeSql(d, buf)45 c.Assert(err, gc.NotNil)46 expr = Tuple(table1Col1, Literal(1), Literal("five"))47 err = expr.SerializeSql(d, buf)48 c.Assert(err, gc.IsNil)49 sql := buf.String()50 c.Assert(sql, gc.Equals, "(`table1`.`col1`,1,'five')")51}52func (s *ExprSuite) TestLikeExpr(c *gc.C) {53 dbName := "db"54 d := NewMySQLDialect(&dbName)55 expr := LikeL(table1Col1, EscapeForLike("%my_prefix")+"%")56 buf := &bytes.Buffer{}57 err := expr.SerializeSql(d, buf)58 c.Assert(err, gc.IsNil)59 sql := buf.String()60 c.Assert(sql, gc.Equals, "`table1`.`col1` LIKE '\\\\%my\\\\_prefix%'")61}62func (s *ExprSuite) TestAndExpr(c *gc.C) {63 dbName := "db"64 d := NewMySQLDialect(&dbName)65 expr := And(EqL(table1Col1, 1), EqL(table1Col2, 2), EqL(table1Col3, 3))66 buf := &bytes.Buffer{}67 err := expr.SerializeSql(d, buf)68 c.Assert(err, gc.IsNil)69 sql := buf.String()70 c.Assert(sql, gc.Equals, "(`table1`.`col1`=1 AND `table1`.`col2`=2 AND `table1`.`col3`=3)")71}72func (s *ExprSuite) TestOrExpr(c *gc.C) {73 dbName := "db"74 d := NewMySQLDialect(&dbName)75 expr := Or(EqL(table1Col1, 1), EqL(table1Col2, 2), EqL(table1Col3, 3))76 buf := &bytes.Buffer{}77 err := expr.SerializeSql(d, buf)78 c.Assert(err, gc.IsNil)79 sql := buf.String()80 c.Assert(sql, gc.Equals, "(`table1`.`col1`=1 OR `table1`.`col2`=2 OR `table1`.`col3`=3)")81}82func (s *ExprSuite) TestAddExpr(c *gc.C) {83 dbName := "db"84 d := NewMySQLDialect(&dbName)85 expr := Add(Literal(1), Literal(2), Literal(3))86 buf := &bytes.Buffer{}87 err := expr.SerializeSql(d, buf)88 c.Assert(err, gc.IsNil)89 sql := buf.String()90 c.Assert(sql, gc.Equals, "(1 + 2 + 3)")91}92func (s *ExprSuite) TestSubExpr(c *gc.C) {93 dbName := "db"94 d := NewMySQLDialect(&dbName)95 expr := Sub(Literal(1), Literal(2), Literal(3))96 buf := &bytes.Buffer{}97 err := expr.SerializeSql(d, buf)98 c.Assert(err, gc.IsNil)99 sql := buf.String()100 c.Assert(sql, gc.Equals, "(1 - 2 - 3)")101}102func (s *ExprSuite) TestMulExpr(c *gc.C) {103 dbName := "db"104 d := NewMySQLDialect(&dbName)105 expr := Mul(Literal(1), Literal(2), Literal(3))106 buf := &bytes.Buffer{}107 err := expr.SerializeSql(d, buf)108 c.Assert(err, gc.IsNil)109 sql := buf.String()110 c.Assert(sql, gc.Equals, "(1 * 2 * 3)")111}112func (s *ExprSuite) TestDivExpr(c *gc.C) {113 dbName := "db"114 d := NewMySQLDialect(&dbName)115 expr := Div(Literal(1), Literal(2), Literal(3))116 buf := &bytes.Buffer{}117 err := expr.SerializeSql(d, buf)118 c.Assert(err, gc.IsNil)119 sql := buf.String()120 c.Assert(sql, gc.Equals, "(1 / 2 / 3)")121}122func (s *ExprSuite) TestBinaryExprNilLHS(c *gc.C) {123 dbName := "db"124 d := NewMySQLDialect(&dbName)125 expr := Gt(nil, table1Col1)126 buf := &bytes.Buffer{}127 err := expr.SerializeSql(d, buf)128 c.Assert(err, gc.NotNil)129}130func (s *ExprSuite) TestNegateExpr(c *gc.C) {131 dbName := "db"132 d := NewMySQLDialect(&dbName)133 expr := Not(EqL(table1Col1, 123))134 buf := &bytes.Buffer{}135 err := expr.SerializeSql(d, buf)136 c.Assert(err, gc.IsNil)137 sql := buf.String()138 c.Assert(sql, gc.Equals, "NOT (`table1`.`col1`=123)")139}140func (s *ExprSuite) TestBinaryExprNilRHS(c *gc.C) {141 dbName := "db"142 d := NewMySQLDialect(&dbName)143 expr := Lt(table1Col1, nil)144 buf := &bytes.Buffer{}145 err := expr.SerializeSql(d, buf)146 c.Assert(err, gc.NotNil)147}148func (s *ExprSuite) TestEqExpr(c *gc.C) {149 dbName := "db"150 d := NewMySQLDialect(&dbName)151 expr := EqL(table1Col1, 321)152 buf := &bytes.Buffer{}153 err := expr.SerializeSql(d, buf)154 c.Assert(err, gc.IsNil)155 sql := buf.String()156 c.Assert(sql, gc.Equals, "`table1`.`col1`=321")157}158func (s *ExprSuite) TestEqExprNilLHS(c *gc.C) {159 dbName := "db"160 d := NewMySQLDialect(&dbName)161 expr := EqL(table1Col1, nil)162 buf := &bytes.Buffer{}163 err := expr.SerializeSql(d, buf)164 c.Assert(err, gc.IsNil)165 sql := buf.String()166 c.Assert(sql, gc.Equals, "`table1`.`col1` IS null")167}168func (s *ExprSuite) TestNeqExpr(c *gc.C) {169 dbName := "db"170 d := NewMySQLDialect(&dbName)171 expr := NeqL(table1Col1, 123)172 buf := &bytes.Buffer{}173 err := expr.SerializeSql(d, buf)174 c.Assert(err, gc.IsNil)175 sql := buf.String()176 c.Assert(sql, gc.Equals, "`table1`.`col1`!=123")177}178func (s *ExprSuite) TestNeqExprNilLHS(c *gc.C) {179 dbName := "db"180 d := NewMySQLDialect(&dbName)181 expr := NeqL(table1Col1, nil)182 buf := &bytes.Buffer{}183 err := expr.SerializeSql(d, buf)184 c.Assert(err, gc.IsNil)185 sql := buf.String()186 c.Assert(sql, gc.Equals, "`table1`.`col1` IS NOT null")187}188func (s *ExprSuite) TestLtExpr(c *gc.C) {189 dbName := "db"190 d := NewMySQLDialect(&dbName)191 expr := LtL(table1Col1, -1.5)192 buf := &bytes.Buffer{}193 err := expr.SerializeSql(d, buf)194 c.Assert(err, gc.IsNil)195 sql := buf.String()196 c.Assert(sql, gc.Equals, "`table1`.`col1`<-1.5")197}198func (s *ExprSuite) TestLteExpr(c *gc.C) {199 dbName := "db"200 d := NewMySQLDialect(&dbName)201 expr := LteL(table1Col1, "foo\"';drop user table;")202 buf := &bytes.Buffer{}203 err := expr.SerializeSql(d, buf)204 c.Assert(err, gc.IsNil)205 sql := buf.String()206 c.Assert(207 sql,208 gc.Equals,209 "`table1`.`col1`<='foo\\\"\\';drop user table;'")210}211func (s *ExprSuite) TestGtExpr(c *gc.C) {212 dbName := "db"213 d := NewMySQLDialect(&dbName)214 expr := GtL(table1Col1, 1.1)215 buf := &bytes.Buffer{}216 err := expr.SerializeSql(d, buf)217 c.Assert(err, gc.IsNil)218 sql := buf.String()219 c.Assert(sql, gc.Equals, "`table1`.`col1`>1.1")220}221func (s *ExprSuite) TestGteExpr(c *gc.C) {222 dbName := "db"223 d := NewMySQLDialect(&dbName)224 expr := GteL(table1Col1, 1)225 buf := &bytes.Buffer{}226 err := expr.SerializeSql(d, buf)227 c.Assert(err, gc.IsNil)228 sql := buf.String()229 c.Assert(sql, gc.Equals, "`table1`.`col1`>=1")230}231func (s *ExprSuite) TestInExpr(c *gc.C) {232 dbName := "db"233 d := NewMySQLDialect(&dbName)234 values := []int32{1, 2, 3}235 expr := In(table1Col1, values)236 buf := &bytes.Buffer{}237 err := expr.SerializeSql(d, buf)238 c.Assert(err, gc.IsNil)239 sql := buf.String()240 c.Assert(sql, gc.Equals, "`table1`.`col1` IN (1,2,3)")241}242func (s *ExprSuite) TestInExprEmptyList(c *gc.C) {243 dbName := "db"244 d := NewMySQLDialect(&dbName)245 values := []int32{}246 expr := In(table1Col1, values)247 buf := &bytes.Buffer{}248 err := expr.SerializeSql(d, buf)249 c.Assert(err, gc.IsNil)250 sql := buf.String()251 c.Assert(sql, gc.Equals, "FALSE")252}253func (s *ExprSuite) TestSqlFuncExprNilInArgList(c *gc.C) {254 dbName := "db"255 d := NewMySQLDialect(&dbName)256 expr := SqlFunc("rand", nil)257 buf := &bytes.Buffer{}258 err := expr.SerializeSql(d, buf)259 c.Assert(err, gc.NotNil)260}261func (s *ExprSuite) TestSqlFuncExprEmptyArgList(c *gc.C) {262 dbName := "db"263 d := NewMySQLDialect(&dbName)264 expr := SqlFunc("rand")265 buf := &bytes.Buffer{}266 err := expr.SerializeSql(d, buf)267 c.Assert(err, gc.IsNil)268 sql := buf.String()269 c.Assert(sql, gc.Equals, "rand()")270}271func (s *ExprSuite) TestSqlFuncExprNonEmptyArgList(c *gc.C) {272 dbName := "db"273 d := NewMySQLDialect(&dbName)274 expr := SqlFunc("add", table1Col1, table1Col2)275 buf := &bytes.Buffer{}276 err := expr.SerializeSql(d, buf)277 c.Assert(err, gc.IsNil)278 sql := buf.String()279 c.Assert(sql, gc.Equals, "add(`table1`.`col1`,`table1`.`col2`)")280}281func (s *ExprSuite) TestOrderByClauseNilExpr(c *gc.C) {282 dbName := "db"283 d := NewMySQLDialect(&dbName)284 clause := Asc(nil)285 buf := &bytes.Buffer{}286 err := clause.SerializeSql(d, buf)287 c.Assert(err, gc.NotNil)288}289func (s *ExprSuite) TestAsc(c *gc.C) {290 dbName := "db"291 d := NewMySQLDialect(&dbName)292 clause := Asc(table1Col1)293 buf := &bytes.Buffer{}294 err := clause.SerializeSql(d, buf)295 c.Assert(err, gc.IsNil)296 sql := buf.String()297 c.Assert(sql, gc.Equals, "`table1`.`col1` ASC")298}299func (s *ExprSuite) TestDesc(c *gc.C) {300 dbName := "db"301 d := NewMySQLDialect(&dbName)302 clause := Desc(table1Col1)303 buf := &bytes.Buffer{}304 err := clause.SerializeSql(d, buf)305 c.Assert(err, gc.IsNil)306 sql := buf.String()307 c.Assert(sql, gc.Equals, "`table1`.`col1` DESC")308}309func (s *ExprSuite) TestIf(c *gc.C) {310 dbName := "db"311 d := NewMySQLDialect(&dbName)312 test := GtL(table1Col1, 1.1)313 clause := If(test, table1Col1, table1Col2)314 buf := &bytes.Buffer{}315 err := clause.SerializeSql(d, buf)316 c.Assert(err, gc.IsNil)317 sql := buf.String()318 c.Assert(319 sql,320 gc.Equals,321 "IF(`table1`.`col1`>1.1,`table1`.`col1`,`table1`.`col2`)")322}323func (s *ExprSuite) TestColumnValue(c *gc.C) {324 dbName := "db"325 d := NewMySQLDialect(&dbName)326 clause := ColumnValue(table1Col1)327 buf := &bytes.Buffer{}328 err := clause.SerializeSql(d, buf)329 c.Assert(err, gc.IsNil)330 sql := buf.String()331 c.Assert(sql, gc.Equals, "VALUES(`table1`.`col1`)")332}333func (s *ExprSuite) TestBitwiseOr(c *gc.C) {334 dbName := "db"335 d := NewMySQLDialect(&dbName)336 clause := BitOr(Literal(1), Literal(2))337 buf := &bytes.Buffer{}338 err := clause.SerializeSql(d, buf)339 c.Assert(err, gc.IsNil)340 sql := buf.String()341 c.Assert(sql, gc.Equals, "1 | 2")342}343func (s *ExprSuite) TestBitwiseAnd(c *gc.C) {344 dbName := "db"345 d := NewMySQLDialect(&dbName)346 clause := BitAnd(Literal(1), Literal(2))347 buf := &bytes.Buffer{}348 err := clause.SerializeSql(d, buf)349 c.Assert(err, gc.IsNil)350 sql := buf.String()351 c.Assert(sql, gc.Equals, "1 & 2")352}353func (s *ExprSuite) TestBitwiseXor(c *gc.C) {354 dbName := "db"355 d := NewMySQLDialect(&dbName)356 clause := BitXor(Literal(1), Literal(2))357 buf := &bytes.Buffer{}358 err := clause.SerializeSql(d, buf)359 c.Assert(err, gc.IsNil)360 sql := buf.String()361 c.Assert(sql, gc.Equals, "1 ^ 2")362}363func (s *ExprSuite) TestPlus(c *gc.C) {364 dbName := "db"365 d := NewMySQLDialect(&dbName)366 clause := Plus(Literal(1), Literal(2))367 buf := &bytes.Buffer{}368 err := clause.SerializeSql(d, buf)369 c.Assert(err, gc.IsNil)370 sql := buf.String()371 c.Assert(sql, gc.Equals, "1 + 2")372}373func (s *ExprSuite) TestMinus(c *gc.C) {374 dbName := "db"375 d := NewMySQLDialect(&dbName)376 clause := Minus(Literal(1), Literal(2))377 buf := &bytes.Buffer{}378 err := clause.SerializeSql(d, buf)379 c.Assert(err, gc.IsNil)380 sql := buf.String()381 c.Assert(sql, gc.Equals, "1 - 2")382}383func (s *ExprSuite) TestBasicSubquery(c *gc.C) {384 dbName := "db"385 d := NewMySQLDialect(&dbName)386 subquery := table1.Select(table1Col2)387 outerquery := table1.Select(table1Col1, table1Col2).Where(InQ(table1Col2, Subquery(subquery)))388 sql, err := outerquery.String(d)389 c.Assert(err, gc.IsNil)390 c.Assert(sql, gc.Equals, "SELECT `table1`.`col1`,`table1`.`col2` FROM `db`.`table1` WHERE `table1`.`col2` IN (SELECT `table1`.`col2` FROM `db`.`table1`)")391}...

Full Screen

Full Screen

coindb.go

Source:coindb.go Github

copy

Full Screen

...24 if err != nil {25 return nil, err26 }27 coin := NewEmptyCoin()28 err = coin.Unserialize(bytes.NewBuffer(coinBuff))29 return coin, err30}31func (coinsViewDB *CoinsDB) HaveCoin(outpoint *outpoint.OutPoint) bool {32 buf := bytes.NewBuffer(nil)33 err := NewCoinKey(outpoint).Serialize(buf)34 if err != nil {35 log.Emergency("db.HaveCoin err:%#v", err)36 return false37 }38 return coinsViewDB.dbw.Exists(buf.Bytes())39}40func (coinsViewDB *CoinsDB) GetBestBlock() (*util.Hash, error) {41 v, err := coinsViewDB.dbw.Read([]byte{db.DbBestBlock})42 if err == leveldb.ErrNotFound {43 return nil, err44 }45 if err != nil {46 return nil, err47 }48 hashBlock := new(util.Hash)49 if v == nil {50 return hashBlock, nil51 }52 _, err = hashBlock.Unserialize(bytes.NewBuffer(v))53 return hashBlock, err54}55func (coinsViewDB *CoinsDB) BatchWrite(cm map[outpoint.OutPoint]*Coin, hashBlock util.Hash) error {56 mapCoins := cm57 batch := db.NewBatchWrapper(coinsViewDB.dbw)58 count := 059 changed := 060 for k, v := range mapCoins {61 if v.dirty {62 entry := NewCoinKey(&k)63 bufEntry := bytes.NewBuffer(nil)64 err := entry.Serialize(bufEntry)65 if err != nil {66 log.Error("coinDB:serialize bufEntry failed %v", err)67 return err68 }69 if v.IsSpent() {70 batch.Erase(bufEntry.Bytes())71 } else {72 coinByte := bytes.NewBuffer(nil)73 err := v.Serialize(coinByte)74 if err != nil {75 log.Error("coinDB:serialize coinByte failed %v", err)76 return err77 }78 batch.Write(bufEntry.Bytes(), coinByte.Bytes())79 }80 changed++81 }82 count++83 delete(cm, k)84 }85 if !hashBlock.IsNull() {86 hashByte := bytes.NewBuffer(nil)87 _, err := hashBlock.Serialize(hashByte)88 if err != nil {89 log.Error("coinDB:Serialize hash block failed<%v>, please check.", err)...

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4}5import (6func main() {7 fmt.Println("Hello World")8}9import (10func main() {11 fmt.Println("Hello World")12}13import (14func main() {15 fmt.Println("Hello World")16}17import (18func main() {19 fmt.Println("Hello World")20}21import (22func main() {23 fmt.Println("Hello World")24}25import (26func main() {27 fmt.Println("Hello World")28}29import (30func main() {31 fmt.Println("Hello World")32}33import (34func main() {35 fmt.Println("Hello World")36}37import (38func main() {39 fmt.Println("Hello World")40}41import (42func main() {43 fmt.Println("Hello World")44}45import (46func main() {47 fmt.Println("Hello World")48}49import (50func main() {51 fmt.Println("Hello World")52}53import (54func main() {55 fmt.Println("Hello

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db := serialize.NewDb()4 db.Add("foo", "bar")5 db.Add("hello", "world")6 db.Add("foo", "baz")7 db.Add("foo", "qux")8 db.Add("foo", "quux")9 db.Add("foo", "quuz")10 db.Add("foo", "corge")11 db.Add("foo", "grault")12 db.Add("foo", "garply")13 db.Add("foo", "waldo")14 db.Add("foo", "fred")15 db.Add("foo", "plugh")16 db.Add("foo", "xyzzy")17 db.Add("foo", "thud")18 db.Add("foo", "hello")19 db.Add("foo", "world")20 db.Add("foo", "foo")21 db.Add("foo", "bar")22 db.Add("foo", "baz")23 db.Add("foo", "qux")24 db.Add("foo", "quux")25 db.Add("foo", "quuz")26 db.Add("foo", "corge")27 db.Add("foo", "grault")28 db.Add("foo", "garply")29 db.Add("foo", "waldo")30 db.Add("foo", "fred")31 db.Add("foo", "plugh")32 db.Add("foo", "xyzzy")33 db.Add("foo", "thud")34 db.Add("foo", "hello")35 db.Add("foo", "world")36 db.Add("foo", "foo")37 db.Add("foo", "bar")38 db.Add("foo", "baz")39 db.Add("foo", "qux")40 db.Add("foo", "quux")41 db.Add("foo", "quuz")42 db.Add("foo", "corge")43 db.Add("foo", "grault")44 db.Add("foo", "garply")45 db.Add("foo", "waldo")46 db.Add("foo", "fred")47 db.Add("foo", "plugh")48 db.Add("foo", "xyzzy")49 db.Add("foo", "thud")50 db.Add("foo", "hello")51 db.Add("foo", "world")52 db.Add("foo", "foo")53 db.Add("foo", "bar")54 db.Add("foo", "baz")55 db.Add("foo", "qux")56 db.Add("foo", "quux")57 db.Add("foo", "quuz")58 db.Add("foo", "cor

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2type DB struct {3}4func (db *DB) serialize() string {5 db.mu.Lock()6 defer db.mu.Unlock()7}8func (db *DB) deserialize(s string) {9 db.mu.Lock()10 defer db.mu.Unlock()11}12func main() {13 db := &DB{data: map[string]string{}}14}15import (16type DB struct {17}18func (db *DB) serialize() string {19}20func (db *DB) deserialize(s string) {21}22func main() {23 db := &DB{data: map[string]string{}, ch: make(chan string)}24}25import (26type DB struct {27}28func (db *DB) serialize() string {29 for !atomic.CompareAndSwapInt64(&db.flag, 0, 1) {}30 atomic.StoreInt64(&db.flag, 0)31}32func (db *DB) deserialize(s string) {33 for !atomic.CompareAndSwapInt64(&db.flag, 0, 1) {}34 atomic.StoreInt64(&db.flag, 0)35}36func main() {37 db := &DB{data: map[string]string{}, flag: 0}

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db.Serialize()4}5func Serialize() {6}7func Deserialize() {8}9import (10func main() {11 db.Serialize()12}13func Serialize() {14}15func Deserialize() {16}17import (18func main() {19 db.Serialize()20}21func Serialize() {22}23func Deserialize() {24}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful