Exemple #1
0
// Initialize External ID map for explorer search
func (db *LevelDb) InitializeExternalIDMap() (extIDMap map[string]bool, err error) {

	var fromkey []byte = []byte{byte(TBL_ENTRY)} // Table Name (1 bytes)

	var tokey []byte = []byte{byte(TBL_ENTRY + 1)} // Table Name (1 bytes)

	extIDMap = make(map[string]bool)

	iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)

	for iter.Next() {
		entry := new(common.Entry)
		_, err := entry.UnmarshalBinaryData(iter.Value())
		if err != nil {
			return nil, err
		}
		if entry.ExtIDs != nil {
			for i := 0; i < len(entry.ExtIDs); i++ {
				mapKey := string(iter.Key()[1:])
				mapKey = mapKey + strings.ToLower(string(entry.ExtIDs[i]))
				extIDMap[mapKey] = true
			}
		}

	}
	iter.Release()
	err = iter.Error()

	return extIDMap, nil
}
Exemple #2
0
// InsertEntry inserts an entry
func (db *LevelDb) InsertEntry(entry *common.Entry) error {
	db.dbLock.Lock()
	defer db.dbLock.Unlock()

	if db.lbatch == nil {
		db.lbatch = new(leveldb.Batch)
	}
	defer db.lbatch.Reset()

	binaryEntry, err := entry.MarshalBinary()
	if err != nil {
		return err
	}
	var entryKey []byte = []byte{byte(TBL_ENTRY)}
	entryKey = append(entryKey, entry.Hash().Bytes()...)
	db.lbatch.Put(entryKey, binaryEntry)

	err = db.lDb.Write(db.lbatch, db.wo)
	if err != nil {
		log.Println("batch failed %v\n", err)
		return err
	}

	return nil
}
Exemple #3
0
func (db *LevelDb) InsertEntryMultiBatch(entry *common.Entry) error {
	if entry == nil {
		return nil
	}

	if db.lbatch == nil {
		return fmt.Errorf("db.lbatch == nil")
	}

	binaryEntry, err := entry.MarshalBinary()
	if err != nil {
		return err
	}
	var entryKey []byte = []byte{byte(TBL_ENTRY)}
	entryKey = append(entryKey, entry.Hash().Bytes()...)
	db.lbatch.Put(entryKey, binaryEntry)

	return nil
}
func FetchAndParseEntry(hash, blockTime string, isFirstEntry bool) (*Entry, error) {
	e := new(Entry)
	raw, err := factom.GetRaw(hash)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	entry := new(common.Entry)
	_, err = entry.UnmarshalBinaryData(raw)
	if err != nil {
		Log("Error unmarshalling data - %v, %x - %v", hash, err, raw)
		return nil, err
	}

	e.ChainID = entry.ChainID.String()
	e.Hash = hash
	str, err := entry.JSONString()
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}
	e.JSONString = str
	e.SpewString = entry.Spew()
	e.BinaryString = fmt.Sprintf("%x", raw)
	e.Timestamp = blockTime

	e.Content = ByteSliceToDecodedStringPointer(entry.Content)
	e.ExternalIDs = make([]DecodedString, len(entry.ExtIDs))
	for i, v := range entry.ExtIDs {
		e.ExternalIDs[i] = ByteSliceToDecodedString(v)
	}

	if isFirstEntry == true {
		//TODO: parse the first entry somehow perhaps?
	} else {
		if IsAnchorChainID(e.ChainID) {
			ar, err := ParseAnchorChainData(e.Content.Decoded)
			if err != nil {
				Log("Error - %v", err)
				return nil, err
			}
			e.AnchorRecord = ar
		}
	}

	err = SaveEntry(e)
	if err != nil {
		Log("Error - %v", err)
		return nil, err
	}

	return e, nil
}