github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/test/kvStore.js (about)

     1  "use strict";
     2  
     3  var WikiItem = function(text) {
     4    if (text) {
     5      var obj = JSON.parse(text);
     6      this.key = obj.key;
     7      this.value = obj.value;
     8      this.author = obj.text;
     9    } else {
    10        this.key = "";
    11        this.author = "";
    12        this.value = "";
    13    }
    14  };
    15  
    16  WikiItem.prototype = {
    17    toString: function () {
    18      return JSON.stringify(this);
    19    }
    20  };
    21  
    22  var SuperWiki = function () {
    23      LocalContractStorage.defineMapProperty(this, "repo", {
    24          parse: function (text) {
    25              return new WikiItem(text);
    26          },
    27          stringify: function (o) {
    28              return o.toString();
    29          }
    30      });
    31  };
    32  
    33  SuperWiki.prototype = {
    34      init: function () {
    35          // todo
    36      },
    37  
    38      testTps: function() {
    39          console.log("child nvm!");
    40      },
    41  
    42      testTimeOut: function() {
    43          while(true){
    44          }
    45      },
    46  
    47      testOom: function(){
    48          var list = new Array();
    49          console.log("oom-=================");
    50          while (true) {
    51              var buffer = new ArrayBuffer(4096);
    52              var array = new Int32Array(buffer);
    53              // for (var i = 0; i < 10240; i++) {
    54              //     array[i] = i;
    55              // }
    56              array[1023] = 1;
    57              list.push(buffer);
    58          }
    59      },
    60  
    61      saveWithNoValue: function (key, value) {
    62          console.log("reach child contract");
    63  
    64          key = key.trim();
    65          value = value.trim();
    66          if (key === "" || value === ""){
    67              throw new Error("empty key / value");
    68          }
    69          if (value.length > 128 || key.length > 128){
    70              throw new Error("key / value exceed limit length")
    71          }
    72  
    73          var from = Blockchain.transaction.from;
    74          var wikiItem = this.repo.get(key);
    75      
    76          if (wikiItem){
    77              throw new Error("value has been taken");
    78          }
    79  
    80          wikiItem = new WikiItem();
    81          wikiItem.author = from;
    82          wikiItem.key = key;
    83          wikiItem.value = value;
    84          this.repo.put(key, wikiItem);
    85      },
    86  
    87      save: function (key, value) {
    88          console.log("reach child contract");
    89          console.log("==========", key, value);
    90          if(Blockchain.transaction.value < 2000000000000000000) {
    91              throw("nas is not enough");
    92          }
    93  
    94          key = key.trim();
    95          value = value.trim();
    96          if (key === "" || value === ""){
    97              throw new Error("empty key / value");
    98          }
    99          if (value.length > 128 || key.length > 128){
   100              throw new Error("key / value exceed limit length")
   101          }
   102  
   103          var from = Blockchain.transaction.from;
   104          var wikiItem = this.repo.get(key);
   105      
   106          if (wikiItem){
   107              throw new Error("value has been taken");
   108          }
   109  
   110          wikiItem = new WikiItem();
   111          wikiItem.author = from;
   112          wikiItem.key = key;
   113          wikiItem.value = value;
   114          this.repo.put(key, wikiItem);
   115      },
   116  
   117      testTimeOut: function() {
   118          while(true){
   119          }
   120      },
   121  
   122      get: function (key) {
   123          console.log("child get");
   124          key = key.trim();
   125          if ( key === "" ) {
   126              throw new Error("empty key")
   127          }
   128          return this.repo.get(key);
   129      }, 
   130  
   131      throwErr: function() {
   132          throw("err for test");
   133      }
   134  };
   135  module.exports = SuperWiki;